diff --git a/README.md b/README.md index 2915512..e56011c 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,15 @@ Mali GPU Ethos U + + SSD MobileNet v1 INT8 * + INT8 + TensorFlow Lite + :heavy_check_mark: + :heavy_multiplication_x: + :heavy_check_mark: + :heavy_multiplication_x: + SSD MobileNet v1 FP32 * FP32 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_fp32/get_class_labels.sh b/models/object_detection/ssd_mobilenet_v1/tflite_fp32/get_class_labels.sh index 5c31a40..4904ed2 100755 --- a/models/object_detection/ssd_mobilenet_v1/tflite_fp32/get_class_labels.sh +++ b/models/object_detection/ssd_mobilenet_v1/tflite_fp32/get_class_labels.sh @@ -19,7 +19,7 @@ git clone --depth 1 https://github.com/tensorflow/models.git ./tf_models cp tf_models/research/object_detection/data/mscoco_label_map.pbtxt . -python scripts/export_labels.py --path mscoco_label_map.pbtxt +python scripts/export_labels.py --path mscoco_label_map.pbtxt --num_classes 90 tr -d \" < temp.txt > labelmapping.txt rm -rf temp.txt mscoco_label_map.pbtxt rm -rf ./tf_models diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_fp32/scripts/export_labels.py b/models/object_detection/ssd_mobilenet_v1/tflite_fp32/scripts/export_labels.py index 1904f78..f1d0b70 100644 --- a/models/object_detection/ssd_mobilenet_v1/tflite_fp32/scripts/export_labels.py +++ b/models/object_detection/ssd_mobilenet_v1/tflite_fp32/scripts/export_labels.py @@ -16,7 +16,6 @@ import argparse import collections -import sys def read_label_map(label_map_path): item_id = None @@ -42,12 +41,16 @@ def read_label_map(label_map_path): return items -def convert_dictionary_to_list(d): +def convert_dictionary_to_list(d, num_classes): output_list = [] # order dictionary by keys d = collections.OrderedDict(sorted(d.items())) - for k, v in d.items(): - output_list.append(v) + + for c in range(num_classes): + if c + 1 in d: + output_list.append(d[c + 1]) + else: + output_list.append('') return output_list @@ -55,11 +58,13 @@ def convert_dictionary_to_list(d): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Process ImageNet labels.") parser.add_argument("--path", type=str, required=True) + parser.add_argument("--num_classes", type=int, required=True) args = parser.parse_args() items = read_label_map(args.path) - items = convert_dictionary_to_list(items) + items = convert_dictionary_to_list(items, args.num_classes) + with open("temp.txt", "w") as f: for item in items: f.write("%s\n" % item) diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/README.md b/models/object_detection/ssd_mobilenet_v1/tflite_int8/README.md new file mode 100644 index 0000000..01c5308 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/README.md @@ -0,0 +1,91 @@ +# SSD MobileNet v1 INT8 + +## Description +SSD MobileNet v1 is a object detection network, that localizes and identifies objects in an input image. This is a TF Lite quantized version that takes a 300x300 input image and outputs detections for this image. This model is converted from FP32 to INT8 using post-training quantization. + +## License +[Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) + +## Related Materials +### Class Labels +The class labels associated with this model can be downloaded by running the script `get_class_labels.sh`. + +### Model Recreation Code +Code to recreate this model can be found [here](recreate_model/). + + +## Network Information +| Network Information | Value | +|---------------------|----------------| +| Framework | TensorFlow Lite | +| SHA-1 Hash | fef68428bd439b70eb983b57d6a342871fa0deaa | +| Size (Bytes) | 7311392 | +| Provenance | https://arxiv.org/abs/1512.02325 | +| Paper | https://arxiv.org/abs/1512.02325 | + +## Performance +| Platform | Optimized | +|----------|:---------:| +| Cortex-A |:heavy_check_mark: | +| Cortex-M |:heavy_multiplication_x: | +| Mali GPU |:heavy_check_mark: | +| Ethos U |:heavy_multiplication_x: | + +### Key +* :heavy_check_mark: - Will run on this platform. +* :heavy_multiplication_x: - Will not run on this platform. + +## Accuracy +Dataset: COCO 2017 Validation + +| Metric | Value | +|--------|-------| +| mAP | 0.234 | + +## Optimizations +| Optimization | Value | +|--------------|---------| +| Quantization | INT8 | + +## Network Inputs + + + + + + + + + + + +
Input Node NameShapeDescription
tfl.quantize(1, 300, 300, 3)A resized and normalized input image.
+ +## Network Outputs + + + + + + + + + + + + + + + + + + + + + + + + + + +
Output Node NameShapeDescription
TFLite_Detection_PostProcess:01()The y1, x1, y2, x2 coordinates of the bounding boxes for each detection
TFLite_Detection_PostProcess:02()The class of each detection
TFLite_Detection_PostProcess:03()The probability score for each classification
TFLite_Detection_PostProcess:04()A vector containing a number corresponding to the number of detections
diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/definition.yaml b/models/object_detection/ssd_mobilenet_v1/tflite_int8/definition.yaml new file mode 100644 index 0000000..af99a1f --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/definition.yaml @@ -0,0 +1,57 @@ +benchmark: + COCO 2017 Validation: + mAP: '0.234' +description: SSD MobileNet v1 is a object detection network, that localizes and identifies + objects in an input image. This is a TF Lite quantized version that takes a 300x300 + input image and outputs detections for this image. This model is converted from + FP32 to INT8 using post-training quantization. +license: +- Apache-2.0 +network: + file_size_bytes: 7311392 + filename: ssd_mobilenet_v1.tflite + framework: TensorFlow Lite + hash: + algorithm: sha1 + value: fef68428bd439b70eb983b57d6a342871fa0deaa + provenance: https://arxiv.org/abs/1512.02325 +network_parameters: + input_nodes: + - description: A resized and normalized input image. + example_input: + path: models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_input/tfl.quantize + name: tfl.quantize + shape: + - 1 + - 300 + - 300 + - 3 + output_nodes: + - description: The y1, x1, y2, x2 coordinates of the bounding boxes for each detection + name: TFLite_Detection_PostProcess:01 + shape: [] + test_output_path: models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:01 + - description: The class of each detection + name: TFLite_Detection_PostProcess:02 + shape: [] + test_output_path: models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:02 + - description: The probability score for each classification + name: TFLite_Detection_PostProcess:03 + shape: [] + test_output_path: models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:03 + - description: A vector containing a number corresponding to the number of detections + name: TFLite_Detection_PostProcess:04 + shape: [] + test_output_path: models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:04 +operators: + TensorFlow Lite: + - CONCATENATION + - CONV_2D + - CUSTOM + - DEPTHWISE_CONV_2D + - DEQUANTIZE + - LOGISTIC + - QUANTIZE + - RELU6 + - RESHAPE +paper: https://arxiv.org/abs/1512.02325 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/get_class_labels.sh b/models/object_detection/ssd_mobilenet_v1/tflite_int8/get_class_labels.sh new file mode 100755 index 0000000..4904ed2 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/get_class_labels.sh @@ -0,0 +1,25 @@ +# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# 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. + +#!/usr/bin/env bash + +git clone --depth 1 https://github.com/tensorflow/models.git ./tf_models +cp tf_models/research/object_detection/data/mscoco_label_map.pbtxt . + +python scripts/export_labels.py --path mscoco_label_map.pbtxt --num_classes 90 +tr -d \" < temp.txt > labelmapping.txt +rm -rf temp.txt mscoco_label_map.pbtxt +rm -rf ./tf_models diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/README.md b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/README.md new file mode 100644 index 0000000..95b707e --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/README.md @@ -0,0 +1,14 @@ +# SSD MobileNet v1 INT8 Re-Creation +This folder contains scripts that allow you to re-create the model and benchmark it's performance. + +## Requirements +The scripts in this folder requires that the following must be installed: +- Python 3.7 +- protoc + +## Running The Script +### Recreate The Model +Run the following command in a terminal: `./quantize_ssd_mobilenet_v1.sh` + +### Benchmarking The Model +Run the following command in a terminal: `./benchmark_ssd_mobilenet_v1.sh` diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/benchmark_model.py b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/benchmark_model.py new file mode 100644 index 0000000..7a161d3 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/benchmark_model.py @@ -0,0 +1,114 @@ +# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# 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. + +import argparse + +import numpy as np +import tensorflow_datasets as tfds +import tensorflow as tf + +from pycocotools.coco import COCO +from pycocotools.cocoeval import COCOeval + +detections = [] + +# Yields the image pre-processed with it's class +def coco_generator(coco_dataset, input_size=(300, 300)): + for item in coco_dataset: + image = item['image'] + image = tf.image.resize(image, input_size) + image = tf.expand_dims(image, 0) + + # MobileNet pre-processing + image = (image / 255. - 0.5) * 2 + + yield image, item['image/id'], item['image'].shape + + +def __convert_to_coco_bbox(b, input_size): + # For COCO it is [x, y, width, height] + # The bounding box b is of type: [ymin, xmin, ymax, xmax] + x = b[1] * input_size[1] + y = b[0] * input_size[0] + width = (b[3] - b[1]) * input_size[1] + height = (b[2] - b[0]) * input_size[0] + + return [x, y, width, height] + + +def process_output(output, image_id, image_size): + detection_boxes, detection_classes, detection_scores, num_detections = output + + detections_in_image = [] + for i in range(int(num_detections[0])): + detections_in_image.append( + { + 'image_id': image_id.numpy(), + 'category_id': int(detection_classes[0, i]) + 1, + 'bbox': __convert_to_coco_bbox(detection_boxes[0, i], image_size), + 'score': detection_scores[0, i] + } + ) + + return detections_in_image + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Benchmark SSD MobileNet v1.') + parser.add_argument('--path', type=str, help='Path to the model.') + + args = parser.parse_args() + + # Get the COCO 2017 validation set + coco_dataset = tfds.load('coco/2017', split='validation') + + # Setup the TensorFlow Lite interpreter + interpreter = tf.lite.Interpreter(model_path=args.path) + interpreter.allocate_tensors() + + input_node = interpreter.get_input_details()[0] + input_t = input_node['index'] + output_t = [details['index'] for details in interpreter.get_output_details()] + + for data, data_id, image_shape in coco_generator(coco_dataset): + # Quantize the input data + scale = input_node["quantization_parameters"]["scales"] + zero_point = input_node["quantization_parameters"]["zero_points"] + + data = data / scale + data += zero_point + + numpy_data = tf.cast(data, tf.int8).numpy() + interpreter.set_tensor(input_t, numpy_data) + interpreter.invoke() + + output = [ interpreter.get_tensor(o) for o in output_t ] + + detection_outputs = process_output(output, data_id, (image_shape[0], image_shape[1])) + detections += detection_outputs + + # Use the COCO API to measure the accuracy on the annotations + coco_ground_truth = COCO('./annotations/instances_val2017.json') + coco_results = coco_ground_truth.loadRes(detections) + + coco_eval = COCOeval(coco_ground_truth, coco_results, 'bbox') + + image_ids = [d['image_id'] for d in detections] + coco_eval.params.imgIds = image_ids + + coco_eval.evaluate() + coco_eval.accumulate() + coco_eval.summarize() diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/benchmark_ssd_mobilenet_v1.sh b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/benchmark_ssd_mobilenet_v1.sh new file mode 100755 index 0000000..020f75e --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/benchmark_ssd_mobilenet_v1.sh @@ -0,0 +1,29 @@ +# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# 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. + +#!/usr/bin/env bash +wget -nc http://images.cocodataset.org/annotations/annotations_trainval2017.zip +unzip -n annotations_trainval2017.zip + +python3.7 -m venv venv + +source venv/bin/activate +pip install --upgrade pip +pip install -r requirements.txt + +pip install tensorflow==2.5.0 + +python benchmark_model.py --path ssd_mobilenet_v1.tflite diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/quantize_model.py b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/quantize_model.py new file mode 100644 index 0000000..5f6ceb1 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/quantize_model.py @@ -0,0 +1,66 @@ +# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# 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. + +import argparse +import numpy as np +import tensorflow as tf +import tensorflow_datasets as tfds + + +def get_dataset(coco_dataset, input_size=(300, 300)): + def representative_dataset_gen(): + for example in coco_dataset.take(10000): + image = tf.image.resize(example['image'], input_size) + image = tf.expand_dims(image, 0) + image = (2.0 / 255.0) * image - 1.0 + + yield [image.numpy()] + + return representative_dataset_gen + +if __name__ == "__main__": + # Get the COCO 2017 dataset + coco_dataset = tfds.load('coco/2017', split='train[:10%]') + + converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph( + graph_def_file='ssd_tflite/tflite_graph.pb', + input_arrays=['normalized_input_image_tensor'], + output_arrays=[ + 'TFLite_Detection_PostProcess:0', + 'TFLite_Detection_PostProcess:1', + 'TFLite_Detection_PostProcess:2', + 'TFLite_Detection_PostProcess:3', + ], + input_shapes={ + 'normalized_input_image_tensor': [1, 300, 300, 3] + } + ) + + # Configure the TF Lite Converter + converter.inference_input_type = tf.int8 + converter.inference_output_type = tf.float32 + converter.allow_custom_ops = True + converter.experimental_new_converter = True + converter.representative_dataset = get_dataset(coco_dataset, (300, 300)) + + converter.optimizations = [tf.lite.Optimize.DEFAULT] + converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, + tf.lite.OpsSet.TFLITE_BUILTINS] + + model = converter.convert() + + with open('ssd_mobilenet_v1.tflite', 'wb') as f: + f.write(model) diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/quantize_ssd_mobilenet_v1.sh b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/quantize_ssd_mobilenet_v1.sh new file mode 100755 index 0000000..d408e81 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/quantize_ssd_mobilenet_v1.sh @@ -0,0 +1,47 @@ +# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# 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. + +#!/usr/bin/env bash +python3.7 -m venv venv + +source venv/bin/activate +pip install --upgrade pip +pip install -r requirements.txt + +git clone https://github.com/tensorflow/models.git + +pushd models/research +export PYTHONPATH=`pwd`:`pwd`/slim:$PYTHONPATH +protoc object_detection/protos/*.proto --python_out=. +popd + +pushd models/ +wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz +tar -xvf ssd_mobilenet_v1_coco_2018_01_28.tar.gz + +mkdir ssd_tflite +python research/object_detection/export_tflite_ssd_graph.py \ + --pipeline_config_path ssd_mobilenet_v1_coco_2018_01_28/pipeline.config \ + --trained_checkpoint_prefix ssd_mobilenet_v1_coco_2018_01_28/model.ckpt \ + --output_directory ssd_tflite/ \ + --max_detections=100 \ + --add_postprocessing_op=true + +mv ssd_tflite/ .. +popd + +pip install tensorflow==2.5.0 +python quantize_model.py diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/requirements.txt b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/requirements.txt new file mode 100644 index 0000000..3e5bcde --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/recreate_model/requirements.txt @@ -0,0 +1,60 @@ +absl-py==0.12.0 +astor==0.8.1 +astunparse==1.6.3 +attrs==21.2.0 +cached-property==1.5.2 +cachetools==4.2.2 +certifi==2021.5.30 +chardet==4.0.0 +cycler==0.10.0 +Cython==0.29.23 +dill==0.3.4 +flatbuffers==1.12 +future==0.18.2 +gast==0.2.2 +google-auth==1.32.0 +google-auth-oauthlib==0.4.4 +google-pasta==0.2.0 +googleapis-common-protos==1.53.0 +grpcio==1.34.1 +h5py==3.1.0 +idna==2.10 +importlib-metadata==4.5.0 +importlib-resources==5.1.4 +Keras-Applications==1.0.8 +keras-nightly==2.5.0.dev2021032900 +Keras-Preprocessing==1.1.2 +kiwisolver==1.3.1 +Markdown==3.3.4 +matplotlib==3.4.2 +numpy==1.19.5 +oauthlib==3.1.1 +opt-einsum==3.3.0 +Pillow==8.2.0 +promise==2.3 +protobuf==3.17.3 +pyasn1==0.4.8 +pyasn1-modules==0.2.8 +pycocotools==2.0.2 +pyparsing==2.4.7 +python-dateutil==2.8.1 +requests==2.25.1 +requests-oauthlib==1.3.0 +rsa==4.7.2 +scipy==1.7.0 +six==1.15.0 +tensorboard==1.15.0 +tensorboard-data-server==0.6.1 +tensorboard-plugin-wit==1.8.0 +tensorflow==1.15.0 +tensorflow-datasets==4.3.0 +tensorflow-estimator==1.15.1 +tensorflow-metadata==1.1.0 +termcolor==1.1.0 +tf-slim==1.1.0 +tqdm==4.61.1 +typing-extensions==3.7.4.3 +urllib3==1.26.5 +Werkzeug==2.0.1 +wrapt==1.12.1 +zipp==3.4.1 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/scripts/export_labels.py b/models/object_detection/ssd_mobilenet_v1/tflite_int8/scripts/export_labels.py new file mode 100644 index 0000000..f1d0b70 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/scripts/export_labels.py @@ -0,0 +1,70 @@ +# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# 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. + +import argparse +import collections + +def read_label_map(label_map_path): + item_id = None + item_name = None + items = {} + + with open(label_map_path, "r") as file: + for line in file: + line.replace(" ", "") + if line == "item{": + pass + elif line == "}": + pass + elif "id" in line: + item_id = int(line.split(":", 1)[1].strip()) + elif "display_name" in line: + item_name = line.split(":", 1)[1].replace("'", "").strip() + + if item_id is not None and item_name is not None: + items[item_id] = item_name + item_id = None + item_name = None + + return items + +def convert_dictionary_to_list(d, num_classes): + output_list = [] + # order dictionary by keys + d = collections.OrderedDict(sorted(d.items())) + + for c in range(num_classes): + if c + 1 in d: + output_list.append(d[c + 1]) + else: + output_list.append('') + + return output_list + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Process ImageNet labels.") + parser.add_argument("--path", type=str, required=True) + parser.add_argument("--num_classes", type=int, required=True) + + args = parser.parse_args() + + items = read_label_map(args.path) + items = convert_dictionary_to_list(items, args.num_classes) + + with open("temp.txt", "w") as f: + for item in items: + f.write("%s\n" % item) diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/ssd_mobilenet_v1.tflite b/models/object_detection/ssd_mobilenet_v1/tflite_int8/ssd_mobilenet_v1.tflite new file mode 100644 index 0000000..733bfe9 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/ssd_mobilenet_v1.tflite @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:107dda1b176518ffaf9b41466329d9f957dd64dc0838f1d376f9d2c6893b3bad +size 7311392 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_input/tfl.quantize/0.npy b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_input/tfl.quantize/0.npy new file mode 100644 index 0000000..5ca3d1a --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_input/tfl.quantize/0.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:827948723adf3753335c2b9cb11f53830b00541f7ea05ded58ac588c756c46f2 +size 270128 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:01/0.npy b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:01/0.npy new file mode 100644 index 0000000..6e970e8 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:01/0.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e02d426164e8fc0d8eb815b22ab7f13ebb9d4daf22d92ef0d7205d10ce96f9ba +size 1728 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:02/0.npy b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:02/0.npy new file mode 100644 index 0000000..f7516f2 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:02/0.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1980fa23ff8319de9454a7cdda7ff43cc72ceb5435a461b9a1c1ecdff6115858 +size 528 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:03/0.npy b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:03/0.npy new file mode 100644 index 0000000..905ed96 --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:03/0.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ae7e74b80dc854442b92876896a296e4a216aa6b4cbfa188e4ee3b1ee201989 +size 528 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:04/0.npy b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:04/0.npy new file mode 100644 index 0000000..eab35bd --- /dev/null +++ b/models/object_detection/ssd_mobilenet_v1/tflite_int8/testing_output/TFLite_Detection_PostProcess:04/0.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4396be9607d0a994dba58eb282e281a00da2c554c420229e52b8900d8ac701b1 +size 132 diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_uint8/get_class_labels.sh b/models/object_detection/ssd_mobilenet_v1/tflite_uint8/get_class_labels.sh index 5c31a40..4904ed2 100755 --- a/models/object_detection/ssd_mobilenet_v1/tflite_uint8/get_class_labels.sh +++ b/models/object_detection/ssd_mobilenet_v1/tflite_uint8/get_class_labels.sh @@ -19,7 +19,7 @@ git clone --depth 1 https://github.com/tensorflow/models.git ./tf_models cp tf_models/research/object_detection/data/mscoco_label_map.pbtxt . -python scripts/export_labels.py --path mscoco_label_map.pbtxt +python scripts/export_labels.py --path mscoco_label_map.pbtxt --num_classes 90 tr -d \" < temp.txt > labelmapping.txt rm -rf temp.txt mscoco_label_map.pbtxt rm -rf ./tf_models diff --git a/models/object_detection/ssd_mobilenet_v1/tflite_uint8/scripts/export_labels.py b/models/object_detection/ssd_mobilenet_v1/tflite_uint8/scripts/export_labels.py index ac774cb..f1d0b70 100644 --- a/models/object_detection/ssd_mobilenet_v1/tflite_uint8/scripts/export_labels.py +++ b/models/object_detection/ssd_mobilenet_v1/tflite_uint8/scripts/export_labels.py @@ -16,7 +16,6 @@ import argparse import collections -import sys def read_label_map(label_map_path): item_id = None @@ -42,12 +41,16 @@ def read_label_map(label_map_path): return items -def convert_dictionary_to_list(d): +def convert_dictionary_to_list(d, num_classes): output_list = [] # order dictionary by keys d = collections.OrderedDict(sorted(d.items())) - for k, v in d.items(): - output_list.append(v) + + for c in range(num_classes): + if c + 1 in d: + output_list.append(d[c + 1]) + else: + output_list.append('') return output_list @@ -55,11 +58,12 @@ def convert_dictionary_to_list(d): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Process ImageNet labels.") parser.add_argument("--path", type=str, required=True) + parser.add_argument("--num_classes", type=int, required=True) args = parser.parse_args() items = read_label_map(args.path) - items = convert_dictionary_to_list(items) + items = convert_dictionary_to_list(items, args.num_classes) with open("temp.txt", "w") as f: for item in items: