inet.models.tf_lite package

Submodules

inet.models.tf_lite.convert_to_tflite module

class ClusterMethod(value)[source]

Bases: enum.Enum

Helper enum to determine a cluster methods

DENSITY_BASED = CentroidInitialization.DENSITY_BASED
KMEANS_PLUS_PLUS = CentroidInitialization.KMEANS_PLUS_PLUS
LINEAR = CentroidInitialization.LINEAR
RANDOM = CentroidInitialization.RANDOM
class QuantizationMethod(value)[source]

Bases: enum.Enum

Helper enum to determine a quantization method

DYNAMIC = 1
FLOAT_16 = 2
FULL_INT = 3
NONE = None
cluster_weights(model, cluster_method: inet.models.tf_lite.convert_to_tflite.ClusterMethod, number_clusters)[source]

Clusters weights of given model in number_clusters clusters, using given method cluster_method.

Note this will change the underlying weights of `model`. In case you want to validate your model, perform a prediction prior to calling this method!

Hint: Use this in combination with CentroidInitialization.KMEANS_PLUS_PLUS for MobileNet on the Regression task, this preserves weights in the domain 1e-15 well.

Parameters
  • model

  • cluster_method – one of CentroidInitialization.KMEANS_PLUS_PLUS, CentroidInitialization.DENSITY_BASED, CentroidInitialization.RANDOM, CentroidInitialization.LINEAR

  • number_clusters

Returns

create_pruned_model(model, test_set)[source]

Method to create and evaluate a pruned version of given model

Parameters
  • model – the model to prune

  • test_set – test set for performance validation

Returns

pruned version of model

create_q_aware_model(model)[source]

Create quantization aware model

Parameters

model – model to convert

Returns

quantization aware model

create_quantize_model(model: inet.models.architectures.base_model.TaskModel, train_set, test_set, quant_method: inet.models.tf_lite.convert_to_tflite.QuantizationMethod)[source]

Method to create and validate a quantized version of model using quant_method.

Parameters
  • model – the model instance to quantize

  • train_set – the train set, will be used as representation when using QuantizationMethod.FULL_INT

  • test_set – to evaluate the models

  • quant_method – quantization method applied onto the model

Returns

q-aware-model

create_tf_lite_q_model(q_model, train_set, quant_method: inet.models.tf_lite.convert_to_tflite.QuantizationMethod = QuantizationMethod.FULL_INT, model_name='bbreg')[source]

converts regular model to q aware model using provided quant_method

Parameters
  • q_model – a quantization aware model

  • train_set – samples representing the train set

  • quant_method – quantization method

  • model_name – resulting model name

Returns

tf lite version of quantization aware model

inet.models.tf_lite.tflite_methods module

evaluate_classification(model_predictions, tfl_model_predictions, test_labels)[source]

Evaluation of classification model

Parameters
  • model_predictions – predictions done by the original model

  • tfl_model_predictions – predictions done by the tflite version of the original model

  • test_labels – ground truth labels

Returns

evaluate_interpreted_model(interpreter, test_images)[source]

Method to evaluate an interpreted (tflite) model

Parameters
  • interpreter – interpreted model

  • test_images – input to evaluate

Returns

predictions of interpreted model

evaluate_q_model(tf_lite_model, test_images)[source]

Evaluation method for quantization aware model

Parameters
  • tf_lite_model – q-aware tflite model

  • test_images – input to perform prediction on

Returns

predictions for given images

evaluate_regression(model_predictions, tfl_model_predictions, test_labels)[source]

Evaluation method for TFLite regression model

Parameters
  • model_predictions – predictions done by the original model

  • tfl_model_predictions – predictions done by the tflite version of the original model

  • test_labels – ground truth labels

Returns

evaluate_two_in_one(model_predictions, tfl_model_predictions, test_labels)[source]

Evaluation of two-in-one model

Parameters
  • model_predictions – predictions done by the original model

  • tfl_model_predictions – predictions done by the tflite version of the original model

  • test_labels – ground truth labels

Returns

get_gzipped_model_size(model)[source]

Computes size of gzip converted model

save_model_file(model)[source]

Writes model to .h5 file

validate_q_model_prediction(model_prediction, tfl_model_prediction, test_labels, model_type) None[source]

Validates a tflite model, comparing values with its original predecessor.

Parameters
  • model_prediction – predictions done by the original model

  • tfl_model_prediction – predictions done by the tflite version of the original model

  • test_labels – ground truth labels

  • model_typeModelType of the underlying model

Returns