def _create_output_metadata(self):
    """Creates the output metadata for a Bert QA model."""

    # Creates outputs info.
    end_meta = _metadata_fb.TensorMetadataT()
    end_meta.name = "end_logits"
    end_meta.description = (
        "logits over the sequence which indicates the"
        " end position of the answer span with closed interval.")
    end_meta.content = _metadata_fb.ContentT()
    end_meta.content.contentPropertiesType = (
        _metadata_fb.ContentProperties.FeatureProperties)
    end_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT()

    start_meta = _metadata_fb.TensorMetadataT()
    start_meta.name = "start_logits"
    start_meta.description = (
        "logits over the sequence which indicates "
        "the start position of the answer span with closed interval.")
    start_meta.content = _metadata_fb.ContentT()
    start_meta.content.contentPropertiesType = (
        _metadata_fb.ContentProperties.FeatureProperties)
    start_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT()

    # The order of output_metadata should match the order of tensor names in
    # OutputTensorNames.
    output_metadata = [start_meta, end_meta]
    # Order the tensor metadata according to the output tensor order.
    ordered_output_names = self._get_output_tensor_names()
    return self._order_tensor_metadata_with_names(output_metadata,
                                                  self.model_info.output_names,
                                                  ordered_output_names)
示例#2
0
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 _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()
示例#4
0
    def _create_input_metadata(self):
        """Creates the input metadata for a Bert model.

    Returns:
      A list of the three input tensor metadata in flatbuffer objects.
    """

        # Creates inputs info.
        ids_meta = _metadata_fb.TensorMetadataT()
        ids_meta.name = "ids"
        ids_meta.description = ("Tokenized ids of input text.")
        ids_meta.content = _metadata_fb.ContentT()
        ids_meta.content.contentPropertiesType = (
            _metadata_fb.ContentProperties.FeatureProperties)
        ids_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT()

        mask_meta = _metadata_fb.TensorMetadataT()
        mask_meta.name = "mask"
        mask_meta.description = (
            "Mask with 1 for real tokens and 0 for padding "
            "tokens.")
        mask_meta.content = _metadata_fb.ContentT()
        mask_meta.content.contentPropertiesType = (
            _metadata_fb.ContentProperties.FeatureProperties)
        mask_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT()

        segment_meta = _metadata_fb.TensorMetadataT()
        segment_meta.name = "segment_ids"
        segment_meta.description = (
            "0 for the first sequence, 1 for the second sequence if exists.")
        segment_meta.content = _metadata_fb.ContentT()
        segment_meta.content.contentPropertiesType = (
            _metadata_fb.ContentProperties.FeatureProperties)
        segment_meta.content.contentProperties = _metadata_fb.FeaturePropertiesT(
        )

        # The order of input_metadata should match the order of tensor names in
        # InputTensorNames.
        input_metadata = [ids_meta, mask_meta, segment_meta]
        # Order the tensor metadata according to the input tensor order.
        ordered_input_names = self._get_input_tensor_names()
        return self._order_tensor_metadata_with_names(
            input_metadata, self.model_info.input_names, ordered_input_names)
示例#5
0
def create_preprocessor_output_metadata():
    output_meta = _metadata_fb.TensorMetadataT()
    output_meta.name = "Preprocessed image"
    image_size = 224
    output_meta.description = f"Preprocessed image, shaped 3 x {image_size} x {image_size}"
    output_meta.content = _metadata_fb.ContentT()
    output_meta.content.content_properties = _metadata_fb.FeaturePropertiesT()
    output_meta.content.contentPropertiesType = (
        _metadata_fb.ContentProperties.FeatureProperties)
    return output_meta
示例#6
0
def create_output_metadata():
    output_metadata = _metadata_fb.TensorMetadataT()
    output_metadata.name = "face_embeddings"
    output_metadata.description = "Embedding vector with 2048 values per face."
    output_metadata.content = _metadata_fb.ContentT()
    output_metadata.content.content_properties = _metadata_fb.FeaturePropertiesT(
    )
    output_metadata.content.contentPropertiesType = (
        _metadata_fb.ContentProperties.FeatureProperties)
    output_stats = _metadata_fb.StatsT()
    # output_stats.max = [1.0]
    # output_stats.min = [0.0]
    output_metadata.stats = output_stats
    return output_metadata
示例#7
0
def create_preprocessor_input_metadata():
    input_meta = _metadata_fb.TensorMetadataT()
    input_meta.name = "image preprocessing"
    input_meta.description = (
        "Input image to be preprocessed. The input image can be of any size.")
    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_meta.processUnits = []
    input_stats = _metadata_fb.StatsT()
    input_stats.max = [255]
    input_stats.min = [0]
    input_meta.stats = input_stats
    return input_meta
示例#8
0
    def _create_output_metadata(self):
        """Creates the output metadata for a Bert text classifier model."""

        # Create output info.
        output_meta = _metadata_fb.TensorMetadataT()
        output_meta.name = "probability"
        output_meta.description = "Probabilities of labels respectively."
        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(self.model_info.label_file)
        label_file.description = ("Labels for classification categories.")
        label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS
        output_meta.associatedFiles = [label_file]
        return [output_meta]
示例#9
0
def create_input_metadata():
    input_metadata = _metadata_fb.TensorMetadataT()
    input_metadata.name = "image"
    input_metadata.description = VggFaceMetadata.Layers.ANDROID_INPUT
    input_metadata.content = _metadata_fb.ContentT()
    input_metadata.content.contentProperties = _metadata_fb.ImagePropertiesT()
    input_metadata.content.contentProperties.colorSpace = _metadata_fb.ColorSpaceType.RGB
    input_metadata.content.contentPropertiesType = _metadata_fb.ContentProperties.ImageProperties

    # Normalization
    input_normalization = _metadata_fb.ProcessUnitT()
    input_normalization.optionsType = _metadata_fb.ProcessUnitOptions.NormalizationOptions
    input_normalization.options = _metadata_fb.NormalizationOptionsT()
    input_normalization.options.mean = [91.4953, 103.8827, 131.0912]
    input_normalization.options.std = [1, 1, 1]
    input_metadata.processUnits = [input_normalization]

    # Input stats
    input_stats = _metadata_fb.StatsT()
    input_metadata.stats = input_stats
    return input_metadata
示例#10
0
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]
示例#11
0
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 PNU building Detection"
model_meta.description = ("Identify which building in PNU")
model_meta.version = "v1"
model_meta.author = "BEOKS"
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.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()
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
示例#13
0
labelmap_file = args.base + "/" + args.labelmap
export_model_path = args.base + "/" + args.model

# Creates model info.
model_meta = _metadata_fb.ModelMetadataT()
model_meta.name = "MobileNetV2 SSD OID TF1 300x300 Object Detector"
model_meta.description = ("Localize the most prominent object in the "
                          "image from a set of N categories such as "
                          "trees, animals, food, vehicles, person etc.")
model_meta.version = "v1"
model_meta.author = "AA"
model_meta.license = ("Apache License. Version 2.0 "
                      "http://www.apache.org/licenses/LICENSE-2.0.")

# Creates input info.
input_meta = _metadata_fb.TensorMetadataT()

# Creates output info.
output_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(300, 300))
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)
示例#14
0
    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()
示例#15
0
    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 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()
示例#17
0
    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(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()
示例#19
0
    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 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()
示例#21
0
  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 = "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()