inet.models.architectures package

Submodules

inet.models.architectures.base_model module

A collection of different base models to build different model architectures.

class Backbone(*args, **kwargs)[source]

Bases: inet.models.architectures.base_model.Model

An alias for Backbone/Feature extractor models.

class Model(*args, **kwargs)[source]

Bases: keras.engine.training.Model

Helper class to store a human-readable name

class SingleTaskModel(*args, **kwargs)[source]

Bases: inet.models.architectures.base_model.TaskModel

Dedicated model architecture to solve a single task, classification or regression. Appends a backbone model with - [optional] global max pooling layer - 1 dropout layer with parameter dropout_factor - 1 dense layer with dense_neurons number neurons - 1 dense block * batch normalization * ReLU activation function * dense layer with num_classes neurons and regularization_factor for L2 kernel regularization

Example:
>>> from tensorflow.keras.applications.vgg16 import VGG16
>>> vgg_backbone = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
>>> # building a classifier for 5 classes
>>> clf = SingleTaskModel(vgg_backbone, 5, 'softmax')
>>> # building a bounding box regressor (outputs: [y, x, height, width])
>>> reg = SingleTaskModel(vgg_backbone, 4, 'relu')
static evaluate_predictions(predictions, labels, features, render_samples=False)[source]

Helper method to evaluate predictive power of model.

Parameters
  • predictions – predictions done by the model

  • labels – true labels for the task

  • features – features used to make predictions

  • render_samples – boolean flag to append visualization of samples

Returns

classmethod from_config(cfg)[source]

Load model from configuration

class TaskModel(*args, **kwargs)[source]

Bases: inet.models.architectures.base_model.Model

A model tailored to solve a task. Essentially keras.Model with helper methods and wrappers for training methods.

Example:
>>> from tensorflow.keras.applications.mobilenet import MobileNet
>>> backbone = MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224))
>>> my_model = TaskModel(inputs=[backbone.input], outputs=[backbone.output])
classmethod default_callbacks(monitoring_val, verbose, model_name)[source]

Hidden helper function to create default callbacks for fit method.

Consists of: - keras.callbacks.TensorBoard - keras.callbacks.ModelCheckpoint - keras.callbacks.EarlyStopping - keras.callbacks.ReduceLROnPlateau - keras.callbacks.ProgbarLogger - keras.callbacks.TerminateOnNaN

Parameters
  • monitoring_val – value to monitor

  • verbose – use verbose output

  • model_name – name of the model

Returns

list of default callbacks

evaluate_model(validation_set: inet.data.datasets.ImageDataSet, preprocessing_method: Optional[Callable] = None, render_samples: bool = False) None[source]

Method to evaluate a models predictive power.

Parameters
  • validation_set – the validation data set to use

  • preprocessing_method – optional preprocessing_method for features. Gets applied before feeding the features into the model

  • render_samples – boolean flag to append visualization of samples

Returns

static evaluate_predictions(predictions, labels, features, render_samples=False) None[source]

Implement this method to evaluate a models predictive power individually. For examples see .classifier.Classifier or .bounding_boxes.BoundingBoxRegressor.

Parameters
  • predictions – predictions done by the model

  • labels – true labels for the task

  • features – features used to make predictions

  • render_samples – boolean flag to append visualization of samples

Returns

extract_backbone_features(train_set, validation_set) Tuple[inet.data.constants.LabelType, inet.data.constants.LabelType][source]

Processes inputs using weights from backbone feature extractor.

Parameters
  • train_set

  • validation_set

Returns

fit(train_set, validation_set, monitoring_val, batch_size: int = 32, epochs: int = 20, verbose: bool = True, *args, **kwargs)[source]

Extended keras.Model.fit to add default callbacks

Parameters
  • train_set – dataset for training

  • validation_set – dataset for validation

  • monitoring_val – value to monitor while using EarlyStopping/ModelCheckpoint callbacks

  • batch_size – the batch size to use when training the model

  • epochs – number of epochs used in the training process

  • verbose – verbosity setting

  • args – further args to pass to keras.Model.fit

  • kwargs – further kwargs to pass to keras.Model.fit

Returns

model_type: inet.data.constants.ModelType = None
to_tflite(quantization_method: QuantizationMethod, train_set, test_set)[source]

Converts the model to a tflite compatible model

inet.models.architectures.bounding_boxes module

class BoundingBoxHyperModel(name=None, tunable=True)[source]

Bases: keras_tuner.engine.hypermodel.HyperModel

HPO wrapper for Bounding Box Regression model.

Used Hyper parameters (HPs): - Dropout Factor alpha: [1e-4, 5e-4, 1e-3, 5e-3, 1e-2] - Learning rate learning_rate: [1e-4, 5e-4, 1e-3, 5e-3, 1e-2]

Example:
>>> import keras_tuner as kt
>>> hpo_model = BoundingBoxHyperModel()
>>> tuner = kt.BayesianOptimization(
...    hpo_model,
...    objective=kt.Objective('val_loss', 'min'),
...    max_trials=36,
...    directory=f'./model-selection/my-model/',
...    project_name='proj_name',
...    seed=42,
...    overwrite=False,
...    num_initial_points=12
...)
>>> tuner.search(
...     train_set=train_set.unbatch(),
...     validation_set=validation_set.unbatch(),
...     monitoring_val='val_loss',
...     epochs=50,
... )
build(hp)[source]

Build model for HPO

Parameters

hp – hp storage

Returns

next model for HPO

model_data: Optional[inet.models.data_structures.ModelArchitecture] = None
class BoundingBoxRegressor(*args, **kwargs)[source]

Bases: inet.models.architectures.base_model.SingleTaskModel

Bounding Box Regression model

Example:
>>> from tensorflow.keras.applications.mobilenet import MobileNet
>>> backbone = MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224))
>>> regressor = BoundingBoxRegressor(backbone, 128, True, 'my-model', 0.125, 0.5, 64, 'relu')
>>> regressor.load_weights('my-weights.h5')
>>> regressor.predict([some_image])
compile(learning_rate: float = 1e-06, loss='mse', metrics=None, *args, **kwargs)[source]

Extended keras.Model.compile. Adds default Adam optimizer and metrics RMSE & GIoU-Loss

Parameters
  • learning_rate – the learning rate to train with

  • loss – the loss function to optimize

  • metrics – additional metrics to calculate during training

  • args – will be passed as args to parent implementation

  • kwargs – will be passed as kwargs to parent implementation

Returns

static evaluate_predictions(predictions, labels, features, render_samples=False) None[source]

Evaluation method for BBox-Regression. Calculates metrics: - GIoU-Loss - GIoU - RMSE

Parameters
  • predictions – predictions done by the model

  • labels – ground truth for predictions

  • features – used input features to perform predictions

  • render_samples – when True renders up to 25 BBox prediction samples

Returns

model_type: inet.data.constants.ModelType = 0

inet.models.architectures.classifier module

class Classifier(*args, **kwargs)[source]

Bases: inet.models.architectures.base_model.SingleTaskModel

Class label prediction model

Example:
>>> from tensorflow.keras.applications.vgg16 import VGG16
>>> backbone = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
>>> clf = Classifier(backbone, 128, 5, True, 'My-Classifier', 0.125, 0.5, 64, FrozenBlockConf.TRAIN_ALL.value)
>>> clf.load_weights('my_weights.h5')
>>> clf.predict(some_input)
compile(learning_rate: float = 1e-06, loss='categorical_crossentropy', metrics=None, *args, **kwargs)[source]

Extended keras.Model.compile. Adds default Adam optimizer and Accuracy metric

Parameters
  • learning_rate – the learning rate to train with

  • loss – the loss function to optimize

  • metrics – additional metrics to calculate during training

  • args – will be passed as args to parent implementation

  • kwargs – will be passed as kwargs to parent implementation

Returns

static evaluate_predictions(predictions, labels, features, render_samples=False) None[source]

Evaluates predictions done by a classification model.

Computes: - Accuracy - F1-Score

Parameters
  • predictions – the predictions performed by the model to evaluate

  • labels – ground truth labels for the predictions

  • features – input features used to perform predictions

  • render_samples – if True renders confusion matrix for predictions

Returns

model_type: inet.data.constants.ModelType = 1
class ClassifierHyperModel(name=None, tunable=True)[source]

Bases: keras_tuner.engine.hypermodel.HyperModel

HPO wrapper for Classifier model.

Used Hyper parameters (HPs): - Dropout factor alpha: [1e-4, 5e-4, 1e-3, 5e-3, 1e-2] - Learning rate learning_rate: [1e-4, 5e-4, 1e-3, 5e-3, 1e-2] - Number frozen layers frozen_layers: [TRAIN_ALL, TRAIN_HALF, TRAIN_NONE]

Example:
>>> import keras_tuner as kt
>>> hpo_model = ClassifierHyperModel()
>>> kt.BayesianOptimization(
...    hpo_model,
...    objective=kt.Objective('val_accuracy', 'max'),
...    max_trials=36,
...    directory=f'./model-selection/my-model/',
...    project_name='proj_name',
...    seed=42,
...    overwrite=False,
...    num_initial_points=12
... )
>>> tuner.search(
...     train_set=train_set.unbatch(),
...     validation_set=validation_set.unbatch(),
...     monitoring_val='val_accuracy',
...     epochs=50,
... )
build(hp)[source]

Builds new classification model for HPO

Parameters

hp – current state of HPs

Returns

model for next iteration in HPO

model_data: Optional[inet.models.data_structures.ModelArchitecture] = None
weights: str = None