我是Python和Tensorflow的新手,现在我正在尝试学习DC GAN。我已经实现了这个版本的鉴别器:
def discriminator(images, reuse=False, alpha=0.2):
with tf.variable_scope('discriminator', reuse=reuse):
x1 = tf.layers.conv2d(images, 128, 5, strides=2, padding='same')
pool1 = tf.layers.max_pooling2d(inputs=x1, pool_size=[2, 2], strides=2)
relu1 = tf.maximum(alpha * pool1, pool1)
x2 = tf.layers.conv2d(relu1, 256, 5, strides=2, padding='same')
pool2 = tf.layers.max_pooling2d(inputs=x2, pool_size=[2, 2], strides=2)
bn2 = tf.layers.batch_normalization(pool2, training=True)
relu2 = tf.maximum(alpha * bn2, bn2)
x3 = tf.layers.conv2d(relu2, 512, 5, strides=2, padding='same')
pool3 = tf.layers.max_pooling2d(inputs=x3, pool_size=[2, 2], strides=2)
bn3 = tf.layers.batch_normalization(pool3, training=True)
relu3 = tf.maximum(alpha * bn3, bn3)
x4 = tf.layers.conv2d(relu2, 512, 5, strides=2, padding='same')
pool4 = tf.layers.max_pooling2d(inputs=x4, pool_size=[2, 2], strides=2)
bn4 = tf.layers.batch_normalization(pool4, training=True)
relu4 = tf.maximum(alpha * bn4, bn4)
flat = tf.reshape(relu3, (-1, 4*4*256))
logits = tf.layers.dense(flat, 1)
out = tf.sigmoid(logits)
return out, logits
但是当我尝试运行它时,我收到了这个错误:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/home/oalarcon/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
669 node_def_str, input_shapes, input_tensors, input_tensors_as_shapes,
--> 670 status)
671 except errors.InvalidArgumentError as err:
/home/oalarcon/anaconda3/envs/ml/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
88 try:
---> 89 next(self.gen)
90 except StopIteration:
/home/oalarcon/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
468 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 469 pywrap_tensorflow.TF_GetCode(status))
470 finally:
InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_3/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,512].
添加所有池化层后出现此错误。
关于如何解决它的任何想法?
答案 0 :(得分:1)
Cov2d_3层(x3)的激活/输出大小是(α,1,1,512)。 pool3无法处理此1x1输出。所以,错误提升了。
你应该输入更大的图像。或者相应地调整conv和pool层的内核。
使用它来估算conv和pool图层的输出大小:
If padding == "SAME": output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])
If padding == "VALID": output_spatial_shape[i] = ceil((input_spatial_shape[i] - (spatial_filter_shape[i]-1) * dilation_rate[i]) / strides[i]).
确保输出尺寸足够大,最终输出尺寸是所需尺寸。
我希望这有帮助!