Esempio n. 1
0
    def __init__(self, configer):
        self.configer = configer

        if self.configer.get('data', 'image_tool') == 'pil':
            self.aug_train_transform = pil_aug_trans.PILAugCompose(
                self.configer, split='train')
        elif self.configer.get('data', 'image_tool') == 'cv2':
            self.aug_train_transform = cv2_aug_trans.CV2AugCompose(
                self.configer, split='train')
        else:
            Log.error('Not support {} image tool.'.format(
                self.configer.get('data', 'image_tool')))
            exit(1)

        if self.configer.get('data', 'image_tool') == 'pil':
            self.aug_val_transform = pil_aug_trans.PILAugCompose(self.configer,
                                                                 split='val')
        elif self.configer.get('data', 'image_tool') == 'cv2':
            self.aug_val_transform = cv2_aug_trans.CV2AugCompose(self.configer,
                                                                 split='val')
        else:
            Log.error('Not support {} image tool.'.format(
                self.configer.get('data', 'image_tool')))
            exit(1)

        self.img_transform = trans.Compose([
            trans.ToTensor(),
            trans.Normalize(**self.configer.get('data', 'normalize')),
        ])

        self.label_transform = trans.Compose([
            trans.ToLabel(),
            trans.ReLabel(255, -1),
        ])
Esempio n. 2
0
def im_detect_bbox(model, images, target_scale, target_max_size, device):
    """
    Performs bbox detection on the original image.
    """
    transform = TT.Compose([
        T.Resize(target_scale, target_max_size),
        TT.ToTensor(),
        T.Normalize(mean=cfg.INPUT.PIXEL_MEAN,
                    std=cfg.INPUT.PIXEL_STD,
                    to_bgr255=cfg.INPUT.TO_BGR255)
    ])
    images = [transform(image) for image in images]
    images = to_image_list(images, cfg.DATALOADER.SIZE_DIVISIBILITY)
    return model(images.to(device))
Esempio n. 3
0
def im_detect_bbox_hflip(model, images, target_scale, target_max_size, device):
    """
    Performs bbox detection on the horizontally flipped image.
    Function signature is the same as for im_detect_bbox.
    """
    transform = TT.Compose([
        T.Resize(target_scale, target_max_size),
        TT.RandomHorizontalFlip(1.0),
        TT.ToTensor(),
        T.Normalize(mean=cfg.INPUT.PIXEL_MEAN,
                    std=cfg.INPUT.PIXEL_STD,
                    to_bgr255=cfg.INPUT.TO_BGR255)
    ])
    images = [transform(image) for image in images]
    images = to_image_list(images, cfg.DATALOADER.SIZE_DIVISIBILITY)
    boxlists = model(images.to(device))

    # Invert the detections computed on the flipped image
    boxlists_inv = [boxlist.transpose(0) for boxlist in boxlists]
    return boxlists_inv
Esempio n. 4
0
def main(cfg):
    util.init_random_seed(cfg.seed)
    os.environ['CUDA_VISIBLE_DEVICES'] = cfg.gpu_devices
    use_gpu = torch.cuda.is_available()
    if cfg.use_cpu:
        use_gpu = False

    cfg.ckpt_dir = "test" if cfg.evaluate else cfg.ckpt_dir
    cfg.save_dir = osp.join(cfg.save_dir, cfg.ckpt_dir)
    util.mkdir_if_missing(cfg.save_dir)

    if not cfg.evaluate:
        sys.stdout = util.Logger(osp.join(cfg.save_dir, 'log_train.txt'))
    else:
        sys.stdout = util.Logger(osp.join(cfg.save_dir, 'log_test.txt'))
    print("==========\nArgs:{}\n==========".format(cfg))

    if use_gpu:
        print("Currently using GPU {}".format(cfg.gpu_devices))
        cudnn.benchmark = True
        torch.cuda.manual_seed_all(cfg.seed)
    else:
        print("Currently using CPU (GPU is highly recommended)")

    # --------------------------------------------------------------------------------------------
    print("* Initializing dataset {}".format(cfg.dataset))
    dataset = getdata.init_dataset(name=cfg.dataset)
    cfg.num_train_pids = dataset.num_train_pids

    transform_train = T.Compose([
        T.Random2DTranslation(cfg.height, cfg.width),
        T.RandomHorizontalFlip(),
        T.ToTensor(),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])

    transform_test = T.Compose([
        T.Resize((cfg.height, cfg.width)),
        T.ToTensor(),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])

    pin_memory = True if use_gpu else False

    trainloader = DataLoader(
        # train_set,
        VideoDataset(dataset.train, seq_len=cfg.seq_len, sample=cfg.train_sample_method, transform=transform_train),
        sampler=RandomIdentitySampler(dataset.train, batch_size=cfg.train_batch, num_instances=cfg.num_instances),
        batch_size=cfg.train_batch, num_workers=cfg.workers,
        pin_memory=pin_memory, drop_last=True,
    )

    queryloader = DataLoader(
        # query_set,
        VideoDataset(dataset.query, seq_len=cfg.seq_len, sample=cfg.test_sample_method, transform=transform_test),
        batch_size=cfg.test_batch, shuffle=False, num_workers=0,
        pin_memory=pin_memory, drop_last=False,
    )

    galleryloader = DataLoader(
        # gallery_set,
        VideoDataset(dataset.gallery, seq_len=cfg.seq_len, sample=cfg.test_sample_method, transform=transform_test),
        batch_size=cfg.test_batch, shuffle=False, num_workers=0,
        pin_memory=pin_memory, drop_last=False,
    )

    # --------------------------------------------------------------------------------------------
    # Initialize model, optimizer, and scheduler
    print("* Initializing model: {}".format(cfg.arch))
    model = get_model(cfg)
    print("Model size: {:.2f}M".format(sum(p.numel() for p in model.parameters()) / 1000000.0))

    criterion_xent = CrossEntropyLabelSmooth(num_classes=dataset.num_train_pids, use_gpu=use_gpu)
    criterion_htri = TripletLoss(margin=cfg.margin)

    optimizer = torch.optim.Adam(model.parameters(), lr=cfg.lr, weight_decay=cfg.weight_decay)
    assert cfg.stepsize > 0
    scheduler = lr_scheduler.StepLR(optimizer, step_size=cfg.stepsize, gamma=cfg.gamma, last_epoch=-1)
    if cfg.warmup_epoch > 0:
        scheduler = WarmUpLR(optimizer, scheduler, cfg.warmup_epoch, len(trainloader))

    # --------------------------------------------------------------------------------------------
    # optionally resume from a checkpoint
    best_rank1 = -np.inf
    start_epoch = cfg.start_epoch
    if cfg.resume:
        checkpoint = torch.load(cfg.resume)
        start_epoch = checkpoint['epoch'] + 1
        best_rank1 = checkpoint['rank1']
        model.load_state_dict(checkpoint['state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        scheduler = lr_scheduler.StepLR(optimizer, step_size=cfg.stepsize, gamma=cfg.gamma,
                                        last_epoch=checkpoint['epoch'])
        print("loaded checkpoint '{}' (epoch {})".format(cfg.resume, checkpoint['epoch']))
        del checkpoint

    if use_gpu:
        model = nn.DataParallel(model).cuda()

    if cfg.evaluate:
        print("* Evaluating")
        with torch.no_grad():
            evaluate(model, queryloader, galleryloader, cfg.pool, use_gpu)
        return

    if cfg.arch == '3d':
        torch.backends.cudnn.benchmark = False

    # --------------------------------------------------------------------------------------------
    print("\n* Start training")
    start_time = time.time()
    for epoch in range(start_epoch, cfg.max_epoch):
        epoch_start_time = time.time()
        update_lr(scheduler, epoch, n_iter=None)
        train_one_epoch(
            model,
            epoch,
            optimizer,
            scheduler,
            trainloader,
            cfg.warmup_epoch,
            criterion_xent,
            criterion_htri,
            use_gpu
        )

        lr_msg = 'used lr: '
        for item in [pg['lr'] for pg in optimizer.param_groups]:
            lr_msg += '%.0E ' % (item)
        print('* end of epoch {}/{}, time taken: {:.0f} sec, {}'.format(
            epoch, cfg.max_epoch, time.time() - epoch_start_time, lr_msg))

        # scheduler.step(epoch + 1)  # setting lr for next epoch, self.last_epoch==epoch+1

        if epoch in cfg.eval_steps or (epoch + 1) == cfg.max_epoch:
            print("* evaluate")
            with torch.no_grad():
                rank1 = eval(model, queryloader, galleryloader, use_gpu)
            is_best = rank1 > best_rank1
            if is_best: best_rank1 = rank1

            if use_gpu:
                state_dict = model.module.state_dict()
            else:
                state_dict = model.state_dict()
            util.save_checkpoint({
                'rank1': rank1,
                'epoch': epoch,
                'state_dict': state_dict,
                'optimizer': optimizer.state_dict(),
            }, is_best, osp.join(cfg.save_dir, 'latest.pth'))

    elapsed = round(time.time() - start_time)
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print("Checkpoints are saved to {}".format(cfg.save_dir))
    print("Finished. Total elapsed time (h:m:s): {}".format(elapsed))