在 tensorflow.js 中加载保存模型后使用自定义模型进行错误预测

安吉特·夏尔马

在编译和训练我的自定义模型后,我保存它并得到两个文件,例如 .bin 和 .json。此外,我在另一个页面上加载了该自定义模型,我将图像作为输入,用于训练该模型并根据加载的自定义模型获取这些图像的预测。

因为它对某些图像工作正常,但对其他图像返回错误的预测。

这是我的代码:

        $("#predict-button").click(async function(){
        let image= $('#selected-image').get(0);
        let image1 = $('#selected-image1').get(0);
        console.log('image:::',image);
        console.log('image1:::',image1);
        let tensorarr = [];
        let tensor1 = preprocessImage(image,$("#model-selector").val());
        tensorarr.push(tensor1);
        let tensor2 = preprocessImage(image1,$("#model-selector").val());
        tensorarr.push(tensor2);
        let resize_image = [];
        let resize;
        for(var i=0; i<tensorarr.length; i++)
        {
            resize = tf.reshape(tensorarr[i], [1, 224, 224, 3],'resize');
            console.log('resize:::',resize);
            resize_image.push(resize);
        }
        // Labels
        const label = ['Shelf','Rack'];
        const setLabel = Array.from(new Set(label));
        let ysarr =[];
        const ys = tf.oneHot(tf.tensor1d(label.map((a) => setLabel.findIndex(e => e === a)), 'int32'), 10)
        console.log('ys:::'+ys);
        const y = tf.reshape(ys, [-1]);
        y.print();
        const d = y.slice([0], [10]);
        d.print();
        ysarr.push(d);
        const e = y.slice([10], [10]);
        e.print();
        ysarr.push(e);
        console.log('ysarr',ysarr);
        model.add(tf.layers.conv2d({
            inputShape: [224, 224 , 3],
            kernelSize: 5,
            filters: 8,
            strides: 1,
            activation: 'relu',
            kernelInitializer: 'VarianceScaling'
        }));

        model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2}));
        model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2}));
        model.add(tf.layers.flatten({}));
        model.add(tf.layers.dense({units: 64, activation: 'relu'}));
        model.add(tf.layers.dense({units: 10, activation: 'softmax'}));
        model.compile({
            loss: 'meanSquaredError',
            optimizer : 'sgd'
        })
        console.log('model:::'+model);
        // Train the model using the data.
        let tesnor_dim =[];
        let tensr;
        for(var j=0; j<2; j++){
            console.log('resize_image',resize_image);
            tensr = tf.expandDims(ysarr[j], 0);
            tesnor_dim.push(tensr);
            console.log('tesnor_dim',tesnor_dim);
            console.log('before resize_image[j]',resize_image[j]);
            console.log('before tesnor_dim[j]',tesnor_dim[j]);
            await model.fit(resize_image[j], tesnor_dim[j], {epochs: 100}).then((loss) => {
                console.log('resize_image.get[j]',resize_image[j]);
                console.log('tesnor_dim[j]',tesnor_dim[j]);
                console.log('loss',loss);
                const t = model.predict(resize_image[j]);
                console.log('Prediction:::'+t);
                pred = t.argMax(1).dataSync(); // get the class of highest probability
                const labelsPred = Array.from(pred).map(e => setLabel[e]);
                console.log('labelsPred:::'+labelsPred);

            }).catch((e) => {
                console.log(e.message);
            })
            }     
                const saveResults = model.save('downloads://my-model-1');
                console.log(saveResults);   
            });
edkeveked

该模型给出了错误的预测。该怎么办 ?

  • 检查模型的准确性。模型的精度非常低将表明该模型不是解决问题的正确模型,或者某些参数需要更改。

  • 即使准确性很好,模型在预测特定类别时也可能是错误的。在这种情况下,混淆矩阵将非常有助于识别错误预测的类别。识别出这些类别后,可以为这些类别使用​​更多的训练数据,以提高训练后的准确性


查看问题的模型,很明显它是一个分类模型,即给定图像,该模型将预测图像所属的类别。

'meanSquaredError'损失不是分类问题的最佳损失函数。categoricalCrossEntropy将达到最佳精度。即使在更改损失函数之后,准确度可能仍然不是预期的。然后需要添加更多层,更改模型的其他参数。然后就开始训练,比较准确率,循环往复……

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在tensorflow.js中加载模型

来自分类Dev

TensorFlow JS-在模型旁边保存最小/最大值并在预测数据旁边重新加载

来自分类Dev

使用Tensorflow 2.1加载自定义模型

来自分类常见问题

使用TensorFlow模型进行预测

来自分类Dev

使用 Tensorflow.js 加载 Mobilenet 模型并在 express 中间件中进行分类

来自分类Dev

TensorFlow.js中使用的Tensorflow对象检测API模型

来自分类Dev

从Tensorflow.js返回'预测'文本毒性检测模型

来自分类Dev

使用Tensorflow 2.1的Keras模型的自定义指标

来自分类Dev

在Python中保存Keras / Sklearn并将已保存的模型加载到tensorflow.js中

来自分类Dev

tensorflow.js模型不学习

来自分类Dev

如何从Tensorflow.js(.json)模型转换为Tensorflow(SavedModel)或Tensorflow Lite(.tflite)模型?

来自分类Dev

使用保存的模型进行TensorFlow推理

来自分类Dev

转换Mozilla DeepSpeech模型以在tensorflow.js环境中使用

来自分类Dev

在TensorFlow中定义自定义模型/架构

来自分类Dev

使用现有的Tensorflow模型进行预测的问题

来自分类Dev

在不同会话中加载 Tensorflow 模型

来自分类Dev

如何在TensorFlow 2.x中加载Tensorflow 1.x保存的模型?

来自分类Dev

保存和加载Tensorflow模型导致Keras错误

来自分类Dev

在TF Summit 2020中实施``学习使用Tensorflow进行阅读''演讲-Tensorflow 2.1 / 2.2中的EncoderDecoder Seq2Seq模型-自定义火车步骤

来自分类Dev

在TensorFlow CTC中使用自定义波束计分器(语言模型)

来自分类Dev

tensorflow federated:使用自定义数据集和模型时出现TypeError

来自分类Dev

在TensorFlow CTC中使用自定义波束计分器(语言模型)

来自分类Dev

tensorflow tf.contrib.learn.SVM 如何重新加载训练好的模型并使用预测对新数据进行分类

来自分类Dev

在tensorflow.js中,如何计算模型输入的梯度?

来自分类Dev

如何为单输出 MLP 设计 Tensorflow Js 模型?

来自分类Dev

Tensorflow无法保存模型

来自分类Dev

使用tensorflow.js打印预测时出现问题

来自分类Dev

无法使用TensorFlow和TFLEARN保存或加载模型

来自分类Dev

是否可以使用在大型GPU上创建的Tensorflow保存的模型在小型CPU上进行预测?

Related 相关文章

  1. 1

    无法在tensorflow.js中加载模型

  2. 2

    TensorFlow JS-在模型旁边保存最小/最大值并在预测数据旁边重新加载

  3. 3

    使用Tensorflow 2.1加载自定义模型

  4. 4

    使用TensorFlow模型进行预测

  5. 5

    使用 Tensorflow.js 加载 Mobilenet 模型并在 express 中间件中进行分类

  6. 6

    TensorFlow.js中使用的Tensorflow对象检测API模型

  7. 7

    从Tensorflow.js返回'预测'文本毒性检测模型

  8. 8

    使用Tensorflow 2.1的Keras模型的自定义指标

  9. 9

    在Python中保存Keras / Sklearn并将已保存的模型加载到tensorflow.js中

  10. 10

    tensorflow.js模型不学习

  11. 11

    如何从Tensorflow.js(.json)模型转换为Tensorflow(SavedModel)或Tensorflow Lite(.tflite)模型?

  12. 12

    使用保存的模型进行TensorFlow推理

  13. 13

    转换Mozilla DeepSpeech模型以在tensorflow.js环境中使用

  14. 14

    在TensorFlow中定义自定义模型/架构

  15. 15

    使用现有的Tensorflow模型进行预测的问题

  16. 16

    在不同会话中加载 Tensorflow 模型

  17. 17

    如何在TensorFlow 2.x中加载Tensorflow 1.x保存的模型?

  18. 18

    保存和加载Tensorflow模型导致Keras错误

  19. 19

    在TF Summit 2020中实施``学习使用Tensorflow进行阅读''演讲-Tensorflow 2.1 / 2.2中的EncoderDecoder Seq2Seq模型-自定义火车步骤

  20. 20

    在TensorFlow CTC中使用自定义波束计分器(语言模型)

  21. 21

    tensorflow federated:使用自定义数据集和模型时出现TypeError

  22. 22

    在TensorFlow CTC中使用自定义波束计分器(语言模型)

  23. 23

    tensorflow tf.contrib.learn.SVM 如何重新加载训练好的模型并使用预测对新数据进行分类

  24. 24

    在tensorflow.js中,如何计算模型输入的梯度?

  25. 25

    如何为单输出 MLP 设计 Tensorflow Js 模型?

  26. 26

    Tensorflow无法保存模型

  27. 27

    使用tensorflow.js打印预测时出现问题

  28. 28

    无法使用TensorFlow和TFLEARN保存或加载模型

  29. 29

    是否可以使用在大型GPU上创建的Tensorflow保存的模型在小型CPU上进行预测?

热门标签

归档