Lasagne / Recipes

Lasagne recipes: examples, IPython notebooks, ...
MIT License
914 stars 419 forks source link

Wrong number of dimensions: expected 4, got 3 with shape #52

Closed antimodular closed 8 years ago

antimodular commented 8 years ago

i am trying to successfully compile the style transfer example here: https://github.com/Lasagne/Recipes/blob/master/examples/styletransfer/Art%20Style%20Transfer.ipynb

installed cud on my OS X machine got through some other cudnn issues. now i am stuck at this error "'Wrong number of dimensions: expected 4, got 3 with shape (768, 1024, 3).')"

the internet things it's something about numpy misshaped array. ? thanks for any advice?

/Users/stephan/anaconda/bin/python /Users/stephan/PycharmProjects/antimodular/Art_Style_Transfer.py
Using gpu device 0: GeForce GT 750M (CNMeM is disabled, CuDNN 4007)
Traceback (most recent call last):
  File "/Users/stephan/PycharmProjects/antimodular/Art_Style_Transfer.py", line 150, in <module>
    photo_features = {k: theano.shared(output.eval({input_im_theano: photo})) for k, output in zip(layers.keys(), outputs)}
  File "/Users/stephan/PycharmProjects/antimodular/Art_Style_Transfer.py", line 150, in <dictcomp>
    photo_features = {k: theano.shared(output.eval({input_im_theano: photo})) for k, output in zip(layers.keys(), outputs)}
  File "/Users/stephan/.local/lib/python3.5/site-packages/theano/gof/graph.py", line 523, in eval
    rval = self._fn_cache[inputs](*args)
  File "/Users/stephan/.local/lib/python3.5/site-packages/theano/compile/function_module.py", line 786, in __call__
    allow_downcast=s.allow_downcast)
  File "/Users/stephan/.local/lib/python3.5/site-packages/theano/tensor/type.py", line 177, in filter
    data.shape))
TypeError: ('Bad input argument to theano function with name "/Users/stephan/PycharmProjects/antimodular/Art_Style_Transfer.py:150"  at index 0(0-based)', 'Wrong number of dimensions: expected 4, got 3 with shape (768, 1024, 3).')

Process finished with exit code 1
f0k commented 8 years ago

Your compiled Theano function has a 4-dimensional tensor variable as its input placeholder, but you're passing a 3-dimensional numpy array. Convolutional layers generally expect input of (batchsize, channels, rows, cols), so if you want to pass a single image, it should be of shape (1, 3, 768, 1024). The prep_image function in that notebook should return the correct shape, and also do another required preprocessing step (subtracting a predefined mean value that was determined before training). Do not try to bypass it!

antimodular commented 8 years ago

you are right. i did comment it out.

# rawim, art = prep_image(art)
# plt.imshow(rawim)

i converted it to run as a .py script in PyCharm IDE and plt.imshow(rawim) caused some error. but now that i put rawim, art = prep_image(art) the above mentioned issues is gone.

thank you very much.