heuritech / convnets-keras

MIT License
594 stars 185 forks source link

Import Error #44

Open yueseW opened 7 years ago

yueseW commented 7 years ago

Hi, I tried your code of transfer Alexnet. But it showed the following error:

File "D:\KERAS\convnets-keras\convnetskeras\customlayers.py", line 2, in from keras.layers.core import Lambda, Merge

ImportError: cannot import name 'Merge'

Please help me to debug this.

agnesmm commented 7 years ago

@yueseW In convnetskeras/customlayers.py change from keras.layers.core import Lambda, Merge by from keras.layers.core import Lambda from keras.layers import Merge

Anabik commented 7 years ago

I replaced from keras.layers.core import Lambda, Merge by from keras.layers.core import Lambda and from keras.layers import Merge in customlayers.py and then compile. But it can not resolve to work with crosschannelnormalization and Merge. Any one please help me.

agnesmm commented 7 years ago

@Anabik can you show the error message?

Anabik commented 7 years ago

My Code:

from keras.models import Sequential, Model Using TensorFlow backend. I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally from keras.layers import Flatten, Dense, Dropout, Reshape, Permute, Activation,Input, merge from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D from keras.engine.topology import Container from keras.optimizers import SGD import numpy as np import scipy.io as sio import h5py from scipy.misc import imread, imresize, imsave from keras import backend as K from keras.utils.np_utils import to_categorical from keras.models import load_model from convnetskeras.convnets import preprocess_image_batch, convnet from keras.callbacks import EarlyStopping image = Input(shape=(3,227,227)) path='/home/cvpr/Anabik/PsoriasisSeverityMTL/Data/alexnet_weights.h5' model = convnet('alexnet',weights_path=path, heatmap=False)

Error message : /home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/convnetskeras/convnets.py:231: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(96, (11, 11), strides=(4, 4), activation="relu", name="conv_1") name='conv_1')(inputs) Traceback (most recent call last): File "", line 1, in File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/convnetskeras/convnets.py", line 67, in convnet convnet = convnet_init(weights_path, heatmap=False) File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/convnetskeras/convnets.py", line 234, in AlexNet conv_2 = crosschannelnormalization(name="convpool_1")(conv_2) File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.py", line 554, in call output = self.call(inputs, kwargs) File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/layers/core.py", line 659, in call return self.function(inputs, arguments) File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/convnetskeras/customlayers.py", line 23, in f , (0,half)) File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1804, in spatial_2d_padding assert len(padding[0]) == 2 TypeError: object of type 'int' has no len()

Please help me.

AlexandreMercierAubin commented 7 years ago

@Anabik &@agnesmm. Seems like I get a similar error with that fix. Have you found the solution to this ?

model=convnet('alexnet',weights_path="C:\Users\alexandre.mercieraub\Documents\AlexTest\alexnet_weights.h5", heatmap=False)

File "C:\ProgramData\Anaconda2\lib\site-packages\convnetskeras\convnets.py", line 65, in convnet convnet = convnet_init(weights_path, heatmap=False)

File "C:\ProgramData\Anaconda2\lib\site-packages\convnetskeras\convnets.py", line 232, in AlexNet conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)

File "C:\ProgramData\Anaconda2\lib\site-packages\keras\engine\topology.py", line 585, in call output = self.call(inputs, **kwargs)

File "C:\ProgramData\Anaconda2\lib\site-packages\keras\layers\core.py", line 659, in call return self.function(inputs, **arguments)

File "C:\ProgramData\Anaconda2\lib\site-packages\convnetskeras\customlayers.py", line 19, in f , (0,half))

File "C:\ProgramData\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 997, in spatial_2d_padding assert len(padding[0]) == 2

TypeError: object of type 'int' has no len()

Anabik commented 7 years ago

I downloaded convnets-keras from https://github.com/lunardog/convnets-keras using the following commands and now working in tensorflow background. git clone https://github.com/lunardog/convnets-keras cd convnets-keras sudo python setup.py install

AlexandreMercierAubin commented 7 years ago

Both versions seems to have the same issue. You mean theano background? Looking at your logs, I can see that you were using tensorflow before solving the problem. Thank you nevertheless, I do appreciate the quick response.

erralves commented 7 years ago

In the customlayers.py file there's a backend function called spatial_2d_padding() (line 17), which needs a tuple of 2 tuples, padding pattern argument. As i can see, only on tuple is fed (0,half). Put ((0,half),(0,0)) and this error is solved. However, making this change causes another problem regarding tensors unequal dimensions.

Debugging the script, i noticed the padding adopted was lower than n parameter defined in crosschannelnormalization(), realized on the permuted 2 and 3 layers. I tried to adjust the padding tuple and with ((0,0),(half,half)) or ((0,0),(0,n)) this problem disappear. Now the extra_channel tensor have the same size (or bigger) than n, solving the indexing problem in the for loop (line 26).

Although, testing the dog picture in the alexnet, i cannot obtained the same heatmap showed in the main example. Maybe i missed something...

EDITED: @AlexandreMercierAubin

Substitute the crosschannelnormalization() function in the customlayers.py file by the code below and use Theano as backend. Such code was merged from pylearn - normalize.py - script.

import theano.tensor as T
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original Alexnet
    combing the conventkeras and pylearn functions.
    erralves
    """
    def f(X):

        ch, r, c, b = X.shape
        half = n // 2
        sq = T.sqr(X)

        extra_channels = T.alloc(0., ch + 2*half, r, c, b)
        sq = T.set_subtensor(extra_channels[half:half+ch,:,:,:], sq)

        scale = k
        for i in range(n):
            scale += alpha * sq[i:i+ch,:,:,:]

        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs)

However, the example test continue to be different...

mkairanbay commented 7 years ago

I have tried to use ((0,0),(half,half)) and ((0,0),(0,n)) in the following code: extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1)) , ((0,0),(half,half))) and extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1)) , ((0,0),(0,n))) However, got the following error: ValueError: ('The specified size contains a dimension with value <= 0', (-15728640,)) I also tried to replace crosschannelnormalization function with new implementation, however, also got the error above. So, s there any idea? Thank you very much!

erralves commented 7 years ago

@mkairanbay

This looks like a problem with the inputs size : Using Theano as backend you state the inputs this way : inputs = Input(shape=(3,227,227)) Using Tensorflow as backend : inputs = Input(shape=(227,227,3))

I suggest you to use Theano as backend. Then, in your keras.json, you can adjust "image_data_format": "channels_last" if you want declare in Tensorflow manner.

mkairanbay commented 7 years ago

@erralves The problem was with Input as you stated. My back-end was tensorflow, however, I used Theano input convention. Thank you. very much! :+1:

schrum2 commented 6 years ago

I've been experiencing this same error with TensorFlow as the backend. However, the main thing I'm trying to do is save a json file describing the AlexNet configuration that corresponds to the linked h5 weights file for AlexNet. Do any of you have such a json file?

kalravibhor commented 6 years ago

Hi, I am trying to load the pre-trained Alexnet using the code shared in the documentation. Unfortunately even after making the requisite changes as stated above, I run into the below mentioned error. @erralves, looking forward to your guidance on resolving the same. Thanks !

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "convnetskeras/convnets.py", line 82, in convnet
    convnet = convnet_init(weights_path, heatmap=False)
File "convnetskeras/convnets.py", line 274, in AlexNet
    dense_1 = Dense(4096, activation='relu', name='dense_1')(dense_1)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 590, in __call__
    self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 842, in build
    constraint=self.kernel_constraint)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 411, in add_weight
    weight = K.variable(initializer(shape),
File "/usr/local/lib/python2.7/dist-packages/keras/initializers.py", line 217, in __call__
    dtype=dtype, seed=self.seed)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 2257, in random_uniform
    return rng.uniform(shape, low=minval, high=maxval, dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/theano/sandbox/rng_mrg.py", line 862, in uniform
    size)
ValueError: ('The specified size contains a dimension with value <= 0', (-3840, 4096))

My keras.json file looks like below

{
    "epsilon": 1e-07,
    "floatx": "float32",
    "image_data_format": "channels_last",
    "backend": "theano"
}
erralves commented 6 years ago

@kalravibhor,

Using Theano as backend, verify if your inputs are declared in this way: inputs = Input(shape=(3,227,227))

kalravibhor commented 6 years ago

@erralves , I am having trouble just declaring the model and loading the pre-trained weights. Just wondering whether my 'image_data_format' should be 'channels_first' ?

I use the following code to load the pre-trained model as mentioned in the documentation. Using a Ubuntu 16.04 LTS with Python 2.7.12

from convnetskeras.convnets import preprocess_image_batch, convnet
model = convnet('alexnet',weights_path="alexnet_weights.h5", heatmap=False)
erralves commented 6 years ago

@kalravibhor, the weights of a pre-trained model are associated with each image layer. Loading inputs in the wrong layer order may cause this error.

If you have code which generated the pre-trained weights, i suggest you to observe how the inputs were declared. If you have only the weights (.h5), try some combination with the "image_data_format" and "backend" fields on keras.json and see if one works.

kalravibhor commented 6 years ago

@erralves , looks like setting 'image_data_format' to 'channels_first' works. I wanted to understand the issue before rectifying it, hence did not try this out. As you said, looks like the model weights are generated while keeping this parameter. Thanks for all your help.

gabrieldemarmiesse commented 6 years ago

If I may do a bit of publicity for my repo, I used this one as based and improved on it. It's up to date, works with keras 2 and Theano, tensorflow and CNTK. You can also use other CNN like ResNet to get heatmaps.

https://github.com/gabrieldemarmiesse/heatmaps

abdougrinzou commented 4 years ago

I downloaded convnets-keras from https://github.com/lunardog/convnets-keras using the following commands and now working in tensorflow background. git clone https://github.com/lunardog/convnets-keras cd convnets-keras sudo python setup.py install

please,how did you install convnet module ??

abdougrinzou commented 4 years ago

I downloaded convnets-keras from https://github.com/lunardog/convnets-keras using the following commands and now working in tensorflow background. git clone https://github.com/lunardog/convnets-keras cd convnets-keras sudo python setup.py install

i-e what to type exactly (and where to type : jupyter or anaconda prompt ) ??

Sourankana30 commented 4 years ago

@abdougrinzou go to convnets-keras path by cd convnets-keras then run python setup.py install in anaconda prompt if you are opening the jupyter notebook from anaconda prompt