Пример #1
0
    def __get_raw_bbox_info(self, img):
        with torch.no_grad():
            im_data = torch.FloatTensor(1).cuda()
            im_info = torch.FloatTensor(1).cuda()
            num_boxes = torch.LongTensor(1).cuda()
            gt_boxes = torch.FloatTensor(1).cuda()
            box_info = torch.FloatTensor(1).cuda()

            im_blob, im_scales = self.__get_image_blob(img)

            im_info_np = np.array(
                [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]],
                dtype=np.float32)

            im_data_pt = torch.from_numpy(im_blob)
            im_data_pt = im_data_pt.permute(0, 3, 1, 2)
            im_info_pt = torch.from_numpy(im_info_np)

            im_data.resize_(im_data_pt.size()).copy_(im_data_pt)
            im_info.resize_(im_info_pt.size()).copy_(im_info_pt)
            gt_boxes.resize_(1, 1, 5).zero_()
            num_boxes.resize_(1).zero_()
            box_info.resize_(1, 1, 5).zero_()

            # forward
            rois, cls_prob, bbox_pred, \
            rpn_loss_cls, rpn_loss_box, \
            RCNN_loss_cls, RCNN_loss_bbox, \
            rois_label, loss_list = self.hand_detector(im_data, im_info, gt_boxes, num_boxes, box_info)

            scores = cls_prob.data
            boxes = rois.data[:, :, 1:5]

            # hand side info (left/right)
            lr_vector = loss_list[2][0].detach()
            lr = torch.sigmoid(lr_vector) > 0.5
            lr = lr.squeeze(0).float()

            box_deltas = bbox_pred.data
            stds = [0.1, 0.1, 0.2, 0.2]
            means = [0.0, 0.0, 0.0, 0.0]
            box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(stds).cuda() \
                + torch.FloatTensor(means).cuda()
            box_deltas = box_deltas.view(1, -1, 4 * len(self.classes))

            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)

            pred_boxes /= im_scales[0]
            scores = scores.squeeze()
            pred_boxes = pred_boxes.squeeze()
        return scores, pred_boxes, lr
Пример #2
0
    def __get_raw_hand_bbox(self, img):
        with torch.no_grad():
            im_data = torch.FloatTensor(1).cuda()
            im_info = torch.FloatTensor(1).cuda()
            num_boxes = torch.LongTensor(1).cuda()
            gt_boxes = torch.FloatTensor(1).cuda()
            box_info = torch.FloatTensor(1).cuda()

            im_blob, im_scales = self.__get_image_blob(img)

            im_info_np = np.array(
                [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]],
                dtype=np.float32)

            im_data_pt = torch.from_numpy(im_blob)
            im_data_pt = im_data_pt.permute(0, 3, 1, 2)
            im_info_pt = torch.from_numpy(im_info_np)

            im_data.resize_(im_data_pt.size()).copy_(im_data_pt)
            im_info.resize_(im_info_pt.size()).copy_(im_info_pt)
            gt_boxes.resize_(1, 1, 5).zero_()
            num_boxes.resize_(1).zero_()
            box_info.resize_(1, 1, 5).zero_()

            # forward
            rois, cls_prob, bbox_pred, \
            rpn_loss_cls, rpn_loss_box, \
            RCNN_loss_cls, RCNN_loss_bbox, \
            rois_label, loss_list = self.hand_detector(im_data, im_info, gt_boxes, num_boxes, box_info)

            scores = cls_prob.data
            boxes = rois.data[:, :, 1:5]

            # hand side info (left/right)
            lr_vector = loss_list[2][0].detach()
            lr = torch.sigmoid(lr_vector) > 0.5
            lr = lr.squeeze(0).float()

            box_deltas = bbox_pred.data
            stds = [0.1, 0.1, 0.2, 0.2]
            means = [0.0, 0.0, 0.0, 0.0]
            box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(stds).cuda() \
                + torch.FloatTensor(means).cuda()
            box_deltas = box_deltas.view(1, -1, 4 * len(self.classes))

            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)

            pred_boxes /= im_scales[0]
            scores = scores.squeeze()
            pred_boxes = pred_boxes.squeeze()

            thresh_hand = 0.5
            j = 2
            inds = torch.nonzero(scores[:, j] > thresh_hand,
                                 as_tuple=False).view(-1)

            if inds.numel() > 0:
                cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]
                cls_scores = scores[:, j][inds]

                _, order = torch.sort(cls_scores, 0, True)

                lr = lr[inds][order]
                cls_boxes = cls_boxes[order]
                cls_scores = cls_scores[order]

                keep = nms(cls_boxes, cls_scores,
                           cfgg.TEST.NMS)  # cfgg.TEST.NMS : 0.3
                cls_boxes = cls_boxes[keep]
                lr_selected = lr[keep]

                cls_boxes_np = cls_boxes.detach().cpu().numpy()
                lr_np = lr_selected.detach().cpu().numpy()

                return cls_boxes_np, lr_np[:, 0]
                # return cls_boxes_np, lr_np
            else:
                return None, None