0
votes

Trying to load Tensorflow trained model into Deeplearning4J with following error:

IllegalStateException: Invalid array shape: cannot associate an array with shape [38880] with a placeholder of shape [-1, -1, -1, 3]:shape is wrong rank or does not match on one or more dimensions
var arr: INDArray = Nd4j.create(data) //.reshape(1, -1, -1, 3);
arr = Nd4j.pile(arr, arr)
sd.associateArrayWithVariable(arr, sd.variables.get(0))

Python model was loaded like that:

# Load image using OpenCV and
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
image = cv2.imread(PATH_TO_IMAGE)
image_expanded = np.expand_dims(image, axis=0)

Please explain any question if you know:

1) What means [1, None, None, 3] in terms of Python arrays

2) What means np.expand_dims(image, axis=0) in Python

3) Deeplearning4J reshape(1, -1, -1, 3);

1

1 Answers

1
votes

You're mixing two different concepts here, TF placeholders, and imperative numpy-like reshape.

In your case, model expects 4D input tensor, with shape [-1, -1, -1, 3]. For human it can be translated to [Any, Any, Any, 3]. But you're trying to feed it with tensor with shape [38880], rank 1.

Now to your questions.

1) See above. -1 is treated as "Any".

2) This function adds 1 as dimension. i.e. if you have [38880], expand_dims at axis=0 will make it [1, 38880]

3) Nope, that's wrong. You should not use that as your shape. You have some image there, so you should specify proper dimensions your image has, i.e. [1, 800, 600, 3].