Esempio n. 1
0
    def _get_anchor_boxes(self, input_size):
        '''Compute anchor boxes for each feature map.

        Args:
          input_size: (tensor) model input size of (w,h).

        Returns:
          anchor_boxes: (tensor) anchor boxes for each feature map. Each of size [#anchors,4],
            where #anchors = fmw * fmh * #anchors_per_cell
        '''
        num_fms = len(self.anchor_areas)
        anchor_wh = self._get_anchor_wh()
        fm_sizes = [(input_size/pow(2.,i+3)).ceil() for i in range(num_fms)]  # p3 -> p7 feature map sizes

        boxes = []
        for i in range(num_fms):
            fm_size = fm_sizes[i]
            grid_size = input_size / fm_size
            fm_w, fm_h = int(fm_size[0]), int(fm_size[1])
            xy = meshgrid(fm_w,fm_h) + 0.5  # [fm_h*fm_w, 2]
            xy = (xy*grid_size).view(fm_h,fm_w,1,2).expand(fm_h,fm_w,9,2)
            wh = anchor_wh[i].view(1,1,9,2).expand(fm_h,fm_w,9,2)
            box = torch.cat([xy-wh/2.,xy+wh/2.], 3)  # [x,y,x,y]
            boxes.append(box.view(-1,4))
        return torch.cat(boxes, 0)
Esempio n. 2
0
    def _get_anchor_boxes(self):
        '''Compute anchor boxes for each feature map.

        Args:
          input_size: (tensor) model input size of (w,h).

        Returns:
          boxes: (list) anchor boxes for each feature map. Each of size [#anchors,4],
                        where #anchors = fmw * fmh * #anchors_per_cell
        '''
        num_fms = len(self.fm_sizes)
        anchor_wh = self._get_anchor_wh()
        # anchor_wh = self._get_manual_anchor_wh()

        # fm_sizes = [(input_size/pow(2.,i+3)).ceil() for i in range(num_fms)]  # p3 -> p7 feature map sizes
        fm_sizes = self.fm_sizes
        input_size = self.input_size

        boxes = []
        for i in range(num_fms):
            num_anchor = self.num_anchors[i]

            fm_size = fm_sizes[i]

            grid_size = [
                input_size[0] / fm_size[0], input_size[1] / fm_size[1]
            ]

            assert len(set(grid_size)) == 1
            grid_size = grid_size[0]

            fm_w, fm_h = int(fm_size[1]), int(fm_size[0])
            xy = meshgrid(fm_w, fm_h) + 0.5  # [fm_h*fm_w, 2]
            xy = (xy * grid_size).view(fm_h, fm_w, 1,
                                       2).expand(fm_h, fm_w, num_anchor, 2)
            wh = anchor_wh[i].view(1, 1, num_anchor,
                                   2).expand(fm_h, fm_w, num_anchor, 2)

            xy = xy.float()

            box = torch.cat([xy, wh], 3)  # [x,y,x,y]
            # box = torch.cat([xy-wh/2.,xy+wh/2.], 3)  # [x,y,x,y]
            # box = torch.cat([xy-wh/2.,wh], 3)  # [x,y,w,h]
            boxes.append(box.view(-1, 4))

            # pdb.set_trace()

        aboxes = torch.cat(boxes, 0)
        # aboxes[:,0] /= input_size[1]
        # aboxes[:,1] /= input_size[0]
        # aboxes[:,2] /= self.anchor_ref_size[1]
        # aboxes[:,3] /= self.anchor_ref_size[0]
        aboxes[:, 0] /= input_size[1]
        aboxes[:, 1] /= input_size[0]
        aboxes[:, 2] /= input_size[1]
        aboxes[:, 3] /= input_size[0]
        return aboxes