官方参考笔记

..../models/research/object_detection/colab_tutorials/convert_odt_model_to_TFLite.ipynb

1.  下载预训练模型或自己数据训练得到的模型

wget http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz

以mobilenetV模型为例,解压后文件包括 :

根目录

checkpoint目录(模型主要文件)

 

 2. 输出pb模型文件,inference graph

python object_detection/export_tflite_graph_tf2.py \
    --trained_checkpoint_dir ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/checkpoint \
    --output_directory ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite \
    --pipeline_config_path ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/pipeline.config

运行结果

2022-09-26 00:33:04.424700: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:04.468000: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:04.468301: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:04.479873: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-09-26 00:33:04.480393: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:04.480556: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:04.480691: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:05.261160: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:05.261287: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:05.261350: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:05.261616: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9858 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
2022-09-26 00:33:08.810228: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:08.810660: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:08.810739: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:08.810834: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:08.810890: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:08.810943: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9858 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
2022-09-26 00:33:09.697849: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:09.697982: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:09.698051: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:09.698146: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:09.698216: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:09.698272: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9858 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.meta_architectures.ssd_meta_arch.SSDMetaArch object at 0x7f6f70070eb8>, because it is not built.
W0926 00:33:09.972333 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.meta_architectures.ssd_meta_arch.SSDMetaArch object at 0x7f6f70070eb8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6f5c37d978>, because it is not built.
W0926 00:33:10.072510 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6f5c37d978>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4ec8d0>, because it is not built.
W0926 00:33:10.072625 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4ec8d0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4eceb8>, because it is not built.
W0926 00:33:10.072688 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4eceb8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6efc4ec0f0>, because it is not built.
W0926 00:33:10.072719 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6efc4ec0f0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4e0be0>, because it is not built.
W0926 00:33:10.072753 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4e0be0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4e00f0>, because it is not built.
W0926 00:33:10.072801 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4e00f0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6efc4e0278>, because it is not built.
W0926 00:33:10.072834 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6efc4e0278>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4e0a90>, because it is not built.
W0926 00:33:10.072867 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4e0a90>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc700978>, because it is not built.
W0926 00:33:10.072907 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc700978>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6efc700780>, because it is not built.
W0926 00:33:10.072938 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.convolutional.SeparableConv2D object at 0x7f6efc700780>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc7003c8>, because it is not built.
W0926 00:33:10.072969 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc7003c8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc7004a8>, because it is not built.
W0926 00:33:10.072999 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc7004a8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6f5c37dcc0>, because it is not built.
W0926 00:33:10.073028 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6f5c37dcc0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53e320>, because it is not built.
W0926 00:33:10.073057 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53e320>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc53e358>, because it is not built.
W0926 00:33:10.073088 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc53e358>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53e5f8>, because it is not built.
W0926 00:33:10.073116 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53e5f8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc53e2e8>, because it is not built.
W0926 00:33:10.073147 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc53e2e8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53edd8>, because it is not built.
W0926 00:33:10.073174 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53edd8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc53ebe0>, because it is not built.
W0926 00:33:10.073201 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc53ebe0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53e5c0>, because it is not built.
W0926 00:33:10.073227 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc53e5c0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6f5c37dcf8>, because it is not built.
W0926 00:33:10.073254 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6f5c37dcf8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc62d9b0>, because it is not built.
W0926 00:33:10.073281 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc62d9b0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc62d518>, because it is not built.
W0926 00:33:10.073308 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc62d518>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc62d320>, because it is not built.
W0926 00:33:10.073335 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc62d320>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc6d7f60>, because it is not built.
W0926 00:33:10.073361 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc6d7f60>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc48c080>, because it is not built.
W0926 00:33:10.073388 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc48c080>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc48c160>, because it is not built.
W0926 00:33:10.073414 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc48c160>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc48c5c0>, because it is not built.
W0926 00:33:10.073441 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc48c5c0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc5da780>, because it is not built.
W0926 00:33:10.073467 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc5da780>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4f3668>, because it is not built.
W0926 00:33:10.073495 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4f3668>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4f35f8>, because it is not built.
W0926 00:33:10.073520 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4f35f8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4f37f0>, because it is not built.
W0926 00:33:10.073546 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4f37f0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4f3828>, because it is not built.
W0926 00:33:10.073614 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4f3828>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4f3390>, because it is not built.
W0926 00:33:10.073644 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc4f3390>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4f3400>, because it is not built.
W0926 00:33:10.073673 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc4f3400>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc727048>, because it is not built.
W0926 00:33:10.073705 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc727048>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6f5c37dd30>, because it is not built.
W0926 00:33:10.073732 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6f5c37dd30>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc705588>, because it is not built.
W0926 00:33:10.073759 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc705588>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc7051d0>, because it is not built.
W0926 00:33:10.073788 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc7051d0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc70d7b8>, because it is not built.
W0926 00:33:10.073815 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc70d7b8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc70d7f0>, because it is not built.
W0926 00:33:10.073844 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc70d7f0>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc70dc50>, because it is not built.
W0926 00:33:10.073874 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc70dc50>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc70d908>, because it is not built.
W0926 00:33:10.073902 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <object_detection.core.freezable_batch_norm.FreezableBatchNorm object at 0x7f6efc70d908>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc70d0b8>, because it is not built.
W0926 00:33:10.073927 140118163120320 save_impl.py:72] Skipping full serialization of Keras layer <keras.layers.core.Lambda object at 0x7f6efc70d0b8>, because it is not built.
2022-09-26 00:33:14.158427: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
2022-09-26 00:33:14.866584: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:14.866734: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:14.866793: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:14.866899: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:14.866963: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:33:14.867012: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9858 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
W0926 00:33:19.997637 140118163120320 save.py:254] Found untraced functions such as WeightSharedConvolutionalBoxPredictor_layer_call_and_return_conditional_losses, WeightSharedConvolutionalBoxPredictor_layer_call_fn, WeightSharedConvolutionalBoxHead_layer_call_and_return_conditional_losses, WeightSharedConvolutionalBoxHead_layer_call_fn, WeightSharedConvolutionalBoxPredictor_layer_call_fn while saving (showing 5 of 260). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model/assets
I0926 00:33:22.204749 140118163120320 builder_impl.py:781] Assets written to: ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model/assets

看最后一句,生成的文件保存在 tflite/saved_model,目录内容:

3. 转为tflite模型

编了个脚本来进行操作

import matplotlib
import matplotlib.pyplot as plt

import os
import random
import io
import imageio
import glob
import scipy.misc
import numpy as np
from six import BytesIO
from PIL import Image, ImageDraw, ImageFont
from IPython.display import display, Javascript
from IPython.display import Image as IPyImage

import tensorflow as tf

from object_detection.utils import label_map_util
from object_detection.utils import config_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.utils import colab_utils
from object_detection.utils import config_util
from object_detection.builders import model_builder


_TFLITE_MODEL_PATH = "/home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model.tflite"

converter = tf.lite.TFLiteConverter.from_saved_model('/home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()

with open(_TFLITE_MODEL_PATH, 'wb') as f:
  f.write(tflite_model)

_ODT_LABEL_MAP_PATH = '/home/hy/tensorflow/models/research/object_detection/data/mscoco_label_map.pbtxt'
_TFLITE_LABEL_PATH = "/home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite_label_map.txt"

category_index = label_map_util.create_category_index_from_labelmap(
    _ODT_LABEL_MAP_PATH)
f = open(_TFLITE_LABEL_PATH, 'w')
for class_id in range(1, 91):
  if class_id not in category_index:
    f.write('???\n')
    continue
  name = category_index[class_id]['name']
  f.write(name+'\n')
f.close()

运行结果

(py36tflite) hy@hy-ONDA-MJZ690:~/tensorflow/hy-tflite$ python convert_640.py 
/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/IPython/utils/traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
  warn("IPython.utils.traitlets has moved to a top-level traitlets package.")
env ok. 
Traceback (most recent call last):
  File "convert_640.py", line 50, in <module>
    converter = tf.lite.TFLiteConverter.from_saved_model('../../ssd_mobilenet_v2_320x320_coco17_tpu-8/tflite/saved_model')
  File "/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 1348, in from_saved_model
    saved_model = _load(saved_model_dir, tags)
  File "/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py", line 864, in load
    result = load_internal(export_dir, tags, options)["root"]
  File "/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py", line 878, in load_internal
    loader_impl.parse_saved_model_with_debug_info(export_dir))
  File "/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/tensorflow/python/saved_model/loader_impl.py", line 60, in parse_saved_model_with_debug_info
    saved_model = _parse_saved_model(export_dir)
  File "/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/tensorflow/python/saved_model/loader_impl.py", line 121, in parse_saved_model
    constants.SAVED_MODEL_FILENAME_PB))
OSError: SavedModel file does not exist at: ../../ssd_mobilenet_v2_320x320_coco17_tpu-8/tflite/saved_model/{saved_model.pbtxt|saved_model.pb}
(py36tflite) hy@hy-ONDA-MJZ690:~/tensorflow/hy-tflite$ python convert_640.py 
/home/hy/miniconda3/envs/py36tflite/lib/python3.6/site-packages/IPython/utils/traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
  warn("IPython.utils.traitlets has moved to a top-level traitlets package.")
env ok. 
2022-09-26 00:44:26.671471: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.676502: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.676638: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.676961: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-09-26 00:44:26.677429: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.677550: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.677679: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.984267: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.984444: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.984579: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-09-26 00:44:26.984676: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9827 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
2022-09-26 00:44:32.137864: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:351] Ignored output_format.
2022-09-26 00:44:32.137890: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:354] Ignored drop_control_dependency.
2022-09-26 00:44:32.137893: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:360] Ignored change_concat_input_ranges.
2022-09-26 00:44:32.138809: I tensorflow/cc/saved_model/reader.cc:38] Reading SavedModel from: /home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model
2022-09-26 00:44:32.198363: I tensorflow/cc/saved_model/reader.cc:90] Reading meta graph with tags { serve }
2022-09-26 00:44:32.198426: I tensorflow/cc/saved_model/reader.cc:132] Reading SavedModel debug info (if present) from: /home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model
2022-09-26 00:44:32.396499: I tensorflow/cc/saved_model/loader.cc:211] Restoring SavedModel bundle.
2022-09-26 00:44:32.765648: I tensorflow/cc/saved_model/loader.cc:195] Running initialization op on SavedModel bundle at path: /home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model
2022-09-26 00:44:32.937852: I tensorflow/cc/saved_model/loader.cc:283] SavedModel load for tags { serve }; Status: success: OK. Took 799046 microseconds.
2022-09-26 00:44:33.563187: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:210] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2022-09-26 00:44:34.109363: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1899] Estimated count of arithmetic ops: 8.251 G  ops, equivalently 4.125 G  MACs
2022-09-26 00:44:34.121292: I tensorflow/lite/tools/optimize/quantize_weights.cc:225] Skipping quantization of tensor ssd_mobile_net_v2fpn_keras_feature_extractor/model/Conv1/Conv2D because it has fewer than 1024 elements (864).
2022-09-26 00:44:34.121326: I tensorflow/lite/tools/optimize/quantize_weights.cc:225] Skipping quantization of tensor ssd_mobile_net_v2fpn_keras_feature_extractor/model/expanded_conv_depthwise_BN/FusedBatchNormV3;ssd_mobile_net_v2fpn_keras_feature_extractor/model/expanded_conv_depthwise/depthwise;ssd_mobile_net_v2fpn_keras_feature_extractor/model/block_5_project/Conv2D because it has fewer than 1024 elements (288).
2022-09-26 00:44:34.121331: I tensorflow/lite/tools/optimize/quantize_weights.cc:225] Skipping quantization of tensor ssd_mobile_net_v2fpn_keras_feature_extractor/model/expanded_conv_project/Conv2D because it has fewer than 1024 elements (512).
2022-09-26 00:44:34.121348: I tensorflow/lite/tools/optimize/quantize_weights.cc:225] Skipping quantization of tensor ssd_mobile_net_v2fpn_keras_feature_extractor/model/block_1_depthwise_BN/FusedBatchNormV3;ssd_mobile_net_v2fpn_keras_feature_extractor/model/block_1_depthwise/depthwise;ssd_mobile_net_v2fpn_keras_feature_extractor/model/block_12_project/Conv2D because it has fewer than 1024 elements (864).

 在指定目录下生成model.tflite模型文件,这个是可以被手机app加载使用的,但是还需要metadata才能正常被官方example应用调用。 

4. 添加metadata

from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils
_TFLITE_MODEL_PATH = "/home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model.tflite"
_TFLITE_MODEL_WITH_METADATA_PATH = "/home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model_with_metadata.tflite"
_TFLITE_LABEL_PATH = "/home/hy/tensorflow/models/research/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite_label_map.txt"
writer = object_detector.MetadataWriter.create_for_inference(
    writer_utils.load_file(_TFLITE_MODEL_PATH), input_norm_mean=[127.5], 
    input_norm_std=[127.5], label_file_paths=[_TFLITE_LABEL_PATH])
writer_utils.save_file(writer.populate(), _TFLITE_MODEL_WITH_METADATA_PATH)

指定目录下生成更新的model_with_metadata.tflite模型文件

5. android studio打开example应用,替换增加tflite模型

   MODEL_mobilenetV2 -> "model_with_metadata.tflite"

 编译运行

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐