user2849678
user2849678

Reputation: 633

A reusable Tensorflow convolutional Network

I want to reuse code from the Tensorflow "MNIST for Pros" CNN example. My images are 388px X 191px, with only 2 output classes. The original code can be found here. I tried to reuse this code by changing the input & output layers ONLY, as shown below:

input layer

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

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

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

output layer

W_fc2 = weight_variable([1024, 2])

b_fc2 = bias_variable([2])

Running the modified code gives a vague stacktrace:

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()

Upvotes: 8

Views: 3307

Answers (1)

dga
dga

Reputation: 21927

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)]]

But the way you executed it prevents you from seeing the actual line causing the problem. Save it to a file and python <file> it.

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

But the answer is that you haven't changed the size of your convolutional and pooling layers, so when you used to run 28x28 images through, they eventually shrunk down to a 7x7x(convolutional_depth) layer. Now you're running huge images through, so after the first convolutional layer and the 2x2 maxpool, you've got a VERY BIG thing you're trying to feed in, but you're reshaping to:

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])

The output of h_pool2 is much larger with your larger images. You need to shrink them down more - likely with more convolutional and maxpooling layers. You could also try increasing the size of W_fc1 to match the input size that's getting there. It's running through two 2x2 maxpools - each shrinks the size by 2 in the x and y dimensions. 28x28x1 --> 14x14x32 --> 7x7x64. So YOUR images are going from 388 x 191 --> 194 x 95 --> 97 x 47

As a warning, a fully connected layer with 97*47 = 4559 inputs is going to be glacially slow.

Upvotes: 7

Related Questions