tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Input appears to be wrong shape

Open Neuroforge opened this issue 8 years ago • 4 comments

To get help from the community, check out our Google group.

TensorFlow.js version

Install via Yarn.

var version = '0.6.0';

Browser version

Chrome Version 65.0.3325.181 (Official Build) (64-bit)

Describe the problem or feature request

I think my input type is wrong.....

Should a list of numpy array be a 4D Tensor?

The original input in the python code is a list of numpy arrays.

[np.ndarray(shape=(target_size[1], target_size[0], 3), dtype=np.float32)]

When i try to input this into TensorFlowjs i am not formatting it correctly.

const img = document.getElementById('myimg');
const tfImg = tf.fromPixels(img);
const smalImg = tf.image.resizeBilinear(tfImg, [368, 432]);
const resized = tf.cast(smalImg, 'float32');

OUTPUT_NODE_NAME = ModelName/concat_stage7

Case 1 (Naked image):

const output1 = mobilenet.execute({image: resized}, OUTPUT_NODE_NAME);

e9e6dd3146dafb04690c5987b15aed49.js:2372 Uncaught Error: All values in axis param must be in range [-3, 3) but got axis 3

Case 2 (Array wrapped image):

const output1 = mobilenet.execute({image: [resized]}, OUTPUT_NODE_NAME);

e9e6dd3146dafb04690c5987b15aed49.js:2372 Uncaught (in promise) Error: Error in conv2d: input must be rank 4, but got rank undefined.

How about converting to a 4D Tensor? No luck

Case 3 as 4D

resized.as4D()

e9e6dd3146dafb04690c5987b15aed49.js:2543 Uncaught Error: Size (476928) must match the product of shape ,,,

Case 4 to a 4D Tensor

tf.tensor4d([resized.dataSync()])

Tensor {isDisposed: false, size: 1, shape: Array(1), dtype: "float32", strides: Array(0), …}

tf.tensor4d([resized.dataSync()]).shape [1]

ORIGNAL PYTHON SETUP

    self.tensor_image = self.graph.get_tensor_by_name('image:0')
    self.tensor_output = self.graph.get_tensor_by_name('ModelName/concat_stage7:0')

    # warm-up
    self.persistent_sess.run(
        self.tensor_output,
        feed_dict={
            self.tensor_image: [np.ndarray(shape=(target_size[1], target_size[0], 3), dtype=np.float32)]
        }
    }


I have tired to create a tf.tensor4d([resized]) but it contains NAN

Neuroforge avatar Apr 14 '18 11:04 Neuroforge

Answer seems to be

const img = document.getElementById('myimg'); const tfImg = tf.fromPixels(img); const smalImg = tf.image.resizeBilinear(tfImg, [368, 432]); const resized = tf.cast(smalImg, 'float32'); const t4d = tf.tensor4d(Array.from(resized.dataSync()),[1,368,432,3])

Is there a cleaner way to do this?

Neuroforge avatar Apr 14 '18 13:04 Neuroforge

@Neuroforge Take a look at https://js.tensorflow.org/api/latest/#stack to create the final 4d tensor.

tafsiri avatar Apr 16 '18 18:04 tafsiri

Hi, can anyone help me with this exact same error in react native app? Here is my code and this https://bit.ly/31VPfF4 is what i am getting..Can somebody help me here please?

  • `getPrediction = async () => {

  • await tf.ready(); // ready tf
    
  • setDisplayText('Loading MobileNet');
    
  • const model = await tf.loadGraphModel(
    
  •   bundleResourceIO(modelJSON, modelWeights),
    
  • );
    
  • setDisplayText('Fetching Image');
    
  • const fileUri = url;
    
  • const imgB64 = await FileSystem.readAsStringAsync(fileUri, {
    
  •   encoding: FileSystem.EncodingType.Base64,
    
  • });
    
  • const imgBuffer = tf.util.encodeString(imgB64, 'base64').buffer;
    
  • setDisplayText('Getting Image Buffer');
    
  • const newData = new Uint8Array(imgBuffer);
    
  • const imageTensor = decodeJpeg(newData); // transforms byte array into 3d tensor
    
  • let input = tf.zeros([1, 224, 224, 3]);
    
  • input[0] = imageTensor;
    
  • const prediction = await model.predict(input);
    
  • setDisplayText(JSON.stringify(prediction));
    
  • };`

dipeshkoirala21 avatar Aug 16 '20 15:08 dipeshkoirala21

I got the same error.

My Scenerio: I trained a pre-trained model from tensorflow model zoo using transfer learning using tensorflow api as saved model (model.pb file) and converted it into tfjs format (model.json and shared .bin files).

When I tried running this model.json on the javascript(web), it gives the below error:

Uncaught (in promise) Error: The dtype of dict['input_tensor'] provided in model.execute(dict) must be int32, but was float32

When I tried someone else's working converted model (model.json and shared .bin files) on my javascript(web), it worked.

Conclusion: There is something wrong with my converted model. I converted it using tensorflowjs_converter. My original model in (model.pb) works accurately in python too.

I'm still trying out to convert my model.pb file with different tensorflowjs_converters as it seems to be the converters versioning issue.

parthlathiya2697 avatar Nov 25 '20 04:11 parthlathiya2697