示例#1
0
    def _restore_rect(self, rect, tensor_lr, tensor_tb):
        assert tensor_lr.shape == tensor_tb.shape
        assert len(tensor_lr.shape) == 3
        assert isinstance(rect, Rect)
        map_h, map_w, _ = tensor_lr.shape

        sum_l, sum_r = 0., 0.
        for i in range(rect.top, rect.bottom + 1):
            sum_l += tensor_lr[i][rect.left][0]
            sum_r += -tensor_lr[i][rect.right][1]

        sum_t, sum_b = 0., 0.
        for i in range(rect.left, rect.right + 1):
            sum_t += tensor_tb[rect.top][i][0]
            sum_b += - tensor_tb[rect.bottom][i][1]

        height, width = rect.bottom - rect.top + 1, rect.right - rect.left + 1
        ans = Rect(rect.left + sum_l / height, rect.top + sum_t / width,
                   rect.right + 1 + sum_r / height, rect.bottom + 1 + sum_b / width)
        #print ans
        ans.stretch(1.0 / map_h, 1.0 / map_w)
        #print ans
        return ans
示例#2
0
    def _restore_rects(self,
                       lr,
                       tb,
                       cls,
                       num_poolings,
                       threshold=None,
                       top=None):
        # print ratio
        map_h, map_w = lr.shape[1:3]
        if self.verbose:
            print map_h, map_w
            if cls is not None:
                print cls.shape

        left_logit = lr[:, :, :, 0]
        right_logit = lr[:, :, :, 1]
        top_logit = tb[:, :, :, 0]
        bottom_logit = tb[:, :, :, 1]

        if cls is not None:
            bg_logit = np.exp(cls[:, :, :, 0])
            bar_logit = np.exp(cls[:, :, :, 1])
            prob = np.exp(bar_logit) / (np.exp(bar_logit) + np.exp(bg_logit))

        dleft = left_logit
        dright = 1.0 - right_logit
        dtop = top_logit
        dbottom = 1.0 - bottom_logit

        def cut_top(res):
            res = sorted(res, reverse=True, key=lambda val: val[0])
            if top is not None:
                res = res[:top]
            return res

        res = []

        for y in range(0, map_h):
            for x in range(0, map_w):

                dl, dr, dt, db = dleft[0, y,
                                       x], dright[0, y,
                                                  x], dtop[0, y,
                                                           x], dbottom[0, y, x]

                #dy, dx, sh, sw = 0,0,0,0
                #sh, sw = 0, 0
                #dy, dx = 0, 0

                # print cls.shape
                conf = prob[0, y, x] if cls is not None else 0

                if self.verbose and conf > 0:
                    print conf, y, x, dl, dr, dt, db
                if threshold is not None or conf > threshold:
                    rect = Rect(x + dl * 2, y + dt * 2, x + dr * 2, y + db * 2)
                    rect.stretch(1 << (num_poolings), 1 << (num_poolings))

                    if self.verbose:
                        print rect

                    res.append((conf, rect))

                    if top is not None and len(res) > 2 * top:
                        res = cut_top(res)

        return cut_top(res)