0

I'm using tfjs-node for loading model and predicting results in my Node.js application. It was providing decent results but for some image, the following error was shown:

Error when checking : expected input to have shape [null,300,300,3] but got array with shape [1,300,300,4].

Code for loading and predicting results:

const loadModel = async (imagePath) => {
  const image = fs.readFileSync(imagePath);
  let tensor = tf.node.decodeImage(image);

  const resizedImage = tensor.resizeNearestNeighbor([300, 300]);
  const batchedImage = resizedImage.expandDims(0);
  const input = batchedImage.toFloat().div(tf.scalar(255));

  const model = await tf.loadLayersModel(
    process.env.ML_MODEL_PATH || "file://./ml-model/model.json"
  );

  let predictions = await model.predict(input).data();
  predictions = Array.from(predictions);
};

How to fix this? Thanks.

suravshrestha
  • 329
  • 1
  • 5
  • 14

1 Answers1

0

Use

let tensor = tf.node.decodeImage(image, 3);

it will remove alpha if the image is allowed to have transparency like png.

Tensorflow documentation source.

Another possibility is that you had CMYK image. For which I wouldn't know the answer, except that you may try to convert to RGB.

Minsky
  • 2,277
  • 10
  • 19