在tensorflow.js中加载保存模型后,使用自定义模型获得错误的预测 [英] getting wrong prediction with custom model after loading save model in tensorflow.js

查看:108
本文介绍了在tensorflow.js中加载保存模型后,使用自定义模型获得错误的预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After compiling and training my custom model, I saved it and got two files such as .bin and .json. Further, I loaded that custom model on another page where I'm giving images as input which I used for training of that model and getting the prediction for those images based on the loaded custom model.

因为它对某些图像效果很好,但对其他图像返回了错误的预测.

Since it works fine for some of the images but returning the wrong prediction for other images.

这是我的代码:

        $("#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);   
            });

推荐答案

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

The model is giving wrong prediction. What to do ?

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

  • check the accuracy of the model. A very low accuracy of the model will indicate that the model is either not the right one for the problem solved or that some parameters needs to be changed.

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

even if the accuracy is good the model can be wrong in predicting a particular class. In that case, the confusion matrix will be of a great help identify the classes incorrectly predicted. When those classes are identified, one can use more training data for those classes to improve their accuracy after the training

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

Looking at the model of the question it is clearly obvious that it is a classification model ie given an image, the model will predict the class the image belongs to.

'meanSquaredError'损失不是分类问题的最佳损失函数. categoricalCrossEntropy将达到最佳精度.即使更改了损失函数,精度仍可能不是预期的.然后,需要添加更多的层,更改模型的其他参数.然后人们将训练并比较精度,然后继续进行下去...

'meanSquaredError' loss is not the best loss function for classification problem. categoricalCrossEntropy will achieve best accuracy. Even after changing the loss function, the accuracy might still not be what is expected. Then one needs to add more layers, change other parameters of the model. Then one will train and compare the accuracy, and the cycle goes on...

这篇关于在tensorflow.js中加载保存模型后,使用自定义模型获得错误的预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆