def test_convolutional_transpose(): x = tensor.tensor4('x') num_channels = 4 num_filters = 3 image_size = (8, 6) original_image_size = (17, 13) batch_size = 5 filter_size = (3, 3) step = (2, 2) conv = ConvolutionalTranspose(filter_size, num_filters, num_channels, step=step, original_image_size=original_image_size, image_size=image_size, weights_init=Constant(1.), biases_init=Constant(5.)) conv.initialize() y = conv.apply(x) func = function([x], y) x_val = numpy.ones((batch_size, num_channels) + image_size, dtype=theano.config.floatX) expected_value = num_channels * numpy.ones((batch_size, num_filters) + original_image_size) expected_value[:, :, 2:-2:2, :] += num_channels expected_value[:, :, :, 2:-2:2] += num_channels expected_value[:, :, 2:-2:2, 2:-2:2] += num_channels assert_allclose(func(x_val), expected_value + 5)
def test_convolutional_transpose(): x = tensor.tensor4('x') num_channels = 4 num_filters = 3 image_size = (8, 6) original_image_size = (17, 13) batch_size = 5 filter_size = (3, 3) step = (2, 2) conv = ConvolutionalTranspose( filter_size, num_filters, num_channels, step=step, original_image_size=original_image_size, image_size=image_size, weights_init=Constant(1.), biases_init=Constant(5.)) conv.initialize() y = conv.apply(x) func = function([x], y) x_val = numpy.ones((batch_size, num_channels) + image_size, dtype=theano.config.floatX) expected_value = num_channels * numpy.ones( (batch_size, num_filters) + original_image_size) expected_value[:, :, 2:-2:2, :] += num_channels expected_value[:, :, :, 2:-2:2] += num_channels expected_value[:, :, 2:-2:2, 2:-2:2] += num_channels assert_allclose(func(x_val), expected_value + 5)
def test_convolutional_transpose_original_size_inference_full_padding(): brick = ConvolutionalTranspose(filter_size=(4, 5), num_filters=10, num_channels=5, step=(3, 2), border_mode='full', image_size=(6, 9)) brick.allocate() assert brick.original_image_size == (13, 13) input_ = tensor.tensor4() dummy = numpy.empty((4, 5, 6, 9), dtype=theano.config.floatX) result = brick.apply(input_).eval({input_: dummy}) assert result.shape == (4, 10, 13, 13)
def test_convolutional_transpose_original_size_inference_unused_edge(): brick = ConvolutionalTranspose(filter_size=(3, 3), num_filters=10, num_channels=5, step=(2, 2), border_mode=(1, 1), image_size=(4, 4), unused_edge=(1, 1)) brick.allocate() assert brick.original_image_size == (8, 8) input_ = tensor.tensor4() dummy = numpy.empty((4, 5, 4, 4), dtype=theano.config.floatX) result = brick.apply(input_).eval({input_: dummy}) assert result.shape == (4, 10, 8, 8)
def test_convolutional_transpose_original_size_inference(): brick = ConvolutionalTranspose(filter_size=(4, 5), num_filters=10, num_channels=5, step=(3, 2), image_size=(6, 9)) brick.allocate() # In x: filter applied 6 times with a step of 3 and filter size of 4 # means 1 dangling pixel, total original image size of 6 * 3 + 1 == 19. # In y: step of 2, applied 9 times, filter size of 5 means 3 # dangling pixels, so original is 2 * 9 + 3 == 21. assert brick.original_image_size == (19, 21) input_ = tensor.tensor4() dummy = numpy.empty((4, 5, 6, 9), dtype=theano.config.floatX) result = brick.apply(input_).eval({input_: dummy}) assert result.shape == (4, 10, 19, 21)