可重复使用的Tensorflow卷积网络

时间:2015-11-24 09:04:16

标签: python neural-network convolution tensorflow conv-neural-network

我想重用Tensorflow "MNIST for Pros" CNN example中的代码。 我的图像是388px X 191px,只有2个输出类。原始代码可以是found here。 我尝试通过更改 输入和放大来重用此代码。输出图层 ,如下所示:

输入图层

x = tf.placeholder("float", shape=[None, 74108])

y_ = tf.placeholder("float", shape=[None, 2])

x_image = tf.reshape(x, [-1,388,191,1])

输出图层

W_fc2 = weight_variable([1024, 2])

b_fc2 = bias_variable([2])

运行修改后的代码会产生模糊的堆栈跟踪:

W tensorflow/core/common_runtime/executor.cc:1027] 0x2136510 Compute status: Invalid argument: Input has 14005248 values, which isn't divisible by 3136
     [[Node: Reshape_4 = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_5, Reshape_4/shape)]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1267, in run
    _run_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2763, in _run_using_default_session
    session.run(operation, feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 345, in run
    results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 419, in _do_run
    e.code)
tensorflow.python.framework.errors.InvalidArgumentError: Input has 14005248 values, which isn't divisible by 3136
     [[Node: Reshape_4 = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_5, Reshape_4/shape)]]
Caused by op u'Reshape_4', defined at:
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 554, in reshape
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1710, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 988, in __init__
    self._traceback = _extract_stack()

1 个答案:

答案 0 :(得分:7)

tensorflow.python.framework.errors.InvalidArgumentError: Input has 14005248 values, which isn't divisible by 3136
 [[Node: Reshape_4 = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_5, Reshape_4/shape)]]

但是你执行它的方式会阻止你看到导致问题的实际行。将它保存到文件和python <file>

  File "<stdin>", line 1, in <module>

但答案是你还没有改变你的卷积和汇集层的大小,所以当你以前运行28x28图像时,它们最终会缩小到7x7x(convolutional_depth)层。现在你正在运行巨大的图像,所以在第一个卷积层和2x2 maxpool之后,你有一个非常大的东西,你正试图进入,但你正在重塑为:

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

对于较大的图像,h_pool2的输出要大得多。你需要将它们缩小得更多 - 可能有更多的卷积和最大化层。您还可以尝试增加W_fc1的大小以匹配到达那里的输入大小。它运行两个2x2 maxpools - 每个在x和y维度上缩小2。 28x28x1 - &gt; 14x14x32 - &gt; 7x7x64。所以你的图像来自388 x 191 - &gt; 194 x 95 - &gt; 97 x 47

作为警告,具有97 * 47 = 4559输入的完全连接层将变得非常缓慢。