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.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, 26], [28, 48, 66, 37], [43, 66, 84, 46], [26, 37, 46, 22]]) y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) 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]) 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 = tf.to_float([[48, 37], [37, 22]]) y4_expected = tf.reshape(y4_expected, [1, n2, n2, 1]) 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()) print("y1:%s, y1_expected:%s " % (y1.eval(), y1_expected.eval())) print("y2:%s, y2_expected:%s " % (y2.eval(), y2_expected.eval())) print("y3:%s, y3_expected:%s " % (y3.eval(), y3_expected.eval())) print("y4:%s, y4_expected:%s " % (y4.eval(), y4_expected.eval()))
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.compat.v1.get_variable_scope().reuse_variables() y1 = slim.separable_conv2d(x, 1, [3, 3], depth_multiplier=1, stride=1, scope='Conv') y1_expected = tf.cast( [[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]], tf.float32) y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) y2 = resnet_utils.subsample(y1, 2) y2_expected = tf.cast([[14, 43, 34], [43, 84, 55], [34, 55, 30]], tf.float32) 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())
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())