From 56bd72ef65e4867512e3b9b5c1c76048c4ec49b9 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 12 Aug 2019 19:22:24 -0700 Subject: [PATCH] Prototype of TFLite model metadata. PiperOrigin-RevId: 263053990 --- tensorflow/lite/schema/BUILD | 6 + tensorflow/lite/schema/metadata_schema.fbs | 160 +++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 tensorflow/lite/schema/metadata_schema.fbs diff --git a/tensorflow/lite/schema/BUILD b/tensorflow/lite/schema/BUILD index 33836913826..8f5a812f742 100644 --- a/tensorflow/lite/schema/BUILD +++ b/tensorflow/lite/schema/BUILD @@ -79,6 +79,12 @@ flatbuffer_cc_library( out_prefix = "reflection/", ) +# Generic schema for model metadata. +flatbuffer_cc_library( + name = "metadata_schema_fbs", + srcs = ["metadata_schema.fbs"], +) + # Schema test to make sure we don't introduce backward incompatible changes # to schemas. cc_test( diff --git a/tensorflow/lite/schema/metadata_schema.fbs b/tensorflow/lite/schema/metadata_schema.fbs new file mode 100644 index 00000000000..d13f9813354 --- /dev/null +++ b/tensorflow/lite/schema/metadata_schema.fbs @@ -0,0 +1,160 @@ +// Copyright 2019 The TensorFlow Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace tflite; + +// WARNING: This file contains experimental interface and is subject to change. + +// This corresponds to the version. +file_identifier "TFLM"; +// File extension of any written files. +file_extension "tflitemeta"; + +enum AssociatedFileType : byte { + UNKNOWN = 0, + // Files such as readme.txt + DESCRIPTIONS = 1, + // Contains class labels used in classification. For example, the label files + // in image classification. + LABELS = 2, + // The vocab files used in NLP. + VOCABULARY = 3, + // Files that translate between languages, for example, the language resource + // file in Android. + LOCALIZATION = 4, +} + +table AssociatedFile { + // Name of this file. + name:string; + + // A description of what the file is. + description:string; + + // Type of the associated file. There may be special pre/post processing for + // some types. For example in image classification, a label file of the output + // will be used to convert object index into string. + type:AssociatedFileType; +} + +// The type of content that a tensor may represent. +enum ContentType : byte { + UNKNOWN = 0, + IMAGE = 1, + VIDEO = 2, + TEXT = 3, + AUDIO = 4, + FEATURE = 5, +} + +// The type of color space of an image. +enum ColorSpaceType : byte { + UNKNOWN = 0, + RGB = 1, + BGR = 2, + YUV = 3, + HSV = 4, + GRAYSCALE = 5, +} + +table ImageSize { + width:uint; + height:uint; +} + +table ImageProperties { + // The color space of the image. + color_space:ColorSpaceType; + + // Indicates the default value of image width and height if the tensor shape + // is dynamic. For fixed-size tensor, this size will be consistent with the + // expected size. + default_size:ImageSize; +} + +// Detailed information of an input or output tensor. +table TensorMetadata { + // Name of the tensor. + name:string; + + // A description of the tensor. + description:string; + + // The type of content that this tensor represents. + content_type:ContentType; + + // Values are normailzed per-channelly by (x - mean) / std. + // If there is only one value in mean and std, we'll propogate the value to + // all channels. + // Mean of the possible values used in normalization. + mean:[float]; + + // Standard dev. of the possible values used in normalization. + std:[float]; + + // Properties that define an image. The section is used when the Content Type + // is specified as image. + image_properties:ImageProperties; + + // A list of associated files of this tensor. + associated_files:[AssociatedFile]; +} + +table SubGraphMetadata { + // Name of the subgraph. + name:string; + + // A description explains details about what the subgraph does. + description:string; + + // Metadata of all input tensors used in this subgraph. + input_tensor_metadata:[TensorMetadata]; + + // Metadata of all output tensors used in this subgraph. + output_tensor_metadata:[TensorMetadata]; + + // A list of associated files of this subgraph. + associated_files:[AssociatedFile]; +} + +table ModelMetadata { + // Name of the model. + name:string; + + // This is duplicated from the Model description in schema. + // description:string; + + // Version of the model that specified by model creators. + version:string; + + // Noted that, the minimum required TFLite runtime version that the model is + // compatible with, has already been added as a metadata entry in tflite + // schema. We'll decide later if we want to move it here, and keep it with + // other metadata entries. + + // Metadata of all the subgraphs of the model. The 0th is assumed to be the + // main subgraph. + subgraph_metadata:[SubGraphMetadata]; + + // The person who creates this model. + author:string; + + // Licenses that may apply to this model. + license:string; + + // A list of associated files of this model. + associated_files:[AssociatedFile]; +} + +root_type ModelMetadata;