Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the size of deconv2d filters for a fixed data size?

I am trying to adjust this DCGAN code to be able to work with 2x80 data samples.

All generator layers are tf.nn.deconv2d other than h0, which is ReLu. Generator filter sizes per level are currently:

Generator: h0: s_h16 x s_w16: 1  x  5
Generator: h1: s_h8 x s_w8: 1  x  10
Generator: h2: s_h4 x s_w4: 1  x  20
Generator: h3: s_h2 x s_w2: 1  x  40
Generator: h4: s_h x s_w: 2  x  80

Because of the nature of my data I would like them to be initially generated as 2 x ..., i.e. for filters to be 2 x 5, 2 x 10, 2 x 20, 2 x 40, and 2 x 80. However when I just manually enter s_h16 = 2 * s_h16 and so on up to s_h2 = 2 * s_h2, I run into the following error:

ValueError: Shapes (64, 1, 40, 64) and (64, 2, 40, 64) are not compatible

So I know that the error happens at the level h3, but I can't quite track it down (64 here is the batch size). Any ideas how this can be fixed?


Edit: the edited DCGANs code is in this repository, after the DCGAN-tensorflow is set-up as in the instructions you'd have to place Data_npy folder into DCGAN-tensorflow/data folder.

Then running python main.py --dataset Data_npy --input_height=2 --output_height=2 --train would provide you with the error I get.

The full error traceback looks as follows:

Traceback (most recent call last):
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 560, in merge_with
    new_dims.append(dim.merge_with(other[i]))
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 135, in merge_with
    self.assert_is_compatible_with(other)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 108, in assert_is_compatible_with
    % (self, other))
ValueError: Dimensions 1 and 2 are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 97, in <module>
    tf.app.run()
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "main.py", line 80, in main
    dcgan.train(FLAGS)
  File "/home/marija/DCGAN-tensorflow/model.py", line 180, in train
    .minimize(self.g_loss, var_list=self.g_vars)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/training/optimizer.py", line 315, in minimize
    grad_loss=grad_loss)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/training/optimizer.py", line 386, in compute_gradients
    colocate_gradients_with_ops=colocate_gradients_with_ops)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/gradients_impl.py", line 580, in gradients
    in_grad.set_shape(t_in.get_shape())
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 413, in set_shape
    self._shape = self._shape.merge_with(shape)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 564, in merge_with
    (self, other))
ValueError: Shapes (64, 1, 40, 64) and (64, 2, 40, 64) are not compatible
like image 985
Massyanya Avatar asked Oct 30 '22 07:10

Massyanya


1 Answers

In your ops.py file

your problem come from the striding size in your deconv filter, modify the header for conv2d and deconv2d to:

def conv2d(input_, output_dim, 
       k_h=5, k_w=5, d_h=1, d_w=2, stddev=0.02,
       name="conv2d"):

def deconv2d(input_, output_shape,
       k_h=5, k_w=5, d_h=1, d_w=2, stddev=0.02,
       name="deconv2d", with_w=False):

Like this it started to train for me. I didn't check the output though.

The problem is considering the shape of your input, strinding on height by 2 (original value for d_h) will result by a (64, 1, 40, 64) shape during back propagation. (Because you only have 2 values)

Also you might think to change k_h=5 to k_h=2 as taking 5 element on height when you have only 2 doesn't make so much sense.

like image 87
rAyyy Avatar answered Nov 15 '22 06:11

rAyyy