Exemple #1
0
def test_random_resized_crop_size():
    image = np.ones((100, 100, 3))
    keypoints = [(0.2, 0.3, 0.6, 0.8), (0.3, 0.4, 0.7, 0.9, 99)]
    aug = RandomResizedCrop(height=50, width=50, p=1.0)
    transformed = aug(image=image, keypoints=keypoints)
    assert transformed["image"].shape == (50, 50, 3)
    assert len(keypoints) == len(transformed["keypoints"])
Exemple #2
0
def test_random_resized_crop_size():
    image = np.ones((100, 100, 3))
    bboxes = [[0.2, 0.3, 0.6, 0.8], [0.3, 0.4, 0.7, 0.9, 99]]
    aug = RandomResizedCrop(height=50, width=50, p=1.)
    transformed = aug(image=image, bboxes=bboxes)
    assert transformed['image'].shape == (50, 50, 3)
    assert len(bboxes) == len(transformed['bboxes'])
Exemple #3
0
def aug_preprocess(resize):
    return Compose([
        RandomResizedCrop(height=resize,
                          width=resize,
                          scale=(0.8, 1.0),
                          ratio=(4 / 5, 5 / 4)),
        RandomBrightnessContrast(0.1, 0.1),
        HorizontalFlip(p=0.5),
        Lambda(image=preprocess_input)
    ],
                   p=1)  # always apply augmentation
Exemple #4
0
    def transform(self, img: t.Any) -> t.Any:
        max_hw = max(img.shape[0:2])
        img = PadIfNeeded(max_hw, max_hw, border_mode=cv2.BORDER_REPLICATE)(image=img)[
            "image"
        ]
        img = Resize(self.resolution, self.resolution)(image=img)["image"]
        img = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225],)(
            image=img
        )["image"]

        if self.mode == "Train":
            img = RandomResizedCrop(self.resolution, self.resolution)(image=img)[
                "image"
            ]
            img = Cutout(p=0.2)(image=img)["image"]
            img = HorizontalFlip(p=0.5)(image=img)["image"]
            img = ShiftScaleRotate(
                shift_limit=0.1, scale_limit=0.0, rotate_limit=10, p=0.3
            )(image=img)["image"]
        img = ToTensorV2()(image=img)["image"]
        return img
 def train_dataloader(self):
     augmentations = Compose([
         RandomResizedCrop(
             height=self.hparams.sz,
             width=self.hparams.sz,
             scale=(0.7, 1.0),
         ),
         #         ToGray(),
         GridDistortion(),
         RandomBrightnessContrast(),
         ShiftScaleRotate(),
         Flip(p=0.5),
         CoarseDropout(
             max_height=int(self.hparams.sz / 10),
             max_width=int(self.hparams.sz / 10),
         ),
         Normalize(
             mean=[0.485, 0.456, 0.406],
             std=[0.229, 0.224, 0.225],
             max_pixel_value=255,
         ),
         ToTensorV2(),
     ])
     train_ds = MelanomaDataset(
         df=self.train_df,
         images_path=self.train_images_path,
         augmentations=augmentations,
         train_or_valid=True,
     )
     return DataLoader(
         train_ds,
         # sampler=sampler,
         batch_size=self.hparams.bs,
         shuffle=True,
         num_workers=os.cpu_count(),
         pin_memory=False,
     )
Exemple #6
0
def main():
    global args, config, best_loss
    args = parser.parse_args()

    with open(args.config) as f:
        config = yaml.load(f)

    for k, v in config['common'].items():
        setattr(args, k, v)
    config = EasyDict(config['common'])

    rank, world_size, device_id = dist_init(
        os.path.join(args.distributed_path, config.distributed_file))

    args.save_path_dated = args.save_path + '/' + args.datetime
    if args.run_tag != '':
        args.save_path_dated += '-' + args.run_tag

    # create model
    model = model_entry(config.model)
    model.cuda()

    model = nn.parallel.DistributedDataParallel(model, device_ids=[device_id])

    # create optimizer
    opt_config = config.optimizer
    opt_config.kwargs.lr = config.lr_scheduler.base_lr
    opt_config.kwargs.params = model.parameters()

    optimizer = optim_entry(opt_config)

    # optionally resume from a checkpoint
    last_iter = -1
    best_loss = 1e9
    if args.load_path:
        if args.recover:
            best_loss, last_iter = load_state(args.load_path,
                                              model,
                                              optimizer=optimizer)
        else:
            load_state(args.load_path, model)

    cudnn.benchmark = True

    # train augmentation
    if config.augmentation.get('imgnet_mean', False):
        model_mean = (0.485, 0.456, 0.406)
        model_std = (0.229, 0.224, 0.225)
    else:
        model_mean = (0.5, 0.5, 0.5)
        model_std = (0.5, 0.5, 0.5)
    trans = albumentations.Compose([
        RandomResizedCrop(config.augmentation.input_size,
                          config.augmentation.input_size,
                          scale=(config.augmentation.min_scale**2., 1.),
                          ratio=(1., 1.)),
        HorizontalFlip(p=0.5),
        RandomBrightnessContrast(brightness_limit=0.25,
                                 contrast_limit=0.1,
                                 p=0.5),
        JpegCompression(p=.2, quality_lower=50),
        MotionBlur(p=0.5),
        Normalize(mean=model_mean, std=model_std),
        ToTensorV2()
    ])

    train_dataset = FaceDataset(config.train_root,
                                config.train_source,
                                transform=trans,
                                resize=config.augmentation.input_size,
                                image_format=config.get('image_format', None),
                                random_frame=config.get(
                                    'train_random_frame', False),
                                bgr=config.augmentation.get('bgr', False))

    train_sampler = DistributedGivenIterationSampler(
        train_dataset,
        config.lr_scheduler.max_iter,
        config.batch_size,
        last_iter=last_iter)
    train_loader = DataLoader(train_dataset,
                              batch_size=config.batch_size,
                              shuffle=False,
                              num_workers=config.workers,
                              pin_memory=True,
                              sampler=train_sampler)

    # validation augmentation
    trans = albumentations.Compose([
        Resize(config.augmentation.input_size, config.augmentation.input_size),
        Normalize(mean=model_mean, std=model_std),
        ToTensorV2()
    ])
    val_multi_loader = []
    if args.val_source != '':
        for dataset_idx in range(len(args.val_source)):
            val_dataset = FaceDataset(
                args.val_root[dataset_idx],
                args.val_source[dataset_idx],
                transform=trans,
                output_index=True,
                resize=config.augmentation.input_size,
                image_format=config.get('image_format', None),
                bgr=config.augmentation.get('bgr', False))
            val_sampler = DistributedSampler(val_dataset, round_up=False)
            val_loader = DataLoader(val_dataset,
                                    batch_size=config.batch_size,
                                    shuffle=False,
                                    num_workers=config.workers,
                                    pin_memory=True,
                                    sampler=val_sampler)
            val_multi_loader.append(val_loader)

    config.lr_scheduler['optimizer'] = optimizer
    config.lr_scheduler['last_iter'] = last_iter
    lr_scheduler = get_scheduler(config.lr_scheduler)

    if rank == 0:
        mkdir(args.save_path)

        mkdir(args.save_path_dated)
        tb_logger = SummaryWriter(args.save_path_dated)

        logger = create_logger('global_logger',
                               args.save_path_dated + '-log.txt')
        logger.info('{}'.format(args))
        logger.info(model)
        logger.info(parameters_string(model))
        logger.info('len(train dataset) = %d' % len(train_loader.dataset))
        for dataset_idx in range(len(val_multi_loader)):
            logger.info(
                'len(val%d dataset) = %d' %
                (dataset_idx, len(val_multi_loader[dataset_idx].dataset)))

        mkdir(args.save_path_dated + '/saves')
    else:
        tb_logger = None

    positive_weight = config.get('positive_weight', 0.5)
    weight = torch.tensor([1. - positive_weight, positive_weight]) * 2.
    if rank == 0:
        logger.info('using class weights: {}'.format(weight.tolist()))

    criterion = nn.CrossEntropyLoss(weight=weight).cuda()

    if args.evaluate:
        if args.evaluate_path:
            all_ckpt = get_all_checkpoint(args.evaluate_path, args.range_list,
                                          rank)

            for ckpt in all_ckpt:
                if rank == 0:
                    logger.info('Testing ckpt: ' + ckpt)
                last_iter = -1
                _, last_iter = load_state(ckpt, model, optimizer=optimizer)
                for dataset_idx in range(len(val_multi_loader)):
                    validate(dataset_idx,
                             val_multi_loader[dataset_idx],
                             model,
                             criterion,
                             tb_logger,
                             curr_step=last_iter,
                             save_softmax=True)
        else:
            for dataset_idx in range(len(val_multi_loader)):
                validate(dataset_idx,
                         val_multi_loader[dataset_idx],
                         model,
                         criterion,
                         tb_logger,
                         curr_step=last_iter,
                         save_softmax=True)

        return

    train(train_loader, val_multi_loader, model, criterion, optimizer,
          lr_scheduler, last_iter + 1, tb_logger)
    return