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

Spatial Transform Input: build() takes exactly 1 argument (2 given), Keras 1.0 #36

Open
kmader opened this issue Jun 28, 2016 · 13 comments

Comments

@kmader
Copy link

kmader commented Jun 28, 2016

The easiest code example is below but the ipython notebook does not work either with keras 1.0.3

import numpy as np
from keras.layers import Input
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from seya.layers.attention import SpatialTransformer
def build_locnet(input_shape):
    locnet = Sequential()
    locnet.add(MaxPooling2D(pool_size=(2,2), input_shape=input_shape))
    locnet.add(Flatten())
    locnet.add(Dense(50))
    locnet.add(Activation('relu'))
    # initial weights for spatial layer
    b = np.zeros((2, 3), dtype='float32')
    b[0, 0] = 1
    b[1, 1] = 1
    W = np.zeros((50, 6), dtype='float32')
    weights = [W, b.flatten()]
    locnet.add(Dense(6, weights=weights))
    return locnet
test_shape = (1,64,64)
simple_locnet = build_locnet(test_shape)
st_layer = SpatialTransformer(localization_net=simple_locnet, downsample_factor = 3)(Input(shape=test_shape))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-b747048206f2> in <module>()
----> 1 st_layer = SpatialTransformer(localization_net=simple_locnet, downsample_factor = 3)(Input(shape=input_shape))

.../anaconda/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
    456                                     '`layer.build(batch_input_shape)`')
    457             if len(input_shapes) == 1:
--> 458                 self.build(input_shapes[0])
    459             else:
    460                 self.build(input_shapes)

TypeError: build() takes exactly 1 argument (2 given)
@kmader
Copy link
Author

kmader commented Jun 28, 2016

Checked keras1 branch and it works fine!

@kmader kmader closed this as completed Jun 28, 2016
@kmader
Copy link
Author

kmader commented Jun 28, 2016

The keras1 branch doesn't handle the learning_phase properly and so dropout layers cannot be used in the model (keras-team/keras#2430)

Error message (on model.fit or model.predict)

("An input of the graph, used to compute DimShuffle{x,x}(keras_learning_phase), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.", keras_learning_phase)

@kmader kmader reopened this Jun 28, 2016
@EderSantana
Copy link
Owner

you mean if you have dropout on your locnet it crashes?

@kmader
Copy link
Author

kmader commented Jul 1, 2016

yea, it won't build the layer. You can make it but as soon as you add it to a model (sequential) or call it with another layer it throws the message

@SulemanKazi
Copy link
Contributor

SulemanKazi commented Jul 1, 2016

The same issue occurs if you add weight regularization to a layer in the locnet. (I'm guessing the Input layer in the locnet is what is causing this).

@EderSantana
Copy link
Owner

a friend of mine was having trouble trying to build models that were not called before hand. We build the locnet here:
https://github.com/EderSantana/seya/blob/keras1/seya/layers/attention.py#L43

Do you guys think that if we compile the locnet before the passing it to the SpatialTransfomer we can solve the problem?

@rburt were you having similar problems the last time you played with SpatialTransfomer?

@rburt
Copy link

rburt commented Jul 6, 2016

Yeah, I've had this problem too. When the locnet has the learning_phase parameter but the main model does not, the parameter isn't passed when during the build.

I was able to get around this error by adding a dropout layer in my model like below:

locnet = Sequential()
locnet.add(MaxPooling2D(pool_size=(2,2), input_shape=input_shape))
locnet.add(Convolution2D(20, 5, 5))
locnet.add(MaxPooling2D(pool_size=(2,2)))
locnet.add(Convolution2D(20, 5, 5))

locnet.add(Flatten())
locnet.add(Dense(50))
locnet.add(Dropout(.5))
locnet.add(Activation('relu'))
locnet.add(Dense(6, weights=weights))

model = Sequential()
model.add(SpatialTransformer(localization_net=locnet,
                             downsample_factor=3, input_shape=input_shape))
model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape= input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256))
model.add(Dropout(.01))  # Added Dropout here to fix error
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Obviously it isn't the most elegant solution but it works as a quick fix for me.

@innovator1108
Copy link

Hello,

I'm also trying to use the spatial transformer code available on the following link (https://github.com/EderSantana/seya/blob/master/examples/Spatial%20Transformer%20Networks.ipynb)

On executing the code, I'm getting following error in the line where we add SpatialTransformer layer :

Traceback (most recent call last):
File "STN.py", line 74, in
model.add(SpatialTransformer(localization_net=locnet, downsample_factor=3, input_shape=input_shape))
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 114, in add
layer.create_input_layer(batch_input_shape, input_dtype)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 341, in create_input_layer
self(x)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 458, in call
self.build(input_shapes[0])
TypeError: build() takes exactly 1 argument (2 given)

Version of Theano that I'm using is 1.0.5

Kindly help me solve this issue.
Thanks in advance!

@EderSantana
Copy link
Owner

@innovator1108 check if your version of keras is > 1.0, if so, use Seya's keras1 brach

@MiaomiaoLiu
Copy link

@kmader @EderSantana I met the same problem if adding dropout in the locnet. Has any of you solve the problem?

@jaggzh
Copy link

jaggzh commented Dec 17, 2016

Unfortunately, it seems the keras-1 branch does not support many of the features I'm using (or will be using). Deconvolution2D doesn't seem to be implemented, and ModelCheckpoint doesn't support save_weights_only (although I could get around that one).

I wouldn't mind using a Dropout(), as I was planning on doing that anyway; however, back at the main Keras branch, I still do get the error, even with Dropout. Also, the error happens at the addition of the SpatialTransformer, before Dropout. How does the Dropout resolve the problem for others?

(I'm really not advanced enough in this to move the Deconv2d and ModelCheckpoint features into the Keras-1 branch. Nor fix the build() argument errors in the master branch. :)

242 locnet = Sequential()
243 locnet.add(MaxPooling2D(pool_size=(2,2), input_shape=input_shape))
244 locnet.add(Convolution2D(20, 5, 5))
245 locnet.add(MaxPooling2D(pool_size=(2,2)))
246 locnet.add(Convolution2D(20, 5, 5))
247 locnet.add(MaxPooling2D(pool_size=(2,2)))
248 locnet.add(Convolution2D(20, 5, 5))
249
250 locnet.add(Flatten())
251 locnet.add(Dense(50))
252 locnet.add(Dropout(.5))
253 locnet.add(Activation('relu'))
254 locnet.add(Dense(6, weights=weights))
255
256 model = Sequential()
257 model.add(SpatialTransformer(localization_net=locnet, downsample_factor=1, input_sha pe=input_shape))
258 model.add(Flatten())
259 model.add(Dense(dim*dim))
260 model.add(Dropout(.01))
261 model.add(Activation('relu'))

File "./keras_stn_imgtransform.py", line 834, in
model = create_nn_stn()
File "./keras_stn_imgtransform.py", line 257, in create_nn_stn
model.add(SpatialTransformer(localization_net=locnet, downsample_factor=1, input_shape=input_shape))
File "/home/jaggz/.local/lib/python2.7/site-packages/keras/models.py", line 294, in add
layer.create_input_layer(batch_input_shape, input_dtype)
File "/home/jaggz/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 398, in create_input_layer
self(x)
File "/home/jaggz/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 543, in call
self.build(input_shapes[0])
TypeError: build() takes exactly 1 argument (2 given)

@rammadhav987
Copy link

hey , @EderSantana what do you exactly mean by " use Seya's keras1 brach" if keras version is>1 .Since I am getting same error and I want to know what change should I make in the code https://github.com/EderSantana/seya/blob/master/examples/Spatial%20Transformer%20Networks.ipynb) or should i clone seya once again to my anaconda can you make it clear please?

@iamsiva11
Copy link

@rammadhav987 @EderSantana meant cloning the keras1 branch, instead of the default master branch which is causing the error. You can do that with
git clone -b keras1 https://github.com/EderSantana/seya.git

I did the same, and it works fine now.

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

9 participants