コード例 #1
0
    def _valid_epoch(self,validiter=-1):
        def _postprocess(pred_bbox, test_input_size, org_img_shape):
            if self.args.MODEL.boxloss == 'KL':
                pred_coor = pred_bbox[:, 0:4]
                pred_vari = pred_bbox[:, 4:8]
                pred_vari = torch.exp(pred_vari)
                pred_conf = pred_bbox[:, 8]
                pred_prob = pred_bbox[:, 9:]
            else:
                pred_coor = pred_bbox[:, 0:4]
                pred_conf = pred_bbox[:, 4]
                pred_prob = pred_bbox[:, 5:]
            org_h, org_w = org_img_shape
            resize_ratio = min(1.0 * test_input_size / org_w, 1.0 * test_input_size / org_h)
            dw = (test_input_size - resize_ratio * org_w) / 2
            dh = (test_input_size - resize_ratio * org_h) / 2
            pred_coor[:, 0::2] = 1.0 * (pred_coor[:, 0::2] - dw) / resize_ratio
            pred_coor[:, 1::2] = 1.0 * (pred_coor[:, 1::2] - dh) / resize_ratio
            x1,y1,x2,y2=torch.split(pred_coor,[1,1,1,1],dim=1)
            x1,y1=torch.max(x1,torch.zeros_like(x1)),torch.max(y1,torch.zeros_like(y1))
            x2,y2=torch.min(x2,torch.ones_like(x2)*(org_w-1)),torch.min(y2,torch.ones_like(y2)*(org_h-1))
            pred_coor=torch.cat([x1,y1,x2,y2],dim=-1)

            # ***********************
            if pred_prob.shape[-1]==0:
                pred_prob = torch.ones((pred_prob.shape[0], 1)).cuda()
            # ***********************
            scores = pred_conf.unsqueeze(-1) * pred_prob
            bboxes = torch.cat([pred_coor, scores], dim=-1)
            if self.args.MODEL.boxloss == 'KL' and self.args.EVAL.varvote:
                return bboxes, pred_vari
            else:
                return bboxes,None
        s = time.time()
        self.model.eval()
        for idx_batch, inputs in tqdm(enumerate(self.test_dataloader),total=len(self.test_dataloader)):
            if idx_batch == validiter:  # to save time
                break
            inputs = [input if isinstance(input, list) else input.squeeze(0) for input in inputs]
            (imgs, imgpath, ori_shapes, *_) = inputs
            imgs = imgs.cuda()
            ori_shapes = ori_shapes.cuda()
            with torch.no_grad():
                outputs = self.model(imgs)
            for imgidx in range(len(outputs)):
                bbox,bboxvari = _postprocess(outputs[imgidx], imgs.shape[-1], ori_shapes[imgidx])
                nms_boxes, nms_scores, nms_labels = torch_nms(self.args.EVAL,bbox,
                                                                 variance=bboxvari)
                if nms_boxes is not None:
                    self.TESTevaluator.append(imgpath[imgidx][0],
                                              nms_boxes.cpu().numpy(),
                                              nms_scores.cpu().numpy(),
                                              nms_labels.cpu().numpy())
        results = self.TESTevaluator.evaluate()
        imgs = self.TESTevaluator.visual_imgs
        for k, v in zip(self.logger_custom, results):
            print("{}:{}".format(k, v))
        return results, imgs
コード例 #2
0
    def _valid_epoch(self, validiter=-1,width_mult=-1,cal_bn=False,verbose=False):
        synchronize()
        self.model.eval()
        #-----------------------

        if self.args.EXPER.US_training or cal_bn:
            self.model.apply(lambda m: setattr(m, 'width_mult',width_mult))
            if cal_bn:
                if width_mult not in [0.4,1.0]:
                    self._cal_bn()
        synchronize()
        def _postprocess(pred_bbox, test_input_size, org_img_shape):
            if self.args.MODEL.boxloss == 'KL':
                pred_coor = pred_bbox[:, 0:4]
                pred_vari = pred_bbox[:, 4:8]
                pred_vari = torch.exp(pred_vari)
                pred_conf = pred_bbox[:, 8]
                pred_prob = pred_bbox[:, 9:]
            else:
                pred_coor = pred_bbox[:, 0:4]
                pred_conf = pred_bbox[:, 4]
                pred_prob = pred_bbox[:, 5:]
            org_h, org_w = org_img_shape
            resize_ratio = min(1.0 * test_input_size / org_w, 1.0 * test_input_size / org_h)
            dw = (test_input_size - resize_ratio * org_w) / 2
            dh = (test_input_size - resize_ratio * org_h) / 2
            pred_coor[:, 0::2] = 1.0 * (pred_coor[:, 0::2] - dw) / resize_ratio
            pred_coor[:, 1::2] = 1.0 * (pred_coor[:, 1::2] - dh) / resize_ratio
            x1, y1, x2, y2 = torch.split(pred_coor, [1, 1, 1, 1], dim=1)
            x1, y1 = torch.max(x1, torch.zeros_like(x1)), torch.max(y1, torch.zeros_like(y1))
            x2, y2 = torch.min(x2, torch.ones_like(x2) * (org_w - 1)), torch.min(y2, torch.ones_like(y2) * (org_h - 1))
            pred_coor = torch.cat([x1, y1, x2, y2], dim=-1)

            # ***********************
            if pred_prob.shape[-1] == 0:
                pred_prob = torch.ones((pred_prob.shape[0], 1)).cuda()
            # ***********************
            scores = pred_conf.unsqueeze(-1) * pred_prob
            bboxes = torch.cat([pred_coor, scores], dim=-1)
            if self.args.MODEL.boxloss == 'KL' and self.args.EVAL.varvote:
                return bboxes, pred_vari
            else:
                return bboxes, None

        self.model.eval()
        for idx_batch, inputs in tqdm(enumerate(self.test_dataloader), total=len(self.test_dataloader)):
        # for idx_batch, inputs in enumerate(self.test_dataloader):
            if idx_batch == validiter:  # to save time
                break
            inputs = [input if isinstance(input, list) else input.squeeze(0) for input in inputs]
            (imgs, imgpath, ori_shapes, *_) = inputs
            imgs = imgs.cuda()
            ori_shapes = ori_shapes.cuda()
            with torch.no_grad():
                outputs = self.model(imgs)
            for imgidx in range(len(outputs)):
                bbox, bboxvari = _postprocess(outputs[imgidx], imgs.shape[-1], ori_shapes[imgidx])
                nms_boxes, nms_scores, nms_labels = torch_nms(self.args.EVAL, bbox,
                                                              variance=bboxvari)
                if nms_boxes is not None:
                    self.TESTevaluator.append(imgpath[imgidx][0],
                                              nms_boxes.cpu().numpy(),
                                              nms_scores.cpu().numpy(),
                                              nms_labels.cpu().numpy())
        ## accumulate prediction results across gpus
        results=all_gather(self.TESTevaluator.rec_pred)
        if is_main_process():
            all_recs=defaultdict(list)
            for rec in results:
                for k,v in rec.items():
                    all_recs[k].extend(v)
            self.TESTevaluator.rec_pred=all_recs
            results = self.TESTevaluator.evaluate()
            imgs = self.TESTevaluator.visual_imgs
            if verbose and is_main_process():
                for k, v in zip(self.logger_custom, results):
                    print("{}:{}".format(k, v))
            return results, imgs
        else:
            return 1,1
コード例 #3
0
    def _valid_epoch(self, multiscale, flip):
        s = time.time()
        self.model.eval()
        for idx_batch, inputs in enumerate(self.test_dataloader):
            if idx_batch == self.args.valid_batch and not self.args.do_test:  # to save time
                break
            inputs = [
                input if isinstance(input, list) else input.squeeze(0)
                for input in inputs
            ]
            (imgs, imgpath, annpath, ori_shapes, *_) = inputs

            ori_shapes = ori_shapes.float().cuda()
            if not multiscale:
                INPUT_SIZES = [self.net_size]
            else:
                INPUT_SIZES = [
                    self.net_size - 32, self.net_size, self.net_size + 32
                ]
            pyramids = makeImgPyramids(imgs.numpy().transpose(0, 2, 3, 1),
                                       scales=INPUT_SIZES,
                                       flip=flip)
            # produce outputFeatures for each scale
            img2multi = defaultdict(list)
            for idx, pyramid in enumerate(pyramids):
                pyramid = torch.from_numpy(pyramid.transpose(0, 3, 1,
                                                             2)).cuda()
                with torch.no_grad():
                    grids = self.model(pyramid)
                for imgidx in range(imgs.shape[0]):
                    img2multi[imgidx].append([grid[imgidx] for grid in grids])

            # append prediction for each image per scale/flip
            for imgidx, scalegrids in img2multi.items():
                allboxes = []
                allscores = []
                for _grids, _scale in zip(scalegrids[:len(INPUT_SIZES)],
                                          INPUT_SIZES):
                    _boxes, _scores = predict_yolo(
                        _grids,
                        self.anchors,
                        _scale,
                        ori_shapes[imgidx],
                        num_classes=self.num_classes)
                    allboxes.append(_boxes)
                    allscores.append(_scores)
                if flip:
                    for _grids, _scale in zip(scalegrids[len(INPUT_SIZES):],
                                              INPUT_SIZES):
                        _boxes, _scores = predict_yolo(
                            _grids,
                            self.anchors,
                            _scale,
                            ori_shapes[imgidx],
                            num_classes=self.num_classes)
                        _boxes = bbox_flip(_boxes.squeeze(0),
                                           flip_x=True,
                                           size=ori_shapes[imgidx])
                        _boxes = _boxes[np.newaxis, :]
                        allboxes.append(_boxes)
                        allscores.append(_scores)
                nms_boxes, nms_scores, nms_labels = torch_nms(
                    torch.cat(allboxes, dim=1),
                    torch.cat(allscores, dim=1),
                    num_classes=self.num_classes)
                if nms_boxes is not None:
                    # print(imgpath[imgidx][0])
                    self.TESTevaluator.append(imgpath[imgidx][0],
                                              annpath[imgidx][0],
                                              nms_boxes.cpu().numpy(),
                                              nms_scores.cpu().numpy(),
                                              nms_labels.cpu().numpy())
        results = self.TESTevaluator.evaluate()
        imgs = self.TESTevaluator.visual_imgs
        for k, v in zip(self.logger_custom, results):
            print("{}:{}".format(k, v))
        print("validation cost {} s".format(time.time() - s))
        return results, imgs
コード例 #4
0
    def _inference_epoch(self,
                         imgdir,
                         outdir=None,
                         multiscale=True,
                         flip=True):
        from dataset import get_imgdir
        from utils.visualize import visualize_boxes
        self.model.eval()
        dataloader = get_imgdir(imgdir, batch_size=8, net_size=self.net_size)
        for i, (imgpath, imgs, ori_shapes) in enumerate(dataloader):
            if i == 50:
                break
            ori_shapes = ori_shapes.cuda()
            if not multiscale:
                INPUT_SIZES = [self.net_size]
            else:
                INPUT_SIZES = [
                    self.net_size - 32, self.net_size, self.net_size + 32
                ]
            pyramids = makeImgPyramids(imgs.numpy().transpose(0, 2, 3, 1),
                                       scales=INPUT_SIZES,
                                       flip=flip)
            # produce outputFeatures for each scale
            img2multi = defaultdict(list)
            for idx, pyramid in enumerate(pyramids):
                pyramid = torch.from_numpy(pyramid.transpose(0, 3, 1,
                                                             2)).cuda()
                with torch.no_grad():
                    grids = self.model(pyramid)
                for imgidx in range(imgs.shape[0]):
                    img2multi[imgidx].append([grid[imgidx] for grid in grids])

            # append prediction for each image per scale/flip
            for imgidx, scalegrids in img2multi.items():
                allboxes = []
                allscores = []
                for _grids, _scale in zip(scalegrids[:len(INPUT_SIZES)],
                                          INPUT_SIZES):
                    _boxes, _scores = predict_yolo(
                        _grids,
                        self.anchors,
                        _scale,
                        ori_shapes[imgidx],
                        num_classes=self.num_classes)
                    allboxes.append(_boxes)
                    allscores.append(_scores)
                if flip:
                    for _grids, _scale in zip(scalegrids[len(INPUT_SIZES):],
                                              INPUT_SIZES):
                        _boxes, _scores = predict_yolo(
                            _grids,
                            self.anchors,
                            _scale,
                            ori_shapes[imgidx],
                            num_classes=self.num_classes)
                        _boxes = bbox_flip(_boxes.squeeze(0),
                                           flip_x=True,
                                           size=ori_shapes[imgidx])
                        _boxes = _boxes[np.newaxis, :]
                        allboxes.append(_boxes)
                        allscores.append(_scores)
                nms_boxes, nms_scores, nms_labels = torch_nms(
                    torch.cat(allboxes, dim=1),
                    torch.cat(allscores, dim=1),
                    num_classes=self.num_classes)
                if nms_boxes is not None:
                    detected_img = visualize_boxes(
                        np.array(Image.open(imgpath[imgidx]).convert('RGB')),
                        boxes=nms_boxes.cpu().numpy(),
                        labels=nms_labels.cpu().numpy(),
                        probs=nms_scores.cpu().numpy(),
                        class_labels=self.labels)
                    if outdir is not None:
                        plt.imsave(
                            os.path.join(outdir,
                                         imgpath[imgidx].split('/')[-1]),
                            detected_img)