模型没有学习 [英] Model is not learning

查看:20
本文介绍了模型没有学习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对来自我的网络摄像头的图像训练 Tensor-flow js 模型.基本上我正在尝试重新创建吃豆人张量流游戏.该模型没有收敛,并且在训练后几乎毫无用处.我有一种感觉,就是我准备数据的方式.

I am trying to train a Tensor-flow js model on images coming in from my web cam. Basically I'm trying to recreate the pac-man tensor-flow game. The model isn't converging and is pretty much useless after training. I have a feeling its how I'm prepping the data.

从画布中抓取图像

function takePhoto(label) {
  let canv = document.getElementById("canv")
  let cont = canv.getContext("2d")
  cont.drawImage(video, 0, 0, width, height)

  let data = tf.browser.fromPixels(canv, 3)
  data.toFloat().div(tf.scalar(127)).sub(tf.scalar(1))
  return data
}

function addExample(label){
      let data = takePhoto()

      addData(train_data => train_data.concat(data))
      addLabel(train_labels => train_labels.concat(labels[label]))
    }

训练函数

export async function train_model(image,label){
    let d = tf.stack(image)

    let l = tf.oneHot(tf.tensor1d(label).toInt(),4)

    let data = await model.fit(d,l,{epochs:10,batchSize:label[0].length,callbacks:{
        onBatchEnd: async  (batch, logs) =>{ 
            console.log(logs.loss.toFixed(5))
        }
    }})
    return data
}

型号

export function buildModel(){
    model = tf.sequential({layers:[ 
        tf.layers.conv2d({inputShape:[width,height,3],
                            kernelSize:3,
                            filters:5, 
                            activation :"relu"}),
        tf.layers.flatten(),
        tf.layers.dense({units:128, activation:"relu",useBias:true}),
        tf.layers.dense({units:32, activation:"relu"}),
        tf.layers.dense({units:4, activation:"softmax"})
    ]})
    model.compile({metrics:["accuracy"], loss:"categoricalCrossentropy", optimizer:"adam",learningRate:.00001})
    console.log(model.summary())
}

预测

export async function predict(img){

    let pred = await tf.tidy(() => {

        img = img.reshape([1,width,height, 3]);

        const output = model.predict(img);

        let predictions = Array.from(output.dataSync());
        return predictions
    })
    return pred
}

回调打印损失,但它们没有收敛到任何东西,并且预测很远(随机)

The callback prints the losses but they do no converge to anything and the predictions are way off (random)

推荐答案

使用的模型是否正确?

人们需要问的第一个问题是所使用的模型是否正确.问题的模型使用卷积层和密集层的混合.但是该模型并没有真正遵循 CNN 的结构,而卷积层后面总是跟有池化层.这是模型没有学习的原因吗?没必要...

The first question one needs to ask is if the model used is the right one. The model of the question uses a mixture of convolutional and dense layers. But the model does not really follow the structure of CNN whereas convolutional layers are always followed by pooling layers. Is it the reason why the model is not learning ? Not necessary ...

在分类问题中,有不同的方法对图像进行分类,每种方法都有其优缺点.FCNN 没有达到很好的准确率,CNN 做到了.但是训练 CNN 模型的计算成本可能很高.这就是迁移学习发挥作用的地方.

In classification problems, there are different ways of classifying images each one with its pros and cons. FCNN does not achieve good accuracy, CNN does. But training a CNN model can be computation expensive. This is where transfer learning comes to play.

pacman 示例使用迁移学习.因此,如果您想复制该示例,请考虑遵循 tfjs 示例的 github 代码.这里的模型只使用了一个卷积层.关于如何编写 CNN 网络迁移学习模型.

The pacman example uses transfer learning. So if you want to replicate the example, consider following the github code of tfjs example. The model here uses only one convolutional layer. There are good tutorials on the official website of tensorflow as regard how to write CNN networks and transfer-learning models.

您使用了多少数据来训练您的模型?

深度学习模型一般需要大量数据.所以除非模型看到了很多被标记的图像,否则它的准确率很低也就不足为奇了.需要多少数据主要是艺术和设计的问题,而不是科学.但一般的经验法则是,数据越多,模型的预测效果就越好.

Deep learning models in general needs a lot of data. So unless the model has seen a lot of images labelled, it won't be surprising if its accuracy is very low. How much data is needed is mostly a question of art and design than science. But a general rule of thumb, more there is data, better is the model in predicting.

调整模型

即使是一个好的模型也需要调整其参数 - 时期数、批量大小、学习率、优化器、损失函数......改变这些参数并观察它们如何解释准确性是获得良好准确性的一个步骤.

Even a good model needs its parameter to be tuned - number of epochs, batchsize, learning rate, optimizer, loss function... Changing those parameters and observe how they account for the accuracy is a step in having good accuracy.

需要指出的是,作为model.compile

这篇关于模型没有学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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