skip to Main Content

I am sure the solution is out there somewhere, but I have been unable to find it.
I originally trained the model in normal tensorflow, but it is being used in tensorflowjs after being converted.
My current error is

Uncaught (in promise) Error: Size(30000) must match the product of shape 100,100,3

Though I have had many others through my attempts.

My code right now is

function preprocess(imageData) {
    //const img_arr = cv.imread(imageData);
    let inputTensor = tf.browser.fromPixels(imageData);
    const offset = tf.scalar(255.0);
    const normalized = tf.scalar(1.0).sub(inputTensor.div(offset));
    const batchInputShape = [100, 100, 3];
    const flattenedInput = tf.reshape(normalized, [batchInputShape]);
    console.log(flattenedInput.shape);
    return flattenedInput;

The result of this function is then fed into my model, which produces the error. I am sure the solution is obvious but I have been unable to find it.

I have also tried

const batchInputShape = [null, 100, 100, 3];
const flattenedInput = tf.reshape(normalized, [batchInputShape, -1]);

Though that did not fair any better.

2

Answers


  1. If you have a large number of images to process,

     batchInputShape = np.stack(list_of_image_arrays, axis=0)
    

    ouput shape: (batch_size, 100, 100, 3)

    Login or Signup to reply.
  2. First of all you might want to remove those brackets around batchInputShape since tf.reshape is expecting a one dimensional array of numbers, and you are feeding it a two dimensional array: [[null, 100, 100, 3]].

    Second I’m actually not familiar with Tensorflow but I’m guessing null in this case means any numeric value will work, since you’ll always get a shape that can be multiplied by the input layer tensor in your model, no matter what the size is along that dimension. Which is the case for batch size so for you it would be the number of images.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search