1

I'm new in Tensorflow. After convolution the shape of my layer is shape=(5, 5, 5, 5), dtype=float32 but when I'm applying deconvolution, getting shape like shape=(?, 25, 25, 640), dtype=float32. That means batch size is not showing properly (? sign) after deconvolution. For deconvolution, I used this Deconvolution function.

Error ValueError: Shape of a new variable (local1/weights) must be fully defined, but instead was (?, 1000).

I already tried example1but didn't work well

Daniel Larsson
  • 527
  • 2
  • 6
  • 23
user1410665
  • 719
  • 7
  • 23

3 Answers3

1

the difference is that the example you send is a tensor getting the wrong data fed to it. Your problem is that the weights of a deconvolutional filter is not fully defined. The weights are not dependent on the batch size, and need to be of fixed size, hence the error. I know you understood the error, just want to make clear that the problem you have and the example has, is quite different.

I recommend to use this function instead:

 tf.nn.conv2d_transpose()

it is defined like you'd do with a normal convolutional layer. It's default in TensorFlow, and I wonder why you didn't use it to start with?

T. Kelher
  • 1,188
  • 10
  • 9
0

From the description of used Deconvolution function

  #Now output.get_shape() is equal (?,?,?,?) which can become a problem in the 
  #next layers. This can be repaired by reshaping the tensor to its shape:
  output = tf.reshape(output, output_shape)
  #now the shape is back to (?, H, W, C) or (?, C, H, W)

Batch size shouldn't be displayed, cause it designed to be unknown. It is made so to preserve an ability to process batches with different size (first dimension size). So that you can run model on batches of different size, for example, train on 5 and predict 20 images in one run.

And fully agree with T. Kelher:

I recommend to use this function instead:

tf.nn.conv2d_transpose()
Taras Khalymon
  • 614
  • 4
  • 11
  • Do you have any idea how can I get a batch size from the previous example? – user1410665 Oct 29 '18 at 20:26
  • Did you tried `result.get_shape()`? Try reading this https://stackoverflow.com/questions/37096225/how-to-understand-static-shape-and-dynamic-shape-in-tensorflow – Taras Khalymon Oct 29 '18 at 20:35
  • Yes, I tried but It displayed the same result. When I'm giving input from a placeholder then I'm getting batch size but in my model, batch size is not showing. Even I tried to use tf.nn.conv2d_transpose() but it requires output size and filters that's why I am using this function. Do you have any link to calculate output size for deconvolution/transpose layer? – user1410665 Oct 29 '18 at 20:47
  • The output shape cannot be displayed, because the input shape is unknown. It is determined from the input size. (In common case, because in you case you are trying to freeze batch size to 5, but used deconvolution function is not designed for this). in regular tensorflow functions output shape is fully determined from previous layer (in this case - input), so if you know all dimension sizes - you know it after layer, and if your batch size, or image size is dynamic (None in a placeholder) - you'll receive dynamic shape on that positions too. – Taras Khalymon Oct 29 '18 at 21:16
  • The issue has been solved and the previous transpose/deconvolution code is running nicely. Just we have to make some minor changes. We have to define batch size in the output shape. – user1410665 Oct 30 '18 at 20:08
  • Can you explain, why do you need it? Generally, if you freeze the batch size, you'll force model to work only with static batch size, but what if you want to calculate less or more than 5 images? That's why you need an unknown batch size. If you want to know, the model's output shape - it's depends on input: you feed `x` images - you get `x` images on output. If you need batch size for calculations, you can get it inside the model (only if you feed some data) using [`tf.shape()`](https://www.tensorflow.org/api_docs/python/tf/shape). – Taras Khalymon Oct 30 '18 at 20:22
  • Yes, I understood your point. In my transpose convolution layer, I was getting output like (?, x, y, z). Hence, in the reshape layer (tf.reshape()) I was getting above error. In my model batch size is not static rather in above transpose convolution function batch size was not defined properly. – user1410665 Oct 30 '18 at 20:37
  • what do you mean by "not defined properly"? By the way (just noticed in your question, that you are new to tf), are you sure, you want a deconvolution with strides 5? maybe you miss-understand it's arguments. You'll get an image of original size * 5 – Taras Khalymon Oct 30 '18 at 21:00
  • The shape is in NHWC format and the stride is not 5. Now my architecture is perfect. – user1410665 Oct 30 '18 at 21:30
0

The issue has been solved and the previous transpose/deconvolution code is running nicely. Just we have to make some minor changes. We have to define batch size in the output shape.

user1410665
  • 719
  • 7
  • 23