seya
seya copied to clipboard
Spatial Transform Input: build() takes exactly 1 argument (2 given), Keras 1.0
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)
Checked keras1 branch and it works fine!
The keras1 branch doesn't handle the learning_phase
properly and so dropout layers cannot be used in the model (https://github.com/fchollet/keras/issues/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)
you mean if you have dropout on your locnet
it crashes?
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
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).
a friend of mine was having trouble trying to build models that were not call
ed 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
?
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.
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
Version of Theano that I'm using is 1.0.5
Kindly help me solve this issue. Thanks in advance!
@innovator1108 check if your version of keras is > 1.0, if so, use Seya's keras1
brach
@kmader @EderSantana I met the same problem if adding dropout in the locnet. Has any of you solve the problem?
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
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?
@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.