Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Images in a tensorflow.js model and train the model for given images labels

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'): 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/[email protected]"></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>

we got the following error while running the code snippet: [email protected]: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 ([email protected]:1) at standardizeInputData ([email protected]:1) at t.standardizeUserData ([email protected]:1) at t. ([email protected]:1) at n ([email protected]:1) at Object.next ([email protected]:1) at [email protected]:1 at new Promise () at __awaiter$15 ([email protected]:1) at t.fit ([email protected]:1)

This error coming while passing this sample error

like image 387
Adarsh4sfdc Avatar asked Dec 06 '25 05:12

Adarsh4sfdc


1 Answers

You simply need to reshape your tensor data.

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)

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()
like image 179
edkeveked Avatar answered Dec 09 '25 05:12

edkeveked