TensorflowJS模型无法正确预测多类数据 [英] TensorflowJS model doesn't predict multiclass data properly

查看:46
本文介绍了TensorflowJS模型无法正确预测多类数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个初学者,我尝试在tensorflowJS中构建一个非常简单的多类分类器,该分类器旨在预测我的视线方向.

As a beginner, I have tried build a really simple multi-class classifier in tensorflowJS which is suppose to predict the direction of my eye sight.

第1步:我在浏览器中创建了数据集,以训练我的模型,在该模型中,将网络摄像头渲染的眼睛图像存储在HTML5画布上.我使用箭头键将图像标记为0 =左,1 =正常和2 =右.为了训练模型,我在传递给方法之前使用tf.onHot()转换了这些标签.

Step 1: I created data set in the browser to train my model where I am storing images of my eyes rendered by webcam on a HTML5 canvas. I use arrow keys to label my images as 0=left,1=normal and 2=right. To train the model, I convert these lables using tf.onHot() before passing to the method.

// data collection 
let imageArray = [];
let labelArray = [];

let collectData = (label) => {
    const img = tf.tidy(() => {
        const captureImg = getImage();
        //console.log(captureImg.shape)
        return captureImg;
    })
    imageArray.push(img)
    labelArray.push(label) //--- labels are 0,1,2
}

// label conversion  
let labelSet = tf.oneHot(tf.tensor1d(labelArray, 'int32'), 3);

第 2 步:我没有加载任何经过训练的模型,而是使用了我使用 tensorflowJS 构建的自定义模型.

Step 2: Instead of loading any per-trained model, I used my custom model that I built using tensorflowJS.

let createModel = () => {
        const model = tf.sequential();
        let config_one = {
            kernelSize: 3,
            filters: 40,
            strides: 1,
            activation: 'relu',
            inputShape: [imageHeight, imageWidth, imageChannels]
        }
        model.add(tf.layers.conv2d(config_one));

        let config_two = {
            poolSize: [2, 2],
            strides: [2, 2],
        }
        model.add(tf.layers.maxPooling2d(config_two));

        model.add(tf.layers.flatten());
        model.add(tf.layers.dropout(0.2));

        // Two output values x and y
        let congfig_output = {
            units: 3,
            activation: 'tanh',
        }
        model.add(tf.layers.dense(congfig_output));

        // Use ADAM optimizer with learning rate of 0.0005 and MSE loss
        let config_compile = {
            optimizer: tf.train.adam(0.00005),
            loss: 'categoricalCrossentropy',
        }
        model.compile(config_compile);

        tf.memory()

        return model;
    }

问题:我现在面临几个问题.

Problems : There are several problems I am facing right now.

  1. 当我将meanSquared用作损失函数且亚当学习率0.000005时,我的模型开始预测,但仅预测正常和左/右眼中的两个状态,因此要进行多类分类,我将损失函数更改为categoricalCrossentropy,但结果仍然相同或有时更糟.

  1. When I use meanSquared as loss function and adam learning rate 0.000005, my model starts predicting but it only predicts two of the eye's state normal and left/right so to do multi-class classification, I changed loss function to categoricalCrossentropy but the result is still same or sometime worst.

我尝试了其他超参数组合,但没有运气.我遇到的最糟糕的情况是我的损失函数仅反复显示三个常数值.

I tried other combination of hyper parameters but no luck. The worst situation I got into was my loss function was showing only three constant values repeatedly.

在以下情况下,我的浏览器将崩溃:-如果-我传递了太多数据或在编译配置中使用了其他类型的优化器,例如sgd或其他任何东西.当我在Google上进行快速搜索时,发现可以使用tf.memory()检查可能导致浏览器崩溃的任何内存泄漏,但该行在控制台中未记录任何内容.

My browser would crashed in some case where - if - I pass too much data or use other type of optimizer in compile config such as sgd or anything else. When I did a quick search on google, I found I can use tf.memory() to check any memory leak which could be causing browser crash but that line didn't log anything in the console.

我正在调整代码中的各种值和参数,并对模型进行了训练,该模型有时使其部分工作,大部分时间甚至无法工作.这一切都是尝试和审判.最终,我了解了编译方法中损失函数的参数以及con2d输入层中的激活函数的参数,但其他方面仍然令人困惑,例如-时期数,批处理大小,在adam中的学习率等.

I was adjusting various values and parameters in the code and training the model which made it work sometimes, partially, and most of the time didn't even work. It was all hit and trial. Eventually I learned about parameters to use for loss function in the compile method and activation function in con2d input layer but other stuff is still confusing such as - number of epochs, batch size, learning rate in adam etc.

我理解还是我理解这些-内核大小,过滤器,步幅,输入形状,但仍然不知道如何确定各种超参数等的层数.

I understood or I think I understood these - kernalsize, filters, strides, inputshape but still have no idea how to decide number of layers various hyper parameters etc.

编辑-这是根据建议更新代码后得到的内容.我仍然没有适当的分类.我正在训练至少1000张以上的图像.

Edit - this is what I get after updating the code as per the suggestion. I still don't proper classification. I am training with minimum of 1000+ images.

A.我仍然会因为固定的瓣膜而反复出现

A. I still get the loss recurring with fixed valeus

B.精度也在以1、0.5和0重复.

B. Accuracy is also repeating itself with 1, 0.5 and 0

function getImage() {
        return tf.tidy(function () {
            const image = tf.browser.fromPixels($('#eyes')[0]);
            const batchedImage = image.expandDims(0);
            const norm = batchedImage.toFloat().div(tf.scalar(255)).sub(tf.scalar(1));
            return norm;
        });
    }

这是控制台输出

示例图像-

推荐答案

对我来说,最明显的错误是输出层的激活功能,在其中使用 tanh 时应使用 softmax 代替.接下来,您可以降低学习率,尝试将其设置为 0.001 ,这是一个很好的默认设置.

Most obvious thing to me that is wrong with this is your output layer's activation function, where you use tanh you should be using softmax instead. Next, your learning rate is way to low try setting it to 0.001 which is a good default to use.

您也可能不需要辍学,因为您没有得到任何结果来证明模型过度拟合.您还可以在其中添加更多卷积层,请尝试以下示例.

You also probably don't need dropout as you have not gotten any results to justify that the model is overfitting. You could also add in more convolutional layers to this, try the example below.

model.add(tf.layers.conv2d({
    inputShape: [28, 28, 1],
    kernelSize: 5,
    filters: 8,
    strides: 1,
    activation: 'relu',
}));

model.add(tf.layers.maxPooling2d({
    poolSize: [2, 2],
    strides: [2, 2],
}));

model.add(tf.layers.conv2d({
    kernelSize: 5,
    filters: 16,
    strides: 1,
    activation: 'relu',
}));

model.add(tf.layers.maxPooling2d({
    poolSize: [2, 2],
    strides: [2, 2],
}));

model.add(tf.layers.flatten());

model.add(tf.layers.dense({
    units: 3,
    activation: 'softmax',
}));

const LEARNING_RATE = 0.001;
const optimizer = tf.train.adam(LEARNING_RATE);

model.compile({
    optimizer: optimizer,
    loss: 'categoricalCrossentropy',
    metrics: ['accuracy'],
});

这篇关于TensorflowJS模型无法正确预测多类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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