Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am stuck by this problem in customlayers.py, please help #2

Open
Rickgewu opened this issue Jun 26, 2017 · 11 comments
Open

I am stuck by this problem in customlayers.py, please help #2

Rickgewu opened this issue Jun 26, 2017 · 11 comments
Assignees

Comments

@Rickgewu
Copy link

40 alexnet = get_alexnet(input_size,nb_classes,mean_flag)
41
42 print (alexnet.summary())

E:\TEMP\R\KERAS\AlexNet-Experiments-Keras-master\AlexNet-Experiments-Keras-master\Code\alexnet_base.py in get_alexnet(input_shape, nb_classes, mean_flag)
36
37 conv_2 = MaxPooling2D((3, 3), strides=(2,2))(conv_1)
---> 38 conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)
39 conv_2 = ZeroPadding2D((2,2))(conv_2)
40 conv_2 = merge([

d:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py in call(self, inputs, **kwargs)
583
584 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 585 output = self.call(inputs, **kwargs)
586 output_mask = self.compute_mask(inputs, previous_mask)
587

d:\ProgramData\Anaconda3\lib\site-packages\keras\layers\core.py in call(self, inputs, mask)
657 if 'mask' in arg_spec.args:
658 arguments['mask'] = mask
--> 659 return self.function(inputs, **arguments)
660
661 def compute_mask(self, inputs, mask=None):

E:\TEMP\R\KERAS\AlexNet-Experiments-Keras-master\AlexNet-Experiments-Keras-master\convnets-keras\convnetskeras\customlayers.py in f(X)
16 square = K.square(X)
17 extra_channels = K.spatial_2d_padding(K.permute_dimensions(square,(0,2,3,1))
---> 18 , (0,half))
19 extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
20 scale = k

d:\ProgramData\Anaconda3\lib\site-packages\keras\backend\theano_backend.py in spatial_2d_padding(x, padding, data_format)
995 """
996 assert len(padding) == 2
--> 997 assert len(padding[0]) == 2
998 assert len(padding[1]) == 2
999 top_pad, bottom_pad = padding[0]

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

@duggalrahul
Copy link
Owner

@Rickgewu,
Which versions of keras and theano have you configured?

@Rickgewu
Copy link
Author

keras: 2.0.4
theano: 0.9.0

@duggalrahul
Copy link
Owner

duggalrahul commented Jun 26, 2017

My bad @Rickgewu .

This project was written using Keras 1.2. Try downgrading to version 1.2 via the command pip install keras==1.2 .

After this, modify the ~/.keras/keras.json file as follows

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

Somehow forgot to mention the keras version in the README. Let me know if this solves your problem.

@Rickgewu
Copy link
Author

I have downgraded keras to 1.2, but still got the below error:

40 alexnet = get_alexnet(input_size,nb_classes,mean_flag)
41
42 print (alexnet.summary())

E:\TEMP\R\KERAS\AlexNet-Experiments-Keras-master\AlexNet-Experiments-Keras-master\Code\alexnet_base.pyc in get_alexnet(input_shape, nb_classes, mean_flag)
63
64 dense_1 = Flatten(name="flatten")(dense_1)
---> 65 dense_1 = Dense(4096, activation='relu',name='dense_1',init='he_normal')(dense_1)
66 dense_2 = Dropout(0.5)(dense_1)
67 dense_2 = Dense(4096, activation='relu',name='dense_2',init='he_normal')(dense_2)

d:\ProgramData\Anaconda2\lib\site-packages\keras\engine\topology.pyc in call(self, x, mask)
541 'layer.build(batch_input_shape)')
542 if len(input_shapes) == 1:
--> 543 self.build(input_shapes[0])
544 else:
545 self.build(input_shapes)

d:\ProgramData\Anaconda2\lib\site-packages\keras\layers\core.pyc in build(self, input_shape)
750 name='{}_W'.format(self.name),
751 regularizer=self.W_regularizer,
--> 752 constraint=self.W_constraint)
753 if self.bias:
754 self.b = self.add_weight((self.output_dim,),

d:\ProgramData\Anaconda2\lib\site-packages\keras\engine\topology.pyc in add_weight(self, shape, initializer, name, trainable, regularizer, constraint)
413 '''
414 initializer = initializations.get(initializer)
--> 415 weight = initializer(shape, name=name)
416 if regularizer is not None:
417 self.add_loss(regularizer(weight))

d:\ProgramData\Anaconda2\lib\site-packages\keras\initializations.pyc in he_normal(shape, name, dim_ordering)
66 fan_in, fan_out = get_fans(shape, dim_ordering=dim_ordering)
67 s = np.sqrt(2. / fan_in)
---> 68 return normal(shape, s, name=name)
69
70
d:\ProgramData\Anaconda2\lib\site-packages\keras\initializations.pyc in normal(shape, scale, name)
35
36 def normal(shape, scale=0.05, name=None):
---> 37 return K.random_normal_variable(shape, 0.0, scale, name=name)
38
39

d:\ProgramData\Anaconda2\lib\site-packages\keras\backend\theano_backend.pyc in random_normal_variable(shape, mean, scale, dtype, name)
181
182 def random_normal_variable(shape, mean, scale, dtype=None, name=None):
--> 183 return variable(np.random.normal(loc=0.0, scale=scale, size=shape),
184 dtype=dtype, name=name)
185
mtrand.pyx in mtrand.RandomState.normal (numpy\random\mtrand\mtrand.c:20639)()

ValueError: scale < 0

@duggalrahul
Copy link
Owner

duggalrahul commented Jun 27, 2017

@Rickgewu . I think the previous problem is solved.

To resolve this issue, please try as follows.

  1. Does the code work if you remove the 'init' parameter from each layer?
  2. Are you sure you updated the keras.json file correctly? Could you post the contents of that file here?

@Rickgewu
Copy link
Author

It works after appending this entry to the json "image_dim_ordering": "th",
now I need to get my GPU working with python 27, thanks

@duggalrahul duggalrahul self-assigned this Jun 28, 2017
@duggalrahul
Copy link
Owner

@Rickgewu ,

Glad your issue is resolved.

@duggalrahul duggalrahul reopened this Jun 28, 2017
@DNXie
Copy link

DNXie commented Dec 10, 2017

I've encountered the same problem.
However, is there any way to solve the problem without install keras version1.2?
Can I use the code with keras 2 ?
@duggalrahul

@duggalrahul
Copy link
Owner

@DNXie The current code would need some revision to make it work with Keras 2 since some keras API's have changed. If you need to do fast prototyping, I'd suggest installing keras 1.2 on a fresh virtual environment.

@geetanjalisharma
Copy link

is it possible to run it into miniconda environment.

@geetanjalisharma
Copy link


TypeError Traceback (most recent call last)
in ()
----> 1 alexnet = get_alexnet(input_size,nb_classes,mean_flag)

~/Desktop/alx/Code/alexnet_base.py in get_alexnet(input_shape, nb_classes, mean_flag)
36
37 conv_2 = MaxPooling2D((3, 3), strides=(2,2))(conv_1)
---> 38 conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)
39 conv_2 = ZeroPadding2D((2,2))(conv_2)
40 conv_2 = merge([

~/miniconda3/envs/intel_python/lib/python3.5/site-packages/keras/engine/topology.py in call(self, inputs, **kwargs)
615
616 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 617 output = self.call(inputs, **kwargs)
618 output_mask = self.compute_mask(inputs, previous_mask)
619

~/miniconda3/envs/intel_python/lib/python3.5/site-packages/keras/layers/core.py in call(self, inputs, mask)
661 if has_arg(self.function, 'mask'):
662 arguments['mask'] = mask
--> 663 return self.function(inputs, **arguments)
664
665 def compute_mask(self, inputs, mask=None):

~/Desktop/alx/convnets-keras/convnetskeras/customlayers.py in f(X)
17 square = K.square(X)
18 extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1))
---> 19 , (0,half))
20 extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
21 scale = k

~/miniconda3/envs/intel_python/lib/python3.5/site-packages/keras/backend/theano_backend.py in spatial_2d_padding(x, padding, data_format)
1066 """
1067 assert len(padding) == 2
-> 1068 assert len(padding[0]) == 2
1069 assert len(padding[1]) == 2
1070 top_pad, bottom_pad = padding[0]

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

i have done all these thing which have already mentioned in the above comments...
plz hlp me how can i fix it and i have used keras 2..
plz hlp me...........

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants