Search code examples
tensorflowneural-networkdeep-learningconv-neural-networktensorflow-slim

slim.fully_connected: Using Tensor.shape to specify `num_outputs`


I would like to use tf.slim.fully_connected for something like this:

conv_out = conv2d(...)
_, h, w, c = conv_out.shape
flat = tf.reshape(conv_out, [-1, h*w*c])
fc_out = fully_connected(flat, h*w*c)

However when I do this I get an error:

ValueError: num_outputs should be int or long, got 49.

h*w*c is of type tensorflow.python.framework.tensor_shape.Dimension.

Is there a way to do this, without knowing whc before hand, and without having to start a session to determine them?


Solution

  • h*w*c is of type tensorflow.python.framework.tensor_shape.Dimension.

    Correct, but slim.fully_connected checks for isinstance(num_outputs, six.integer_types). It doesn't expect a Dimension instance.

    That's why you should manually convert h*w*c to int:

    fc_out = fully_connected(flat, int(h*w*c))