Calling Keras Convolutional Layer on TensorFlow Tensor Error.
('x_train shape:', (50000, 32, 32, 3))
# Basic info
self.batch_num = 50
self.img_row = 32
self.img_col = 32
self.img_channels = 3
self.nb_classes = 10
img = tf.placeholder(tf.float32, shape=(self.batch_num, self.img_col, self.img_row, self.img_channels))
labels = tf.placeholder(tf.float32, shape=(self.batch_num, self.nb_classes))
x = Convolution2D(16, 3, 3, border_mode='same')(img)
x = BatchNormalization(axis=3)(x)
x = Activation('relu')(x)
x = AveragePooling2D(pool_size=(8, 8), strides=None, border_mode='valid')(x)
x = Flatten()(x)
preds = Dense(self.nb_classes, activation='softmax')(x)
I tried using the following and it still gives the same error. It seems we can't call a Convolution2D layer on an input? @fchollet
If I do not use a placeholder and use inputs = Input(shape=(self.img_rows, self.img_cols, self.img_channels), it works.
I noticed a similar error persisting with https://github.com/fchollet/keras/issues/3450
img = K.placeholder(ndim=4)
I'm having the following error:
Traceback (most recent call last):
File "cnn.py", line 176, in <module>
a.step()
File "cnn.py.py", line 156, in step
preds = Dense(self.nb_classes, activation='softmax')(x)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 487, in __call__
self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 695, in build
name='{}_W'.format(self.name))
File "/usr/local/lib/python2.7/dist-packages/keras/initializations.py", line 58, in glorot_uniform
s = np.sqrt(6. / (fan_in + fan_out))
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
I'm using this with TensorFlow due to the flexibility I need. But I broke it down to a simple example and I can't figure out why I'm getting an error for such a simple problem.
So I managed to solve this with 2 steps:
K.set_learning_phase(0) before everything.Flatten, change to tf.reshape(x, [-1, np.prod(x.get_shape()[1:].as_list())]). I gathered this fix from studying a few issues:
https://github.com/fchollet/keras/pull/3253
https://github.com/fchollet/keras/issues/3450
https://github.com/fchollet/keras/pull/3253
I personally think this deserves to be updated in Keras :)
Is it finally updated? What is the current status?
also experiencing this issue. where i have a conv layer => flatten => dense layer.
gives error:
File "/Users/mjdietzx/Documents/GitHub/keras/keras/initializations.py", line 65, in glorot_uniform
s = np.sqrt(6. / (fan_in + fan_out))
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
using your workaround fixes
I am also encountering the same problem
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.
Most helpful comment
So I managed to solve this with 2 steps:
K.set_learning_phase(0)before everything.Flatten, change totf.reshape(x, [-1, np.prod(x.get_shape()[1:].as_list())]).I gathered this fix from studying a few issues:
https://github.com/fchollet/keras/pull/3253
https://github.com/fchollet/keras/issues/3450
https://github.com/fchollet/keras/pull/3253
I personally think this deserves to be updated in Keras :)