Reputation: 2207
I'm trying to use the Deconvolution2D of keras with Tensorflow backend.
But I got some issues. First, in the output_shape, if I pass None for the batch_size, I get this error :
TypeError: Expected binary or unicode string, got None
And if I change None by the batch size I use, here is the error.. :
InvalidArgumentError (see above for traceback): Conv2DCustomBackpropInput: input and out_backprop must have the same batch size
[[Node: conv2d_transpose = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/cpu:0"](conv2d_transpose/output_shape, transpose, Reshape_4)]]
Here is the model I use :
model = Sequential()
reg = lambda: l1l2(l1=1e-7, l2=1e-7)
h = 5
model.add(Dense(input_dim=100, output_dim=nch * 4 * 4, W_regularizer=reg()))
model.add(BatchNormalization(mode=0))
model.add(Reshape((4, 4, nch)))
model.add(Deconvolution2D(256, h,h, output_shape=(128,8,8,256 ), subsample=(2,2), border_mode='same'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(Deconvolution2D(256, h,h, output_shape=(128,16,16,256 ), subsample=(2,2), border_mode='same'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(Deconvolution2D(64, h,h, output_shape=(128,32,32,64), subsample=(2,2), border_mode='same'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg()))
model.add(Activation('sigmoid'))
model.summary()
Upvotes: 3
Views: 722
Reputation: 807
This was an annoyance with deconvolution in previous versions of Keras, always having to give a fixed batch size and manually compute output_shape. This also meant that your data set size had to be divisible by 'batch_size' or an error would be raised on the last (smaller) batch.
Fortunately this was fixed in Keras 2.0. Deconvolution2D has been replaced by Conv2DTranspose and you don't even have to give output_shape as an argument any more:
model.add(Conv2DTranspose(filters=256, kernel_size=(h,h), strides=(2,2), padding='same'))
Upvotes: 2