def testComputeAreaFeatures1D(self): features = tf.constant([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1], [9.1, 10.1]]], dtype=tf.float32) area_mean, area_std, area_sum, area_height, area_widths = ( area_attention.compute_area_features(features, max_area_width=3, epsilon=0.)) with self.test_session() as session: session.run(tf.global_variables_initializer()) res1, res2, res3, res4, res5 = session.run([area_mean, area_std, area_sum, area_height, area_widths]) self.assertAllClose(((((1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (2, 3), (4, 5), (6, 7), (8, 9), (3, 4), (5, 6), (7, 8)), ((1.1, 2.1), (3.1, 4.1), (5.1, 6.1), (7.1, 8.1), (9.1, 10.1), (2.1, 3.1), (4.1, 5.1), (6.1, 7.1), (8.1, 9.1), (3.1, 4.1), (5.1, 6.1), (7.1, 8.1)))), res1, msg="mean_1d") expected_std = np.array([[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [1, 1], [1, 1], [1, 1], [1, 1], [1.63299, 1.63299], [1.63299, 1.63299], [1.63299, 1.63299]], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [1, 1], [1, 1], [1, 1], [1, 1], [1.63299, 1.63299], [1.63299, 1.63299], [1.63299, 1.63299]]]) self.assertAllClose(expected_std, res2, atol=1e-2, msg="std_1d") self.assertAllClose([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [4, 6], [8, 10], [12, 14], [16, 18], [9, 12], [15, 18], [21, 24]], [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1], [9.1, 10.1], [4.2, 6.2], [8.2, 10.2], [12.2, 14.2], [16.2, 18.2], [9.3, 12.3], [15.3, 18.3], [21.3, 24.3]]], res3, msg="sum_1d") self.assertAllEqual([[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1]], [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]], res4, msg="height_1d") self.assertAllEqual([[[1], [1], [1], [1], [1], [2], [2], [2], [2], [3], [3], [3]], [[1], [1], [1], [1], [1], [2], [2], [2], [2], [3], [3], [3]]], res5, msg="width_1d")
def _area_key_value_fn(keys, values): """Custom fn for computing area keys and values.""" tf.logging.info("max_area_width=%d, area_key_mode=%s, area_value_mode=%s", hparams.get("max_area_width", 1), hparams.get("area_key_mode", "none"), hparams.get("area_value_mode", "none")) keys = area_attention.compute_area_key( keys, max_area_width=hparams.get("max_area_width", 1), mode=hparams.get("area_key_mode", "none"), name="decoder_encoder", training=(hparams.mode == tf.estimator.ModeKeys.TRAIN)) if hparams.get("area_value_mode", "none") == "sum": _, _, values, _, _ = area_attention.compute_area_features( values, max_area_width=hparams.get("max_area_width", 1)) elif hparams.get("area_value_mode", "none") == "mean": values, _, _, _, _ = area_attention.compute_area_features( values, max_area_width=hparams.get("max_area_width", 1)) else: raise ValueError( "Unsupported area_value_mode: %s" % hparams.get( "area_value_mode", "none")) return keys, values
def testComputeAreaFeatures2D(self): features = tf.constant( [[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1], [9.1, 10.1], [11.1, 12.1]]], dtype=tf.float32) area_mean, area_std, area_sum, area_height, area_widths = ( area_attention.compute_area_features(features, max_area_width=3, max_area_height=2, height=2, epsilon=0.)) with self.test_session() as session: session.run(tf.global_variables_initializer()) res1, _, res3, res4, res5 = session.run( [area_mean, area_std, area_sum, area_height, area_widths]) expected_means = [[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [2, 3], [4, 5], [8, 9], [10, 11], [3, 4], [9, 10], [4, 5], [6, 7], [8, 9], [5, 6], [7, 8], [6, 7]], [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1], [9.1, 10.1], [11.1, 12.1], [2.1, 3.1], [4.1, 5.1], [8.1, 9.1], [10.1, 11.1], [3.1, 4.1], [9.1, 10.1], [4.1, 5.1], [6.1, 7.1], [8.1, 9.1], [5.1, 6.1], [7.1, 8.1], [6.1, 7.1]]] self.assertAllClose(expected_means, res1, msg="mean_1d") expected_heights = [ [ [1], [1], [1], [1], [1], [1], # 1x2 [1], [1], [1], [1], # 1x3 [1], [1], # 2x1 [2], [2], [2], # 2x2 [2], [2], # 2x3 [2] ], [ [1], [1], [1], [1], [1], [1], # 1x2 [1], [1], [1], [1], # 1x3 [1], [1], # 2x1 [2], [2], [2], # 2x2 [2], [2], # 2x3 [2] ] ] self.assertAllEqual(expected_heights, res4, msg="height_1d") expected_widths = [ [ [1], [1], [1], [1], [1], [1], # 1x2 [2], [2], [2], [2], # 1x3 [3], [3], # 2x1 [1], [1], [1], # 2x2 [2], [2], # 2x3 [3] ], [ [1], [1], [1], [1], [1], [1], # 1x2 [2], [2], [2], [2], # 1x3 [3], [3], # 2x1 [1], [1], [1], # 2x2 [2], [2], # 2x3 [3] ] ] self.assertAllEqual(expected_widths, res5, msg="width_1d") sizes = np.multiply(np.array(expected_heights), np.array(expected_widths)) expected_sums = np.multiply(np.array(expected_means), sizes) self.assertAllClose(expected_sums, res3, msg="sum_1d")