Clarification needed for GAN C3W2A_Assignment UNQ_C3 ExpandingBlock

Hi experts,

Need some clarification regarding the GAN C3W2A_Assignment UNQ_C3 ExpandingBlock.

Getting error for torch.cat([x, orginal_x], axis=1) in test_expanding_block():

test_samples: 100, test_channels: 10, test_size: 50
test_block: ExpandingBlock(
  (upsample): Upsample(scale_factor=2.0, mode=bilinear)
  (conv1): Conv2d(10, 5, kernel_size=(2, 2), stride=(1, 1))
  (conv2): Conv2d(10, 5, kernel_size=(3, 3), stride=(1, 1))
  (conv3): Conv2d(5, 5, kernel_size=(3, 3), stride=(1, 1))
  (activation): ReLU()
)
skip_con_x shape: torch.Size([100, 5, 106, 106])
x shape: torch.Size([100, 10, 50, 50])
x upsample shape: torch.Size([100, 10, 100, 100])
x conv1 shape: torch.Size([100, 5, 99, 99])
assert 1
orginal_x shape: torch.Size([100, 5, 100, 100]), x.shape: torch.Size([100, 5, 99, 99])

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [97], in <cell line: 37>()
     34     assert tuple(x.shape) == (test_samples, test_channels // 2,  test_size * 2 - 5, test_size * 2 - 5)
     35     x = test_block.activation(x)
---> 37 test_expanding_block()
     38 print("Success!")

Input In [97], in test_expanding_block(test_samples, test_channels, test_size)
     25 orginal_x = crop(skip_con_x, x.shape)
     26 print( f"orginal_x shape: {orginal_x.shape}, x.shape: {x.shape}" )
---> 27 x = torch.cat([x, orginal_x], axis=1)
     28 x = test_block.conv2(x)
     30 # Make sure that the second convolution produces the right shape

RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 99 but got size 100 for tensor number 1 in the list.


So x image height and width reduced to 99 after conv1(). Is this correct?
Or is my crop() wrong although it passes test_expanding_block_crop().
crop() is achieved with image height and width dimensions slicing of original image.

Thanks,
MCW

Hmmm. Looks like my first attempt of slicing to compute start and end
offsets from original image height and width doesnt work for odd
numbers, i.e:

new_height_start:-height_offset
new_width_start:-width_offset

But sorted now.

1 Like

It’s great that you were able to solve this under your own power. I went back and reminded myself what my code looks like and the way I implemented the crop function involved halving the differences in the dimensions, so I needed to use “ceil” to make sure I ended up rounding in the correct direction.

Hi Paul,

Yeah thanks for your hint about mid point. That’s the bit I was blind sided to
right from the start as I thought it should be straight forward using the
tensor slicing on both ends of the dimension. Unit test kinda cunning to not
test odd number from what I can remember :wink:

You won’t believe the number of print() I needed to put in and compare
2+ diff versions of crop() for me to see where things actually went wrong. lol

And … the next 2B assignment is again confusing as always to try try figure
out what “true” label actually means in that context. lol!

1 Like

Yes, the test cases here are pretty amazing. Sometimes it really takes some work to figure out what tricks they are playing. They really take maximum advantage of the fact that they are passing function references as arguments to most of the target functions here. :nerd_face: