inet.data package

Submodules

inet.data.augmentation module

Helper classes/methods to generate augmented dataset.

class AugmentationMethod(probability: float = 1.0)[source]

Bases: abc.ABC

Abstract Base Class for augmentation methods.

Require implementation of process method, as well as passing probability to the constructor.

label_value_type: Optional[inet.data.constants.LabelType] = None
process(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Processing method for AugmentationMethod implementations. Override this method to perform augmentation techniques onto features and labels

Parameters
  • features – feature vector of single sample

  • labels – label vector of single sample (multiple labels allowed)

Returns

processed pair of (features, labels)

class DataAugmentationHelper(number_samples: int, operations: Optional[List[inet.data.augmentation.AugmentationMethod]] = None, batch_size: int = 32, seed: int = 42, bbox_label_index: Optional[int] = None, label_value_type: inet.data.constants.LabelType = 1, output_signature: Optional[Tuple] = None)[source]

Bases: object

Augmentation helper to generate an augmentation pipeline.

Example:
>>> helper = DataAugmentationHelper(
...     1024,
...     operations=[
...         RandomCrop(0.5),
...         RandomFlip(0.5),
...     ]
... )
>>> augmented_dataset = helper.transform(my_dataset)
transform(dataset: tensorflow.python.data.ops.dataset_ops.DatasetV2) tensorflow.python.data.ops.dataset_ops.DatasetV2[source]

Wrapper method to generate a tf.data.Dataset with augmentation according to passed operations array.

Parameters

dataset – the dataset to convert to an augmented dataset

Returns

augmented dataset

transform_generator(dataset: tensorflow.python.data.ops.dataset_ops.DatasetV2) tensorflow.python.data.ops.dataset_ops.DatasetV2[source]

Generator method yielding number_samples augmented samples.

This method is essentially just a wrapper around __perform_transformation, it configures the environment and yields number_samples augmented samples sequentially using the provided dataset. After each cycle of the dataset it gets shuffled prior to yielding the next sample.

Parameters

dataset – input dataset to augment on

Returns

generator yielding augmented samples

class MultiProbabilityAugmentationMethod(probability: float, shared_probabilities: Optional[Tuple[float, ...]] = None)[source]

Bases: inet.data.augmentation.AugmentationMethod

Helper class to perform probability based operation on a sample.

Inherit from this class to implement AugmentationMethod with multiple processing methods.

property method_list: List[Callable[[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]], Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]]]]

Property of implemented child methods.

Implement this in the child class.

Returns

list of methods to chose from

process(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Helper method to process a sample.

Determines child method to use based on configured shared_probabilities attribute. Sequentially iterates over list of methods, then decides individually :param features: vector holding features to augment :param labels: label vector related to features :return:

shared_probabilities = []
class RandomChannelIntensity(probability: float, shared_probabilities: Optional[Tuple[float, ...]] = None)[source]

Bases: inet.data.augmentation.MultiProbabilityAugmentationMethod

Method to randomly change the color intensity contrast of the input image.

No transformation of input labels.

property method_list: List[Callable[[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]], Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]]]]

See MultiProbabilityAugmentationMethod.method_list

static random_scale_n_channels(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to scale N (max 3) random channels by an individual random factor

Parameters
  • features – features to perform method on

  • labels – optional array of labels, will not be transformed

Returns

transformed sample

static random_scale_single_channel(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to scale one random channel by a random factor.

Parameters
  • features – features to perform method on

  • labels – optional array of labels, will not be transformed

Returns

transformed sample

static random_set_single_channel(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to set a random value for a channel.

Parameters
  • features – features to perform method on

  • labels – optional array of labels, will not be transformed

Returns

transformed sample

shared_probabilities = (0.3333333333333333, 0.3333333333333333, 0.3333333333333333)
class RandomContrast(probability: float = 1.0)[source]

Bases: inet.data.augmentation.AugmentationMethod

Method to randomly increases/decreases contrast of input image by multiplying the feature vector with a random value from the set [0.5, 0.6, 0.7, …, 1.3].

No transformation of input labels.

process(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Processing method for AugmentationMethod implementations. Override this method to perform augmentation techniques onto features and labels

Parameters
  • features – feature vector of single sample

  • labels – label vector of single sample (multiple labels allowed)

Returns

processed pair of (features, labels)

class RandomCrop(probability: float, shared_probabilities: Optional[Tuple[float, ...]] = None)[source]

Bases: inet.data.augmentation.MultiProbabilityAugmentationMethod

Method to randomly crop the input image.

Available cropping methods:
  • left

  • right

  • top

  • bottom

  • top-left

  • bottom-right

Under the hood the method crops pct percent from the area between the chosen side and the provided Bounding Box label.

Note: Requires Bounding Box labels to be passed.

bottom(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None, pct=50) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Crops pct % of area between image top-border and bounding box.

Parameters
  • features – features to perform cropping on

  • labels – optional array of labels, will be transformed accordingly

  • pct – Cut-off percentage (100 full, 0 none)

Returns

cropped sample

bottom_right(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None, pct=100) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Combined cropping method, crops first right, then bottom part, sequentially, using same pct %.

Parameters
  • features – features to perform cropping on

  • labels – optional array of labels, will be transformed accordingly

  • pct – Cut-off percentage (100 full, 0 none)

Returns

cropped sample

left(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None, pct=50) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Crops pct % of area between image top-border and bounding box.

Parameters
  • features – features to perform cropping on

  • labels – optional array of labels, will be transformed accordingly

  • pct – Cut-off percentage (100 full, 0 none)

Returns

cropped sample

property method_list: List[Callable[[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]], Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]]]]

See MultiProbabilityAugmentationMethod.method_list

right(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None, pct=50) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Crops pct % of area between image top-border and bounding box.

Parameters
  • features – features to perform cropping on

  • labels – optional array of labels, will be transformed accordingly

  • pct – Cut-off percentage (100 full, 0 none)

Returns

cropped sample

shared_probabilities = (0.1, 0.1, 0.1, 0.1, 0.5, 0.5)
top(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None, pct=50) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Crops pct % of area between image top-border and bounding box.

Parameters
  • features – features to perform cropping on

  • labels – optional array of labels, will be transformed accordingly

  • pct – Cut-off percentage (100 full, 0 none)

Returns

cropped sample

top_left(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None, pct=100) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Combined cropping method, crops first left, then top part, sequentially, using same pct %.

Parameters
  • features – features to perform cropping on

  • labels – optional array of labels, will be transformed accordingly

  • pct – Cut-off percentage (100 full, 0 none)

Returns

cropped sample

class RandomFlip(probability: float, shared_probabilities: Optional[Tuple[float, ...]] = None)[source]

Bases: inet.data.augmentation.MultiProbabilityAugmentationMethod

Method to randomly flip the input features horizontal or vertical. Flips the provided Bounding Box values accordingly.

static horizontal_flip(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to flip a sample on its horizontal axis.

Parameters
  • features – features to perform flipping on

  • labels – optional array of labels, will be transformed accordingly

Returns

property method_list: List[Callable[[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]], Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]]]]

See MultiProbabilityAugmentationMethod.method_list

shared_probabilities = (0.5, 0.5)
static vertical_flip(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to flip a sample on its vertical axis

Parameters
  • features – features to perform flipping on

  • labels – optional array of labels, will be transformed accordingly

Returns

transformed sample

class RandomRotate90(probability: float, shared_probabilities: Optional[Tuple[float, ...]] = None)[source]

Bases: inet.data.augmentation.MultiProbabilityAugmentationMethod

Method to randomly rotate left, or right.

Transforms label accordingly.

property method_list: List[Callable[[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]], Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]]]]

See MultiProbabilityAugmentationMethod.method_list

static rotate_left(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to rotate left.

Parameters
  • features – features to perform flipping on

  • labels – optional array of labels, will be transformed accordingly

Returns

transformed sample

static rotate_right(features: Iterable[Any], labels: Optional[Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]] = None) Tuple[Iterable[Any], Iterable[Union[ClassLabelType, BoundingBoxLabelType, Tuple[ClassLabelType, BoundingBoxLabelType]]]][source]

Method to rotate a sample to the right.

Parameters
  • features – features to perform flipping on

  • labels – optional array of labels, will be transformed accordingly

Returns

transformed sample

shared_probabilities = (0.5, 0.5)

inet.data.constants module

class LabelType(value)[source]

Bases: enum.Enum

An enumeration.

MULTI = 2
NONE = 0
SINGLE = 1
class ModelType(value)[source]

Bases: enum.Enum

An enumeration.

CLASSIFICATION = 1
REGRESSION = 0
TWO_IN_ONE = 2

inet.data.datasets module

Data set helpers. Use the methods defined in load_datasets.py to

class ImageBoundingBoxDataSet(parent_directory: str, img_width: int, img_height: int, set_name: Optional[str] = None, batch_size: int = 32, class_names: Optional[List[str]] = None)[source]

Bases: inet.data.datasets.ImageDataSet

yields objects of form (image, bounding box).

Image in pixel color values (0, 255) Bounding Box in percentages (0, 100)

label_key: str = 'bbs'
output_signature: Tuple[tensorflow.python.framework.tensor_spec.TensorSpec, Union[tensorflow.python.framework.tensor_spec.TensorSpec, Tuple[tensorflow.python.framework.tensor_spec.TensorSpec, ...]]] = (TensorSpec(shape=(), dtype=tf.string, name=None), TensorSpec(shape=(4,), dtype=tf.float32, name=None))
class ImageDataSet(parent_directory: str, img_width: int, img_height: int, set_name: Optional[str] = None, batch_size: int = 32, class_names: Optional[List[str]] = None)[source]

Bases: object

Base class to define general behavior of an image data set.

build_dataset()[source]

Method to build the dataset :return: the data set wrapped inside a tf.data.Dataset instance

label_key: str = None
output_signature: Tuple[tensorflow.python.framework.tensor_spec.TensorSpec, Union[tensorflow.python.framework.tensor_spec.TensorSpec, Tuple[tensorflow.python.framework.tensor_spec.TensorSpec, ...]]] = None
class ImageLabelDataSet(parent_directory: str, img_width: int, img_height: int, set_name: Optional[str] = None, batch_size: int = 32, class_names: Optional[List[str]] = None)[source]

Bases: inet.data.datasets.ImageDataSet

yields objects of form (image, label)

label_key: str = 'label'
class ImageTwoInOneDataSet(parent_directory: str, img_width: int, img_height: int, set_name: Optional[str] = None, batch_size: int = 32, class_names: Optional[List[str]] = None)[source]

Bases: inet.data.datasets.ImageDataSet

yields objects of form (image, (label, bounding box))

inet.data.load_dataset module

directory_to_classification_dataset(directory: str, batch_size: int = 32, img_height: int = 224, img_width: int = 224, class_names: Optional[List[str]] = None)[source]
directory_to_regression_dataset(directory: str, batch_size: int = 32, img_height: int = 224, img_width: int = 224, class_names: Optional[List[str]] = None) Tuple[inet.data.datasets.ImageBoundingBoxDataSet, inet.data.datasets.ImageBoundingBoxDataSet, inet.data.datasets.ImageBoundingBoxDataSet][source]
directory_to_two_in_one_dataset(directory: str, batch_size: int = 32, img_height: int = 224, img_width: int = 224, class_names: Optional[List[str]] = None) Tuple[inet.data.datasets.ImageTwoInOneDataSet, inet.data.datasets.ImageTwoInOneDataSet, inet.data.datasets.ImageTwoInOneDataSet][source]

inet.data.visualization module

plot_confusion_matrix(y_true, y_pred, classes, normalize=False, colormap=<matplotlib.colors.LinearSegmentedColormap object>, title=None)[source]

This function prints and plots the confusion matrix. Normalization can be applied by setting normalize=True. potentially good color maps from matplotlib:

Color maps to visualize positive cases [‘Blues’, ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘Greens’, ‘Greys’, ‘OrRd’, ‘Oranges’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘Purples’, ‘RdPu’, ‘Reds’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, ‘YlOrRd’, ‘afmhot_r’, ‘autumn_r’, ‘binary’, ‘bone_r’, ‘cividis_r’, ‘cool_r’, ]

Color maps to visualize negative cases [‘Wistia’, ‘brg_r’, ‘bwr_r’]

Parameters
  • y_true – array of ground truth values

  • y_pred – predictions done by a model

  • classes – verbatim class names

  • normalize – use normalized confusion matrix

  • colormap – the color map to use

  • title – the title for the resulting plot

Returns

a matplotlib.pyplot.axis object containing the generated plot

plot_histories(hists, keys, titles)[source]

Method to visualize loss/accuracy course during the training phase

Parameters
  • hists

  • keys

  • titles

Returns

plot_prediction(image, bb, true_bb=None, label=None, color='y')[source]

Helper method to plot bounding box and label of a single sample

Parameters
  • image

  • bb

  • true_bb

  • label

  • color

Returns

plot_prediction_samples(predicted_bbs, validation_features, predicted_labels=None, validation_bbs=None, img_width=224, img_height=224, include_score=False) None[source]

Method to plot up to 25 samples of combined bounding box and class labels.

Parameters
  • predicted_bbs – bounding box predictions by a method

  • predicted_labels – class labels predicted by a method

  • validation_features – used features to extract predicted_bbs and predicted_labels

  • validation_bbs – true class labels

  • img_width – original image width

  • img_height – original image height

  • include_score – if true renders class label confidence into label

Returns