Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将SSD对象检测模型转换为TFLite,并将其从浮点量化为EdgeTPU的uint8_Python_Tensorflow_Tensorflow Lite_Google Coral_Edge Tpu - Fatal编程技术网

Python 将SSD对象检测模型转换为TFLite,并将其从浮点量化为EdgeTPU的uint8

Python 将SSD对象检测模型转换为TFLite,并将其从浮点量化为EdgeTPU的uint8,python,tensorflow,tensorflow-lite,google-coral,edge-tpu,Python,Tensorflow,Tensorflow Lite,Google Coral,Edge Tpu,我在将SSD对象检测模型转换为EdgeTPU的uint8 TFLite时遇到问题 据我所知,我一直在不同的论坛中搜索,堆栈溢出线程和github问题,我认为我遵循了正确的步骤。我的jupyter笔记本一定出了什么问题,因为我无法实现我的建议 我和你们分享我在Jupyter笔记本上解释的步骤。我想会更清楚 #!/usr/bin/env python # coding: utf-8 组织 此步骤是克隆存储库。如果您以前做过一次,可以省略此步骤。 进口 必要步骤:这只是为了制造进口产品 下载友好的模

我在将SSD对象检测模型转换为EdgeTPU的uint8 TFLite时遇到问题

据我所知,我一直在不同的论坛中搜索,堆栈溢出线程和github问题,我认为我遵循了正确的步骤。我的jupyter笔记本一定出了什么问题,因为我无法实现我的建议

我和你们分享我在Jupyter笔记本上解释的步骤。我想会更清楚

#!/usr/bin/env python
# coding: utf-8
组织 此步骤是克隆存储库。如果您以前做过一次,可以省略此步骤。 进口 必要步骤:这只是为了制造进口产品 下载友好的模型 对于tflite,建议使用SSD网络。 我已经下载了以下模型,它是关于“目标检测”。它适用于320x320图像。
# Download the checkpoint and put it into models/research/object_detection/test_data/

!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz
!tar -xf ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz
!if [ -d "models/research/object_detection/test_data/checkpoint" ]; then rm -Rf models/research/object_detection/test_data/checkpoint; fi
!mkdir models/research/object_detection/test_data/checkpoint
!mv ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint models/research/object_detection/test_data/
用于为每个框添加正确标签的字符串列表。 导出并使用TFLite运行 模型转换 在这一步中,我将pb保存的模型转换为.tflite 模型量化(从浮点到uint8) 一旦模型被转换,我需要量化它。原始模型选取一个浮点作为张量输入。因为我想在边缘TPU上运行它,所以我需要输入和输出张量为uint8。 生成校准数据集。 (不要运行这个)。这是上述步骤,但具有随机值 如果没有数据集,还可以引入随机生成的值,就像它是图像一样。这是我用来这样做的代码:
####THIS IS A RANDOM-GENERATED DATASET#### 
def representative_dataset_gen():
    for _ in range(320):
      data = np.random.rand(1, 320, 320, 3)
      yield [data.astype(np.float32)]
要求模型转换 警告: 转换步骤返回一个警告

警告:absl:对于包含不受支持的操作的模型输入 无法量化,
推断\u输入\u类型
属性将默认 与原始类型相同。警告:absl:对于包含 无法量化的不受支持的操作
推断输出类型
属性将默认为原始类型

这让我觉得转换是不正确的

保存模型 测验 测试1:获取TensorFlow版本 我读到建议每晚使用。所以在我的例子中,版本是2.6.0

print(tf.version.VERSION)
测试2:获取输入/输出张量详细信息 测试2结果: 我得到以下信息:

[{'name':'serving_default_输入:0','index':0','shape':数组([1, 3203203],dtype=int32),“shape_签名”:数组([1320320, 3] ,dtype=int32),'dtype':,'quantization': (0.007843137718737125127),“量化参数”:{“尺度”: 数组([0.00784314],dtype=float32),“零点”:数组([127], dtype=int32),“量化的_维度”:0},“稀疏性_参数”:{}] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

[{'name':'StatefulPartitionedCall:31','index':377','shape':数组([ 1,10,4],dtype=int32),“形状签名”:数组([1,10,4], dtype=int32),'dtype':,'quantization':(0.0, 0),“量化参数”:{“刻度”:数组([],dtype=float32), “零点”:数组([],dtype=int32),“量化的维度”:0}, 'sparsity_parameters':{},{'name':'StatefulPartitionedCall:32', “索引”:378,“形状”:数组([1,10],dtype=int32), “shape_签名”:数组([1,10],dtype=int32),“dtype”:,“quantization”:(0.0,0),“quantization_参数”: {'scales':数组([],dtype=float32),'zero_points':数组([], dtype=int32),“量化的_维度”:0},“稀疏性_参数”:{}, {'name':'StatefulPartitionedCall:33','index':379','shape':数组([ 1,10],dtype=int32),“shape_签名”:数组([1,10],dtype=int32), “数据类型”:“量化”:(0.0,0), '量化参数':{'scales':数组([],dtype=float32), “零点”:数组([],dtype=int32),“量化的维度”:0}, 'sparsity_parameters':{},{'name':'StatefulPartitionedCall:34', “index”:380,“shape”:数组([1],dtype=int32),“shape\u签名”: 数组([1],dtype=int32),“dtype”:, ‘量化’:(0.0,0),‘量化参数’:{‘尺度’: 数组([],dtype=float32),“零点”:数组([],dtype=int32), “量化的_维度”:0},“稀疏性_参数”:{}]

所以,我认为它没有正确地量化它

将生成的模型转换为EdgeTPU jose@jose-VirtualBox:~/python envs$edgetpu_编译器-s /home/jose/codespace-2.4.1/tf_2.4.1/tflite/model_full_integer_quant.tflite Edge TPU编译器版本15.0.340273435

模型在1136毫秒内编译成功

输入模型: /home/jose/codespace-2.4.1/tf_2.4.1/tflite/model_full_integer_quant.tflite 输入大小:3.70MiB输出型号: 型号\完整\整数\数量\ edgetpu.tflite输出大小:4.21MiB片上 用于缓存模型参数的内存:3.42MiB片上内存 剩余用于缓存模型参数:使用4.31MiB片外内存 对于流式未缓存模型参数:0.00B个边缘TPU 子图:1操作总数:162操作日志: model_full_integer_quant_edgetpu.log

已成功编译模型,但不支持所有操作 边缘TPU。一定比例的模型将在CPU上运行, 这比较慢。如果可能,考虑更新您的模型只使用 边缘TPU支持的操作。详情请浏览 g、 文书主任/珊瑚/模型需求。将在边缘TPU上运行的操作数: 112将在CPU上运行的操作数:50

操作员计数状态

后勤1运作则不然 支持,但由于某些未指定的限制而未映射 深度CONV 2D 14包含多个子图 不支持深度转换2D 37映射到边缘 TPU量化1映射到边缘TPU 否则,量化4操作无效 支持,但由于某些未指定的限制CONV_2D未映射
58映射到边缘TPU CONV_2D 14
不支持多个子图去量化
1操作正在处理不受支持的数据类型反量化 1除此之外,还支持其他操作,bu
PATH_TO_LABELS = '/home/jose/codeWorkspace-2.4.1/tf_2.4.1/models/research/object_detection/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
!tflite_convert --saved_model_dir=/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/saved_model --output_file=/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/model.tflite
def representative_dataset_gen():
    folder = "/home/jose/codeWorkspace-2.4.1/tf_2.4.1/images_ssd_mb2_2"
    image_size = 320
    raw_test_data = []

    files = glob.glob(folder+'/*.jpeg')
    for file in files:
        image = Image.open(file)
        image = image.convert("RGB")
        image = image.resize((image_size, image_size))
        #Quantizing the image between -1,1;
        image = (2.0 / 255.0) * np.float32(image) - 1.0
        #image = np.asarray(image).astype(np.float32)
        image = image[np.newaxis,:,:,:]
        raw_test_data.append(image)

    for data in raw_test_data:
        yield [data]
####THIS IS A RANDOM-GENERATED DATASET#### 
def representative_dataset_gen():
    for _ in range(320):
      data = np.random.rand(1, 320, 320, 3)
      yield [data.astype(np.float32)]
converter = tf.lite.TFLiteConverter.from_saved_model('/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/saved_model')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, tf.lite.OpsSet.SELECT_TF_OPS]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
converter.allow_custom_ops = True
converter.representative_dataset = representative_dataset_gen
tflite_model = converter.convert()
with open('/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/model_full_integer_quant.tflite'.format('/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/saved_model'), 'wb') as w:
    w.write(tflite_model)
print("tflite convert complete! - {}/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/model_full_integer_quant.tflite".format('/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/saved_model'))
print(tf.version.VERSION)
interpreter = tf.lite.Interpreter(model_path="/home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/model_full_integer_quant.tflite")
interpreter.allocate_tensors()

print(interpreter.get_input_details())
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
print(interpreter.get_output_details())
!edgetpu_compiler -s /home/jose/codeWorkspace-2.4.1/tf_2.4.1/tflite/model_full_integer_quant.tflite