gaurav
gaurav

Reputation: 33

decodePng in tensorflowjs throws shape error

I am trying to follow a tutorial and just want to load an image in TensorFlowJS.

import * as tf from '@tensorflow/tfjs-node';
import fs from 'fs';

(async () => {
  const desk = fs.readFileSync(__dirname + '/' + 'desk.png');
  const buf = Buffer.from(desk);
  const imageArray = new Uint8Array(buf);
  const pngDecodedTensor = tf.node.decodePng(imageArray);
})();

When I run the above code, I see this error:

The shape of dict['image_tensor'] provided in model.execute(dict) must be [-1,-1,-1,3], but was [1,4032,3024,4]

The image is 3024x4032 and 10.4MB

Thanks for your help

Upvotes: 1

Views: 265

Answers (2)

edkeveked
edkeveked

Reputation: 18381

The issue is related to the tensor shape when making the prediction. The model is expecting a tensor with 3 channels whereas the tense passed as argument has 4 channels.

The tensor can be sliced to use only 3 of its 4 channels.

pngDecodedTensor = tf.node.decodePng(imageArray).slice([0], [-1, -1, 3])

Upvotes: 1

Jason Mayes
Jason Mayes

Reputation: 301

You may want to try the fromPixels function like this:

const { Image } = require('canvas')

// From a buffer:
fs.readFile('images/squid.png', (err, squid) => {
  if (err) throw err
  const img = new Image()
  img.onload = () => ctx.drawImage(img, 0, 0)
  img.onerror = err => { throw err }
  img.src = squid
})

// From a local file path:
const img = new Image()
img.onload = () => ctx.drawImage(img, 0, 0)
img.onerror = err => { throw err }
img.src = 'images/squid.png'

// From a remote URL:
img.src = 'http://picsum.photos/200/300'
// ... as above

var imgAsTensor = tf.fromPixels(img);
// ... now use it as you wish.

You can learn more about this function here:

Upvotes: 1

Related Questions