Comments (21)
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...
from convnets-keras.
@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
from convnets-keras.
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.
from convnets-keras.
@Anabik can you show the error message?
from convnets-keras.
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.
from convnets-keras.
@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]) == 2TypeError: object of type 'int' has no len()
from convnets-keras.
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
from convnets-keras.
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.
from convnets-keras.
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!
from convnets-keras.
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.
from convnets-keras.
@erralves The problem was with Input as you stated. My back-end was tensorflow, however, I used Theano input convention. Thank you. very much! 👍
from convnets-keras.
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?
from convnets-keras.
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"
}
from convnets-keras.
Using Theano as backend, verify if your inputs are declared in this way: inputs = Input(shape=(3,227,227))
from convnets-keras.
@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)
from convnets-keras.
@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.
from convnets-keras.
@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.
from convnets-keras.
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
from convnets-keras.
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 ??
from convnets-keras.
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 ) ??
from convnets-keras.
@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
from convnets-keras.
Related Issues (20)
- Heatmap has low resolution (43x73 in the dog example) HOT 1
- add license
- raise error when using process_image_batch (using sample code)
- Are you interested in doubling the heatmap resolution? (4x more pixels) HOT 2
- support more networks
- vgg networks dont't produce heatmaps HOT 1
- error while loading weights in case of heatmap=true
- Why don't you assign regularisation to the layers?
- I cannot reproduce your results. HOT 3
- What is geometric norm?
- Alexnet issue with Keras 2
- why can't I reproduce your result? HOT 3
- Load weights error in theano HOT 2
- Share information about training
- error: Load weight file (ValueError: You are trying to load a weight file containing 11 layers into a model with 7 layers.)
- Dimensions must be equal, HOT 2
- convnets.py in 'module' object is not callable HOT 1
- Unable to install h5py and convnets-keras HOT 2
- Unable to download weights for AlexNet HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from convnets-keras.