def convert(): saved_model_path = 'gaze_prediction_model.h5' gaze_pred_model = tf.keras.models.load_model(saved_model_path) # some layer names have been renamed to: """ Left Eye: placeholder Right Eye: placeholder_1 Face: face Face Mask: placeholder_2 """ # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = "EyeTracker Gaze Prediction Model" model_meta.description = "Predicts the coordinates relative to the camera position of the phone camera" model_meta.version = "v1" model_meta.author = "EyeCon" # Create Input Info. input_left_eye_meta = _metadata_fb.TensorMetadataT() input_right_eye_meta = _metadata_fb.TensorMetadataT() input_face = _metadata_fb.TensorMetadataT() input_face_mask = _metadata_fb.TensorMetadataT() converter = tf.lite.TFLiteConverter.from_keras_model(gaze_pred_model) tflite_model = converter.convert() # save the model with open('gaze_predictor_model.tflite', 'wb') as f: f.write(tflite_model)
def _create_metadata(self): """Creates model metadata for bert models.""" model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = self.model_info.description model_meta.version = self.model_info.version model_meta.author = "TensorFlow Lite Model Maker" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates the tokenizer info. if self.model_info.tokenizer_type is Tokenizer.BERT_TOKENIZER: tokenizer = self._create_bert_tokenizer() elif self.model_info.tokenizer_type is Tokenizer.SENTENCE_PIECE: tokenizer = self._create_sentence_piece_tokenizer() else: raise ValueError("The tokenizer type, {0}, is unsupported.".format( self.model_info.tokenizer_type)) # Creates subgraph info. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = self._create_input_metadata() subgraph.outputTensorMetadata = self._create_output_metadata() subgraph.inputProcessUnits = [tokenizer] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
def _write_metadata(model_path: str, label_map_path: str, mean: List[float], std: List[float]): """Add normalization option and label map TFLite metadata to the model. Args: model_path: The path of the TFLite model label_map_path: The path of the label map file mean: The mean value used to normalize input image tensor std: The standard deviation used to normalize input image tensor """ # Creates flatbuffer for model information. model_meta = _metadata_fb.ModelMetadataT() # Creates flatbuffer for model input metadata. # Here we add the input normalization info to input metadata. input_meta = _metadata_fb.TensorMetadataT() input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = mean input_normalization.options.std = std input_meta.processUnits = [input_normalization] # Creates flatbuffer for model output metadata. # Here we add label file to output metadata. output_meta = _metadata_fb.TensorMetadataT() label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(label_map_path) label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS output_meta.associatedFiles = [label_file] # Creates subgraph to contain input and output information, # and add subgraph to the model information. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [output_meta] model_meta.subgraphMetadata = [subgraph] # Serialize the model metadata buffer we created above using flatbuffer # builder. b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) metadata_buf = b.Output() # Populates metadata and label file to the model file. populator = _metadata.MetadataPopulator.with_model_file(model_path) populator.load_metadata_buffer(metadata_buf) populator.load_associated_files([label_map_path]) populator.populate()
def write_metadata(model_filename): metadata = _metadata_fb.ModelMetadataT() metadata.description = VggFaceMetadata.SHORT_DESCRIPTION metadata.name = VggFaceMetadata.NAME metadata.author = VggFaceMetadata.AUTHOR metadata.license = VggFaceMetadata.LICENSE metadata.version = VggFaceMetadata.VERSION subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [create_input_metadata()] subgraph.outputTensorMetadata = [create_output_metadata()] metadata.subgraphMetadata = [subgraph] save_metadata_to_model_file(metadata, model_filename)
def write_preprocessor_metadata(image_preprocessor_filename): metadata = _metadata_fb.ModelMetadataT() metadata.description = "A preprocessing model used to channel-reverse, resize and finally depthwise normalize images prior to " \ "processing through the face recognition model, {0}. If you use this model, then don't do " \ "the preprocessing manually since this model does it for you.".format(VggFaceMetadata.NAME) metadata.name = VggFaceMetadata.NAME + " preprocessing" metadata.author = "Ben Butterworth" metadata.license = "Github: See https://github.com/popsa-hq/keras-vggface" metadata.version = VggFaceMetadata.VERSION subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [create_preprocessor_input_metadata()] subgraph.outputTensorMetadata = [create_preprocessor_output_metadata()] metadata.subgraphMetadata = [subgraph] save_metadata_to_model_file(metadata, image_preprocessor_filename)
def _create_metadata(self): """Creates the metadata for an image classifier.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = ("Identify the most prominent breed of a dog in the " "image from a set of %d breeds." % self.model_info.num_classes) model_meta.version = self.model_info.version model_meta.author = "https://github.com/puntogris/" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "image" input_meta.description = ( "Input image to be classified. The expected image is {0} x {1}, with " "three channels (red, blue, and green) per pixel. Each value in the " "tensor is a single byte between {2} and {3}.".format( self.model_info.image_width, self.model_info.image_height, self.model_info.image_min, self.model_info.image_max)) input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = self.model_info.mean input_normalization.options.std = self.model_info.std input_meta.processUnits = [input_normalization] input_stats = _metadata_fb.StatsT() input_stats.max = [self.model_info.image_max] input_stats.min = [self.model_info.image_min] input_meta.stats = input_stats # Creates output info. output_meta = _metadata_fb.TensorMetadataT() output_meta.name = "probability" output_meta.description = "Probabilities of the %d labels respectively." % self.model_info.num_classes output_meta.content = _metadata_fb.ContentT() output_meta.content.content_properties = _metadata_fb.FeaturePropertiesT() output_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_stats = _metadata_fb.StatsT() output_stats.max = [1.0] output_stats.min = [0.0] output_meta.stats = output_stats label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(self.label_file_path) label_file.description = "Labels for objects that the model can recognize." label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS output_meta.associatedFiles = [label_file] # Creates subgraph info. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [output_meta] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish( model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
def _create_metadata(self): """Creates the metadata for an image classifier.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = ("Identify the most prominent object in the " "image from a set of %d categories." % self.model_info.num_classes) model_meta.version = self.model_info.version model_meta.author = "TensorFlow" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "image" input_meta.description = ( "Input image to be classified. The expected image is {0} x {1}, with " "three channels (red, blue, and green) per pixel. Each value in the " "tensor is a single byte between {2} and {3}.".format( self.model_info.image_width, self.model_info.image_height, self.model_info.image_min, self.model_info.image_max)) input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = self.model_info.mean input_normalization.options.std = self.model_info.std input_meta.processUnits = [input_normalization] input_stats = _metadata_fb.StatsT() input_stats.max = [self.model_info.image_max] input_stats.min = [self.model_info.image_min] input_meta.stats = input_stats # Creates output info. output_location_meta = _metadata_fb.TensorMetadataT() output_location_meta.name = "location" output_location_meta.description = "The locations of the detected boxes." output_location_meta.content = _metadata_fb.ContentT() output_location_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.BoundingBoxProperties) output_location_meta.content.contentProperties = ( _metadata_fb.BoundingBoxPropertiesT()) output_location_meta.content.contentProperties.index = [1, 0, 3, 2] output_location_meta.content.contentProperties.type = ( _metadata_fb.BoundingBoxType.BOUNDARIES) output_location_meta.content.contentProperties.coordinateType = ( _metadata_fb.CoordinateType.RATIO) output_location_meta.content.range = _metadata_fb.ValueRangeT() output_location_meta.content.range.min = 0 output_location_meta.content.range.max = 255 output_confidence_meta = _metadata_fb.TensorMetadataT() output_confidence_meta.name = "confidence" output_confidence_meta.description = "The confidences of the detected boxes." output_confidence_meta.content = _metadata_fb.ContentT() output_confidence_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_confidence_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) output_confidence_meta.content.range = _metadata_fb.ValueRangeT() output_confidence_meta.content.range.min = 0 output_confidence_meta.content.range.max = 255 # label_file = _metadata_fb.AssociatedFileT() # label_file.name = os.path.basename("labelmap.txt") # label_file.description = "Label of objects that this model can recognize." # label_file.type = _metadata_fb.AssociatedFileType.TENSOR_VALUE_LABELS # output_confidence_meta.associatedFiles = [label_file] output_landmark_meta = _metadata_fb.TensorMetadataT() output_landmark_meta.name = "landmark" output_landmark_meta.description = "The landmark of the detected boxes." output_landmark_meta.content = _metadata_fb.ContentT() output_landmark_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_landmark_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) output_landmark_meta.content.range = _metadata_fb.ValueRangeT() output_landmark_meta.content.range.min = 0 output_landmark_meta.content.range.max = 255 # Creates subgraph info. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [ output_location_meta, output_confidence_meta, output_landmark_meta ] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
def _create_metadata(self): """Creates the metadata for an object detector.""" model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = ( "Identify which of a known set of objects might be present and provide " "information about their positions within the given image or a video " "stream.") model_meta.version = self.model_info.version model_meta.author = "TensorFlow Lite Model Maker" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "image" input_meta.description = ( "Input image to be detected. The expected image is {0} x {1}, with " "three channels (red, blue, and green) per pixel. Each value in the " "tensor is a single byte between 0 and 255.".format( self.model_info.image_width, self.model_info.image_height)) input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = self.model_info.mean input_normalization.options.std = self.model_info.std input_meta.processUnits = [input_normalization] input_stats = _metadata_fb.StatsT() input_stats.max = [self.model_info.image_max] input_stats.min = [self.model_info.image_min] input_meta.stats = input_stats # Creates outputs info. output_location_meta = _metadata_fb.TensorMetadataT() output_location_meta.name = "location" output_location_meta.description = "The locations of the detected boxes." output_location_meta.content = _metadata_fb.ContentT() output_location_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.BoundingBoxProperties) output_location_meta.content.contentProperties = ( _metadata_fb.BoundingBoxPropertiesT()) output_location_meta.content.contentProperties.index = [1, 0, 3, 2] output_location_meta.content.contentProperties.type = ( _metadata_fb.BoundingBoxType.BOUNDARIES) output_location_meta.content.contentProperties.coordinateType = ( _metadata_fb.CoordinateType.RATIO) output_location_meta.content.range = _metadata_fb.ValueRangeT() output_location_meta.content.range.min = 2 output_location_meta.content.range.max = 2 output_class_meta = _metadata_fb.TensorMetadataT() output_class_meta.name = "category" output_class_meta.description = "The categories of the detected boxes." output_class_meta.content = _metadata_fb.ContentT() output_class_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_class_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) output_class_meta.content.range = _metadata_fb.ValueRangeT() output_class_meta.content.range.min = 2 output_class_meta.content.range.max = 2 label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(self.associated_files[0]) label_file.description = "Label of objects that this model can recognize." label_file.type = _metadata_fb.AssociatedFileType.TENSOR_VALUE_LABELS output_class_meta.associatedFiles = [label_file] output_score_meta = _metadata_fb.TensorMetadataT() output_score_meta.name = "score" output_score_meta.description = "The scores of the detected boxes." output_score_meta.content = _metadata_fb.ContentT() output_score_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_score_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) output_score_meta.content.range = _metadata_fb.ValueRangeT() output_score_meta.content.range.min = 2 output_score_meta.content.range.max = 2 output_number_meta = _metadata_fb.TensorMetadataT() output_number_meta.name = "number of detections" output_number_meta.description = "The number of the detected boxes." output_number_meta.content = _metadata_fb.ContentT() output_number_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_number_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) # Creates subgraph info. group = _metadata_fb.TensorGroupT() group.name = "detection result" group.tensorNames = [ output_location_meta.name, output_class_meta.name, output_score_meta.name ] subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [ output_location_meta, output_class_meta, output_score_meta, output_number_meta ] subgraph.outputTensorGroups = [group] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
def _create_metadata(self): """Creates the metadata for a text classifier.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = self.model_info.description model_meta.version = self.model_info.version model_meta.author = "TensorFlow" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "input_text" input_meta.description = ( "Embedding vectors representing the input text to be classified. The " "input need to be converted from raw text to embedding vectors using " "the attached dictionary file.") # Create the vocab file. vocab_file = _metadata_fb.AssociatedFileT() vocab_file.name = os.path.basename(self.associated_files[1]) vocab_file.description = ("Vocabulary file to convert natural language " "words to embedding vectors.") vocab_file.type = _metadata_fb.AssociatedFileType.VOCABULARY # Create the RegexTokenizer. tokenizer = _metadata_fb.ProcessUnitT() tokenizer.optionsType = ( _metadata_fb.ProcessUnitOptions.RegexTokenizerOptions) tokenizer.options = _metadata_fb.RegexTokenizerOptionsT() tokenizer.options.delimRegexPattern = self.model_info.delim_regex_pattern tokenizer.options.vocabFile = [vocab_file] input_meta.content = _metadata_fb.ContentT() input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) input_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT() input_meta.processUnits = [tokenizer] # Creates output info. output_meta = _metadata_fb.TensorMetadataT() output_meta.name = "probability" output_meta.description = "Probabilities of the labels respectively." output_meta.content = _metadata_fb.ContentT() output_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT() output_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_stats = _metadata_fb.StatsT() output_stats.max = [1.0] output_stats.min = [0.0] output_meta.stats = output_stats label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(self.associated_files[0]) label_file.description = ("Labels for the categories that the model can " "classify.") label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS output_meta.associatedFiles = [label_file] # Creates subgraph info. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [output_meta] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish( model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
from tflite_support import metadata as _metadata from tflite_support import metadata_schema_py_generated as _metadata_fb import flatbuffers import os model_meta = _metadata_fb.ModelMetadataT() model_meta.name = "MobilenNetV2 SSD 300x300 Object Detector" model_meta.description = ("Localize the most prominent object in the" "image from a set of N categories such as" "Crosswalk, Green/Red signal, Car") model_meta.version = 'V1' model_meta.author = "Phoenix" model_meta.license = ('Apache License. Version 2.0 ' 'http://www.apache.org/licenses/LICENSE-2.0. ') input_meta = _metadata_fb.TensorMetadataT() input_meta.name = 'Image' input_meta.description = ( "Input image to be classified. The expected image is {0} x {1}, with " "three channels (red, blue, green) per pixel. Each value in the " "tensor is a single byte between 0 and 255. ".format(640, 640)) input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = _metadata_fb.ColorSpaceType.RGB input_meta.content.contentPropertiesType = _metadata_fb.ContentProperties.ImageProperties input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = _metadata_fb.ProcessUnitOptions.NormalizationOptions input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = [127.5] input_normalization.options.std = [127.5]
def build_metadata(name: str, version: str, labels: List[LabelDescription], output_types: List[OutputTensorType], output_interpretation: str = "", task: Optional[str] = None, author: str = "", task_params: str = "") -> bytearray: """ Args: name: version: labels: output_types: output_interpretation: task: author: task_params: Returns: """ if task is None or task == "": # Auto infer detection or localization from targets types = set(output_types) if types == { OutputTensorType.BOX_SHAPE, OutputTensorType.OBJECTNESS, OutputTensorType.CLASSES, }: task = TaskType.OBJECT_DETECTION.value elif types == { OutputTensorType.BOX_SHAPE, OutputTensorType.OBJECTNESS }: task = TaskType.OBJECT_LOCALIZATION.value else: raise ValueError( f"Cannot infer task_type from output_types: {output_types}") metadata = ModelMetadata( name=name, version=version, task=task, output_interpretation=output_interpretation, labels=labels, task_params=task_params, ) model_meta = _metadata_fb.ModelMetadataT() model_meta.name = name model_meta.description = json.dumps(metadata.asdict) model_meta.version = version model_meta.author = author input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "image" output_metas = [] for t in output_types: output_meta = _metadata_fb.TensorMetadataT() output_meta.name = t.value output_metas.append(output_meta) subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = output_metas model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) metadata_buf = b.Output() return metadata_buf
def build_metadata( model_file_path: str, #model_name: str, #model_description: str, #model_author: str, #model_license: str, vocab_file_path: str=None, sentencepiece_model_path: str=None): model_meta = _metadata_fb.ModelMetadataT() model_meta.name = 'distilbert news' model_meta.description = 'some model description' model_meta.version = 'v1' model_meta.author = 'Unknown' model_meta.license = 'Apache License. Version 2.0' model_meta.minParserVersion = '1.1.0' # Creates input info. ids = _metadata_fb.TensorMetadataT() segment_ids = _metadata_fb.TensorMetadataT() mask = _metadata_fb.TensorMetadataT() ids.name = "ids" ids.description = "Tokenized ids of input text." ids.content = _metadata_fb.ContentT() ids.content.contentProperties = _metadata_fb.FeaturePropertiesT() ids.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) segment_ids.name = "segment_ids" segment_ids.description = "0 for the first sequence, 1 for the second sequence if exits." segment_ids.content = _metadata_fb.ContentT() segment_ids.content.contentProperties = _metadata_fb.FeaturePropertiesT() segment_ids.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) mask.name = "mask" mask.description = "Mask with 1 for real tokens and 0 for padding tokens." mask.content = _metadata_fb.ContentT() mask.content.contentProperties = _metadata_fb.FeaturePropertiesT() mask.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) # Creates output info. output_meta = _metadata_fb.TensorMetadataT() output_meta.name = "output" output_meta.description = "the id of the output class" output_meta.content = _metadata_fb.ContentT() output_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT() output_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) """ label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(label_file_path) label_file.description = "Labels for the categories to be predicted." label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS output_meta.associatedFiles = [label_file] """ input_process_units = [] if sentencepiece_model_path is not None: #Add sentencepiece specific process unit sentencepiece_process_unit = _metadata_fb.ProcessUnitT() sentencepiece_process_unit.optionsType = ( _metadata_fb.ProcessUnitOptions.SentencePieceTokenizerOptions) sentencepiece_process_unit.options = _metadata_fb.SentencePieceTokenizerOptionsT() sentencepiece_model = AssociatedFileT() sentencepiece_model.name="30k-clean-model", sentencepiece_model.description="The sentence piece model file." sentencepiece_process_unit.options.sentencePieceModel = [sentencepiece_model] input_process_units.append(sentencepiece_process_unit) if vocab_file_path is not None: model_process_unit = _metadata_fb.ProcessUnitT() model_process_unit.optionsType = ( _metadata_fb.ProcessUnitOptions.BertTokenizerOptions) model_process_unit.options = _metadata_fb.BertTokenizerOptionsT() vocab_file = AssociatedFileT() vocab_file.name="jp word piece vocab", vocab_file.description="Japanese Vocabulary file for the BERT tokenizer.", vocab_file.type=_metadata_fb.AssociatedFileType.VOCABULARY model_process_unit.options.vocabFile = [vocab_file] input_process_units.append(model_process_unit) #Put metadata together subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [ids, mask, segment_ids] subgraph.outputTensorMetadata = [output_meta] subgraph.inputProcessUnits = input_process_units model_meta.subgraphMetadata = [subgraph] #create flat buffers for metadata b = flatbuffers.Builder(0) b.Finish( model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) metadata_buf = b.Output() #associate files to metadata populator = _metadata.MetadataPopulator.with_model_file(model_file_path) populator.load_metadata_buffer(metadata_buf) if vocab_file_path is not None: files = [vocab_file_path] else: files = [sentencepiece_model_path] populator.load_associated_files(files) populator.populate()
def create_metadata(model_file_name, label_map_file_name, num_labels): from tflite_support import flatbuffers from tflite_support import metadata as _metadata from tflite_support import metadata_schema_py_generated as _metadata_fb """ ... """ """Creates the metadata for an image classifier.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = 'MobileNetV1 image classifier' model_meta.description = ('Identify the most prominent object in the ' 'image from a set of 1,001 categories such as ' 'trees, animals, food, vehicles, person etc.') model_meta.version = 'v1' model_meta.author = 'TensorFlow' model_meta.license = ('Apache License. Version 2.0 ' 'http://www.apache.org/licenses/LICENSE-2.0.') # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = 'image' input_meta.description = ( 'Input image to be classified. The expected image is {0} x {1}, with ' 'three channels (red, blue, and green) per pixel. Each value in the ' 'tensor is a single byte between 0 and 255.'.format(416, 416)) input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = [127.5] input_normalization.options.std = [127.5] input_meta.processUnits = [input_normalization] input_stats = _metadata_fb.StatsT() input_stats.max = [255] input_stats.min = [0] input_meta.stats = input_stats # Creates output info. bbox_meta = _metadata_fb.TensorMetadataT() bbox_meta.name = 'bbox' bbox_meta.description = '.' bbox_meta.content = _metadata_fb.ContentT() bbox_meta.content.content_properties = _metadata_fb.FeaturePropertiesT() bbox_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) bbox_stats = _metadata_fb.StatsT() bbox_stats.max = [416.0] bbox_stats.min = [0.0] bbox_meta.stats = bbox_stats classes_meta = _metadata_fb.TensorMetadataT() classes_meta.name = 'classes' classes_meta.description = '.' classes_meta.content = _metadata_fb.ContentT() classes_meta.content.content_properties = _metadata_fb.FeaturePropertiesT() classes_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) classes_stats = _metadata_fb.StatsT() classes_stats.max = [num_labels] classes_stats.min = [0] classes_meta.stats = classes_stats label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(label_map_file_name) label_file.description = 'Labels for objects that the model can recognize.' label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS classes_meta.associatedFiles = [label_file] confidence_meta = _metadata_fb.TensorMetadataT() confidence_meta.name = 'confidence' confidence_meta.description = '.' confidence_meta.content = _metadata_fb.ContentT() confidence_meta.content.content_properties = _metadata_fb.FeaturePropertiesT( ) confidence_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) confidence_stats = _metadata_fb.StatsT() confidence_stats.max = [1.0] confidence_stats.min = [0.0] confidence_meta.stats = confidence_stats num_dets_meta = _metadata_fb.TensorMetadataT() num_dets_meta.name = 'num_dets' num_dets_meta.description = '.' num_dets_meta.content = _metadata_fb.ContentT() num_dets_meta.content.content_properties = _metadata_fb.FeaturePropertiesT( ) num_dets_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) num_dets_stats = _metadata_fb.StatsT() num_dets_stats.max = [200] num_dets_stats.min = [0] num_dets_meta.stats = num_dets_stats raw_output_meta = _metadata_fb.TensorMetadataT() raw_output_meta.name = 'raw_output' raw_output_meta.description = '.' raw_output_meta.content = _metadata_fb.ContentT() raw_output_meta.content.content_properties = _metadata_fb.FeaturePropertiesT( ) raw_output_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [ bbox_meta, classes_meta, confidence_meta, num_dets_stats ] # raw_output_meta model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) metadata_buf = b.Output() populator = _metadata.MetadataPopulator.with_model_file(model_file_name) populator.load_metadata_buffer(metadata_buf) populator.load_associated_files([label_map_file_name]) populator.populate()
def _create_metadata(self): """Creates the metadata for an image classifier.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = ("Equipment.") model_meta.version = self.model_info.version model_meta.author = "TensorFlow" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "image" input_meta.description = ( "The expected image is 300 x 300, with three channels " "(re, blue, and green) per pixel. Each value in the tensor is between" " 0 and 1.") input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_normalization.options = _metadata_fb.NormalizationOptionsT() input_normalization.options.mean = self.model_info.mean input_normalization.options.std = self.model_info.std input_meta.processUnits = [input_normalization] input_stats = _metadata_fb.StatsT() input_stats.max = [self.model_info.image_max] input_stats.min = [self.model_info.image_min] input_meta.stats = input_stats # Creates output info. output_location_meta = _metadata_fb.TensorMetadataT() output_location_meta.name = "location" output_location_meta.description = "The locations of the detected boxes." output_location_meta.content = _metadata_fb.ContentT() output_location_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.BoundingBoxProperties) output_location_meta.content.contentProperties = ( _metadata_fb.BoundingBoxPropertiesT()) output_location_meta.content.contentProperties.index = [1, 0, 3, 2] output_location_meta.content.contentProperties.type = ( _metadata_fb.BoundingBoxType.BOUNDARIES) output_location_meta.content.contentProperties.coordinateType = ( _metadata_fb.CoordinateType.RATIO) output_location_meta.content.range = _metadata_fb.ValueRangeT() output_location_meta.content.range.min = 2 output_location_meta.content.range.max = 2 output_class_meta = _metadata_fb.TensorMetadataT() output_class_meta.name = "category" output_class_meta.description = "The categories of the detected boxes." output_class_meta.content = _metadata_fb.ContentT() output_class_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_class_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) output_class_meta.content.range = _metadata_fb.ValueRangeT() output_class_meta.content.range.min = 2 output_class_meta.content.range.max = 2 label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(self.label_file_path) label_file.description = "Label of objects that this model can recognize." label_file.type = _metadata_fb.AssociatedFileType.TENSOR_VALUE_LABELS output_class_meta.associatedFiles = [label_file] output_score_meta = _metadata_fb.TensorMetadataT() output_score_meta.name = "score" output_score_meta.description = "The scores of the detected boxes." output_score_meta.content = _metadata_fb.ContentT() output_score_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_score_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) output_score_meta.content.range = _metadata_fb.ValueRangeT() output_score_meta.content.range.min = 2 output_score_meta.content.range.max = 2 output_number_meta = _metadata_fb.TensorMetadataT() output_number_meta.name = "number of detections" output_number_meta.description = "The number of the detected boxes." output_number_meta.content = _metadata_fb.ContentT() output_number_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_number_meta.content.contentProperties = ( _metadata_fb.FeaturePropertiesT()) # Creates subgraph info. group = _metadata_fb.TensorGroupT() group.name = "detection result" group.tensorNames = [ output_location_meta.name, output_class_meta.name, output_score_meta.name ] subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [ output_location_meta, output_class_meta, output_score_meta, output_number_meta ] subgraph.outputTensorGroups = [group] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
def _create_metadata(self): """Creates the metadata for the selfie2anime model.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = "Selfie2Anime" model_meta.description = ("Convert selfie to anime.") model_meta.version = "v1" model_meta.author = "TensorFlow" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates info for the input, selfie image. input_image_meta = _metadata_fb.TensorMetadataT() input_image_meta.name = "selfie_image" input_image_meta.description = ( "The expected image is 256 x 256, with three channels " "(red, blue, and green) per pixel. Each value in the tensor is between" " 0 and 1.") input_image_meta.content = _metadata_fb.ContentT() input_image_meta.content.contentProperties = ( _metadata_fb.ImagePropertiesT()) input_image_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) input_image_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_image_normalization = _metadata_fb.ProcessUnitT() input_image_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) input_image_normalization.options = _metadata_fb.NormalizationOptionsT( ) input_image_normalization.options.mean = [0.0] input_image_normalization.options.std = [255] input_image_meta.processUnits = [input_image_normalization] input_image_stats = _metadata_fb.StatsT() input_image_stats.max = [1.0] input_image_stats.min = [0.0] input_image_meta.stats = input_image_stats # Creates output info, anime image output_image_meta = _metadata_fb.TensorMetadataT() output_image_meta.name = "anime_image" output_image_meta.description = "Image styled." output_image_meta.content = _metadata_fb.ContentT() output_image_meta.content.contentProperties = _metadata_fb.ImagePropertiesT( ) output_image_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.RGB) output_image_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) output_image_normalization = _metadata_fb.ProcessUnitT() output_image_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) output_image_normalization.options = _metadata_fb.NormalizationOptionsT( ) output_image_normalization.options.mean = [0.0] output_image_normalization.options.std = [0.003921568627] # 1/255 output_image_meta.processUnits = [output_image_normalization] output_image_stats = _metadata_fb.StatsT() output_image_stats.max = [1.0] output_image_stats.min = [0.0] output_image_meta.stats = output_image_stats # Creates subgraph info. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_image_meta ] # Updated by Margaret subgraph.outputTensorMetadata = [output_image_meta ] # Updated by Margaret model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()
def _create_metadata(self): """Creates the metadata for an image classifier.""" # Creates model info. model_meta = _metadata_fb.ModelMetadataT() model_meta.name = self.model_info.name model_meta.description = "Letter Recognition Model for handwrite letter recognition" model_meta.version = self.model_info.version model_meta.author = "Sebastainj1w" model_meta.license = ("Apache License. Version 2.0 " "http://www.apache.org/licenses/LICENSE-2.0.") # Creates input info. input_meta = _metadata_fb.TensorMetadataT() input_meta.name = "sensor_data" input_meta.description = "Input sensor data to be recognition. The expected data is 252 x 9, with one " \ "channels per pixel. Each value in the tensor is a float32. " input_meta.content = _metadata_fb.ContentT() input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT() input_meta.content.contentProperties.colorSpace = ( _metadata_fb.ColorSpaceType.GRAYSCALE) input_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.ImageProperties) input_normalization = _metadata_fb.ProcessUnitT() input_normalization.optionsType = ( _metadata_fb.ProcessUnitOptions.NormalizationOptions) # input_normalization.options = _metadata_fb.NormalizationOptionsT() # input_normalization.options.mean = self.model_info.mean # input_normalization.options.std = self.model_info.std # input_meta.processUnits = [input_normalization] input_stats = _metadata_fb.StatsT() input_stats.max = [self.model_info.image_max] input_stats.min = [self.model_info.image_min] input_meta.stats = input_stats # Creates output info. output_meta = _metadata_fb.TensorMetadataT() output_meta.name = "probability" output_meta.description = "Probabilities of the %d labels respectively." % self.model_info.num_classes output_meta.content = _metadata_fb.ContentT() output_meta.content.content_properties = _metadata_fb.FeaturePropertiesT() output_meta.content.contentPropertiesType = ( _metadata_fb.ContentProperties.FeatureProperties) output_stats = _metadata_fb.StatsT() output_stats.max = [1.0] output_stats.min = [0.0] output_meta.stats = output_stats label_file = _metadata_fb.AssociatedFileT() label_file.name = os.path.basename(self.label_file_path) label_file.description = "Labels for objects that the model can recognize." label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS output_meta.associatedFiles = [label_file] # Creates subgraph info. subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [input_meta] subgraph.outputTensorMetadata = [output_meta] model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish( model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) self.metadata_buf = b.Output()