The implicit shape can't be a fractional number

Viewed 511

I am trying to create a label in TensorFlow. This is image inside the code below.

async function main(){
    const model = await mobilenet.load();
    const classifier = await knnClassifier.create();
    const buf = fs.readFileSync('./41.png');
    const decode = tfnode.node.decodeImage(buf);
    console.log(decode)
    return new Promise(async resolve => {
        const embedding = model.infer(
            decode,
            false
        );
        classifier.addExample(
            embedding,
            'rong'
        )
    })
}

And I got this error:

(node:17992) UnhandledPromiseRejectionWarning: Error: The implicit shape can't be a fractional number. Got 200704 / 150528

This is when I console.log() the decode:

Tensor {
  kept: false,
  isDisposedInternal: false,
  shape: [ 475, 475, 4 ],
  dtype: 'int32',
  size: 902500,
  strides: [ 1900, 4 ],
  dataId: {},
  id: 264,
  rankType: '3',
  scopeId: 311
}
1 Answers

I had the same problem as yours just last night. I was wondering why it would not work with PNG images but JPG images work just fine. As it turned out, it has something to do the number of channels used when decoding the image from buffer.

When we use the decodeImage function (reference), it uses 3 channels for JPG and 4 channels for PNG. However, the model expects 3 channels. Upon decoding the image, we can explicitly tell the decodeImage to use 3 channels for all types of images to be decoded.

The revised code would look like this:

async function main() {
    const model = await mobilenet.load();
    const classifier = await knnClassifier.create();
    const buf = fs.readFileSync('./41.png');
    const decode = tfnode.node.decodeImage(buf, 3);

    return new Promise(async resolve => {
        const embedding = model.infer(decode, false);
        classifier.addExample(embedding, 'rong');
    })
}

Where the decodeImage now accepts a second argument indicating the number of channels to be used during image decoding.

Related