Beispiel #1
0
train_augmentations = albu.Compose(
    [
        albu.RandomSizedCrop(
            min_max_height=(
                int(0.5 * (train_parameters["height_crop_size"])),
                int(2 * (train_parameters["height_crop_size"])),
            ),
            height=train_parameters["height_crop_size"],
            width=train_parameters["width_crop_size"],
            w2h_ratio=1.0,
            p=1,
        ),
        albu.ShiftScaleRotate(border_mode=cv2.BORDER_CONSTANT,
                              rotate_limit=10,
                              scale_limit=0,
                              p=0.5,
                              mask_value=ignore_index),
        albu.RandomBrightnessContrast(p=0.5),
        albu.RandomGamma(p=0.5),
        albu.ImageCompression(quality_lower=20, quality_upper=100, p=0.5),
        albu.GaussNoise(p=0.5),
        albu.Blur(p=0.5),
        albu.CoarseDropout(p=0.5, max_height=26, max_width=16),
        albu.OneOf([albu.HueSaturationValue(p=0.5),
                    albu.RGBShift(p=0.5)],
                   p=0.5),
        normalization,
    ],
    p=1,
)
Beispiel #2
0
    albumentations.Transpose(p=0.5),
    albumentations.Flip(p=0.5),
    albumentations.OneOf([
        albumentations.CLAHE(clip_limit=2),
        albumentations.IAASharpen(),
        albumentations.IAAEmboss(),
        albumentations.RandomBrightness(),
        albumentations.RandomContrast(),
        albumentations.JpegCompression(),
        albumentations.Blur(),
        albumentations.GaussNoise()
    ],
                         p=0.5),
    # albumentations.HueSaturationValue(p=0.5),
    albumentations.ShiftScaleRotate(shift_limit=0.15,
                                    scale_limit=0.15,
                                    rotate_limit=45,
                                    p=0.5),
    albumentations.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    # albumentations.ToFloat()
])

data_transforms_test = albumentations.Compose([
    albumentations.Resize(args.image_height, args.image_width),
    albumentations.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    # albumentations.ToFloat()
])

data_transforms_tta0 = albumentations.Compose([
    albumentations.Resize(args.image_height, args.image_width),
def load_data(fold: int, params: Dict[str, Any]) -> Any:
    torch.multiprocessing.set_sharing_strategy('file_system')
    cudnn.benchmark = True

    logger.info('Options:')
    logger.info(pprint.pformat(opt))

    full_df = pd.read_csv(opt.TRAIN.CSV)
    print('full_df', full_df.shape)
    train_df, val_df = train_val_split(full_df, fold)
    print('train_df', train_df.shape, 'val_df', val_df.shape)
    test_df = pd.read_csv(opt.TEST.CSV)

    # transform_train = transforms.Compose([
    #     # transforms.Resize((opt.MODEL.IMAGE_SIZE)), # smaller edge
    #     transforms.RandomCrop(opt.MODEL.INPUT_SIZE),
    #     transforms.RandomHorizontalFlip(),
    #     # transforms.ColorJitter(brightness=0.2, contrast=0.2),
    #     # transforms.RandomAffine(degrees=20, scale=(0.8, 1.2), shear=10, resample=PIL.Image.BILINEAR),
    #     # transforms.RandomCrop(opt.MODEL.INPUT_SIZE),
    # ])

    augs = []
    augs.append(albu.HorizontalFlip(.5))
    if int(params['vflip']):
        augs.append(albu.VerticalFlip(.5))
    if int(params['rotate90']):
        augs.append(albu.RandomRotate90())

    if params['affine'] == 'soft':
        augs.append(albu.ShiftScaleRotate(shift_limit=0.075, scale_limit=0.15, rotate_limit=10, p=.75))
    elif params['affine'] == 'medium':
        augs.append(albu.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2))
    elif params['affine'] == 'hard':
        augs.append(albu.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75))

    if float(params['noise']) > 0.1:
        augs.append(albu.OneOf([
            albu.IAAAdditiveGaussianNoise(),
            albu.GaussNoise(),
        ], p=float(params['noise'])))

    if float(params['blur']) > 0.1:
        augs.append(albu.OneOf([
            albu.MotionBlur(p=.2),
            albu.MedianBlur(blur_limit=3, p=0.1),
            albu.Blur(blur_limit=3, p=0.1),
        ], p=float(params['blur'])))

    if float(params['distortion']) > 0.1:
        augs.append(albu.OneOf([
            albu.OpticalDistortion(p=0.3),
            albu.GridDistortion(p=.1),
            albu.IAAPiecewiseAffine(p=0.3),
        ], p=float(params['distortion'])))

    if float(params['color']) > 0.1:
        augs.append(albu.OneOf([
            albu.CLAHE(clip_limit=2),
            albu.IAASharpen(),
            albu.IAAEmboss(),
            albu.RandomBrightnessContrast(),
        ], p=float(params['color'])))


    transform_train = albu.Compose([
        albu.RandomCrop(height=opt.MODEL.INPUT_SIZE, width=opt.MODEL.INPUT_SIZE),
        albu.Compose(augs, p=float(params['aug_global_prob']))
        ])

    transform_test = albu.Compose([
        # transforms.CenterCrop(opt.MODEL.INPUT_SIZE),
        albu.RandomCrop(height=opt.MODEL.INPUT_SIZE, width=opt.MODEL.INPUT_SIZE),
        albu.HorizontalFlip(),
    ])


    train_dataset = Dataset(train_df, path=opt.TRAIN.PATH, mode='train',
                            num_classes=opt.MODEL.NUM_CLASSES, resize=False,
                            augmentor=transform_train)

    val_dataset = Dataset(val_df, path=opt.TRAIN.PATH, mode='val',
                          # image_size=opt.MODEL.INPUT_SIZE,
                          num_classes=opt.MODEL.NUM_CLASSES, resize=False,
                          num_tta=1, # opt.TEST.NUM_TTAS,
                          augmentor=transform_test)
    test_dataset = Dataset(test_df, path=opt.TEST.PATH, mode='test',
                           # image_size=opt.MODEL.INPUT_SIZE,
                           num_classes=opt.MODEL.NUM_CLASSES, resize=False,
                           num_tta=opt.TEST.NUM_TTAS,
                           augmentor=transform_test)

    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=opt.TRAIN.BATCH_SIZE, shuffle=True,
        num_workers=opt.TRAIN.WORKERS)

    val_loader = torch.utils.data.DataLoader(
        val_dataset, batch_size=opt.TRAIN.BATCH_SIZE, shuffle=False, num_workers=opt.TRAIN.WORKERS)

    test_loader = torch.utils.data.DataLoader(
        test_dataset, batch_size=opt.TRAIN.BATCH_SIZE, shuffle=False, num_workers=opt.TRAIN.WORKERS)

    return train_loader, val_loader, test_loader
    g_truth = g_truth.reset_index()
    g_truth = g_truth.sort_values(by='id')

    return g_prob.drop('id', 1).values, g_truth['id'].values, g_truth.drop('id', 1).values


if __name__ == '__main__':

    with open('SETTINGS.json', 'r') as f:
        path_dict = json.load(f)

    # TTA
    valid_transform_aug = albumentations.Compose([

        albumentations.Flip(p=0.75),
        albumentations.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=90, interpolation=cv2.INTER_LINEAR,border_mode=cv2.BORDER_REFLECT_101, p=1),

        albumentations.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, p=1.0)
    ])

    csv_path = path_dict['KAGGLE_LABEL_TRAIN_DATA_CLEAN_PATH']
    df_all = pd.read_csv(csv_path)

    # load test images' name
    test_csv_path = path_dict['SUBMIT_CSV_PATH']
    df_test = pd.read_csv(test_csv_path)
    c_test = list(set(df_test['Id'].values.tolist()))

    is_aug = True
    num_aug = 5
    batch_size = 8
normalization = albu.Normalize(mean=mean, std=std, p=1)

train_augmentations = albu.Compose(
    [
        albu.RandomSizedCrop(
            min_max_height=(
                int(0.5 * (train_parameters["height_crop_size"])),
                int(2 * (train_parameters["height_crop_size"])),
            ),
            height=train_parameters["height_crop_size"],
            width=train_parameters["width_crop_size"],
            w2h_ratio=1.0,
            p=1,
        ),
        albu.ShiftScaleRotate(rotate_limit=20, scale_limit=0, p=0.5),
        albu.RandomBrightnessContrast(p=0.5),
        albu.RandomGamma(p=0.5),
        albu.HueSaturationValue(p=0.5),
        albu.HorizontalFlip(p=0.5),
        normalization,
    ],
    p=1,
)

val_augmentations = albu.Compose(
    [
        albu.PadIfNeeded(
            min_height=1024, min_width=2048, border_mode=cv2.BORDER_CONSTANT, mask_value=ignore_index, p=1
        ),
        normalization,
Beispiel #6
0
    RandomHorizontalFlip(0.5),
    ])  


tensor_transform = Compose([
    ToTensor(),
    Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])

    
albu_transform = A.Compose([
        A.LongestMaxSize(SIZE, interpolation=cv2.INTER_AREA),
        A.PadIfNeeded(SIZE, SIZE, border_mode=cv2.BORDER_CONSTANT),   
        
        A.OneOf([
            A.ShiftScaleRotate(scale_limit=0.2, shift_limit=0, rotate_limit=10, border_mode=cv2.BORDER_CONSTANT),
            A.ElasticTransform(sigma=10, alpha_affine=10, border_mode=cv2.BORDER_CONSTANT),
        ]),

        A.Cutout(),
        A.HorizontalFlip(),
        A.Normalize()
        ])


valid_transform = A.Compose([
        A.LongestMaxSize(SIZE, interpolation=cv2.INTER_AREA),
        A.PadIfNeeded(SIZE, SIZE, border_mode=cv2.BORDER_CONSTANT),       
        A.Normalize()
        ])
 
Beispiel #7
0
args['neck'] = 'option-D'
args['head'] = 'arc_margin'
args['pretrained_weights'] = None
args['optim'] = 'sgd'
args['batch_size'] = 32
args['n_splits'] = 5
args['fold'] = 0
args['seed'] = 9999
args['device'] = 'cuda:0'
args['out_dim'] = 1049
args['n_classes'] = 1000
args['class_weights'] = 'log'
args['class_weights_norm'] = 'batch'
args['normalization'] = 'imagenet'
args['crop_size'] = 448
args['tr_aug'] = A.Compose([
    A.HorizontalFlip(p=0.5),
    A.ImageCompression(quality_lower=99, quality_upper=100),
    A.ShiftScaleRotate(shift_limit=0.2,
                       scale_limit=0.2,
                       rotate_limit=10,
                       border_mode=0,
                       p=0.7),
    A.Cutout(max_h_size=int(256 * 0.4),
             max_w_size=int(256 * 0.4),
             num_holes=1,
             p=0.5),
])
args['val_aug'] = A.Compose([
    A.ImageCompression(quality_lower=99, quality_upper=100),
])
Beispiel #8
0
    def __init__(self, hparams: Dict):
        super(AircraftClassifier, self).__init__()

        self.journal = logging.getLogger(__name__)

        self.hparams = hparams
        self.config = from_dict(AircraftClassifierConfig, hparams)

        self.random_state = RandomState(self.config.random_state_seed)

        #
        # Statistics
        #
        self.epoch_fit_metrics = None
        self.epoch_samples: Dict[str, ReservoirSamplingMeter] = {}
        self.epoch_predictions: Dict[str, PredictionMeter] = {}

        self.training_outputs = []

        #
        # Model
        #
        self.target_classes = load_classes(self.config.classes_path)
        self.num_classes = len(self.target_classes)

        self.model = torch.hub.load(self.config.repo,
                                    self.config.network,
                                    pretrained=True,
                                    verbose=False)
        self.model.fc = nn.Linear(in_features=self.model.fc.in_features,
                                  out_features=self.num_classes)
        self.group_names, self.optimizer_params = self.adjust_model_params()

        self.train_accuracy = DiceAccuracy()
        self.train_loss = SoftLabelCrossEntropyLoss(
            smooth_factor=self.config.kv['x_lbs'],
            smooth_dist=torch.full([self.num_classes],
                                   fill_value=1.0 / self.num_classes))

        self.eval_accuracy = ExactMatchAccuracy()
        self.eval_loss = CrossEntropyLoss()

        #
        # Transforms
        #
        normalize_mean = [0.485, 0.456, 0.406]
        normalize_std = [0.229, 0.224, 0.225]

        mean_color = tuple([int(round(c * 255)) for c in normalize_mean])

        self.initial_transform = AlbumentationsTransform(
            albu.Compose([
                albu_ext.Rescale(scale=0.5, interpolation=cv2.INTER_AREA),
                albu_ext.PadToSquare(fill=mean_color)
            ]))

        self.train_transform_mix = albu.Compose([
            albu.ShiftScaleRotate(shift_limit=0.1,
                                  scale_limit=(-0.15, 0.15),
                                  rotate_limit=20,
                                  border_mode=cv2.BORDER_CONSTANT,
                                  value=mean_color,
                                  interpolation=cv2.INTER_AREA,
                                  p=1.0),
            albu.Resize(height=224, width=224, interpolation=cv2.INTER_AREA),
            albu.HorizontalFlip(p=0.5),
            albu.HueSaturationValue(hue_shift_limit=30,
                                    sat_shift_limit=30,
                                    val_shift_limit=30,
                                    p=1.0)
        ])

        self.train_transform_final = albu.Compose([
            albu.MultiplicativeNoise(multiplier=(0.9, 1.1), p=1.0),
            albu.CoarseDropout(max_holes=12,
                               max_height=12,
                               max_width=12,
                               min_holes=6,
                               min_height=6,
                               min_width=6,
                               fill_value=mean_color,
                               p=1.0)
        ])

        self.eval_transform = albu.Compose(
            [albu.Resize(height=224, width=224, interpolation=cv2.INTER_AREA)])

        self.model_transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=normalize_mean, std=normalize_std)
        ])

        self.sample_transform = torchviz_ext.InvNormalize(mean=normalize_mean,
                                                          std=normalize_std)

        #
        # DataLoaders
        #
        self.train_root_dataset = None
        self.train_orig_loader = None
        self.val_loader = None
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--local_rank", type=int, default=-1, help="local_rank for distributed training on gpus")
    args = parser.parse_args()
    torch.cuda.set_device(args.local_rank)
    device = torch.device("cuda", args.local_rank)
    torch.distributed.init_process_group(backend="nccl")
    args.device = device

    seed = 2001
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

    # prepare input
    import pickle
    with open('../process_input/splitall/image_list_train.pickle', 'rb') as f:
        image_list_train = pickle.load(f) 
    with open('../process_input/splitall/image_dict.pickle', 'rb') as f:
        image_dict = pickle.load(f) 
    with open('../lung_localization/splitall/bbox_dict_train.pickle', 'rb') as f:
        bbox_dict_train = pickle.load(f) 
    print(len(image_list_train), len(image_dict), len(bbox_dict_train))

    # hyperparameters
    learning_rate = 0.0004
    batch_size = 32
    image_size = 576
    num_epoch = 1

    # build model
    if args.local_rank != 0:
        torch.distributed.barrier()
    model = seresnext50()
    if args.local_rank == 0:
        torch.distributed.barrier()

    model.to(args.device)

    num_train_steps = int(len(image_list_train)/(batch_size*4)*num_epoch)   # 4 GPUs
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)
    scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=num_train_steps)
    model, optimizer = amp.initialize(model, optimizer, opt_level="O1",verbosity=0)
    model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank], output_device=args.local_rank, find_unused_parameters=True)
    criterion = nn.BCEWithLogitsLoss().to(args.device)

    # training
    train_transform = albumentations.Compose([
        albumentations.RandomContrast(limit=0.2, p=1.0),
        albumentations.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=20, border_mode=cv2.BORDER_CONSTANT, p=1.0),
        albumentations.Cutout(num_holes=2, max_h_size=int(0.4*image_size), max_w_size=int(0.4*image_size), fill_value=0, always_apply=True, p=1.0),
        albumentations.Normalize(mean=(0.456, 0.456, 0.456), std=(0.224, 0.224, 0.224), max_pixel_value=255.0, p=1.0)
    ])

    # iterator for training
    datagen = PEDataset(image_dict=image_dict, bbox_dict=bbox_dict_train, image_list=image_list_train, target_size=image_size, transform=train_transform)
    sampler = DistributedSampler(datagen)
    generator = DataLoader(dataset=datagen, sampler=sampler, batch_size=batch_size, num_workers=5, pin_memory=True)

    for ep in range(num_epoch):
        losses = AverageMeter()
        model.train()
        for j,(images,labels) in enumerate(generator):
            images = images.to(args.device)
            labels = labels.float().to(args.device)

            logits = model(images)
            loss = criterion(logits.view(-1),labels)
            losses.update(loss.item(), images.size(0))

            optimizer.zero_grad()
            with amp.scale_loss(loss, optimizer) as scaled_loss:
                scaled_loss.backward()
            optimizer.step()
            scheduler.step()

        if args.local_rank == 0:
            print('epoch: {}, train_loss: {}'.format(ep,losses.avg), flush=True)

        if args.local_rank == 0:
            out_dir = 'weights/'
            if not os.path.exists(out_dir):
                os.makedirs(out_dir)
            torch.save(model.module.state_dict(), out_dir+'epoch{}'.format(ep))
def create_dataloader_train_valid_test(train_batch_size=32,
                                       valid_batch_size=16,
                                       test_batch_size=16):

    common_transform_tensor = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        #transforms.Normalize(mean=[0.485, 0.456, 0.406],
        #                             std=[0.229, 0.224, 0.225])
    ])

    normalize_mask_transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        #transforms.Normalize(mean=[0.5],
        #                             std=[0.5])
    ])

    train_transform = albu.Compose([
        albu.RandomBrightnessContrast(p=0.5),
        albu.Blur(p=0.5),
        albu.HueSaturationValue(p=0.5),
        albu.ShiftScaleRotate(scale_limit=(-0.1, 0),
                              rotate_limit=20,
                              border_mode=0),
        albu.RGBShift(p=0.5),
        albu.CLAHE(p=0.5),
        albu.RandomGamma(p=0.5)
    ])

    train_dataset, train_sampler = load_dataset(
        root_dir=
        '/media/huxi/DATA/inf_master/Semester-5/Thesis/code/dataset/train/',
        class_name_to_id=class_name_to_id_,
        aug_transform_list=[
            common_transform_tensor, train_transform, normalize_mask_transform
        ])

    valid_dataset, valid_sampler = load_dataset(
        root_dir=
        '/media/huxi/DATA/inf_master/Semester-5/Thesis/code/dataset/valid/',
        class_name_to_id=class_name_to_id_,
        aug_transform_list=[
            common_transform_tensor, None, normalize_mask_transform
        ])

    test_dataset, test_sampler = load_dataset(
        root_dir=
        '/media/huxi/DATA/inf_master/Semester-5/Thesis/code/dataset/test/',
        class_name_to_id=class_name_to_id_,
        aug_transform_list=[
            common_transform_tensor, None, normalize_mask_transform
        ])

    train_data_loader = DataLoader(train_dataset,
                                   batch_size=train_batch_size,
                                   sampler=train_sampler,
                                   num_workers=8,
                                   pin_memory=True)
    valid_data_loader = DataLoader(valid_dataset,
                                   batch_size=valid_batch_size,
                                   sampler=valid_sampler,
                                   num_workers=8,
                                   pin_memory=True)

    test_data_loader = DataLoader(test_dataset,
                                  batch_size=test_batch_size,
                                  sampler=test_sampler,
                                  num_workers=8,
                                  pin_memory=True)

    dataloaders = {
        'train': train_data_loader,
        'valid': valid_data_loader,
        'test': test_data_loader,
    }
    return dataloaders
def main():
    parser = ArgumentParser(
        description="Script to do data augmentation tests with albumentations."
    )
    parser.add_argument("data_path", type=Path, help="Path to the dataset.")
    args = parser.parse_args()

    data_path: Path = args.data_path
    sizes = (512, 512)
    p_value = 1

    # Data augmentation done on cpu.
    pipeline = albumentations.Compose([
        albumentations.HorizontalFlip(p=0.5),
        albumentations.VerticalFlip(p=0.5),
        # albumentations.RandomRotate90(p=0.2),
        # albumentations.CLAHE(),
        albumentations.RandomBrightnessContrast(brightness_limit=0.1,
                                                contrast_limit=0.1,
                                                p=p_value),
        albumentations.HueSaturationValue(hue_shift_limit=10,
                                          sat_shift_limit=15,
                                          val_shift_limit=10,
                                          p=p_value),
        albumentations.ShiftScaleRotate(scale_limit=0.05,
                                        rotate_limit=10,
                                        shift_limit=0.06,
                                        p=p_value,
                                        border_mode=cv2.BORDER_CONSTANT,
                                        value=0),
        # albumentations.GridDistortion(p=0.5),
        albumentations.Normalize(mean=(0.041, 0.129, 0.03),
                                 std=(0.054, 0.104, 0.046),
                                 max_pixel_value=255.0,
                                 p=1.0),
        albumentations.Resize(*sizes, interpolation=cv2.INTER_LINEAR)
    ])

    # This assumes that the masks' paths contain either "mask" or "seg" (and that the main image does not).
    exts = [".jpg", ".png", ".tiff"]
    img_path_list = list([
        p for p in data_path.rglob('*')
        if p.suffix in exts and "seg" not in str(p) and "mask" not in str(p)
    ])

    nb_imgs = len(img_path_list)
    for i, img_path in enumerate(img_path_list, start=1):
        msg = f"Processing image {img_path.name}    ({i}/{nb_imgs})"
        print(
            msg + ' ' *
            (shutil.get_terminal_size(fallback=(156, 38)).columns - len(msg)),
            end='\r')

        img = cv2.imread(str(img_path))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        transformed = pipeline(image=img)
        aug_img = transformed["image"]

        show_img(img, "Original image")
        show_img(aug_img, "Augmented normalized image")
        denormalized_img = denormalize(aug_img,
                                       mean=(0.041, 0.129, 0.03),
                                       std=(0.054, 0.104, 0.046))
        show_img(denormalized_img, "Augmented denormalized image")
        self.augmentations = A.Compose(augmentations)
    
    def __call__(self, image):
        image = self.augmentations(image=image)['image']
        return image
        

preprocess = [
    A.CenterCrop(height=137, width=IMG_WIDTH),
    A.Resize(height=IMG_HEIGHT, width=IMG_WIDTH, always_apply=True),
]

augmentations = [
    A.PadIfNeeded(min_height=256, min_width=256, border_mode=cv2.BORDER_CONSTANT, value=[255, 255, 255], always_apply=True),
    A.imgaug.transforms.IAAAffine(shear=5, mode='constant', cval=255, always_apply=True),
    A.ShiftScaleRotate(rotate_limit=5, border_mode=cv2.BORDER_CONSTANT, value=[255, 255, 255], mask_value=[255, 255, 255], always_apply=True),
    A.RandomCrop(height=IMG_HEIGHT, width=IMG_WIDTH, always_apply=True),
]


train_transform = transforms.Compose([
    np.uint8,
    transforms.Lambda(lambda x: np.array([x, x, x]).transpose((1, 2, 0)) ),
    np.uint8,
    Albumentations(preprocess + augmentations),
    transforms.ToTensor(),
    transforms.Normalize(mean=MEAN, std=STD),
#     transforms.ToPILImage(),
])
valid_transform = transforms.Compose([
    np.uint8,
Beispiel #13
0
def get_aug(aug, min_area=0.0, min_visibility=0.0):
    return albumentations.Compose(
        aug,
        bbox_params={
            "format": "pascal_voc",
            "min_area": min_area,
            "min_visibility": min_visibility,
            "label_fields": ["category_id"],
        },
    )


train_augs = [
    albumentations.RandomBrightnessContrast(always_apply=True),
    albumentations.ShiftScaleRotate(rotate_limit=10, always_apply=True),
    albumentations.augmentations.transforms.HorizontalFlip(),
    # albumentations.augmentations.transforms.RandomCrop(448, 448, p=1.0),
]


class BBoxDataset(Dataset):
    def __init__(self, csv_file, size=500, type="train", aug=None, fastai_out=True):
        super(BBoxDataset).__init__()
        if csv_file is None:
            csv_file = get_one_sample_csv()
        self.df = pd.read_csv(csv_file, converters={"bbox": literal_eval})
        print("Dataset has {} samples.".format((self.df.shape[0])))
        if type == "train":
            self.aug = get_aug(train_augs) if aug is None else get_aug(aug)
        else:
        for ch in range(img.shape[2]):
            alpha = 1.0 + random.uniform(self.contrast_limit[0],
                                         self.contrast_limit[1])
            beta = 0.0 + random.uniform(self.brightness_limit[0],
                                        self.brightness_limit[1])
            img[..., ch] = brightness_contrast_adjust(img[..., ch], alpha,
                                                      beta)

        return img


albu_tfms = A.Compose([
    A.OneOf([
        A.ShiftScaleRotate(shift_limit=0.05,
                           scale_limit=0.1,
                           rotate_limit=15,
                           border_mode=cv2.BORDER_CONSTANT,
                           value=0),
        A.OpticalDistortion(distort_limit=0.11,
                            shift_limit=0.15,
                            border_mode=cv2.BORDER_CONSTANT,
                            value=0),
        A.NoOp()
    ]),
    A.OneOf([
        A.RandomBrightnessContrast(brightness_limit=0.5, contrast_limit=0.4),
        IndependentRandomBrightnessContrast(brightness_limit=0.25,
                                            contrast_limit=0.24),
        A.RandomGamma(gamma_limit=(50, 150)),
        A.NoOp()
    ]),
aug_types = albu.Compose([
    albu.HorizontalFlip(p=0.5),
    albu.OneOf([
        albu.RandomBrightnessContrast(),
        albu.RandomGamma(),
    ], p=0.3),
    albu.OneOf([
        albu.ElasticTransform(
            alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),
        albu.GridDistortion(),
        albu.OpticalDistortion(distort_limit=2, shift_limit=0.5),
    ],
               p=0.3),
    albu.ShiftScaleRotate(scale_limit=0.1,
                          rotate_limit=10,
                          shift_limit=0.1,
                          p=0.5,
                          border_mode=0),
])

#Péparation de la base de données
listmimg = os.listdir(brain_path)
listmMask = os.listdir(mask_path)

database = {'img': listmimg, 'mask': listmMask}
print("test", database)

df = pd.DataFrame(database)

df_train, df_val = train_test_split(df, train_size=0.75, random_state=107)
df_train.to_csv('df_train.csv.gz', compression='gzip', index=False)
Beispiel #16
0
            # A.ImageCompression(quality_lower=0.7, p=1),
            A.GaussianBlur(p=1),
        ],
        p=0.2,
    ),
    post_transform,
])

valid_transform_3 = valid_transform_1
test_transform_3 = test_transform_1

# crop 768 and very hard augs
train_transform_4 = A.Compose([
    A.ShiftScaleRotate(scale_limit=0.2,
                       rotate_limit=45,
                       border_mode=0,
                       value=0,
                       p=0.7),
    A.PadIfNeeded(768, 768, border_mode=0, value=0, p=1.),
    A.RandomCrop(768, 768, p=1.),
    A.Flip(p=0.75),
    A.Downscale(scale_min=0.5, scale_max=0.75, p=0.05),
    A.MaskDropout(max_objects=3, image_fill_value=0, mask_fill_value=0, p=0.1),

    # color transforms
    A.OneOf(
        [
            A.RandomBrightnessContrast(
                brightness_limit=0.3, contrast_limit=0.3, p=1),
            A.RandomGamma(gamma_limit=(70, 130), p=1),
            A.ChannelShuffle(p=0.2),
from tqdm import tqdm

from vision.utils.split_data import split_data

n_images = 500

bg_dir = '/home/mathias/Downloads/Linemod_preprocessed/data/01/rgb'
bgs = [os.path.join(bg_dir, f) for f in os.listdir(bg_dir)]

mask_path = 'vision/create_fence/data/fence_mask.png'

img_dir = 'vision/create_fence/data/mask_fence/images'
mask_dir = 'vision/create_fence/data/mask_fence/labels'

transforms = A.Compose([
    A.ShiftScaleRotate(p=1.0),
    A.HorizontalFlip(),
    A.VerticalFlip(),
    A.Transpose(),
    A.RandomRotate90(),
    A.OpticalDistortion(),
    A.GridDistortion(),
    A.Blur(),
    A.CLAHE(),
    A.RandomGamma(p=1.0),
    A.GaussNoise(p=1.0)
])


def rand_color():
    color = list(np.random.choice(range(10, 30), size=3))
Beispiel #18
0
    tfms = A.Compose([
        A.HorizontalFlip(),
        A.OneOf([
            A.RandomContrast(),
            A.RandomGamma(),
            A.RandomBrightness(),
        ],
                p=0.3),
        A.OneOf([
            A.ElasticTransform(
                alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),
            A.GridDistortion(),
            A.OpticalDistortion(distort_limit=2, shift_limit=0.5),
        ],
                p=0.3),
        A.ShiftScaleRotate(),
        A.Normalize(max_pixel_value=1.0,
                    mean=(0.521, 0.521, 0.521),
                    std=(0.304, 0.304, 0.304)),
        ToTensor(),
    ])

    params = {
        "batch_size":
        32,
        "stages": [{
            "epochs": 8,
            "lr": 0.001,
            "freeze_to": -1
        }, {
            "epochs": 8,
Beispiel #19
0
def get_augmentations(resize: int = 224, augmentation_intensity: Optional[str] = None) -> A.Compose:
    fix_seeds(24)
    crop_limits = (int(resize * 0.85), resize)

    if augmentation_intensity == "slight":
        p_augment = 0.15
        p_scale = 0.15
        p_blur = 0.05
        p_dropout = 0.05
        p_flip = 0.15
        p_noise = 0.15
        gauss_limit = 0.005

    elif augmentation_intensity == "light":
        p_augment = 0.25
        p_scale = 0.2
        p_blur = 0.1
        p_dropout = 0.05
        p_flip = 0.2
        p_noise = 0.2
        gauss_limit = 0.01

    elif augmentation_intensity == "medium":
        p_augment = 0.5
        p_scale = 0.2
        p_blur = 0.2
        p_dropout = 0.1
        p_flip = 0.2
        p_noise = 0.2
        gauss_limit = 0.015

    elif augmentation_intensity == "heavy":
        p_augment = 0.5
        p_scale = 0.35
        p_blur = 0.35
        p_dropout = 0.15
        p_flip = 0.35
        p_noise = 0.35
        gauss_limit = 0.02

    elif augmentation_intensity is None:
        return None
    else:
        raise ValueError("Improper augmentation flag: should be equal to None, light, medium, or heavraisey")

    augmentation = A.Compose(
        [
            A.OneOf(
                [A.HorizontalFlip(), A.VerticalFlip()],
                p=p_flip,
            ),
            A.OneOf(
                [A.Rotate(p=1.0, limit=30), A.RandomRotate90(p=1.0)],
                p=p_scale,
            ),
            A.OneOf(
                [
                    A.ShiftScaleRotate(p=1.0, rotate_limit=30),
                    A.RandomSizedCrop(
                        min_max_height=crop_limits,
                        height=resize,
                        width=resize,
                        w2h_ratio=1.0,
                        interpolation=cv2.INTER_CUBIC,
                    ),
                ],
                p=p_scale,
            ),
            A.Blur(blur_limit=3, p=p_blur),
            A.CoarseDropout(max_height=7, max_width=7, p=p_dropout),
            A.GaussNoise(var_limit=(0.0, gauss_limit), p=p_noise),
        ],
        p=p_augment,
    )

    return augmentation
test_df = pd.read_csv("sample_submission.csv")

bbox_df = pd.read_csv("bounding_boxes.csv")

RESIZE_H = 224
RESIZE_W = 224

data_transforms = albumentations.Compose([
    albumentations.Resize(RESIZE_H, RESIZE_W),
    albumentations.HorizontalFlip(),
    albumentations.OneOf([
        albumentations.RandomContrast(),
        albumentations.RandomBrightness(),
        albumentations.Blur()
    ]),
    albumentations.ShiftScaleRotate(rotate_limit=10, scale_limit=0.15),
    albumentations.JpegCompression(80),
    albumentations.HueSaturationValue(),
    albumentations.Normalize(),
    AT.ToTensor()
])

data_transforms_test = albumentations.Compose([
    albumentations.Resize(RESIZE_H, RESIZE_W),
    albumentations.Normalize(),
    AT.ToTensor()
])


# ## Set up
Beispiel #21
0
    torch.backends.cudnn.deterministic = True
    # the following line gives ~10% speedup
    # but may lead to some stochasticity in the results
    torch.backends.cudnn.benchmark = True


seed_everything(cfg.SEED)

tfms = alb.Compose([
    alb.HorizontalFlip(),
    alb.VerticalFlip(),
    alb.RandomRotate90(),
    alb.RandomBrightness(),
    alb.ShiftScaleRotate(shift_limit=0.0625,
                         scale_limit=0.2,
                         rotate_limit=15,
                         p=0.9,
                         border_mode=cv2.BORDER_REFLECT),
    alb.OneOf([
        alb.HueSaturationValue(10, 15, 10),
        alb.CLAHE(clip_limit=2),
        alb.RandomBrightnessContrast(),
    ],
              p=0.3),
],
                   p=0.8)

df_train = pd.read_csv(cfg.csv_path)
df_train['id'] = [x[:9] for x in df_train['id'].values]
if cfg.use_test:
    pass
Beispiel #22
0
import albumentations as A
image_size = 512

train_transforms = A.Compose(
    [  #flip horizontal, randomcrop, scaling, clahe, randombrightnesscontrast, shiftscalerotate, blur
        A.ShiftScaleRotate(p=0.1),
        A.HorizontalFlip(p=0.5),
        #A.RandomCrop(512, 512, p=0.05),
        A.CLAHE(p=0.5),
        A.OneOf([
            A.HueSaturationValue(hue_shift_limit=0.2,
                                 sat_shift_limit=0.2,
                                 val_shift_limit=0.2,
                                 p=0.5),
            A.RandomBrightnessContrast(
                brightness_limit=0.2, contrast_limit=0.2, p=0.5),
        ],
                p=0.5),
        A.OneOf(
            [A.MedianBlur(p=0.5), A.GaussianBlur(p=0.5)], p=0.5),
        A.Normalize(p=1.0),
        A.Resize(height=image_size, width=image_size, p=1)
    ],
    bbox_params=A.BboxParams(format='pascal_voc', min_visibility=0.9))

train_transforms_only_image = A.Compose(
    [  #flip horizontal, randomcrop, scaling, clahe, randombrightnesscontrast, shiftscalerotate, blur
        A.ShiftScaleRotate(p=0.1),
        A.HorizontalFlip(p=0.5),
        #A.RandomCrop(512, 512, p=0.05),
        A.CLAHE(p=0.5),
Beispiel #23
0
normalization = albu.Normalize(mean=mean, std=std, p=1)

train_augmentations = albu.Compose(
    [
        albu.RandomSizedCrop(
            min_max_height=(
                int(0.5 * (train_parameters["height_crop_size"])),
                int(2 * (train_parameters["height_crop_size"])),
            ),
            height=train_parameters["height_crop_size"],
            width=train_parameters["width_crop_size"],
            w2h_ratio=1.0,
            p=1,
        ),
        albu.ShiftScaleRotate(
            rotate_limit=20, scale_limit=0, p=0.5, mask_value=ignore_index),
        albu.RandomBrightnessContrast(p=0.5),
        albu.RandomGamma(p=0.5),
        albu.HueSaturationValue(p=0.5),
        albu.HorizontalFlip(p=0.5),
        normalization,
    ],
    p=1,
)

val_augmentations = albu.Compose(
    [
        albu.PadIfNeeded(min_height=1024,
                         min_width=2048,
                         border_mode=cv2.BORDER_CONSTANT,
                         mask_value=ignore_index,
Beispiel #24
0
    return LOGGER


EXP_ID = "exp48_densenet121_backborn_75epoch_1e-4"
LOGGER_PATH = f"logs/log_{EXP_ID}.txt"
setup_logger(out_file=LOGGER_PATH)
LOGGER.info("seed={}".format(SEED))

SIZE = 128
HEIGHT = 137
WIDTH = 236
OUT_DIR = 'models'

data_transforms_96 = albumentations.Compose([
    albumentations.ShiftScaleRotate(shift_limit=0.0625,
                                    scale_limit=0.1,
                                    rotate_limit=(5, 15),
                                    p=0.5),
    albumentations.CenterCrop(96, 96, p=1),
    albumentations.Resize(256, 256, p=1),
    albumentations.Cutout(num_holes=1, max_h_size=40, max_w_size=15, p=0.1),
    albumentations.OneOf([
        GridMask(num_grid=1, rotate=(5, 15), p=0.15),
        GridMask(num_grid=2, rotate=(5, 15), p=0.15),
    ],
                         p=0.1)
])

data_transforms_104 = albumentations.Compose([
    albumentations.ShiftScaleRotate(shift_limit=0.0625,
                                    scale_limit=0.1,
                                    rotate_limit=(5, 15),
Beispiel #25
0
def prepare_net(config, model, GCNModel, _use='train'):
    # img_size = (config['img_size'], config['img_size'])
    def worker_init_fn(worker_id):
        random.seed(config['seed'] + worker_id)

    if _use == 'train':
        if config['optim'] == 'Adam':
            optimizer = torch.optim.Adam(model.parameters(),
                                         config['lr'],
                                         weight_decay=config['weight_decay'])
            gcn_optimizer = torch.optim.Adam(
                GCNModel.parameters(),
                config['lr'],
                weight_decay=config['weight_decay'])

        if config['optim'] == 'RMSprop':
            optimizer = torch.optim.RMSprop(
                model.parameters(),
                config['lr'],
                weight_decay=config['weight_decay'])
            gcn_optimizer = torch.optim.RMSprop(
                GCNModel.parameters(),
                config['lr'],
                weight_decay=config['weight_decay'])

        elif config['optim'] == 'SGD':
            optimizer = torch.optim.SGD(model.parameters(),
                                        config['lr'],
                                        momentum=config['momentum'],
                                        weight_decay=config['weight_decay'],
                                        nesterov=config['nesterov'])
            gcn_optimizer = torch.optim.SGD(
                GCNModel.parameters(),
                config['lr'],
                momentum=config['momentum'],
                weight_decay=config['weight_decay'],
                nesterov=config['nesterov'])

        folds = [fold for fold in range(config['n_fold'])]
        train_dataset = CustomDataset(
            'train',
            config['DataRoot'],
            config['TrainFold'],
            None,
            transform=albumentations.Compose([
                albumentations.Resize(config['img_size'], config['img_size']),
                albumentations.OneOf(
                    [
                        # albumentations.RandomGamma(gamma_limit=(60, 120), p=0.9),
                        albumentations.RandomBrightnessContrast(
                            brightness_limit=0.05, contrast_limit=0.05, p=0.9),
                    ],
                    p=0.5),
                albumentations.OneOf(
                    [
                        albumentations.Blur(blur_limit=4, p=1),
                        # albumentations.MotionBlur(blur_limit=4, p=1),
                        #   albumentations.MedianBlur(
                        #       blur_limit=4, p=1)
                    ],
                    p=0.5),
                albumentations.HorizontalFlip(p=0.5),
                albumentations.ShiftScaleRotate(
                    shift_limit=0.01,
                    scale_limit=0.01,
                    rotate_limit=3,
                    interpolation=cv2.INTER_LINEAR,
                    border_mode=cv2.BORDER_CONSTANT,
                    p=0.5),
                #   albumentations.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0, always_apply=False, p=1.0)
            ]),
        )

        labeled_df = pd.read_csv(config['TrainFold'])
        labeled_fold = [i for i in config['label_fold']]
        labeled_df = labeled_df[labeled_df.fold_label.isin(labeled_fold)]
        labeled_fold_name = labeled_df.image.tolist()
        labeled_idxs, unlabeled_idxs = relabel_dataset(train_dataset,
                                                       labeled_fold_name)
        batch_sampler = TwoStreamBatchSampler(unlabeled_idxs, labeled_idxs,
                                              config['batchsize'],
                                              config['label_bs'])

        train_loader = torch.utils.data.DataLoader(
            train_dataset,
            num_workers=config['num_workers'],
            batch_sampler=batch_sampler,
            pin_memory=True,
            worker_init_fn=worker_init_fn)

        # Count different classes num in train dataset
        # all_label = np.array([label for _, _, label, _, _ in train_dataset])
        # class_sample_count = np.array([len(np.where(all_label == t)[0]) for t in np.unique(all_label)])
        # for index in range(len(config['Data_CLASSES'])):
        #     print("Train class {}: Num {}".format(index, class_sample_count[index]))

        valid_dataset = CustomDataset('valid',
                                      config['DataRoot'],
                                      config['ValidFold'],
                                      None,
                                      transform=albumentations.Compose([
                                          albumentations.Resize(
                                              config['img_size'],
                                              config['img_size']),
                                      ]))
        valid_loader = torch.utils.data.DataLoader(
            valid_dataset,
            batch_size=10,
            shuffle=False,
            num_workers=config['num_workers'],
            drop_last=False,
            pin_memory=True,
            worker_init_fn=worker_init_fn)

        # Count different classes num in valid dataset
        # all_label = np.array([label for _, _, label, _, _ in valid_dataset])
        # class_sample_count = np.array([len(np.where(all_label == t)[0]) for t in np.unique(all_label)])
        # for index in range(len(config['Data_CLASSES'])):
        #     print("Valid class {}: Num {}".format(index, class_sample_count[index]))

        return optimizer, gcn_optimizer, train_loader, valid_loader

    elif _use == 'infer':
        infer_dataset = CustomDataset('infer',
                                      config['DataRoot'],
                                      config['TestFold'],
                                      None,
                                      transform=albumentations.Compose([
                                          albumentations.Resize(
                                              config['img_size'],
                                              config['img_size']),
                                      ]))
        infer_loader = torch.utils.data.DataLoader(
            infer_dataset,
            batch_size=10,
            shuffle=False,
            num_workers=config['num_workers'],
            drop_last=False,
            pin_memory=True,
            worker_init_fn=worker_init_fn)

        return infer_loader
    alb.OneOf([
        alb.GridDistortion(num_steps=8, distort_limit=0.5, p=1.0),
        alb.OpticalDistortion(
            distort_limit=0.5,
            shift_limit=0.5,
            p=1.0,
        ),
        alb.ElasticTransform(alpha=3, p=1.0)
    ],
              p=0.5),
    alb.RandomResizedCrop(height=int(0.8192 * width_size),
                          width=width_size,
                          scale=(0.5, 1.5),
                          p=0.5),
    alb.ShiftScaleRotate(shift_limit=0.025,
                         scale_limit=0.1,
                         rotate_limit=20,
                         p=0.5),
    alb.CoarseDropout(max_holes=12,
                      min_holes=6,
                      max_height=int(0.8192 * width_size / 6),
                      max_width=int(width_size / 6),
                      min_height=int(0.8192 * width_size / 20),
                      min_width=int(width_size / 20),
                      p=0.5),
    alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
    ToTensorV2()
])
train_set = ImagesWithAnnotationsDataset(train_df,
                                         annot_df,
                                         train_image_transforms,
                                         '../ranzcr/train',
Beispiel #27
0
    def __init__(self,
                 image_paths,
                 labels=None,
                 train=True,
                 test=False,
                 aug=None,
                 use_onehot=False):
        self.paths = image_paths
        self.test = test
        self.use_onehot = use_onehot
        if self.test == False:
            self.labels = labels
        self.train = train
        # self.transform = albu.Compose([albu.HorizontalFlip(p=0.5),
        #                           albu.VerticalFlip(p=0.5),
        #                           albu.ShiftScaleRotate(rotate_limit=25.0, p=0.7),
        #                           albu.OneOf([albu.IAAEmboss(p=1),
        #                                  albu.IAASharpen(p=1),
        #                                  albu.Blur(p=1)], p=0.5),
        #                           albu.IAAPiecewiseAffine(p=0.5),
        #                           albu.Resize(545, 545, always_apply=True),
        #                           albu.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        #                           albu.pytorch.ToTensor(),
        #                           ])

        self.transform = []
        self.transform.append(albu.HorizontalFlip(p=0.5))
        self.transform.append(albu.VerticalFlip(p=0.5))
        if aug is not None and 'rt90' in aug:
            print('=> using rotate 90.')
            self.transform.append(albu.RandomRotate90(p=0.5))
        self.transform.append(albu.ShiftScaleRotate(rotate_limit=25.0, p=0.7))
        self.transform.append(
            albu.OneOf(
                [albu.IAAEmboss(p=1),
                 albu.IAASharpen(p=1),
                 albu.Blur(p=1)],
                p=0.5))
        self.transform.append(albu.IAAPiecewiseAffine(p=0.5))
        if aug is not None and 'cj' in aug:
            print('=> using color jittering.')
            self.transform.append(albu.RandomBrightness(p=0.5))
            self.transform.append(albu.HueSaturationValue(p=0.5))

        self.transform.append(albu.Resize(545, 545, always_apply=True))
        if aug is not None and 'autoaug' in aug:
            print('=> using autoaug.')
            self.transform.append(albuImageNetPolicy())
        self.transform.append(
            albu.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)))
        self.transform.append(albu.pytorch.ToTensor())
        self.transform = albu.Compose(self.transform)
        self.usere = False
        if aug is not None and 're' in aug:
            self.usere = True
            print('=> using random erasing.')
            self.re = RandomErasing()
        self.default_transform = albu.Compose([
            albu.Resize(545, 545, always_apply=True),
            albu.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225),
                           always_apply=True),
            albu.pytorch.ToTensor()
        ])  # normalized for pretrained network
    "pretrained": True,
    "fp16": True,
    "grayscale": False,
    "DEBUG": True
}

args['tr_aug'] = A.Compose(
    [
        # A.Resize(512, 512),
        # A.RandomCrop(448, 448, p=0.5),
        # A.RandomResizedCrop(512, 512),
        A.Resize(512, 512),
        A.Transpose(p=0.5),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.ShiftScaleRotate(p=0.5),
        # A.OneOf([
        #         A.MotionBlur(blur_limit=5),
        #         A.MedianBlur(blur_limit=5),
        #         #A.GaussianBlur(blur_limit=5),
        #         A.GaussNoise(var_limit=(5.0, 30.0))], p=0.5),
        # A.Normalize(),
    ],
    p=1.)

args['val_aug'] = A.Compose(
    [
        # A.Resize(512, 512),
        # A.RandomCrop(448, 448, p=0.5),
        # A.RandomResizedCrop(512, 512),
        A.Resize(512, 512),
Beispiel #29
0
        [A.RandomBrightness(limit=0.1, p=1),
         A.RandomContrast(limit=0.1, p=1)]),
    A.OneOf(
        [
            A.MotionBlur(blur_limit=3),
            A.MedianBlur(blur_limit=3),
            A.GaussianBlur(blur_limit=3),
        ],
        p=0.5,
    ),
    A.VerticalFlip(p=0.5),
    A.HorizontalFlip(p=0.5),
    A.ShiftScaleRotate(
        shift_limit=0.2,
        scale_limit=0.2,
        rotate_limit=20,
        interpolation=cv2.INTER_LINEAR,
        border_mode=cv2.BORDER_REFLECT_101,
        p=1,
    ),
    A.Cutout(num_holes=6, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
    A.Normalize(mean=[0.4309, 0.4968, 0.3135], std=[0.2131, 0.2179, 0.1940]),
    ToTensorV2(p=1.0),
])

valid_transforms = A.Compose([
    A.Resize(img_size[0], img_size[1], p=1.0),
    A.Normalize(mean=[0.4309, 0.4968, 0.3135], std=[0.2131, 0.2179, 0.1940]),
    ToTensorV2(p=1.0),
],
                             p=1.0)
Beispiel #30
0
    args.sep_conv = bool(args.sep_conv)

    # for reproduciblity
    if args.resume_dir is None:
        make_reproducible()

    # define device
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    # create logdir
    save_path = f'logs/{args.logdir}'
    mkdir(save_path)

    # define augmentation
    train_transform = albu.Compose([
        albu.ShiftScaleRotate(shift_limit=0.125, border_mode=0, value=0, p=1),
        albu.HorizontalFlip(p=0.5),
        albu.CoarseDropout(max_holes=3, max_height=8, max_width=8),
        albu.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
    ])
    val_transform = albu.Compose([
        albu.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
    ])

    # load data
    x_train = joblib.load('data/x_data.jl')
    y_train = joblib.load('data/y_data.jl')
    x_val = joblib.load('data/x_test.jl')
    y_val = joblib.load('data/y_test.jl')
    train_loader = get_loader(x_train, y_train, batch_size=args.batch_size, num_workers=args.num_workers,
                              transforms=train_transform, shuffle=True)