我从 Tensorflow Hub 重新训练了一个 mobilenet-v1 图像分类模型,并使用 toco 将其转换为使用 Tensorflow Lite 进行推理。
但是,当我使用 tflite 模型运行推理时,它需要的输入大小与我使用--input_shape
.
如何根据自己的数据重新训练 mobilenetv1 量化模型?
这是我尝试的步骤:
- 从 tensorflow 为诗人 codelab 下载训练数据集
使用上面的数据集在 TF Hub 上重新训练mobilenet v1 量化模型
python retrain.py \ --bottleneck_dir="${IMAGE_DIR}"/tf_files/bottlenecks/ \ --how_many_training_steps=1000 \ --model_dir="${IMAGE_DIR}"/tf_files/models/mobilenet_v1_050_224 \ --summaries_dir="${IMAGE_DIR}"/tf_files/training_summaries/mobilenet_v1_050_224/ \ --output_graph="${IMAGE_DIR}"/tf_files/retrained_mobilenet_v1_050_224.pb \ --output_labels="${IMAGE_DIR}"/tf_files/retrained_labels.txt \ --tfhub_module=https://tfhub.dev/google/imagenet/mobilenet_v1_050_224/quantops/classification/1 \ --image_dir="${IMAGE_DIR}"/tf_files/flower_photos
验证模型是否经过正确训练,输入/输出张量名称是否正确
python label_image.py \ --graph="${IMAGE_DIR}"/tf_files/retrained_mobilenet_v1_050_224.pb \ --labels="${IMAGE_DIR}"/tf_files/retrained_labels.txt \ --input_layer=Placeholder \ --output_layer=final_result \ --input_height=224 --input_width=224 \ --image="${IMAGE_DIR}"/tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg
将模型转换为 tflite
toco \ --input_file="${IMAGE_DIR}"/tf_files/retrained_mobilenet_v1_050_224.pb \ --output_file="${IMAGE_DIR}"/tf_files/mobilenet_v1_050_224_quant.tflite \ --input_format=TENSORFLOW_GRAPHDEF \ --output_format=TFLITE \ --input_shape=1,224,224,3 \ --input_array=Placeholder \ --output_array=final_result \ --inference_type=QUANTIZED_UINT8 \ --input_data_type=FLOAT
虽然我指定--input_shape=1,224,224,3
了 ,但是当我运行推理时,我得到了一个错误:
java.lang.IllegalArgumentException: DataType (1) of input data does not match with the DataType (3) of model inputs.