def test_create_metadata_should_succeed(self): label_file_en = metadata_info.LabelFileMd( file_path=self._LABEL_FILE_EN, locale="en") label_file_cn = metadata_info.LabelFileMd( file_path=self._LABEL_FILE_CN, locale="cn") tesnor_md = metadata_info.CategoryTensorMd( name=self._NAME, description=self._DESCRIPTION, label_files=[label_file_en, label_file_cn]) tensor_metadata = tesnor_md.create_metadata() metadata_json = _metadata.convert_to_json( _create_dummy_model_metadata(tensor_metadata)) expected_json = test_utils.load_file(self._EXPECTED_TENSOR_JSON, "r") self.assertEqual(metadata_json, expected_json)
def test_create_metadata_should_succeed(self, tensor_type, golden_json): label_file_en = metadata_info.LabelFileMd( file_path=self._LABEL_FILE_EN, locale="en") label_file_cn = metadata_info.LabelFileMd( file_path=self._LABEL_FILE_CN, locale="cn") tesnor_md = metadata_info.ClassificationTensorMd( name=self._NAME, description=self._DESCRIPTION, label_files=[label_file_en, label_file_cn], tensor_type=tensor_type) tensor_metadata = tesnor_md.create_metadata() metadata_json = _metadata.convert_to_json( _create_dummy_model_metadata(tensor_metadata)) expected_json = test_utils.load_file(golden_json, "r") self.assertEqual(metadata_json, expected_json)
def create_for_inference(cls, model_buffer: bytearray, input_norm_mean: List[float], input_norm_std: List[float], label_file_paths: List[str], score_calibration_md: Optional[ metadata_info.ScoreCalibrationMd] = None): """Creates mandatory metadata for TFLite Support inference. The parameters required in this method are mandatory when using TFLite Support features, such as Task library and Codegen tool (Android Studio ML Binding). Other metadata fields will be set to default. If other fields need to be filled, use the method `create_from_metadata_info` to edit them. Args: model_buffer: valid buffer of the model file. input_norm_mean: the mean value used in the input tensor normalization [1]. input_norm_std: the std value used in the input tensor normalizarion [1]. label_file_paths: paths to the label files [2] in the category tensor. Pass in an empty list, If the model does not have any label file. score_calibration_md: information of the score calibration operation [3] in the classification tensor. Optional if the model does not use score calibration. [1]: https://www.tensorflow.org/lite/convert/metadata#normalization_and_quantization_parameters [2]: https://github.com/tensorflow/tflite-support/blob/b80289c4cd1224d0e1836c7654e82f070f9eefaa/tensorflow_lite_support/metadata/metadata_schema.fbs#L108 [3]: https://github.com/tensorflow/tflite-support/blob/5e0cdf5460788c481f5cd18aab8728ec36cf9733/tensorflow_lite_support/metadata/metadata_schema.fbs#L434 Returns: A MetadataWriter object. """ input_md = metadata_info.InputImageTensorMd( name=_INPUT_NAME, description=_INPUT_DESCRIPTION, norm_mean=input_norm_mean, norm_std=input_norm_std, color_space_type=_metadata_fb.ColorSpaceType.RGB, tensor_type=writer_utils.get_input_tensor_types(model_buffer)[0]) output_category_md = metadata_info.CategoryTensorMd( name=_OUTPUT_CATRGORY_NAME, description=_OUTPUT_CATEGORY_DESCRIPTION, label_files=[ metadata_info.LabelFileMd(file_path=file_path) for file_path in label_file_paths ]) output_score_md = metadata_info.ClassificationTensorMd( name=_OUTPUT_SCORE_NAME, description=_OUTPUT_SCORE_DESCRIPTION, score_calibration_md=score_calibration_md) return cls.create_from_metadata_info( model_buffer, input_md=input_md, output_category_md=output_category_md, output_score_md=output_score_md)
def create_for_inference(cls, model_buffer: bytearray, sample_rate: int, channels: int, label_file_paths: List[str], score_calibration_md: Optional[ metadata_info.ScoreCalibrationMd] = None): """Creates mandatory metadata for TFLite Support inference. The parameters required in this method are mandatory when using TFLite Support features, such as Task library and Codegen tool (Android Studio ML Binding). Other metadata fields will be set to default. If other fields need to be filled, use the method `create_from_metadata_info` to edit them. Args: model_buffer: valid buffer of the model file. sample_rate: the sample rate in Hz when the audio was captured. channels: the channel count of the audio. label_file_paths: paths to the label files [1] in the classification tensor. Pass in an empty list if the model does not have any label file. score_calibration_md: information of the score calibration operation [2] in the classification tensor. Optional if the model does not use score calibration. [1]: https://github.com/tensorflow/tflite-support/blob/b80289c4cd1224d0e1836c7654e82f070f9eefaa/tensorflow_lite_support/metadata/metadata_schema.fbs#L95 [2]: https://github.com/tensorflow/tflite-support/blob/5e0cdf5460788c481f5cd18aab8728ec36cf9733/tensorflow_lite_support/metadata/metadata_schema.fbs#L434 Returns: A MetadataWriter object. """ # To make Task Library working properly, sample_rate, channels need to be # positive. if sample_rate <= 0: raise ValueError( "sample_rate should be positive, but got {}.".format( sample_rate)) if channels <= 0: raise ValueError( "channels should be positive, but got {}.".format(channels)) input_md = metadata_info.InputAudioTensorMd(_INPUT_NAME, _INPUT_DESCRIPTION, sample_rate, channels) output_md = metadata_info.ClassificationTensorMd( name=_OUTPUT_NAME, description=_OUTPUT_DESCRIPTION, label_files=[ metadata_info.LabelFileMd(file_path=file_path) for file_path in label_file_paths ], tensor_type=writer_utils.get_output_tensor_types(model_buffer)[0], score_calibration_md=score_calibration_md) return cls.create_from_metadata_info(model_buffer, input_md=input_md, output_md=output_md)
def test_create_from_metadata_info_succeeds_for_multihead(self): calibration_file1 = test_utils.create_calibration_file( self.get_temp_dir(), "score_cali_1.txt") calibration_file2 = test_utils.create_calibration_file( self.get_temp_dir(), "score_cali_2.txt") general_md = metadata_info.GeneralMd(name="AudioClassifier") input_md = metadata_info.InputAudioTensorMd( name="audio_clip", sample_rate=_SAMPLE_RATE, channels=_CHANNELS) # The output tensors in the model are: Identity, Identity_1 # Create metadata in a different order to test if MetadataWriter can correct # it. output_head_md_1 = metadata_info.ClassificationTensorMd( name="head1", label_files=[ metadata_info.LabelFileMd("labels_en_1.txt"), metadata_info.LabelFileMd("labels_cn_1.txt") ], score_calibration_md=metadata_info.ScoreCalibrationMd( _metadata_fb.ScoreTransformationType.LOG, _DEFAULT_SCORE_CALIBRATION_VALUE, calibration_file1), tensor_name="Identity_1") output_head_md_2 = metadata_info.ClassificationTensorMd( name="head2", label_files=[ metadata_info.LabelFileMd("labels_en_2.txt"), metadata_info.LabelFileMd("labels_cn_2.txt") ], score_calibration_md=metadata_info.ScoreCalibrationMd( _metadata_fb.ScoreTransformationType.LOG, _DEFAULT_SCORE_CALIBRATION_VALUE, calibration_file2), tensor_name="Identity") writer = ( audio_classifier.MetadataWriter.create_from_metadata_info_for_multihead( test_utils.load_file(_MULTIHEAD_MODEL), general_md, input_md, [output_head_md_1, output_head_md_2])) metadata_json = writer.get_metadata_json() expected_json = test_utils.load_file(_JSON_MULTIHEAD, "r") self.assertEqual(metadata_json, expected_json)
def test_create_metadata_should_succeed(self, tensor_type, golden_json): label_file_en = metadata_info.LabelFileMd( file_path=self._LABEL_FILE_EN, locale="en") label_file_cn = metadata_info.LabelFileMd( file_path=self._LABEL_FILE_CN, locale="cn") score_calibration_md = metadata_info.ScoreCalibrationMd( _metadata_fb.ScoreTransformationType.IDENTITY, self._CALIBRATION_DEFAULT_SCORE, self._SCORE_CALIBRATION_FILE) tesnor_md = metadata_info.ClassificationTensorMd( name=self._NAME, description=self._DESCRIPTION, label_files=[label_file_en, label_file_cn], tensor_type=tensor_type, score_calibration_md=score_calibration_md) tensor_metadata = tesnor_md.create_metadata() metadata_json = _metadata.convert_to_json( _create_dummy_model_metadata_with_tensor(tensor_metadata)) expected_json = test_utils.load_file(golden_json, "r") self.assertEqual(metadata_json, expected_json)
def add_classification_output( self, labels: Labels, score_calibration: Optional[ScoreCalibration] = None, name=_OUTPUT_CLASSIFICATION_NAME, description=_OUTPUT_CLASSIFICATION_DESCRIPTION): """Marks model's next output tensor as a classification head. Example usage: writer.add_classification_output( Labels() .add(['cat', 'dog], 'en') .add(['chat', 'chien], 'fr') .add(['/m/011l78', '/m/031d23'], use_as_category_name=True)) Args: labels: an instance of Labels helper class. score_calibration: an instance of ScoreCalibration helper class. name: Metadata name of the tensor. Note that this is different from tensor name in the flatbuffer. description: human readable description of what the tensor does. Returns: The current Writer instance to allow chained operation. """ calibration_md = None if score_calibration: calibration_md = metadata_info.ScoreCalibrationMd( score_transformation_type=score_calibration. transformation_type, default_score=score_calibration.default_score, file_path=self._export_calibration_file( 'score_calibration.txt', score_calibration.parameters)) idx = len(self._output_mds) label_files = [] for item in labels._labels: # pylint: disable=protected-access label_files.append( metadata_info.LabelFileMd(self._export_labels( item.filename, item.names), locale=item.locale)) output_md = metadata_info.ClassificationTensorMd( name=name, description=description, label_files=label_files, tensor_type=self._output_tensor_type(idx), score_calibration_md=calibration_md, ) self._output_mds.append(output_md) return self
def create_for_inference( cls, model_buffer: bytearray, tokenizer_md: Optional[metadata_info.RegexTokenizerMd], label_file_paths: List[str]): """Creates mandatory metadata for TFLite Support inference. The parameters required in this method are mandatory when using TFLite Support features, such as Task library and Codegen tool (Android Studio ML Binding). Other metadata fields will be set to default. If other fields need to be filled, use the method `create_from_metadata_info` to edit them. Args: model_buffer: valid buffer of the model file. tokenizer_md: information of the tokenizer used to process the input string, if any. Only `RegexTokenizer` [1] is currently supported. If the tokenizer is `BertTokenizer` [2] or `SentencePieceTokenizer` [3], refer to `bert_nl_classifier.MetadataWriter`. [1]: https://github.com/tensorflow/tflite-support/blob/b80289c4cd1224d0e1836c7654e82f070f9eefaa/tensorflow_lite_support/metadata/metadata_schema.fbs#L475 [2]: https://github.com/tensorflow/tflite-support/blob/b80289c4cd1224d0e1836c7654e82f070f9eefaa/tensorflow_lite_support/metadata/metadata_schema.fbs#L436 [3]: https://github.com/tensorflow/tflite-support/blob/b80289c4cd1224d0e1836c7654e82f070f9eefaa/tensorflow_lite_support/metadata/metadata_schema.fbs#L473 label_file_paths: paths to the label files [4] in the classification tensor. Pass in an empty list if the model does not have any label file. [4]: https://github.com/tensorflow/tflite-support/blob/b80289c4cd1224d0e1836c7654e82f070f9eefaa/tensorflow_lite_support/metadata/metadata_schema.fbs#L95 Returns: A MetadataWriter object. """ input_md = metadata_info.InputTextTensorMd( name=_INPUT_NAME, description=_INPUT_DESCRIPTION, tokenizer_md=tokenizer_md) output_md = metadata_info.ClassificationTensorMd( name=_OUTPUT_NAME, description=_OUTPUT_DESCRIPTION, label_files=[ metadata_info.LabelFileMd(file_path=file_path) for file_path in label_file_paths ], tensor_type=writer_utils.get_output_tensor_types(model_buffer)[0]) return cls.create_from_metadata_info(model_buffer, input_md=input_md, output_md=output_md)