【问题标题】:getting wrong prediction with custom model after loading save model in tensorflow.js在 tensorflow.js 中加载保存模型后,使用自定义模型进行错误预测
【发布时间】:2019-02-28 06:05:56
【问题描述】:

编译和训练我的自定义模型后,我保存它并得到两个文件,例如 .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);   
            });

【问题讨论】:

    标签: javascript tensorflow machine-learning tensorflow.js


    【解决方案1】:

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

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

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


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

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

    【讨论】:

    • 我在损失函数中使用了“categoricalCrossEntropy”,但这只给了我一个标签。我需要添加更多图层吗?如果是,那么这些层是什么?
    • 这里有一个教程:js.tensorflow.org/tutorials/mnist.html关于如何为mnist数据集编写卷积层
    • 我使用了“categoricalCrossEntropy”和您共享的相应链接中的图层,因为它在加载保存的训练模型后无法正常进行预测。我们使用了大约 50 张图像进行训练,带有两个标签。我使用“meanSquaredError”和“categoricalCrossEntropy”进行模型编译并保存了模型。保存模型后,我有另一个页面,我根据保存的模型和训练时使用的图像进行预测。所以我加载了保存的模型以及我在训练时使用但没有给出正确标签的输入图像。
    • @ANKITSHARMA,你能用这个问题打开一个新问题吗?您可以在此处的评论中发布链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多