コード例 #1
0
ファイル: utils.py プロジェクト: yekeren/Ads-KB
        def _cumsum_gradient(score_map, box):
            b, n, m, c = utils.get_tensor_shape(score_map)
            _, p, _ = utils.get_tensor_shape(box)

            expanded_box = _get_expanded_box(box,
                                             img_h=n,
                                             img_w=m,
                                             border_ratio=border_ratio)

            (box_h, box_w) = _get_box_shape(box)
            (expanded_box_h, expanded_box_w) = _get_box_shape(expanded_box)

            cumsum = imgproc.calc_cumsum_2d(
                score_map, tf.concat([box, expanded_box], axis=1))

            area = tf.expand_dims(tf.cast(box_h * box_w, tf.float32), axis=-1)
            area_border = tf.expand_dims(tf.cast(
                expanded_box_h * expanded_box_w - box_h * box_w, tf.float32),
                                         axis=-1)

            avg_val = tf.div(cumsum[:, :p, :], tf.maximum(_SMALL_NUMBER, area))
            avg_val_in_border = tf.div(cumsum[:, p:, :] - cumsum[:, :p, :],
                                       tf.maximum(_SMALL_NUMBER, area_border))

            return avg_val - avg_val_in_border
コード例 #2
0
ファイル: imgproc_test.py プロジェクト: yekeren/Ads-KB
    def test_calc_cumsum_2d(self):
        tf.reset_default_graph()

        image = tf.placeholder(tf.float32, shape=[None, None, None])
        box = tf.placeholder(tf.int64, shape=[None, 4])
        cumsum = imgproc.calc_cumsum_2d(tf.expand_dims(image, axis=0),
                                        tf.expand_dims(box, axis=0))[0]

        with self.test_session() as sess:

            # 3x3 image, 1 channel.

            result = sess.run(cumsum,
                              feed_dict={
                                  image: [[[1], [2], [3]], [[4], [5], [6]],
                                          [[7], [8], [9]]],
                                  box: [
                                      [0, 0, 1, 1],
                                      [2, 2, 3, 3],
                                      [1, 1, 2, 2],
                                      [0, 0, 2, 2],
                                      [1, 1, 3, 3],
                                      [0, 0, 3, 3],
                                      [0, 0, 2, 3],
                                  ]
                              })
            self.assertAllClose(result,
                                [[1], [9], [5], [12], [28], [45], [21]])

            # 3x3 image, 2 channels.

            result = sess.run(cumsum,
                              feed_dict={
                                  image: [[[1, 1], [2, 2], [3, 3]],
                                          [[4, 1], [5, 2], [6, 3]],
                                          [[7, 1], [8, 2], [9, 3]]],
                                  box: [
                                      [0, 0, 1, 1],
                                      [2, 2, 3, 3],
                                      [1, 1, 2, 2],
                                      [0, 0, 2, 2],
                                      [1, 1, 3, 3],
                                      [0, 0, 3, 3],
                                      [0, 0, 2, 3],
                                  ]
                              })
            self.assertAllClose(result, [[1, 1], [9, 3], [5, 2], [12, 6],
                                         [28, 10], [45, 18], [21, 12]])
コード例 #3
0
ファイル: utils.py プロジェクト: yekeren/Ads-KB
        def _cumsum_gradient(score_map, box):
            b, n, m, c = utils.get_tensor_shape(score_map)
            _, p, _ = utils.get_tensor_shape(box)

            # Leave a border for the image border.
            ymin, xmin, ymax, xmax = tf.unstack(box, axis=-1)
            ymin, xmin = tf.maximum(ymin, 2), tf.maximum(xmin, 2)
            ymax, xmax = tf.minimum(ymax, tf.to_int64(n - 2)), tf.minimum(
                xmax, tf.to_int64(m - 2))

            box = tf.stack([ymin, xmin, ymax, xmax], axis=-1)
            box_exp = _get_expanded_box(box,
                                        img_h=n,
                                        img_w=m,
                                        border_ratio=border_ratio)

            box_list = [box, box_exp]

            area_list = [
                tf.cast(_get_box_area(b), tf.float32) for b in box_list
            ]
            cumsum = imgproc.calc_cumsum_2d(score_map,
                                            tf.concat(box_list, axis=1))
            cumsum_list = [
                cumsum[:, i * p:(i + 1) * p, :] for i in range(len(box_list))
            ]

            # The main box has to be valid, including the four shrinked boxes.
            assert_op = tf.Assert(
                tf.reduce_all(tf.greater(area_list[0], 0)),
                ["Check area of the main box failed:", area_list[0]])

            with tf.control_dependencies([assert_op]):
                border_area = area_list[1] - area_list[0]
                border_cumsum = cumsum_list[1] - cumsum_list[0]

                border_avg = tf.div(
                    border_cumsum,
                    tf.maximum(_SMALL_NUMBER,
                               tf.expand_dims(border_area, axis=-1)))
                box_avg = tf.div(
                    cumsum_list[0],
                    tf.maximum(_SMALL_NUMBER,
                               tf.expand_dims(area_list[0], axis=-1)))

                return purity_weight * box_avg - border_avg
コード例 #4
0
ファイル: utils.py プロジェクト: yekeren/Ads-KB
        def _cumsum_gradient(score_map, box):
            b, n, m, c = utils.get_tensor_shape(score_map)
            _, p, _ = utils.get_tensor_shape(box)

            n, m = tf.cast(n, tf.int64), tf.cast(m, tf.int64)

            # Leave a border for the image border.
            ymin, xmin, ymax, xmax = tf.unstack(box, axis=-1)
            ymin, xmin = tf.maximum(ymin, 3), tf.maximum(xmin, 3)
            ymax, xmax = tf.minimum(ymax, n - 3), tf.minimum(xmax, m - 3)
            box = tf.stack([ymin, xmin, ymax, xmax], axis=-1)

            box_exp = _get_expanded_box(box,
                                        img_h=n,
                                        img_w=m,
                                        border_ratio=border_ratio)
            box_shr = _get_shrinked_box(box,
                                        img_h=n,
                                        img_w=m,
                                        border_ratio=border_ratio)

            ymin, xmin, ymax, xmax = tf.unstack(box, axis=-1)
            ymin_exp, xmin_exp, ymax_exp, xmax_exp = tf.unstack(box_exp,
                                                                axis=-1)
            ymin_shr, xmin_shr, ymax_shr, xmax_shr = tf.unstack(box_shr,
                                                                axis=-1)

            box_list = [
                box,
                tf.stack([ymin, xmin, ymax, xmin_shr], axis=-1),
                tf.stack([ymin, xmax_shr, ymax, xmax], axis=-1),
                tf.stack([ymin, xmin, ymin_shr, xmax], axis=-1),
                tf.stack([ymax_shr, xmin, ymax, xmax], axis=-1),
                tf.stack([ymin, xmin_exp, ymax, xmin], axis=-1),
                tf.stack([ymin, xmax, ymax, xmax_exp], axis=-1),
                tf.stack([ymin_exp, xmin, ymin, xmax], axis=-1),
                tf.stack([ymax, xmin, ymax_exp, xmax], axis=-1)
            ]

            area_list = [
                tf.cast(_get_box_area(b), tf.float32) for b in box_list
            ]
            cumsum = imgproc.calc_cumsum_2d(score_map,
                                            tf.concat(box_list, axis=1))
            cumsum_list = [
                cumsum[:, i * p:(i + 1) * p, :] for i in range(len(box_list))
            ]

            # Compute the averaged cumsum inside each box.
            cumsum_avg_list = [
                tf.div(
                    cumsum_list[i],
                    tf.expand_dims(tf.maximum(_SMALL_NUMBER, area_list[i]),
                                   axis=-1)) for i in range(len(box_list))
            ]

            # The main box has to be valid, including the four shrinked boxes.
            assert_op = tf.Assert(
                tf.reduce_all(tf.greater(tf.stack(area_list, axis=-1), 0)),
                ["Check area of the main box failed:", area_list[0]])

            with tf.control_dependencies([assert_op]):
                grad_list = []
                for i in [1, 2, 3, 4]:
                    grad = cumsum_avg_list[i] - cumsum_avg_list[i + 4]
                    grad_list.append(grad)

                return purity_weight * cumsum_avg_list[0] + tf.reduce_min(
                    tf.stack(grad_list, axis=-1), axis=-1)
コード例 #5
0
ファイル: utils.py プロジェクト: yekeren/Ads-KB
 def _cumsum_avg(score_map, box):
     ymin, xmin, ymax, xmax = tf.unstack(box, axis=-1)
     area = tf.expand_dims((ymax - ymin) * (xmax - xmin), axis=-1)
     return tf.div(imgproc.calc_cumsum_2d(score_map, box),
                   tf.maximum(_SMALL_NUMBER, tf.cast(area, tf.float32)))