如何在tensorflow.js模型中添加图像并为给定图像标签训练模型

时间:2019-02-14 11:53:57

标签: tensorflow keras image-recognition tensor tensorflow.js

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

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

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

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)

This error coming while passing this sample error

2 个答案:

答案 0 :(得分:0)

TLDR:您只需使用np.expand_dims()np.reshape()来调整数据大小。

首先,让我们生成一些模拟您当前张量输入的随机张量-

# Some random numpy array
In [20]: x = np.random.random((2,2,4))

In [21]: x
Out[21]: 
array([[[0.8454901 , 0.75157647, 0.1511371 , 0.53809724],
        [0.50779498, 0.41321185, 0.45686143, 0.80532259]],

       [[0.93412402, 0.02820063, 0.5452628 , 0.8462806 ],
        [0.4315332 , 0.9528761 , 0.69604215, 0.538589  ]]])

# Currently your tensor is a similar 3D shape like x
In [22]: x.shape
Out[22]: (2, 2, 4)

现在您可以像这样将其转换为4D张量-

[23]: y = np.expand_dims(x, axis = 3)

In [24]: y
Out[24]: 
array([[[[0.8454901 ],
         [0.75157647],
         [0.1511371 ],
         [0.53809724]],

        [[0.50779498],
         [0.41321185],
         [0.45686143],
         [0.80532259]]],


       [[[0.93412402],
         [0.02820063],
         [0.5452628 ],
         [0.8462806 ]],

        [[0.4315332 ],
         [0.9528761 ],
         [0.69604215],
         [0.538589  ]]]])

In [25]: y.shape
Out[25]: (2, 2, 4, 1)

您可以找到np.expand_dims文档here


编辑:这是单线

np.reshape(np.ravel(x), (x.shape[0], x.shape[1], x.shape[2], 1)).shape 

您可以查看np.reshape文档here

答案 1 :(得分:0)

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

您传递给模型的数据应比inputShape大一维。实际上,predict采用了一组形状为InputShape的元素。元素数是批量大小。因此,您的图像数据应具有以下形状[batchsize, ...inputShape](使用省略号rest参数表示该形状的后面部分与inputShape相等)

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

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