javascripttensorflow.jsragged

How to create ragged tensors with tensorflow.js


I can't seem to figure out how to create a ragged tensor with tensorflow.js. I also can't find anything in the api docs.

I am working on making a simple cat vs dog AI and have images that are different sizes. I am using fs.readFileSync to get a buffer, using the toJson method and then using the data property on that object to use as a vector in my training data. Because the images are different each vector is a different size and thus the need for a ragged tensor. I have already tried using null or NaN in my shape with no results.

I am also fairly new to tensorflow so any suggestions are welcome.

Here is my code:

const tensorflow = require('@tensorflow/tfjs')
const fs = require('fs')

exports.ImagePredict = function (test, ...dataSets) {
  const images = {}

  for(const i of dataSets) {
    images[i.name] = []
    for(const x of i.data) {
      images[i.name].push(fs.readFileSync(x).toJSON().data)
    }

    images[i.name] = tensorflow.tensor(images[i.name], [images[i.name].length, null])
  }

  return images
}

console.log(exports.ImagePredict(null, {name: 'Cat', data: ['../Sets/PetImages/Cat/1.jpg', '../Sets/PetImages/Cat/2.jpg', '../Sets/PetImages/Cat/3.jpg']}))

I expected the function to return a ragged tensor but instead it threw this error:

throw new Error(typeof msg === 'string' ? msg : msg());
        ^

Error: Element arr[1] should have 16868 elements, but has 26997 elements
    at Object.assert (C:\Users\Miles\Desktop\AI\tensorflow.js\node_modules\@tensorflow\tfjs-core\dist\util.js:48:15)
    at deepAssertShapeConsistency (C:\Users\Miles\Desktop\AI\tensorflow.js\node_modules\@tensorflow\tfjs-core\dist\tensor_util_env.js:34:12)
    at deepAssertShapeConsistency (C:\Users\Miles\Desktop\AI\tensorflow.js\node_modules\@tensorflow\tfjs-core\dist\tensor_util_env.js:38:9)
    at Object.inferShape (C:\Users\Miles\Desktop\AI\tensorflow.js\node_modules\@tensorflow\tfjs-core\dist\tensor_util_env.js:20:9)
    at Object.tensor (C:\Users\Miles\Desktop\AI\tensorflow.js\node_modules\@tensorflow\tfjs-core\dist\ops\tensor_ops.js:23:43)
    at Object.exports.ImagePredict (C:\Users\Miles\Desktop\AI\tensorflow.js\index.js:13:33)
    at Object.<anonymous> (C:\Users\Miles\Desktop\AI\tensorflow.js\index.js:19:21)
    at Module._compile (internal/modules/cjs/loader.js:736:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)

Solution

  • Currently, tfjs does not support ragged tensor. If your images are of different shape consider either cropping them or resizing them to be of shape the inputShape of your model. Actually the latter is a common processing in most classification model