コード例 #1
0
def retina_temp():
    from nets.retinanet import RetinaNet
    from losses.atss_retina_loss import ATSSRetinaLoss
    dataset = COCODataSets(
        img_root="/home/huffman/data/val2017",
        annotation_path="/home/huffman/data/annotations/instances_val2017.json",
        use_crowd=True,
        augments=True,
        remove_blank=True,
        img_size=640)
    dataloader = DataLoader(dataset=dataset,
                            batch_size=4,
                            shuffle=False,
                            num_workers=4,
                            collate_fn=dataset.collate_fn)
    net = RetinaNet(backbone="resnet18")
    creterion = ATSSRetinaLoss()
    for img_input, targets, _ in dataloader:
        _, _, h, w = img_input.shape
        targets[:, 3:] = targets[:, 3:] * torch.tensor(data=[w, h, w, h])
        cls_outputs, reg_outputs, anchors = net(img_input)
        creterion(cls_outputs, reg_outputs, anchors, targets)
        # total_loss, detail_loss, pos_num = creterion(cls_outputs, reg_outputs, anchors, targets)
        # cls_loss, reg_loss = detail_loss
        # print(total_loss)
        # print(cls_loss, reg_loss, pos_num)
        break
コード例 #2
0
ファイル: eval.py プロジェクト: liangheming/paav1
def eval_model(weight_path="weights/paa_resnet50_last.pth", device="cuda:0"):
    from pycocotools.coco import COCO
    device = torch.device(device)
    with open("config/paa.yaml", 'r') as rf:
        cfg = yaml.safe_load(rf)
    net = RetinaNet(**{**cfg['model'], 'pretrained': False})
    net.load_state_dict(torch.load(weight_path, map_location="cpu")['ema'])
    net.to(device)
    net.eval().half()
    data_cfg = cfg['data']
    basic_transform = RandScaleToMax(max_threshes=[data_cfg['max_thresh']])
    coco = COCO(data_cfg['val_annotation_path'])
    coco_predict_list = list()
    time_logger = AverageLogger()
    pbar = tqdm(coco.imgs.keys())
    for img_id in pbar:
        file_name = coco.imgs[img_id]['file_name']
        img_path = os.path.join(data_cfg['val_img_root'], file_name)
        img = cv.imread(img_path)
        h, w, _ = img.shape
        img, ratio, (left, top) = basic_transform.make_border(
            img, max_thresh=data_cfg['max_thresh'], border_val=(103, 116, 123))
        img_inp = (img[:, :, ::-1] / 255.0 -
                   np.array(rgb_mean)) / np.array(rgb_std)
        img_inp = torch.from_numpy(img_inp).unsqueeze(0).permute(
            0, 3, 1, 2).contiguous().float().to(device).half()
        tic = time.time()
        predict = net(img_inp)["predicts"][0]
        duration = time.time() - tic
        time_logger.update(duration)
        pbar.set_description("fps:{:4.2f}".format(1 / time_logger.avg()))
        if predict is None:
            continue
        predict[:, [0, 2]] = ((predict[:, [0, 2]] - left) / ratio).clamp(min=0,
                                                                         max=w)
        predict[:, [1, 3]] = ((predict[:, [1, 3]] - top) / ratio).clamp(min=0,
                                                                        max=h)
        box = predict.cpu().numpy()
        coco_box = box[:, :4]
        coco_box[:, 2:] = coco_box[:, 2:] - coco_box[:, :2]
        for p, b in zip(box.tolist(), coco_box.tolist()):
            coco_predict_list.append({
                'image_id': img_id,
                'category_id': coco_ids[int(p[5])],
                'bbox': [round(x, 3) for x in b],
                'score': round(p[4], 5)
            })
    with open("predicts.json", 'w') as file:
        json.dump(coco_predict_list, file)
    coco_eavl(anno_path=data_cfg['val_annotation_path'],
              pred_path="predicts.json")
コード例 #3
0
ファイル: test.py プロジェクト: liangheming/retinanetv1
def write_coco_json():
    from pycocotools.coco import COCO
    device = torch.device("cuda:6")
    img_root = "/home/huffman/data/val2017"
    model = RetinaNet(backbone='resnet50')
    weights = torch.load("weights/0_retinanet_last.pth")['ema']
    model.load_state_dict(weights)
    model.to(device)
    model.eval()

    basic_transform = ScalePadding(target_size=(896, 896), padding_val=(103, 116, 123))
    coco = COCO("/home/huffman/data/annotations/instances_val2017.json")
    coco_predict_list = list()
    for img_id in tqdm(coco.imgs.keys()):
        file_name = coco.imgs[img_id]['file_name']
        img_path = os.path.join(img_root, file_name)
        img = cv.imread(img_path)
        # ori_img = img.copy()
        img, ratio, (left, top) = basic_transform.make_border(img)
        h, w = img.shape[:2]
        img_out = img[:, :, [2, 1, 0]].astype(np.float32) / 255.0
        img_out = ((img_out - np.array(rgb_mean)) / np.array(rgb_std)).transpose(2, 0, 1).astype(np.float32)
        img_out = torch.from_numpy(np.ascontiguousarray(img_out)).unsqueeze(0).float().to(device)
        predicts = model(img_out)
        for i in range(len(predicts)):
            predicts[i][:, [0, 2]] = predicts[i][:, [0, 2]].clamp(min=0, max=w)
            predicts[i][:, [1, 3]] = predicts[i][:, [1, 3]].clamp(min=0, max=h)
        box = non_max_suppression(predicts,
                                  conf_thresh=0.05,
                                  iou_thresh=0.5,
                                  max_det=300)[0]
        if box is None:
            continue
        box[:, [0, 2]] = (box[:, [0, 2]] - left) / ratio[0]
        box[:, [1, 3]] = (box[:, [1, 3]] - top) / ratio[1]
        box = box.detach().cpu().numpy()
        # ret_img = draw_box(ori_img, box[:, [4, 5, 0, 1, 2, 3]], colors=coco_colors)
        # cv.imwrite(file_name, ret_img)
        coco_box = box[:, :4]
        coco_box[:, 2:] = coco_box[:, 2:] - coco_box[:, :2]
        for p, b in zip(box.tolist(), coco_box.tolist()):
            coco_predict_list.append({'image_id': img_id,
                                      'category_id': coco_ids[int(p[5])],
                                      'bbox': [round(x, 3) for x in b],
                                      'score': round(p[4], 5)})
    with open("predicts.json", 'w') as file:
        json.dump(coco_predict_list, file)
コード例 #4
0
def valid_model():
    model = RetinaNet()
    weights = torch.load("weights/0_retinanet_free_anchor_last.pth")['ema']
    model.load_state_dict(weights)
    model.cuda().eval()
    predict_list = list()
    target_list = list()
    vdata = COCODataSets(img_root="/home/huffman/data/val2017",
                         annotation_path="/home/huffman/data/annotations/instances_val2017.json",
                         use_crowd=False,
                         augments=False,
                         debug=False,
                         remove_blank=False,
                         img_size=640
                         )
    vloader = DataLoader(dataset=vdata,
                         batch_size=4,
                         num_workers=4,
                         collate_fn=vdata.collate_fn,
                         shuffle=False
                         )
    pbar = tqdm(vloader)
    for img_tensor, targets_tensor, _ in pbar:
        _, _, h, w = img_tensor.shape
        targets_tensor[:, 3:] = targets_tensor[:, 3:] * torch.tensor(data=[w, h, w, h])
        img_tensor = img_tensor.cuda()
        targets_tensor = targets_tensor.cuda()
        predicts = model(img_tensor)

        for i in range(len(predicts)):
            predicts[i][:, [0, 2]] = predicts[i][:, [0, 2]].clamp(min=0, max=w)
            predicts[i][:, [1, 3]] = predicts[i][:, [1, 3]].clamp(min=0, max=h)
        predicts = non_max_suppression(predicts,
                                       conf_thresh=0.05,
                                       iou_thresh=0.5,
                                       max_det=300)
        for i, predict in enumerate(predicts):
            predict_list.append(predict)
            targets_sample = targets_tensor[targets_tensor[:, 0] == i][:, 2:]
            target_list.append(targets_sample)
    mp, mr, map50, map = coco_map(predict_list, target_list)
    print(map50, map)
コード例 #5
0
    def __init__(self, cfg_path):
        with open(cfg_path, 'r') as rf:
            self.cfg = yaml.safe_load(rf)
        self.data_cfg = self.cfg['data']
        self.model_cfg = self.cfg['model']
        self.optim_cfg = self.cfg['optim']
        self.hyper_params = self.cfg['hyper_params']
        self.val_cfg = self.cfg['val']
        print(self.data_cfg)
        print(self.model_cfg)
        print(self.optim_cfg)
        print(self.hyper_params)
        print(self.val_cfg)
        os.environ['CUDA_VISIBLE_DEVICES'] = self.cfg['gpus']
        dist.init_process_group(backend='nccl')
        self.tdata = COCODataSets(
            img_root=self.data_cfg['train_img_root'],
            annotation_path=self.data_cfg['train_annotation_path'],
            img_size=self.data_cfg['img_size'],
            debug=self.data_cfg['debug'],
            use_crowd=self.data_cfg['use_crowd'],
            augments=True,
            remove_blank=self.data_cfg['remove_blank'])
        self.tloader = DataLoader(dataset=self.tdata,
                                  batch_size=self.data_cfg['batch_size'],
                                  num_workers=self.data_cfg['num_workers'],
                                  collate_fn=self.tdata.collate_fn,
                                  sampler=DistributedSampler(
                                      dataset=self.tdata, shuffle=True))
        self.vdata = COCODataSets(
            img_root=self.data_cfg['val_img_root'],
            annotation_path=self.data_cfg['val_annotation_path'],
            img_size=self.data_cfg['img_size'],
            debug=self.data_cfg['debug'],
            use_crowd=self.data_cfg['use_crowd'],
            augments=False,
            remove_blank=False)
        self.vloader = DataLoader(dataset=self.vdata,
                                  batch_size=self.data_cfg['batch_size'],
                                  num_workers=self.data_cfg['num_workers'],
                                  collate_fn=self.vdata.collate_fn,
                                  sampler=DistributedSampler(
                                      dataset=self.vdata, shuffle=False))
        print("train_data: ", len(self.tdata), " | ", "val_data: ",
              len(self.vdata), " | ", "empty_data: ",
              self.tdata.empty_images_len)
        print("train_iter: ", len(self.tloader), " | ", "val_iter: ",
              len(self.vloader))
        model = RetinaNet(
            num_cls=self.model_cfg['num_cls'],
            anchor_sizes=self.model_cfg['anchor_sizes'],
            strides=self.model_cfg['strides'],
            backbone=self.model_cfg['backbone'],
        )
        if self.model_cfg.get("backbone_weight", None):
            weights = torch.load(self.model_cfg['backbone_weight'])
            model.load_backbone_weighs(weights)
        self.best_map = 0.
        self.best_map50 = 0.
        optimizer = split_optimizer(model, self.optim_cfg)
        local_rank = dist.get_rank()
        self.local_rank = local_rank
        self.device = torch.device("cuda", local_rank)
        model.to(self.device)
        model, optimizer = amp.initialize(model,
                                          optimizer,
                                          opt_level='O1',
                                          verbosity=0)
        if self.optim_cfg['sync_bn']:
            model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
        self.model = nn.parallel.distributed.DistributedDataParallel(
            model, device_ids=[local_rank], output_device=local_rank)
        self.optimizer = optimizer
        self.ema = ModelEMA(self.model)

        beta = eval(self.hyper_params['beta']) if isinstance(self.hyper_params['beta'], str) \
            else self.hyper_params['beta']

        self.creterion = RetinaAnchorFreeLoss(
            alpha=self.hyper_params['alpha'],
            gamma=self.hyper_params['gamma'],
            beta=beta,
            top_k=self.hyper_params['top_k'],
            box_iou_thresh=self.hyper_params['box_iou_thresh'],
            box_reg_weight=self.hyper_params['box_reg_weight'])
        self.lr_adjuster = WarmUpCosineDecayMultiStepLRAdjust(
            init_lr=self.optim_cfg['lr'],
            milestones=self.optim_cfg['milestones'],
            warm_up_epoch=self.optim_cfg['warm_up_epoch'],
            iter_per_epoch=len(self.tloader),
            epochs=self.optim_cfg['epochs'],
            cosine_weights=self.optim_cfg['cosine_weights'])
コード例 #6
0
from losses.retina_anchor_free import RetinaAnchorFreeLoss
from datasets.coco import COCODataSets
from torch.utils.data.dataloader import DataLoader

if __name__ == '__main__':
    dataset = COCODataSets(
        img_root="/home/huffman/data/val2017",
        annotation_path="/home/huffman/data/annotations/instances_val2017.json",
        use_crowd=True,
        augments=True,
        remove_blank=True,
        img_size=640)
    dataloader = DataLoader(dataset=dataset,
                            batch_size=4,
                            shuffle=True,
                            num_workers=4,
                            collate_fn=dataset.collate_fn)
    net = RetinaNet(backbone="resnet18")
    creterion = RetinaAnchorFreeLoss()
    for img_input, targets, _ in dataloader:
        _, _, h, w = img_input.shape
        targets[:, 3:] = targets[:, 3:] * torch.tensor(data=[w, h, w, h])
        cls_outputs, reg_outputs, anchors = net(img_input)
        total_loss, detail_loss, pos_num = creterion(cls_outputs, reg_outputs,
                                                     anchors, targets)
        # creterion(cls_outputs, reg_outputs, anchors, targets)
        cls_loss, reg_loss = detail_loss
        print(total_loss)
        print(cls_loss, reg_loss, pos_num)
        break
コード例 #7
0
    def __init__(self, cfg_path):
        with open(cfg_path, 'r') as rf:
            self.cfg = yaml.safe_load(rf)
        self.data_cfg = self.cfg['data']
        self.model_cfg = self.cfg['model']
        self.optim_cfg = self.cfg['optim']
        self.hyper_params = self.cfg['hyper_params']
        self.val_cfg = self.cfg['val']
        print(self.data_cfg)
        print(self.model_cfg)
        print(self.optim_cfg)
        print(self.hyper_params)
        print(self.val_cfg)

        self.tdata = COCODataSets(
            img_root=self.data_cfg['train_img_root'],
            annotation_path=self.data_cfg['train_annotation_path'],
            img_size=self.data_cfg['img_size'],
            debug=self.data_cfg['debug'],
            use_crowd=self.data_cfg['use_crowd'],
            augments=True,
            remove_blank=self.data_cfg['remove_blank'])
        self.tloader = DataLoader(dataset=self.tdata,
                                  batch_size=self.data_cfg['batch_size'],
                                  num_workers=self.data_cfg['num_workers'],
                                  collate_fn=self.tdata.collate_fn,
                                  shuffle=True)
        self.vdata = COCODataSets(
            img_root=self.data_cfg['val_img_root'],
            annotation_path=self.data_cfg['val_annotation_path'],
            img_size=self.data_cfg['img_size'],
            debug=self.data_cfg['debug'],
            use_crowd=self.data_cfg['use_crowd'],
            augments=False,
            remove_blank=False)
        self.vloader = DataLoader(dataset=self.vdata,
                                  batch_size=self.data_cfg['batch_size'],
                                  num_workers=self.data_cfg['num_workers'],
                                  collate_fn=self.vdata.collate_fn,
                                  shuffle=False)
        print("train_data: ", len(self.tdata), " | ", "val_data: ",
              len(self.vdata), " | ", "empty_data: ",
              self.tdata.empty_images_len)
        print("train_iter: ", len(self.tloader), " | ", "val_iter: ",
              len(self.vloader))
        model = RetinaNet(
            num_cls=self.model_cfg['num_cls'],
            anchor_sizes=self.model_cfg['anchor_sizes'],
            strides=self.model_cfg['strides'],
            backbone=self.model_cfg['backbone'],
        )
        if self.model_cfg.get("backbone_weight", None):
            weights = torch.load(self.model_cfg['backbone_weight'])
            model.load_backbone_weighs(weights)
        self.best_map = 0.
        self.best_map50 = 0.
        optimizer = split_optimizer(model, self.optim_cfg)

        self.local_rank = 0
        self.device = torch.device("cuda:0")
        model.to(self.device)

        self.model = model
        self.optimizer = optimizer
        self.ema = ModelEMA(self.model)

        beta = eval(self.hyper_params['beta']) if isinstance(self.hyper_params['beta'], str) \
            else self.hyper_params['beta']

        self.creterion = RetinaAnchorFreeLoss(
            alpha=self.hyper_params['alpha'],
            gamma=self.hyper_params['gamma'],
            beta=beta,
            top_k=self.hyper_params['top_k'],
            box_iou_thresh=self.hyper_params['box_iou_thresh'],
            box_reg_weight=self.hyper_params['box_reg_weight'])
        self.lr_adjuster = WarmUpCosineDecayMultiStepLRAdjust(
            init_lr=self.optim_cfg['lr'],
            milestones=self.optim_cfg['milestones'],
            warm_up_epoch=self.optim_cfg['warm_up_epoch'],
            iter_per_epoch=len(self.tloader),
            epochs=self.optim_cfg['epochs'],
            cosine_weights=self.optim_cfg['cosine_weights'])
コード例 #8
0
ファイル: ddp_mix_solver.py プロジェクト: liangheming/paav1
 def __init__(self, cfg_path):
     with open(cfg_path, 'r') as rf:
         self.cfg = yaml.safe_load(rf)
     self.data_cfg = self.cfg['data']
     self.model_cfg = self.cfg['model']
     self.optim_cfg = self.cfg['optim']
     self.val_cfg = self.cfg['val']
     print(self.data_cfg)
     print(self.model_cfg)
     print(self.optim_cfg)
     print(self.val_cfg)
     os.environ['CUDA_VISIBLE_DEVICES'] = self.cfg['gpus']
     self.gpu_num = len(self.cfg['gpus'].split(','))
     dist.init_process_group(backend='nccl')
     self.tdata = COCODataSets(
         img_root=self.data_cfg['train_img_root'],
         annotation_path=self.data_cfg['train_annotation_path'],
         max_thresh=self.data_cfg['max_thresh'],
         debug=self.data_cfg['debug'],
         use_crowd=self.data_cfg['use_crowd'],
         augments=True,
         remove_blank=self.data_cfg['remove_blank'])
     self.tloader = DataLoader(dataset=self.tdata,
                               batch_size=self.data_cfg['batch_size'],
                               num_workers=self.data_cfg['num_workers'],
                               collate_fn=self.tdata.collect_fn,
                               sampler=DistributedSampler(
                                   dataset=self.tdata, shuffle=True))
     self.vdata = COCODataSets(
         img_root=self.data_cfg['val_img_root'],
         annotation_path=self.data_cfg['val_annotation_path'],
         max_thresh=self.data_cfg['max_thresh'],
         debug=self.data_cfg['debug'],
         use_crowd=self.data_cfg['use_crowd'],
         augments=False,
         remove_blank=False)
     self.vloader = DataLoader(dataset=self.vdata,
                               batch_size=self.data_cfg['batch_size'],
                               num_workers=self.data_cfg['num_workers'],
                               collate_fn=self.vdata.collect_fn,
                               sampler=DistributedSampler(
                                   dataset=self.vdata, shuffle=False))
     print("train_data: ", len(self.tdata), " | ", "val_data: ",
           len(self.vdata), " | ", "empty_data: ",
           self.tdata.empty_images_len)
     print("train_iter: ", len(self.tloader), " | ", "val_iter: ",
           len(self.vloader))
     model = RetinaNet(**self.model_cfg)
     self.best_map = 0.
     optimizer = split_optimizer(model, self.optim_cfg)
     local_rank = dist.get_rank()
     self.local_rank = local_rank
     self.device = torch.device("cuda", local_rank)
     model.to(self.device)
     if self.optim_cfg['sync_bn']:
         model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
     self.model = nn.parallel.distributed.DistributedDataParallel(
         model, device_ids=[local_rank], output_device=local_rank)
     self.scaler = amp.GradScaler(
         enabled=True) if self.optim_cfg['amp'] else None
     self.optimizer = optimizer
     self.ema = ModelEMA(self.model)
     self.lr_adjuster = IterWarmUpCosineDecayMultiStepLRAdjust(
         init_lr=self.optim_cfg['lr'],
         milestones=self.optim_cfg['milestones'],
         warm_up_epoch=self.optim_cfg['warm_up_epoch'],
         iter_per_epoch=len(self.tloader),
         epochs=self.optim_cfg['epochs'],
     )
     self.cls_loss_logger = AverageLogger()
     self.box_loss_logger = AverageLogger()
     self.iou_loss_logger = AverageLogger()
     self.match_num_logger = AverageLogger()
     self.loss_logger = AverageLogger()
コード例 #9
0
import torch
from nets.retinanet import RetinaNet
from datasets.coco import COCODataSets
from torch.utils.data.dataloader import DataLoader

if __name__ == '__main__':
    dataset = COCODataSets(
        img_root="/home/huffman/data/val2017",
        annotation_path="/home/huffman/data/annotations/instances_val2017.json",
        use_crowd=True,
        augments=True,
        remove_blank=True,
        max_thresh=640)
    dataloader = DataLoader(dataset=dataset,
                            batch_size=4,
                            shuffle=True,
                            num_workers=1,
                            collate_fn=dataset.collect_fn)
    net = RetinaNet(**{"dist_train": False})
    for img_input, targets, batch_len in dataloader:
        ret = net(img_input,
                  targets={
                      "target": targets,
                      "batch_len": batch_len
                  })
        print(ret)
        break