OGeek|极客世界-中国程序员成长平台

标题: python - Tensorflow Lite iOS 相机示例不适用于重新训练的 MobileNet 模型 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:50
标题: python - Tensorflow Lite iOS 相机示例不适用于重新训练的 MobileNet 模型

我正在尝试运行 the Tensorflow Lite Camera example使用经过重新训练的 Mobilenet 模型。

我根据the instructions成功运行iOS相机app和 this fix .该应用程序在模型 mobilenet_v1_1.0_224.tflite 上按预期工作。

我安装了 Tensorflow:

pip3 install -U virtualenv
virtualenv --system-site-packages -p python3 ./venv
source ./venv/bin/activate
pip install --upgrade pip
pip install --upgrade tensorflow==1.12.0
pip install --upgrade tensorflow-hub==40.6.2

我现在想 retrain the model使用 the flowers set .我下载了flowers文件夹并运行:

python retrain.py \
    --bottleneck_dir=bottleneck \
    --how_many_training_steps=400 \
    --model_dir=model \
    --output_graph=pola_retrained.pb \
    --output_labels=pola_retrained_labels.txt \
    --tfhub_module https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/quantops/feature_vector/1 \
    --image_dir=flower_photos

注意:我可以使用 label_image.py 脚本成功测试重新训练的模型。

我将重新训练的模型转换为其 tflite 格式:

toco \
  --graph_def_file=pola_retrained.pb \
  --input_format=TENSORFLOW_GRAPHDEF \
  --output_format=TFLITE \
  --output_file=mobilenet_v1_1.0_224.tflite \
  --inference_type=FLOAT \
  --input_type=FLOAT \
  --input_arrays=Placeholder \
  --output_arrays=final_result \
  --input_shapes=1,224,224,3

我将新模型和标签文件都复制到 iOS 应用程序。我在CameraExampleViewController.mm中修改app参数如下:

// These dimensions need to match those the model was trained with.
const int wanted_input_width = 224;
const int wanted_input_height = 224;
const int wanted_input_channels = 3;
const float input_mean = 128.0f;
const float input_std = 128.0f;
const std::string input_layer_name = "input";
const std::string output_layer_name = "final_result";

应用程序崩溃。 354" rel="noreferrer noopener nofollow">the recognized object is outside of the range of trained objects的索引.置信度高于 1。



Best Answer-推荐答案


Tensorflow Lite Camera 示例将输出张量大小硬编码为 1000。如果您使用输出数量较少的重新训练模型测试示例,iOS 应用程序会崩溃。替换CameraExampleViewController.mm中的以下代码:

const int output_size = 1000;

与:

// read output size from the output sensor
const int output_tensor_index = interpreter->outputs()[0];
TfLiteTensor* output_tensor = interpreter->tensor(output_tensor_index);
TfLiteIntArray* output_dims = output_tensor->dims;
assert(output_dims->size == 2);
const int output_size = output_dims->data[1]-output_dims->data[0];

上面的代码通过从模型输出维度读取输出大小解决了这个问题。 The appropriate PR已提交。

关于python - Tensorflow Lite iOS 相机示例不适用于重新训练的 MobileNet 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53364001/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4