示例#1
0
    def testSeparableConv2DSameWithInputOddSize(self):
        n, n2 = 5, 3

        # Input image.
        x = create_test_input(1, n, n, 1)

        # Convolution kernel.
        dw = create_test_input(1, 3, 3, 1)
        dw = tf.reshape(dw, [3, 3, 1, 1])

        tf.get_variable('Conv/depthwise_weights', initializer=dw)
        tf.get_variable('Conv/pointwise_weights',
                        initializer=tf.ones([1, 1, 1, 1]))
        tf.get_variable('Conv/biases', initializer=tf.zeros([1]))
        tf.get_variable_scope().reuse_variables()

        y1 = slim.separable_conv2d(x,
                                   1, [3, 3],
                                   depth_multiplier=1,
                                   stride=1,
                                   scope='Conv')
        y1_expected = tf.to_float([[14, 28, 43, 58, 34], [28, 48, 66, 84, 46],
                                   [43, 66, 84, 102,
                                    55], [58, 84, 102, 120, 64],
                                   [34, 46, 55, 64, 30]])
        y1_expected = tf.reshape(y1_expected, [1, n, n, 1])

        y2 = resnet_utils.subsample(y1, 2)
        y2_expected = tf.to_float([[14, 43, 34], [43, 84, 55], [34, 55, 30]])
        y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1])

        y3 = xception.separable_conv2d_same(x,
                                            1,
                                            3,
                                            depth_multiplier=1,
                                            regularize_depthwise=True,
                                            stride=2,
                                            scope='Conv')
        y3_expected = y2_expected

        y4 = slim.separable_conv2d(x,
                                   1, [3, 3],
                                   depth_multiplier=1,
                                   stride=2,
                                   scope='Conv')
        y4_expected = y2_expected

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            self.assertAllClose(y1.eval(), y1_expected.eval())
            self.assertAllClose(y2.eval(), y2_expected.eval())
            self.assertAllClose(y3.eval(), y3_expected.eval())
            self.assertAllClose(y4.eval(), y4_expected.eval())
示例#2
0
    def testSeparableConv2DSameWithInputEvenSize(self):
        n, n2 = 4, 2

        # Input image.
        x = create_test_input(1, n, n, 1)

        # Convolution kernel =======================================
        dw = create_test_input(1, 3, 3, 1)
        dw = tf.reshape(dw, [3, 3, 1, 1])

        # tf variables for kernel in slim.separable_conv2d()
        # enable reuse of tf variables in slim.separable_conv2d()
        tf.get_variable('Conv/depthwise_weights', initializer=dw)
        tf.get_variable('Conv/pointwise_weights',
                        initializer=tf.ones([1, 1, 1, 1]))
        tf.get_variable('Conv/biases', initializer=tf.zeros([1]))
        tf.get_variable_scope().reuse_variables()

        #-------------------------------------------------------
        # test 1: separable conv 2d of 4x4 input
        y1 = slim.separable_conv2d(x,
                                   1, [3, 3],
                                   depth_multiplier=1,
                                   stride=1,
                                   scope='Conv')

        # an expected result when n=4 from separable_conv2d()
        y1_expected = tf.to_float([[14, 28, 43, 26], [28, 48, 66, 37],
                                   [43, 66, 84, 46], [26, 37, 46, 22]])

        # reshape to [n,n] to [1,n,n,1]
        y1_expected = tf.reshape(y1_expected, [1, n, n, 1])

        #-------------------------------------------------------
        # test 2: subsampling to 4x4 to 2x2

        # subsampling of test result y from 4x4 to 2x2
        y2 = resnet_utils.subsample(y1, 2)
        y2_expected = tf.to_float([[14, 43], [43, 84]])
        y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1])

        #-------------------------------------------------------
        # test 3: separable conv 2d of 4x4 input with stride2 and use_explicit_padding= True
        y3 = xception.separable_conv2d_same(x,
                                            1,
                                            3,
                                            depth_multiplier=1,
                                            regularize_depthwise=True,
                                            stride=2,
                                            scope='Conv')
        y3_expected = y2_expected

        #-------------------------------------------------------
        # test 4: separable conv 2d of 4x4 input with stride2
        y4 = slim.separable_conv2d(x,
                                   1, [3, 3],
                                   depth_multiplier=1,
                                   stride=2,
                                   scope='Conv')
        y4_expected = tf.to_float([[48, 37], [37, 22]])
        y4_expected = tf.reshape(y4_expected, [1, n2, n2, 1])

        # run tests by tf.sess
        with self.test_session() as sess:
            print('[tf.Test] run testSeparableConv2DSameWithInputEvenSize()')
            sess.run(tf.global_variables_initializer())
            self.assertAllClose(y1.eval(), y1_expected.eval())
            self.assertAllClose(y2.eval(), y2_expected.eval())
            self.assertAllClose(y3.eval(), y3_expected.eval())
            self.assertAllClose(y4.eval(), y4_expected.eval())