Model traing – Shape input is for but got 5

Issue

I’m trying to train the model with multiple images (14) of myself (face), But I keep getting 1 more layer than what is needed as it gives null. The model shows null in the first field. I’m trying to make a CNN for image/object/face recognition. I really need help with this and it would be very gratefully appreciated.

Error:

Error when checking input: expected conv2d_Conv2D1_input to have 4 dimension(s). but got array with shape 14,1,1280,720,3

Model:

__________________________________________________________________________________________
Layer (type)                Input Shape               Output shape              Param #   
==========================================================================================
conv2d_Conv2D1 (Conv2D)     [[null,1280,720,3]]       [null,1278,718,16]        448       
__________________________________________________________________________________________
max_pooling2d_MaxPooling2D1 [[null,1278,718,16]]      [null,639,359,16]         0
__________________________________________________________________________________________
conv2d_Conv2D2 (Conv2D)     [[null,639,359,16]]       [null,637,357,32]         4640
__________________________________________________________________________________________
max_pooling2d_MaxPooling2D2 [[null,637,357,32]]       [null,318,178,32]         0
__________________________________________________________________________________________
flatten_Flatten1 (Flatten)  [[null,318,178,32]]       [null,1811328]            0
__________________________________________________________________________________________
dense_Dense1 (Dense)        [[null,1811328]]          [null,10]                 18113290
==========================================================================================
Total params: 18118378
Trainable params: 18118378
Non-trainable params: 0

Code:

  const trainingData = [];
  const trainingLabels = [];


  fs.readdirSync("./data/train/").forEach((file) => {
    const image = tf.node.decodeImage(fs.readFileSync(`./data/train/${file}`)).resizeNearestNeighbor([1280,720])
    .toFloat()
    .div(tf.scalar(255.0))
    .expandDims();
    trainingData.push(image);
    trainingLabels.push("Ewen");
  });


  const model = tf.sequential();

  model.add(tf.layers.conv2d({
    inputShape: [1280,720,3],
    kernelSize: 3,
    filters: 16,
    strides: 1,
    activation: 'relu'
  }));

  model.add(tf.layers.maxPooling2d({
    poolSize: [2, 2]
  }))

  model.add(tf.layers.conv2d({
    kernelSize: 3,
    filters: 32,
    strides: 1,
    activation: 'relu'
  }));

  model.add(tf.layers.maxPooling2d({
    poolSize: [2, 2]
  }))

  model.add(tf.layers.flatten());

  model.add(tf.layers.dense({
    units: 10,
    activation: 'softmax'
  }))
  model.compile({
    optimizer: "adam",
    loss: "categoricalCrossentropy",
    metrics: ["accuracy"],
  });

  logger.success("Model compiled: ");
  model.summary();

Solution

The shape that is inputted (14, 1, 1280, 720, 3) has 5 dimensions. I think you should try and use a reshape function so that it’s 4 dimensions instead ie. just take out the dimension that has 1 as its value. I am not entirely sure what language you’re using but here’s what the code should look like:

input_imgs = reshape(input_imgs, (14, 1280, 720, 3));

You just need to look up the reshape function for the language and framework that you’re using. Hope this helps!

Answered By – Rashaad Meyer

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published