def test_deconvolution_repr(self): layer = layers.Deconvolution((3, 3, 10), name='deconv') self.assertEqual( str(layer), ( "Deconvolution((3, 3, 10), padding='VALID', stride=(1, 1), " "weight=HeNormal(gain=2), bias=Constant(0), name='deconv')" ) )
def test_deconvolution(self): network = layers.join( layers.Input((10, 10, 3)), layers.Convolution((3, 3, 7)), layers.Deconvolution((3, 3, 4)), ) shapes = [layer.output_shape for layer in network.layers] self.assertSequenceEqual(shapes, [(10, 10, 3), (8, 8, 7), (10, 10, 4)]) input_value = asfloat(np.random.random((1, 10, 10, 3))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape, (1, 10, 10, 4))
def test_deconvolution_for_random_cases(self): # A few random cases will check if output shape computed from # the network is the same as the shape that we get after we # propagated input through the network. for test_id in range(30): width = random.randint(7, 20) height = random.randint(7, 20) fh = random.randint(1, 7) fw = random.randint(1, 7) pad = random.choice([ 'valid', 'same', random.randint(0, 10), ( random.randint(0, 10), random.randint(0, 10), ), ]) stride = random.choice([ random.randint(1, 4), ( random.randint(1, 4), random.randint(1, 4), ), ]) print('\n------------') print("Test case #{}".format(test_id)) print('------------') print("Image shape: {}x{}".format(height, width)) print("Filter shape: {}x{}".format(fh, fw)) print("Padding: {}".format(pad)) print("Stride: {}".format(stride)) network = layers.join( layers.Input((height, width, 1)), layers.Convolution((fh, fw, 2), padding=pad, stride=stride), layers.Deconvolution((fh, fw, 1), padding=pad, stride=stride), ) input_value = asfloat(np.random.random((1, height, width, 1))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape[1:], network.output_shape)
def test_deconv_unknown_input_width_and_height(self): network = layers.join( layers.Input((None, None, 3)), layers.Convolution((3, 3, 7)), layers.Deconvolution((3, 3, 4)), ) shapes = [layer.output_shape for layer in network.layers] self.assertSequenceEqual(shapes, [(None, None, 3), (None, None, 7), (None, None, 4)]) input_value = asfloat(np.random.random((1, 10, 10, 3))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape, (1, 10, 10, 4)) input_value = asfloat(np.random.random((1, 7, 7, 3))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape, (1, 7, 7, 4))
def test_deconvolution_tuple_padding(self): network = layers.join( layers.Input((10, 10, 3)), layers.Convolution((3, 3, 7), padding=(9, 3)), layers.Deconvolution((3, 3, 4), padding=(9, 3)), ) shapes = network.output_shapes_per_layer shapes = {l: shape_to_tuple(s) for l, s in shapes.items()} self.assertSequenceEqual( shapes, { network.layers[0]: (None, 10, 10, 3), network.layers[1]: (None, 26, 14, 7), network.layers[2]: (None, 10, 10, 4), }) input_value = asfloat(np.random.random((1, 10, 10, 3))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape, (1, 10, 10, 4))
def test_deconv_unknown_input_width_and_height(self): network = layers.join( layers.Input((None, None, 3)), layers.Convolution((3, 3, 7)), layers.Deconvolution((3, 3, 4)), ) shapes = network.output_shapes_per_layer shapes = {l: shape_to_tuple(s) for l, s in shapes.items()} self.assertDictEqual( shapes, { network.layers[0]: (None, None, None, 3), network.layers[1]: (None, None, None, 7), network.layers[2]: (None, None, None, 4), }) input_value = asfloat(np.random.random((1, 10, 10, 3))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape, (1, 10, 10, 4)) input_value = asfloat(np.random.random((1, 7, 7, 3))) actual_output = self.eval(network.output(input_value)) self.assertEqual(actual_output.shape, (1, 7, 7, 4))