-1

I'm trying to found a way to convolution transpose with images of variable size. I used tf.nn.conv2d_transpose api, but I failed.

import tensorflow as tf

def conv2d_transpose(inputs, filters_shape, strides, name, padding="SAME", activation=None):
  filters = get_conv_filters(filters_shape, name)

  inputs_shape = inputs.get_shape().as_list()
  output_shape = tf.stack(calc_output_shape(inputs_shape, filters_shape, strides, padding)) #tf.pack renamed tf.stack
  strides = [1,*strides,1]

  conv_transpose = tf.nn.conv2d_transpose(inputs, filters, output_shape=output_shape,
                                          strides=strides, padding=padding, name=name+"transpose")

  if activation != None:
    conv_transpose = activation(conv_transpose)

  return conv_transpose

def get_conv_filters(filters_size, name):
  conv_weights = tf.Variable(tf.truncated_normal(filters_size), name=name + "weights")
  return conv_weights

def calc_output_shape(inputs_shape, filters_shape, strides, padding): # For conv_transpose
  batch_size, inputs_height, inputs_width, n_channel = inputs_shape
  filters_height, filters_width, before_n_channel, after_n_channel = filters_shape
  strides_height, strides_width = strides

  if padding =="SAME":
    output_height = inputs_height*strides_height
    output_width = inputs_width*strides_width

  else: # padding="VALID"
    output_height = (inputs_height-1)*strides_height+filters_height
    output_width = (inputs_width-1)*strides_width+filters_width

  return [batch_size, output_height, output_width, after_n_channel]

main

input_images = tf.placeholder(tf.float32, [None, None, None, 3])
transpose_layer = conv2d_transpose(input_images, filters_shape=[3,3,3,3], strides=[2,2], name="conv_3_transpose", padding="SAME", activation=tf.nn.relu)

then, I get the following error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-0a594abc4f59> in <module>()
     36 
     37 input_images = tf.placeholder(tf.float32, [None, None, None, 3])
---> 38 transpose_layer = conv2d_transpose(input_images, filters_shape=[3,3,3,3], strides=[2,2], name="conv_3_transpose", padding="SAME", activation=tf.nn.relu)
     39 

<ipython-input-11-0a594abc4f59> in conv2d_transpose(inputs, filters_shape, strides, name, padding, activation)
      5 
      6   inputs_shape = inputs.get_shape().as_list()
----> 7   output_shape = tf.stack(calc_output_shape(inputs_shape, filters_shape, strides, padding)) #tf.pack renamed tf.stack
      8   strides = [1,*strides,1]
      9 

<ipython-input-11-0a594abc4f59> in calc_output_shape(inputs_shape, filters_shape, strides, padding)
     26 
     27   if padding =="SAME":
---> 28     output_height = inputs_height*strides_height
     29     output_width = inputs_width*strides_width
     30 

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

I think the reason for this error is that the input_shape is not fixed. So, error occurs in calculating the output_shape. How should I overcome this problem?

KiHyun Nam
  • 77
  • 6

1 Answers1

2

Use dynamic shapes, you can find the details here. Your input_shape should be:

inputs_shape = tf.shape(inputs)
...
batch_size, inputs_height, inputs_width, n_channel = inputs_shape[0],inputs_shape[1],inputs_shape[2],inputs_shape[3]  
Vijay Mariappan
  • 16,921
  • 3
  • 40
  • 59