Esempio n. 1
0
 def test_dilated_max_pool_3d(self):
     custom_objects = {'DilatedMaxPool3D': layers.DilatedMaxPool3D}
     pool_size = (3, 3, 3)
     for strides in [1, 2, None]:
         for dilation_rate in [1, 2]:
             with self.test_session(use_gpu=True):
                 testing_utils.layer_test(layers.DilatedMaxPool3D,
                                          kwargs={
                                              'strides': strides,
                                              'dilation_rate':
                                              dilation_rate,
                                              'pool_size': pool_size
                                          },
                                          custom_objects=custom_objects,
                                          input_shape=(3, 11, 12, 10, 4))
                 testing_utils.layer_test(layers.DilatedMaxPool3D,
                                          kwargs={
                                              'strides': strides,
                                              'dilation_rate':
                                              dilation_rate,
                                              'data_format':
                                              'channels_first',
                                              'pool_size': pool_size
                                          },
                                          custom_objects=custom_objects,
                                          input_shape=(3, 4, 11, 12, 10))
Esempio n. 2
0
 def test_dilated_max_pool_3d(self):
     custom_objects = {'DilatedMaxPool3D': layers.DilatedMaxPool3D}
     pool_size = (3, 3, 3)
     for strides in [1, 2, None]:
         for dilation_rate in [1, 2, (1, 2, 2)]:
             for padding in ['valid', 'same']:
                 with self.cached_session():
                     testing_utils.layer_test(layers.DilatedMaxPool3D,
                                              kwargs={
                                                  'strides': strides,
                                                  'padding': padding,
                                                  'dilation_rate':
                                                  dilation_rate,
                                                  'pool_size': pool_size
                                              },
                                              custom_objects=custom_objects,
                                              input_shape=(3, 11, 12, 10,
                                                           4))
                     testing_utils.layer_test(layers.DilatedMaxPool3D,
                                              kwargs={
                                                  'strides': strides,
                                                  'padding': padding,
                                                  'dilation_rate':
                                                  dilation_rate,
                                                  'data_format':
                                                  'channels_first',
                                                  'pool_size': pool_size
                                              },
                                              custom_objects=custom_objects,
                                              input_shape=(3, 4, 11, 12,
                                                           10))
Esempio n. 3
0
 def test_dilated_max_pool_2d(self):
     pool_size = (3, 3)
     custom_objects = {'DilatedMaxPool2D': layers.DilatedMaxPool2D}
     for strides in [(1, 1), (2, 2), None]:
         for dilation_rate in [1, 2]:
             with self.test_session(use_gpu=True):
                 testing_utils.layer_test(layers.DilatedMaxPool2D,
                                          kwargs={
                                              'strides': strides,
                                              'pool_size': pool_size,
                                              'dilation_rate':
                                              dilation_rate,
                                              'data_format': 'channels_last'
                                          },
                                          custom_objects=custom_objects,
                                          input_shape=(3, 5, 6, 4))
                 testing_utils.layer_test(layers.DilatedMaxPool2D,
                                          kwargs={
                                              'strides': strides,
                                              'pool_size': pool_size,
                                              'dilation_rate':
                                              dilation_rate,
                                              'data_format':
                                              'channels_first'
                                          },
                                          custom_objects=custom_objects,
                                          input_shape=(3, 4, 5, 6))
Esempio n. 4
0
 def test_dilated_max_pool_2d(self):
     pool_size = (3, 3)
     custom_objects = {'DilatedMaxPool2D': layers.DilatedMaxPool2D}
     for strides in [(1, 1), (2, 2), None]:
         for dilation_rate in [1, 2, (1, 2)]:
             for padding in ['valid', 'same']:
                 testing_utils.layer_test(layers.DilatedMaxPool2D,
                                          kwargs={
                                              'strides': strides,
                                              'pool_size': pool_size,
                                              'padding': padding,
                                              'dilation_rate':
                                              dilation_rate,
                                              'data_format': 'channels_last'
                                          },
                                          custom_objects=custom_objects,
                                          input_shape=(3, 5, 6, 4))
                 testing_utils.layer_test(layers.DilatedMaxPool2D,
                                          kwargs={
                                              'strides': strides,
                                              'pool_size': pool_size,
                                              'padding': padding,
                                              'dilation_rate':
                                              dilation_rate,
                                              'data_format':
                                              'channels_first'
                                          },
                                          custom_objects=custom_objects,
                                          input_shape=(3, 4, 5, 6))
Esempio n. 5
0
 def test_resize_2d(self):
     testing_utils.layer_test(layers.Resize2D,
                              kwargs={'scale': 2},
                              custom_objects={'Resize2D': layers.Resize2D},
                              input_shape=(3, 5, 6, 4))
     testing_utils.layer_test(layers.Resize2D,
                              kwargs={
                                  'scale': 3,
                                  'data_format': 'channels_first'
                              },
                              custom_objects={'Resize2D': layers.Resize2D},
                              input_shape=(3, 5, 6, 4))
Esempio n. 6
0
 def test_simple(self):
     testing_utils.layer_test(layers.Upsample,
                              kwargs={'target_size': (2, 2)},
                              custom_objects={'Upsample': layers.Upsample},
                              input_shape=(3, 5, 6, 4))
     testing_utils.layer_test(layers.Upsample,
                              kwargs={
                                  'target_size': (2, 2),
                                  'data_format': 'channels_first'
                              },
                              custom_objects={'Upsample': layers.Upsample},
                              input_shape=(3, 4, 5, 6))
Esempio n. 7
0
    def test_conv_gru_2d(self, data_format, return_sequences):
        num_row = 3
        num_col = 3
        filters = 2
        num_samples = 1
        input_channel = 2
        input_num_row = 5
        input_num_col = 5
        sequence_len = 2

        custom_objects = {'ConvGRU2D': layers.ConvGRU2D}

        if data_format == 'channels_first':
            inputs = np.random.rand(num_samples, sequence_len,
                                    input_channel,
                                    input_num_row, input_num_col)
        else:
            inputs = np.random.rand(num_samples, sequence_len,
                                    input_num_row, input_num_col,
                                    input_channel)

        # test for return state:
        x = keras.layers.Input(batch_shape=inputs.shape)
        kwargs = {'data_format': data_format,
                  'return_sequences': return_sequences,
                  'return_state': True,
                  'stateful': True,
                  'filters': filters,
                  'kernel_size': (num_row, num_col),
                  'padding': 'valid'}
        layer = layers.ConvGRU2D(**kwargs)
        layer.build(inputs.shape)

        outputs = layer(x)
        _, states = outputs[0], outputs[1:]
        self.assertEqual(len(states), len(layer.cell.state_size))
        model = keras.models.Model(x, states[0])
        state = model.predict(inputs)

        self.assertAllClose(
            keras.backend.eval(layer.states[0]), state, atol=1e-4)

        # test for output shape:
        testing_utils.layer_test(
            layers.ConvGRU2D,
            kwargs={'data_format': data_format,
                    'return_sequences': return_sequences,
                    'filters': filters,
                    'kernel_size': (num_row, num_col),
                    'padding': 'valid'},
            custom_objects=custom_objects,
            input_shape=inputs.shape)
Esempio n. 8
0
 def test_location_3d(self):
     with self.test_session():
         testing_utils.layer_test(
             layers.Location3D,
             kwargs={'in_shape': (11, 12, 10, 4),
                     'data_format': 'channels_last'},
             custom_objects={'Location3D': layers.Location3D},
             input_shape=(3, 11, 12, 10, 4))
         testing_utils.layer_test(
             layers.Location3D,
             kwargs={'in_shape': (4, 11, 12, 10),
                     'data_format': 'channels_first'},
             custom_objects={'Location3D': layers.Location3D},
             input_shape=(3, 4, 11, 12, 10))
Esempio n. 9
0
 def test_location_2d(self):
     with self.test_session():
         testing_utils.layer_test(
             layers.Location2D,
             kwargs={'in_shape': (5, 6, 4),
                     'data_format': 'channels_last'},
             custom_objects={'Location2D': layers.Location2D},
             input_shape=(3, 5, 6, 4))
         testing_utils.layer_test(
             layers.Location2D,
             kwargs={'in_shape': (4, 5, 6),
                     'data_format': 'channels_first'},
             custom_objects={'Location2D': layers.Location2D},
             input_shape=(3, 4, 5, 6))
Esempio n. 10
0
 def test_conv_gru_2d_dropout(self):
     # check dropout
     with self.cached_session():
         testing_utils.layer_test(
             layers.ConvGRU2D,
             kwargs={'data_format': 'channels_last',
                     'return_sequences': False,
                     'filters': 2,
                     'kernel_size': (3, 3),
                     'padding': 'same',
                     'dropout': 0.1,
                     'recurrent_dropout': 0.1},
             custom_objects={'ConvGRU2D': layers.ConvGRU2D},
             input_shape=(1, 2, 5, 5, 2))
Esempio n. 11
0
    def test_reflection_padding_3d(self):
        num_samples = 2
        stack_size = 2
        input_len_dim1 = 4
        input_len_dim2 = 5
        input_len_dim3 = 3

        custom_objects = {'ReflectionPadding3D': layers.ReflectionPadding3D}
        inputs1 = np.ones((num_samples, input_len_dim1, input_len_dim2,
                           input_len_dim3, stack_size))
        inputs2 = np.ones((num_samples, stack_size, input_len_dim1,
                           input_len_dim2, input_len_dim3))
        data_formats = ['channels_first', 'channels_last']
        for data_format, inputs in zip(data_formats, [inputs2, inputs1]):
            # basic test
            with self.test_session():
                testing_utils.layer_test(layers.ReflectionPadding3D,
                                         kwargs={
                                             'padding': (2, 2, 2),
                                             'data_format': data_format
                                         },
                                         custom_objects=custom_objects,
                                         input_shape=inputs.shape)

        # correctness test
        # with self.test_session():
        #     layer = layers.ReflectionPadding3D(padding=(2, 2, 2))
        #     layer.build(inputs.shape)
        #     output = layer(keras.backend.variable(inputs))
        #     if context.executing_eagerly():
        #         np_output = output.numpy()
        #     else:
        #         np_output = keras.backend.eval(output)
        #     for offset in [0, 1, -1, -2]:
        #         np.testing.assert_allclose(np_output[:, offset, :, :, :], 0.)
        #         np.testing.assert_allclose(np_output[:, :, offset, :, :], 0.)
        #         np.testing.assert_allclose(np_output[:, :, :, offset, :], 0.)
        #     np.testing.assert_allclose(np_output[:, 2:-2, 2:-2, 2:-2, :], 1.)

        # test incorrect use
        with self.assertRaises(ValueError):
            layers.ReflectionPadding3D(padding=(1, 1))
        with self.assertRaises(ValueError):
            layers.ReflectionPadding3D(padding=None)
Esempio n. 12
0
 def test_normalize_3d(self):
     custom_objects = {'ImageNormalization3D': layers.ImageNormalization3D}
     norm_methods = [None, 'std', 'max', 'whole_image']
     with self.test_session():
         # test each norm method
         for norm_method in norm_methods:
             testing_utils.layer_test(layers.ImageNormalization3D,
                                      kwargs={
                                          'norm_method': norm_method,
                                          'filter_size': 3,
                                          'data_format': 'channels_last'
                                      },
                                      custom_objects=custom_objects,
                                      input_shape=(3, 11, 12, 10, 4))
             testing_utils.layer_test(layers.ImageNormalization3D,
                                      kwargs={
                                          'norm_method': norm_method,
                                          'filter_size': 3,
                                          'data_format': 'channels_first'
                                      },
                                      custom_objects=custom_objects,
                                      input_shape=(3, 4, 11, 12, 10))
         # test constraints and bias
         k_constraint = keras.constraints.max_norm(0.01)
         b_constraint = keras.constraints.max_norm(0.01)
         layer = layers.ImageNormalization3D(use_bias=True,
                                             kernel_constraint=k_constraint,
                                             bias_constraint=b_constraint)
         layer(keras.backend.variable(np.ones((3, 4, 11, 12, 10))))
         # self.assertEqual(layer.kernel.constraint, k_constraint)
         # self.assertEqual(layer.bias.constraint, b_constraint)
         # test bad norm_method
         with self.assertRaises(ValueError):
             layer = layers.ImageNormalization3D(norm_method='invalid')
         # test bad input dimensions
         with self.assertRaises(ValueError):
             layer = layers.ImageNormalization3D()
             layer.build([3, 5, 6, 4])
         # test invalid channel
         with self.assertRaises(ValueError):
             layer = layers.ImageNormalization3D()
             layer.build([3, 10, 11, 12, None])
Esempio n. 13
0
 def test_anchors_2d(self):
     with self.test_session(use_gpu=True):
         testing_utils.layer_test(
             layers.Anchors,
             kwargs={
                 'size': 1,
                 'stride': 1,
                 'data_format': 'channels_last'
             },
             custom_objects={'Anchors': layers.Anchors},
             input_shape=(3, 5, 6, 4))
         testing_utils.layer_test(
             layers.Anchors,
             kwargs={
                 'size': 1,
                 'stride': 1,
                 'data_format': 'channels_last'
             },
             custom_objects={'Anchors': layers.Anchors},
             input_shape=(3, None, None, None))
         testing_utils.layer_test(
             layers.Anchors,
             kwargs={
                 'size': 1,
                 'stride': 1,
                 'data_format': 'channels_first'
             },
             custom_objects={'Anchors': layers.Anchors},
             input_shape=(3, 5, 6, 4))
Esempio n. 14
0
    def test_reflection_padding_2d(self):
        num_samples = 2
        stack_size = 2
        input_num_row = 4
        input_num_col = 5

        custom_objects = {'ReflectionPadding2D': layers.ReflectionPadding2D}
        ins1 = np.ones((num_samples, input_num_row, input_num_col, stack_size))
        ins2 = np.ones((num_samples, stack_size, input_num_row, input_num_col))
        data_formats = ['channels_first', 'channels_last']
        for data_format, inputs in zip(data_formats, [ins2, ins1]):
            # basic test
            with self.test_session():
                testing_utils.layer_test(layers.ReflectionPadding2D,
                                         kwargs={
                                             'padding': (2, 2),
                                             'data_format': data_format
                                         },
                                         custom_objects=custom_objects,
                                         input_shape=inputs.shape)
                testing_utils.layer_test(layers.ReflectionPadding2D,
                                         kwargs={
                                             'padding': ((1, 2), (3, 4)),
                                             'data_format': data_format
                                         },
                                         custom_objects=custom_objects,
                                         input_shape=inputs.shape)

            # correctness test
            # with self.test_session():
            #     layer = layers.ReflectionPadding2D(
            #         padding=(2, 2), data_format=data_format)
            #     layer.build(inputs.shape)
            #     output = layer(keras.backend.variable(inputs))
            #     if context.executing_eagerly():
            #         np_output = output.numpy()
            #     else:
            #         np_output = keras.backend.eval(output)
            #     if data_format == 'channels_last':
            #         for offset in [0, 1, -1, -2]:
            #             np.testing.assert_allclose(np_output[:, offset, :, :], 0.)
            #             np.testing.assert_allclose(np_output[:, :, offset, :], 0.)
            #         np.testing.assert_allclose(np_output[:, 2:-2, 2:-2, :], 1.)
            #     elif data_format == 'channels_first':
            #         for offset in [0, 1, -1, -2]:
            #             np.testing.assert_allclose(np_output[:, :, offset, :], 0.)
            #             np.testing.assert_allclose(np_output[:, :, :, offset], 0.)
            #         np.testing.assert_allclose(np_output[:, 2:-2, 2:-2, :], 1.)

            #     layer = layers.ReflectionPadding2D(
            #         padding=((1, 2), (3, 4)), data_format=data_format)
            #     layer.build(inputs.shape)
            #     output = layer(keras.backend.variable(inputs))
            #     if context.executing_eagerly():
            #         np_output = output.numpy()
            #     else:
            #         np_output = keras.backend.eval(output)
            #     if data_format == 'channels_last':
            #         for top_offset in [0]:
            #             np.testing.assert_allclose(np_output[:, top_offset, :, :], 0.)
            #         for bottom_offset in [-1, -2]:
            #             np.testing.assert_allclose(np_output[:, bottom_offset, :, :], 0.)
            #         for left_offset in [0, 1, 2]:
            #             np.testing.assert_allclose(np_output[:, :, left_offset, :], 0.)
            #         for right_offset in [-1, -2, -3, -4]:
            #             np.testing.assert_allclose(np_output[:, :, right_offset, :], 0.)
            #         np.testing.assert_allclose(np_output[:, 1:-2, 3:-4, :], 1.)
            #     elif data_format == 'channels_first':
            #         for top_offset in [0]:
            #             np.testing.assert_allclose(np_output[:, :, top_offset, :], 0.)
            #         for bottom_offset in [-1, -2]:
            #             np.testing.assert_allclose(np_output[:, :, bottom_offset, :], 0.)
            #         for left_offset in [0, 1, 2]:
            #             np.testing.assert_allclose(np_output[:, :, :, left_offset], 0.)
            #         for right_offset in [-1, -2, -3, -4]:
            #             np.testing.assert_allclose(np_output[:, :, :, right_offset], 0.)
            #         np.testing.assert_allclose(np_output[:, :, 1:-2, 3:-4], 1.)

            # test incorrect use
            with self.assertRaises(ValueError):
                layers.ReflectionPadding2D(padding=(1, 1, 1))
            with self.assertRaises(ValueError):
                layers.ReflectionPadding2D(padding=None)
Esempio n. 15
0
    def test_tensorproduct(self):
        custom_objects = {'TensorProduct': layers.TensorProduct}

        testing_utils.layer_test(
            layers.TensorProduct,
            kwargs={'output_dim': 3},
            custom_objects=custom_objects,
            input_shape=(3, 2))

        testing_utils.layer_test(
            layers.TensorProduct,
            kwargs={'output_dim': 3},
            custom_objects=custom_objects,
            input_shape=(3, 4, 2))

        testing_utils.layer_test(
            layers.TensorProduct,
            kwargs={'output_dim': 3},
            custom_objects=custom_objects,
            input_shape=(None, None, 2))

        testing_utils.layer_test(
            layers.TensorProduct,
            kwargs={'output_dim': 3},
            custom_objects=custom_objects,
            input_shape=(3, 4, 5, 2))

        testing_utils.layer_test(
            layers.TensorProduct,
            kwargs={'output_dim': 3,
                    'data_format': 'channels_first'},
            custom_objects=custom_objects,
            input_shape=(3, 2, 4, 5))

        # test no bias
        testing_utils.layer_test(
            layers.TensorProduct,
            kwargs={'output_dim': 2,
                    'use_bias': False},
            custom_objects=custom_objects,
            input_shape=(3, 5, 6, 4))

        # test bad input channel
        with self.assertRaises(ValueError):
            testing_utils.layer_test(
                layers.TensorProduct,
                kwargs={'output_dim': 3},
                custom_objects=custom_objects,
                input_shape=(3, 5, 6, None))