def __init__(self, model_spec, index_to_label, shuffle=True, hparams=hub_lib.get_default_hparams(), use_augmentation=False, representative_data=None): """Init function for ImageClassifier class. Args: model_spec: Specification for the model. index_to_label: A list that map from index to label class name. shuffle: Whether the data should be shuffled. hparams: A namedtuple of hyperparameters. This function expects .dropout_rate: The fraction of the input units to drop, used in dropout layer. .do_fine_tuning: If true, the Hub module is trained together with the classification layer on top. use_augmentation: Use data augmentation for preprocessing. representative_data: Representative dataset for full integer quantization. Used when converting the keras model to the TFLite model with full interger quantization. """ super(ImageClassifier, self).__init__(model_spec, index_to_label, shuffle, hparams.do_fine_tuning) num_classes = len(index_to_label) self._hparams = hparams self.preprocess = image_preprocessing.Preprocessor( self.model_spec.input_image_shape, num_classes, self.model_spec.mean_rgb, self.model_spec.stddev_rgb, use_augmentation=use_augmentation) self.history = None # Training history that returns from `keras_model.fit`. self.representative_data = representative_data
def __init__(self, model_spec, index_to_label, num_classes, shuffle=True, hparams=hub_lib.get_default_hparams(), use_augmentation=False): """Init function for ImageClassifier class. Args: model_spec: Specification for the model. index_to_label: A list that map from index to label class name. num_classes: Number of label classes. shuffle: Whether the data should be shuffled. hparams: A namedtuple of hyperparameters. This function expects .dropout_rate: The fraction of the input units to drop, used in dropout layer. .do_fine_tuning: If true, the Hub module is trained together with the classification layer on top. use_augmentation: Use data augmentation for preprocessing. """ super(ImageClassifier, self).__init__(model_spec, index_to_label, num_classes, shuffle, hparams.do_fine_tuning) self.hparams = hparams self.model = self._create_model() self.preprocessor = image_preprocessing.Preprocessor( self.model_spec.input_image_shape, num_classes, self.model_spec.mean_rgb, self.model_spec.stddev_rgb, use_augmentation=use_augmentation) self.history = None # Training history that returns from `keras_model.fit`.
def __init__(self, model_export_format, model_spec, index_to_label, num_classes, shuffle=True, hparams=lib.get_default_hparams(), use_augmentation=False): """Init function for ImageClassifier class. Args: model_export_format: Model export format such as saved_model / tflite. model_spec: Specification for the model. index_to_label: A list that map from index to label class name. num_classes: Number of label classes. shuffle: Whether the data should be shuffled. hparams: A namedtuple of hyperparameters. This function expects .dropout_rate: The fraction of the input units to drop, used in dropout layer. use_augmentation: Use data augmentation for preprocessing. """ super(ImageClassifier, self).__init__(model_export_format, model_spec, index_to_label, num_classes, shuffle, hparams.do_fine_tuning) self.hparams = hparams self.model = self._create_model() self.preprocessor = image_preprocessing.Preprocessor( self.model_spec.input_image_shape, num_classes, self.model_spec.mean_rgb, self.model_spec.stddev_rgb, use_augmentation=use_augmentation)
def test_without_augmentation(self): preprocessor = image_preprocessing.Preprocessor([2, 2], 2, mean_rgb=[0.0], stddev_rgb=[255.0], use_augmentation=False) actual_image = np.array([[[0., 0.00392157, 0.00784314], [0.14117648, 0.14509805, 0.14901961]], [[0.37647063, 0.3803922, 0.38431376], [0.5176471, 0.52156866, 0.5254902]]]) image = _get_preprocessed_image(preprocessor) self.assertTrue(np.allclose(image, actual_image, atol=1e-05))
def test_with_augmentation(self): image_preprocessing.CROP_PADDING = 1 preprocessor = image_preprocessing.Preprocessor([2, 2], 2, mean_rgb=[0.0], stddev_rgb=[255.0], use_augmentation=True) # Tests validation image. actual_eval_image = np.array([[[0.17254902, 0.1764706, 0.18039216], [0.26666668, 0.27058825, 0.27450982]], [[0.42352945, 0.427451, 0.43137258], [0.5176471, 0.52156866, 0.5254902]]]) image = _get_preprocessed_image(preprocessor, is_training=False) self.assertTrue(np.allclose(image, actual_eval_image, atol=1e-05)) # Tests training image. image1 = _get_preprocessed_image(preprocessor, is_training=True) image2 = _get_preprocessed_image(preprocessor, is_training=True) self.assertFalse(np.allclose(image1, image2, atol=1e-05)) self.assertEqual(image1.shape, (2, 2, 3)) self.assertEqual(image2.shape, (2, 2, 3))