CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

The shape of the sequence 0 is not compatible with the sample shape - Python

Open Arminea opened this issue 9 years ago • 6 comments

Hi, I'm trying to evaluate a saved convolutional network according to this tutorial https://github.com/Microsoft/CNTK/wiki/Evaluate-a-saved-convolutional-network but I'm not able to load an image. Error I'm getting is ValueError: Value::Create:: The shape of the sequence 0 ([224 x 224]) is not compatible with the sample shape ([3 x 224 x 224])

Here is my code

import numpy as np
from PIL import Image
import matplotlib.image as img
import cntk as ct
from cntk import load_model

if __name__ == "__main__":
	pathToImage = 'C:\\Apache24\\htdocs\\diplomka\\images\\yay.png'
	size = 224, 224

	rgb_image = np.asarray(Image.open(pathToImage).resize(size), dtype=np.float32) - 128
	bgr_image = rgb_image[..., [2, 1, 0]]
	pic = np.ascontiguousarray(np.rollaxis(bgr_image, 2))
	print(np.shape(pic)) # shape is (3x224x224) here

	z = load_model("model.dnn")
	z_out = ct.combine([z.outputs[2].owner])
	predictions = np.squeeze(z_out.eval({z_out.arguments[0]:pic}))
	top_class = np.argmax(predictions)

And here is the image I used yay

Arminea avatar Feb 17 '17 15:02 Arminea

Hi

Your image has 4 channels, because it has png format. Line 14 will print (224, 224, 4) as the shape of image. You should put its transparency channel away before feed it into network.

@hajix I tried to remove the transparency layer of png image. It didn't help. The error is the same for jpg images.

Arminea avatar Feb 17 '17 19:02 Arminea

@zhouwangzw Hi, could you please look into it? I can't tell if it's another bug or not :(

Arminea avatar Feb 25 '17 09:02 Arminea

@Arminea try:

...
predictions = np.squeeze(z_out.eval({z_out.arguments[0]: [pic]}))
...

i.e. put pic in a list.

I found a Stack Overflow post that helped me with a similar issue: http://stackoverflow.com/a/42354910/1226799

juharris avatar Feb 27 '17 18:02 juharris

juharris is correct, pic should be in a list. I also updated the Wiki page.

zhouwangzw avatar Feb 28 '17 07:02 zhouwangzw

@juharris That solved my issue. Thank you :)

Arminea avatar Feb 28 '17 21:02 Arminea