如何在tensorflow.js模型中添加图像并为给定图像标签训练模型 [英] How to add Images in a tensorflow.js model and train the model for given images labels

查看:162
本文介绍了如何在tensorflow.js模型中添加图像并为给定图像标签训练模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用TensorFlow.js创建和训练模型.我们使用tf.fromPixels()函数将图像转换为张量. 我们要创建一个具有以下属性的自定义模型:

We are using TensorFlow.js to create and train the model. We use tf.fromPixels() function to convert an image into tensor. We want to create a custom model with the below properties:

AddImage(HTML_Image_Element,'Label'):添加带有自定义标签的imageElement Train()/fit():训练带有关联标签的此自定义模型 Predict():使用相关标签预测图像,并且它将返回带有每个图像附加标签的预测响应. 为了更好地理解,让我们举个例子: 假设我们有三个用于预测的图像,即:img1,img2,img3,分别带有三个标签"A","B"和"C". 因此,我们想使用这些图像和相应的标签来创建和训练我们的模型,如下所示: 当用户想要预测"img1"时,它会类似地显示预测"A",对于"img2"使用"B"进行预测,对于"img3"使用"C"进行预测

AddImage( HTML_Image_Element, 'Label'): Add an imageElement with a custom label Train() / fit() : Train this custom model with associated labels Predict(): Predict the images with their associated label, and it will return the predicted response with the attached label of every image. For better understanding let's take an example: Let's say we have three images for prediction i.e: img1, img2, img3 with three labels 'A', 'B' and 'C' respectively. So we want to create and train our model with these images and respective labels like below : When user want to predict 'img1' then it shows the prediction 'A', similarly, for 'img2' predict with 'B' and for 'img3' predict with 'C'

请向我建议如何创建和训练该模型.

Please suggest to me how can we create and train this model.

This is webpage we used to create a model with images and its associate labels:
 
<apex:page id="PageId" showheader="false">
    <head>
        <title>Image Classifier with TensorFlowJS</title> 
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <div id="output_field"></div>
    <img id="imgshow" src="{!$Resource.cat}" crossorigin="anonymous" width="400" height="300" />
    
    <script>
    async function learnlinear(){
        
        
        //img data set
        const imageHTML = document.getElementById('imgshow');           
        console.log('imageHTML::'+imageHTML.src);
        
        //convert to tensor 
        const tensorImg = tf.fromPixels(imageHTML);
        tensorImg.data().then(async function (stuffTensImg){
            console.log('stuffTensImg::'+stuffTensImg.toString());
            
        });
        const model = tf.sequential();
            
        model.add(tf.layers.conv2d({
            kernelSize: 5,
            filters: 20,
            strides: 1,
            activation: 'relu',
            inputShape: [imageHTML.height, imageHTML.width, 3],
        }));
        
        model.add(tf.layers.maxPooling2d({
            poolSize: [2, 2],
            strides: [2, 2],
        }));
        
        model.add(tf.layers.flatten());
        
        model.add(tf.layers.dropout(0.2));
        
        // Two output values x and y
        model.add(tf.layers.dense({
            units: 2,
            activation: 'tanh',
        }));
        
        // Use ADAM optimizer with learning rate of 0.0005 and MSE loss
        model.compile({
            optimizer: tf.train.adam(0.0005),
            loss: 'meanSquaredError',
        });
        await model.fit(tensorImg, {epochs: 500});
        model.predict(tensorImg).print();
    }
    learnlinear();
    </script>
   
</apex:page>

在运行代码段时出现以下错误: tfjs@0.11.2:1未捕获(承诺)错误:检查输入时出错:预期conv2d_Conv2D1_input具有4个维.但是得到了形状为300,400,3的数组 在新的t(tfjs@0.11.2:1) 在standardizeInputData(tfjs@0.11.2:1) 在t.standardizeUserData(tfjs@0.11.2:1) 在t. (tfjs@0.11.2:1) 在n(tfjs@0.11.2:1) 在Object.next(tfjs@0.11.2:1) 在tfjs@0.11.2:1 在新的Promise() at __awaiter $ 15(tfjs@0.11.2:1) 在t.fit(tfjs@0.11.2:1)

we got the following error while running the code snippet: tfjs@0.11.2:1 Uncaught (in promise) Error: Error when checking input: expected conv2d_Conv2D1_input to have 4 dimension(s). but got an array with shape 300,400,3 at new t (tfjs@0.11.2:1) at standardizeInputData (tfjs@0.11.2:1) at t.standardizeUserData (tfjs@0.11.2:1) at t. (tfjs@0.11.2:1) at n (tfjs@0.11.2:1) at Object.next (tfjs@0.11.2:1) at tfjs@0.11.2:1 at new Promise () at __awaiter$15 (tfjs@0.11.2:1) at t.fit (tfjs@0.11.2:1)

此错误在传递此示例错误时出现

推荐答案

您只需要重塑张量数据即可.

You simply need to reshape your tensor data.

您传递给模型的数据应比inputShape大一维.实际上,predict接受形状为InputShape的元素数组.元素数是批量大小.因此,您的图像数据应具有以下形状[batchsize, ...inputShape](使用省略号"作为"rest"参数来指示形状的后半部分与inputShape相同)

The data you passed in to your model should be one dimension bigger than the inputShape. Actually predict takes an array of elements of shape InputShape. The number of elements is the batch size. Therefore your image data should have the following shape [batchsize, ...inputShape] (using the ellipsis for rest parameter to indicate that the later part of the shape is equal to that of inputShape)

由于您只训练一个元素(在实际情况中并不会真正发生),因此只需使用批处理大小为1即可.

Since you're training with only one element (which does not really happen in real case) one simply needs to use a batchsize of 1.

model.predict(tensorImg.expandDims(0)).print()

这篇关于如何在tensorflow.js模型中添加图像并为给定图像标签训练模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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