Keras问题下的反卷积
Deconvolution under Keras issues
我正在尝试将 keras 的 Deconvolution2D 与 Tensorflow 后端一起使用。
但是我遇到了一些问题。
首先,在 output_shape 中,如果我为 batch_size 传递 None,我会收到此错误:
TypeError: Expected binary or unicode string, got None
如果我按我使用的批量大小更改 None,则会出现错误。.:
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)]]
这是我使用的模型:
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()
这是 Keras 以前版本中反卷积的一个烦恼,总是必须给出固定的批量大小并手动计算 output_shape。这也意味着您的数据集大小必须被 'batch_size' 整除,否则会在最后(较小的)批次上引发错误。
幸运的是,这个问题在 Keras 2.0 中得到了修复。 Deconvolution2D 已被 Conv2DTranspose 取代,您甚至不必再提供 output_shape 作为参数:
model.add(Conv2DTranspose(filters=256, kernel_size=(h,h), strides=(2,2), padding='same'))
我正在尝试将 keras 的 Deconvolution2D 与 Tensorflow 后端一起使用。
但是我遇到了一些问题。 首先,在 output_shape 中,如果我为 batch_size 传递 None,我会收到此错误:
TypeError: Expected binary or unicode string, got None
如果我按我使用的批量大小更改 None,则会出现错误。.:
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)]]
这是我使用的模型:
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()
这是 Keras 以前版本中反卷积的一个烦恼,总是必须给出固定的批量大小并手动计算 output_shape。这也意味着您的数据集大小必须被 'batch_size' 整除,否则会在最后(较小的)批次上引发错误。
幸运的是,这个问题在 Keras 2.0 中得到了修复。 Deconvolution2D 已被 Conv2DTranspose 取代,您甚至不必再提供 output_shape 作为参数:
model.add(Conv2DTranspose(filters=256, kernel_size=(h,h), strides=(2,2), padding='same'))