heuritech / convnets-keras

MIT License
594 stars 185 forks source link

"negative dimensions are not allowed" #18

Open pmayd opened 7 years ago

pmayd commented 7 years ago

Since the last update of Keras, the code seems no longer to work properly. I get an error trying the example code:

`from keras.optimizers import SGD from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/dog.jpg'], img_size=(256,256), crop_size=(227,227), color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False) model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)`

Full error log: Using Theano backend. Traceback (most recent call last): File "D:/Meine Daten/Dropbox/studium/BSc Psychologie in IT/Bachelor Thesis/03 Software/burkhardt/convnets/test.py", line 7, in <module> model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False) File "C:\Users\Michael\Anaconda3\lib\site-packages\convnetskeras\convnets.py", line 65, in convnet convnet = convnet_init(weights_path, heatmap=False) File "C:\Users\Michael\Anaconda3\lib\site-packages\convnetskeras\convnets.py", line 265, in AlexNet dense_1 = Dense(4096, activation='relu',name='dense_1')(dense_1) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\engine\topology.py", line 487, in __call__ self.build(input_shapes[0]) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\layers\core.py", line 695, in build name='{}_W'.format(self.name)) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\initializations.py", line 59, in glorot_uniform return uniform(shape, s, name=name) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\initializations.py", line 32, in uniform return K.random_uniform_variable(shape, -scale, scale, name=name) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\backend\theano_backend.py", line 140, in random_uniform_variable return variable(np.random.uniform(low=low, high=high, size=shape), File "mtrand.pyx", line 1568, in mtrand.RandomState.uniform (numpy\random\mtrand\mtrand.c:17350) File "mtrand.pyx", line 234, in mtrand.cont2_array_sc (numpy\random\mtrand\mtrand.c:3092) ValueError: negative dimensions are not allowed

As far as I could debug, the error occurs in the method convnet() and there in the method AlexNet and there in the dense_1 line. But I am not able to fix the error.

EgorLakomkin commented 7 years ago

Have the same issue

RBookForFE commented 7 years ago

I suppose you can solve this issue by switching keras backend, as keras changed default backend to TensorFlow since ver 1.1.0. you can switch backend from tf to theano by editing ~/.keras/keras.json . "image_dim_ordering" : "th" "backend" : "theano"

detailed information can be found here : https://keras.io/backend/

pmayd commented 7 years ago

This has absolutely nothing to do with it, if backend had changed, it would not even start because TensorFlow is not even installed. Problem lies within last changes made in Keras, but as I said was not able to fix it yet. And the very first sentence of my error log was: Using Theano backend.

dvro commented 7 years ago

@Jamesadamar, as @RBookForFE mentioned, this is from the keras.json file.

At least in my case, the image_dim_ordering is tf, and should be th, like the following:

{
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "theano"
}

It worked from me.

pmayd commented 7 years ago

Ah I see, I only read the part about the backend and I didn't noticed that you can have there theano as backend but image_dim_ordering as tf, which is quite a senseless combination. That you very much to pointing that out again, I'm sorry

andresrosso commented 7 years ago

Setting Image_dim_ordering to th worked for me:

{ "image_dim_ordering": "th", "epsilon": 1e-07, "floatx": "float32", "backend": "theano" }

piero10 commented 7 years ago

for me too)

lunardog commented 7 years ago

You can set image_dim_ordering in the script (line 58 in convnets.py):


    K.set_image_dim_ordering('th')

    # Select the network
    if network == 'vgg_16':
        convnet_init = VGG_16
    elif network == 'vgg_19':
        convnet_init = VGG_19
...
piero10 commented 7 years ago

lunardog, greate thanks, it works