【问题标题】:Image Classification Graph model making wrong predictions图像分类图模型做出错误的预测
【发布时间】:2020-04-16 04:55:13
【问题描述】:

我正在使用 make_image_classifier python 脚本在一组新图像上重新训练 mobilenetv2。我的最终目标是在浏览器中使用 tfjs 进行预测。

这正是我正在做的:

第 1 步:重新训练模型

make_image_classifier \
  --image_dir input_data \
  --tfhub_module https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4 \
  --image_size 224 \
  --saved_model_dir ./trained_model \
  --labels_output_file class_labels.txt \
  --tflite_output_file new_mobile_model.tflite

第二步:使用 tensorflowjs_converter 将 tf 保存的模型转换为图模型

tensorflowjs_converter \
    --input_format=tf_saved_model \
    --output_format=tfjs_graph_model \
    --signature_name=serving_default \
    --saved_model_tags=serve \
    trained_model/ \
    web_model/

第 3 步:在浏览器中加载新模型,预处理图像输入并要求模型进行预测

const model =  tf.loadGraphModel('model.json').then(function(m){
var img = document.getElementById("img");
var processed=preprocessImage(img, "mobilenet")
    window.prediction=m.predict(processed)
        window.prediction.print();
    })
})

function preprocessImage(image,modelName){
    let tensor=tf.browser.fromPixels(image)
    .resizeNearestNeighbor([224,224])
    .toFloat();
    console.log('tensor pro', tensor);
    if(modelName==undefined)
    {
        return tensor.expandDims();
    }
    if(modelName=="mobilenet")
    {
        let offset=tf.scalar(127.5);
        console.log('offset',offset);
        return tensor.sub(offset)
        .div(offset)
        .expandDims();
    }
    else
    {
        throw new Error("Unknown Model error");
    }
}

我得到了无效的结果。我检查了初始模型所做的预测,它们是正确的,所以我在想要么转换没有正确发生,要么我没有以与初始脚本相同的方式预处理图像。

帮助。

P.S:运行转换器时,我收到以下消息。不确定它是否与我的经历直接相关。

tensorflow/core/graph/graph_constructor.cc:750 节点“StatefulPartitionedCall”有 71 个输出,但 _output_shapes 属性为 605 个输出指定形状。输出形状可能不准确。

【问题讨论】:

    标签: tensorflow tensorflow.js tensorflowjs-converter mobilenet


    【解决方案1】:

    make_image_classifier 创建一个为 tensorflow lite 指定的 saved_model。如果您想将 mobilenet 转换为 tensorflow.js,请在此 answer 中给出要使用的命令。

    您需要使用retrain.py,而不是使用make_image_classifier,可以通过以下方式下载

    curl -LO https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py
    

    【讨论】:

    • 非常感谢。做到了。 retrain.py 的自述文件提到它正在被 make_image_classifier 取代。我假设他们做了同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多