dltk.io package¶
dltk.io.augmentation module¶
-
dltk.io.augmentation.
add_gaussian_noise
(image, sigma=0.05)[source]¶ Add Gaussian noise to an image
Parameters: - image (np.ndarray) – image to add noise to
- sigma (float) – stddev of the Gaussian distribution to generate noise from
Returns: same as image but with added offset to each channel
Return type: np.ndarray
-
dltk.io.augmentation.
add_gaussian_offset
(image, sigma=0.1)[source]¶ Add Gaussian offset to an image. Adds the offset to each channel independently.
Parameters: - image (np.ndarray) – image to add noise to
- sigma (float) – stddev of the Gaussian distribution to generate noise from
Returns: same as image but with added offset to each channel
Return type: np.ndarray
-
dltk.io.augmentation.
elastic_transform
(image, alpha, sigma)[source]¶ Elastic deformation of images as described in [1].
- [1] Simard, Steinkraus and Platt, “Best Practices for Convolutional
- Neural Networks applied to Visual Document Analysis”, in Proc. of the International Conference on Document Analysis and Recognition, 2003.
Based on gist https://gist.github.com/erniejunior/601cdf56d2b424757de5
Parameters: - image (np.ndarray) – image to be deformed
- alpha (list) – scale of transformation for each dimension, where larger values have more deformation
- sigma (list) – Gaussian window of deformation for each dimension, where smaller values have more localised deformation
Returns: deformed image
Return type: np.ndarray
-
dltk.io.augmentation.
extract_class_balanced_example_array
(image, label, example_size=[1, 64, 64], n_examples=1, classes=2, class_weights=None)[source]¶ - Extract training examples from an image (and corresponding label) subject
- to class balancing. Returns an image example array and the corresponding label array.
Parameters: - image (np.ndarray) – image to extract class-balanced patches from
- label (np.ndarray) – labels to use for balancing the classes
- example_size (list or tuple) – shape of the patches to extract
- n_examples (int) – number of patches to extract in total
- classes (int or list or tuple) – number of classes or list of classes to extract
Returns: - class-balanced patches extracted from full
images with the shape [batch, example_size..., image_channels]
Return type: np.ndarray, np.ndarray
-
dltk.io.augmentation.
extract_random_example_array
(image_list, example_size=[1, 64, 64], n_examples=1)[source]¶ - Randomly extract training examples from image (and a corresponding label).
- Returns an image example array and the corresponding label array.
Parameters: - image_list (np.ndarray or list or tuple) – image(s) to extract random patches from
- example_size (list or tuple) – shape of the patches to extract
- n_examples (int) – number of patches to extract in total
Returns: class-balanced patches extracted from full images with the shape [batch, example_size..., image_channels]
Return type: np.ndarray, np.ndarray
-
dltk.io.augmentation.
flip
(imagelist, axis=1)[source]¶ Randomly flip spatial dimensions
Parameters: - imagelist (np.ndarray or list or tuple) – image(s) to be flipped
- axis (int) – axis along which to flip the images
Returns: - same as imagelist but randomly flipped
along axis
Return type: np.ndarray or list or tuple
dltk.io.preprocessing module¶
-
dltk.io.preprocessing.
normalise_one_one
(image)[source]¶ Image normalisation. Normalises image to fit [-1, 1] range.
-
dltk.io.preprocessing.
normalise_zero_one
(image)[source]¶ Image normalisation. Normalises image to fit [0, 1] range.
-
dltk.io.preprocessing.
resize_image_with_crop_or_pad
(image, img_size=(64, 64, 64), **kwargs)[source]¶ - Image resizing. Resizes image by cropping or padding dimension
- to fit specified size.
Parameters: - image (np.ndarray) – image to be resized
- img_size (list or tuple) – new image size
- kwargs () – additional arguments to be passed to np.pad
Returns: resized image
Return type: np.ndarray
dltk.core.io.abstract_reader module¶
-
class
dltk.io.abstract_reader.
IteratorInitializerHook
[source]¶ Bases:
tensorflow.python.training.session_run_hook.SessionRunHook
Hook to initialise data iterator after Session is created.
-
class
dltk.io.abstract_reader.
Reader
(read_fn, dtypes)[source]¶ Bases:
object
Wrapper for dataset generation given a read function
Constructs a Reader instance
Parameters: - read_fn – Input function returning features which is a dictionary of string feature name to Tensor or SparseTensor. If it returns a tuple, first item is extracted as features. Prediction continues until input_fn raises an end-of-input exception (OutOfRangeError or StopIteration).
- dtypes – A nested structure of tf.DType objects corresponding to each component of an element yielded by generator.
-
get_dataset
(file_references, mode, example_shapes=None, shuffle_cache_size=100, batch_size=4, params=None)[source]¶ Function to get a tf.data.Dataset from a python generator.
Parameters: - file_references – An array like structure that holds the reference to the file to read. It can also be None if not needed.
- mode – A tf.estimator.ModeKeys. It is passed on to read_fn to trigger specific functions there.
- example_shapes (optional) – A nested structure of lists or tuples corresponding to the shape of each component of an element yielded by generator.
- shuffle_cache_size (int, optional) – An int determining the number of examples that are held in the shuffle queue.
- batch_size (int, optional) – An int specifying the number of examples returned in a batch.
- params (dict, optional) – A dict passed on to the read_fn.
Returns: The dataset holding the data to look at.
Return type: tf.data.Dataset
-
get_inputs
(file_references, mode, example_shapes=None, shuffle_cache_size=100, batch_size=4, params=None)[source]¶ Function to provide the input_fn for a tf.Estimator.
Parameters: - file_references – An array like structure that holds the reference to the file to read. It can also be None if not needed.
- mode – A tf.estimator.ModeKeys. It is passed on to read_fn to trigger specific functions there.
- example_shapes (optional) – A nested structure of lists or tuples corresponding to the shape of each component of an element yielded by generator.
- shuffle_cache_size (int, optional) – An int determining the number of examples that are held in the shuffle queue.
- batch_size (int, optional) – An int specifying the number of examples returned in a batch.
- params (dict, optional) – A dict passed on to the read_fn.
Returns: - a handle to the input_fn to be passed the relevant
tf estimator functions.
- tf.train.SessionRunHook: A hook to initialize the queue within
the dataset.
Return type: function
-
serving_input_receiver_fn
(placeholder_shapes)[source]¶ Build the serving inputs.
Parameters: placeholder_shapes – A nested structure of lists or tuples corresponding to the shape of each component of the feature elements yieled by the read_fn. Returns: A function to be passed to the tf.estimator.Estimator instance when exporting a saved model with estimator.export_savedmodel. Return type: function