def run_train(data_path: Path): cfg = BasicConfig( seed=444, name='test_center_vgg_retrain2', num_workers=4, gpus=(0,), batch_size=24, num_epochs=15, steps=(4, 9, np.inf), warmup_epoch=1, cooldown_epoch=2, train_augmentations=alb.Compose([ alb.Resize(128, 128), alb.OneOf([ alb.MotionBlur(blur_limit=5, p=0.2), alb.MedianBlur(blur_limit=3, p=0.1), alb.Blur(blur_limit=5, p=0.1) ], p=0.2), alb.OneOf([ alb.ImageCompression(70, compression_type=alb.ImageCompression.ImageCompressionType.JPEG), alb.ImageCompression(70, compression_type=alb.ImageCompression.ImageCompressionType.WEBP) ], p=0.2), alb.OneOf([ alb.CLAHE(clip_limit=2), alb.IAASharpen(), alb.IAAEmboss(), alb.RandomBrightnessContrast(), ], p=0.1), alb.Rotate(5, border_mode=cv2.BORDER_REFLECT, p=0.2), alb.OneOf([ alb.RandomResizedCrop(112, 112, scale=(0.9, 1.0), ratio=(0.8, 1.1), p=0.5), alb.Resize(112, 112, p=0.5), ], p=1.0), alb.HorizontalFlip(p=0.5), alb.HueSaturationValue(p=0.7), alb.ChannelShuffle(p=0.5) ]), normalize=True, weight_normalize=True, uniform_subjects=True, classifier_mult=3, lr_factor=0.1, initial_lr=1e-2, # extra_rec=(Path('/run/media/andrey/Fast/FairFace/faces_emore/train.rec'),) ) np.random.seed(cfg.seed) mx.random.seed(cfg.seed) train_df = load_info(data_path, Path('data/train_df.csv')) lib.train.train(cfg, train_df)
def medium_3(image_size, p=1.0): cutout_crop = int(0.25 * image_size) return A.Compose( [ # RandomCrop(input_size) / RandomResizedCrop (0.08, 1) A.HorizontalFlip(p=0.5), # vflip A.VerticalFlip(p=0.5), # hflip A.ShiftScaleRotate( shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.3), A.OneOf([ A.RandomFog( fog_coef_lower=0.3, fog_coef_upper=1.0, alpha_coef=.1), A.ImageCompression(quality_lower=20, quality_upper=99), ], p=0.3), A.RandomBrightnessContrast(brightness_limit=0.125, contrast_limit=0.2, p=0.5), # contrast_limit=0.5 A.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=30, val_shift_limit=20, p=0.2), A.GaussNoise(var_limit=(1, 50), p=0.4), A.CoarseDropout(min_holes=1, max_holes=2, max_height=cutout_crop, max_width=cutout_crop, p=0.5), ], p=p)
def __init__(self, p=0.1, quality_lower=40, quality_upper=90): self.p = p self.quality_lower = quality_lower self.quality_upper = quality_upper self.aug = A.ImageCompression(p=p, quality_lower=quality_lower, quality_upper=quality_upper)
def get_strong_train_transform(): return A.Compose( [ #A.Resize(height=IMG_SIZE, width=IMG_SIZE, p=1), A.RandomSizedBBoxSafeCrop( IMG_SIZE, IMG_SIZE, interpolation=1, p=0.33), A.HorizontalFlip(), A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1, rotate_limit=10, interpolation=1, p=0.5), A.OneOf([ A.Blur(blur_limit=(1, 3), p=0.33), A.MedianBlur(blur_limit=3, p=0.33), A.ImageCompression(quality_lower=50, p=0.33), ], p=0.5), A.OneOf([ A.RandomGamma(gamma_limit=(85, 115), p=0.33), A.RandomBrightnessContrast(brightness_limit=0.2, p=0.33), A.HueSaturationValue(hue_shift_limit=25, sat_shift_limit=25, val_shift_limit=30, p=0.5) ], p=0.34), A.CLAHE(clip_limit=2.5, p=0.5), A.Normalize(always_apply=True, p=1.0), ToTensorV2(p=1.0) ], bbox_params=A.BboxParams(format='pascal_voc', min_area=5, min_visibility=0.1, label_fields=['labels']))
def get_transforms(image_size): transforms_train = albumentations.Compose([ albumentations.HorizontalFlip(p=0.5), albumentations.ImageCompression(quality_lower=99, quality_upper=100), albumentations.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=10, border_mode=0, p=0.7), albumentations.Resize(image_size, image_size), albumentations.Cutout( max_h_size=int(image_size * 0.4), max_w_size=int(image_size * 0.4), num_holes=1, p=0.5, ), albumentations.Normalize(), ]) transforms_val = albumentations.Compose([ albumentations.Resize(image_size, image_size), albumentations.Normalize() ]) return transforms_train, transforms_val
def __init__(self): self.transform = None try: import albumentations as A check_version(A.__version__, '1.0.3', hard=True) # version requirement self.transform = A.Compose([ A.Blur(p=0.01), A.MedianBlur(p=0.01), A.ToGray(p=0.01), A.CLAHE(p=0.01), A.RandomBrightnessContrast(p=0.0), A.RandomGamma(p=0.0), A.ImageCompression(quality_lower=75, p=0.0) ], bbox_params=A.BboxParams( format='yolo', label_fields=['class_labels'])) LOGGER.info( colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p)) except ImportError: # package not installed, skip pass except Exception as e: LOGGER.info(colorstr('albumentations: ') + f'{e}')
def __init__(self, root_dir, is_train): super(FaceDataset, self).__init__() #self.local_rank = local_rank self.is_train = is_train self.input_size = 256 self.num_kps = 68 transform_list = [] if is_train: transform_list += \ [ A.ColorJitter(brightness=0.8, contrast=0.5, p=0.5), A.ToGray(p=0.1), A.ISONoise(p=0.1), A.MedianBlur(blur_limit=(1,7), p=0.1), A.GaussianBlur(blur_limit=(1,7), p=0.1), A.MotionBlur(blur_limit=(5,12), p=0.1), A.ImageCompression(quality_lower=50, quality_upper=90, p=0.05), A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=40, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT, value=0, mask_value=0, p=0.8), A.HorizontalFlip(p=0.5), RectangleBorderAugmentation(limit=0.33, fill_value=0, p=0.2), ] transform_list += \ [ A.geometric.resize.Resize(self.input_size, self.input_size, interpolation=cv2.INTER_LINEAR, always_apply=True), A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), ToTensorV2(), ] self.transform = A.ReplayCompose(transform_list, keypoint_params=A.KeypointParams( format='xy', remove_invisible=False)) self.root_dir = root_dir with open(osp.join(root_dir, 'annot.pkl'), 'rb') as f: annot = pickle.load(f) self.X, self.Y = annot train_size = int(len(self.X) * 0.99) if is_train: self.X = self.X[:train_size] self.Y = self.Y[:train_size] else: self.X = self.X[train_size:] self.Y = self.Y[train_size:] #if local_rank==0: # logging.info('data_transform_list:%s'%transform_list) flip_parts = ([1, 17], [2, 16], [3, 15], [4, 14], [5, 13], [6, 12], [7, 11], [8, 10], [18, 27], [19, 26], [20, 25], [21, 24], [22, 23], [32, 36], [33, 35], [37, 46], [38, 45], [39, 44], [40, 43], [41, 48], [42, 47], [49, 55], [50, 54], [51, 53], [62, 64], [61, 65], [68, 66], [59, 57], [60, 56]) self.flip_order = np.arange(self.num_kps) for pair in flip_parts: self.flip_order[pair[1] - 1] = pair[0] - 1 self.flip_order[pair[0] - 1] = pair[1] - 1 logging.info('len:%d' % len(self.X)) print('!!!len:%d' % len(self.X))
def get_transformer(face_policy: str, patch_size: int, net_normalizer: transforms.Normalize, train: bool): # Transformers and traindb if face_policy == 'scale': # The loader crops the face isotropically then scales to a square of size patch_size_load loading_transformations = [ A.PadIfNeeded(min_height=patch_size, min_width=patch_size, border_mode=cv2.BORDER_CONSTANT, value=0,always_apply=True), A.Resize(height=patch_size,width=patch_size,always_apply=True), ] if train: downsample_train_transformations = [ A.Downscale(scale_max=0.5, scale_min=0.5, p=0.5), # replaces scaled dataset ] else: downsample_train_transformations = [] elif face_policy == 'tight': # The loader crops the face tightly without any scaling loading_transformations = [ A.LongestMaxSize(max_size=patch_size, always_apply=True), A.PadIfNeeded(min_height=patch_size, min_width=patch_size, border_mode=cv2.BORDER_CONSTANT, value=0,always_apply=True), ] if train: downsample_train_transformations = [ A.Downscale(scale_max=0.5, scale_min=0.5, p=0.5), # replaces scaled dataset ] else: downsample_train_transformations = [] else: raise ValueError('Unknown value for face_policy: {}'.format(face_policy)) if train: aug_transformations = [ A.Compose([ A.HorizontalFlip(), A.OneOf([ A.RandomBrightnessContrast(), A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=30, val_shift_limit=20), ]), A.OneOf([ A.ISONoise(), A.IAAAdditiveGaussianNoise(scale=(0.01 * 255, 0.03 * 255)), ]), A.Downscale(scale_min=0.7, scale_max=0.9, interpolation=cv2.INTER_LINEAR), A.ImageCompression(quality_lower=50, quality_upper=99), ], ) ] else: aug_transformations = [] # Common final transformations final_transformations = [ A.Normalize(mean=net_normalizer.mean, std=net_normalizer.std, ), ToTensorV2(), ] transf = A.Compose( loading_transformations + downsample_train_transformations + aug_transformations + final_transformations) return transf
def get_obliterate_augs(): """ Get the augmentation that can obliterate the hidden signal. This is used as augmentation to create negative sample from positive one. :return: """ return A.OneOf( [ A.ImageCompression(quality_lower=70, quality_upper=95, p=1), A.Downscale(p=1), A.GaussianBlur(blur_limit=(5, 9), p=1), ], p=1, )
def get_train_transform(img_size): return A.Compose( [ A.Resize(height=img_size, width=img_size, p=1), A.RandomSizedBBoxSafeCrop( img_size, img_size, interpolation=1, p=0.33 ), A.Flip(), A.ShiftScaleRotate( shift_limit=0.05, scale_limit=0.1, rotate_limit=10, interpolation=1, border_mode=0, value=0, p=0.5, ), A.OneOf( [ A.Blur(blur_limit=(1, 3), p=0.33), A.MedianBlur(blur_limit=3, p=0.33), A.ImageCompression(quality_lower=50, p=0.33), ], p=0.33, ), A.OneOf( [ A.RandomGamma(gamma_limit=(90, 110), p=0.2), A.RandomBrightnessContrast(brightness_limit=0.2, p=0.4), A.HueSaturationValue( hue_shift_limit=25, sat_shift_limit=25, val_shift_limit=30, p=0.5, ), ], p=0.4, ), A.CLAHE(clip_limit=2, p=0.2), A.Normalize(always_apply=True, p=1.0), ToTensorV2(p=1.0), ], bbox_params=A.BboxParams( format="pascal_voc", min_area=64, min_visibility=0.1, label_fields=["labels"], ), )
def setup_augmentors(self, augmentations): self.augmentors = [] for aug_name, aug_config in augmentations.items(): aug = None def get_albu(aug): return albu.Compose(aug) if aug_name == 'image_compression': aug = get_albu([ albu.ImageCompression( quality_lower=aug_config.get('quality_lower', 90), quality_upper=aug_config.get('quality_upper'), p=aug_config.get('probabilty', 0.5)) ]) elif aug_name == 'posterize': aug = get_albu([ albu.Posterize(num_bits=aug_config.get('num_bits', 4), p=aug_config.get('probability', 0.5)) ]) elif aug_name == 'blur': aug = get_albu([ albu.Blur(blur_limit=aug_config.get('blur_limit', 7), p=aug_config.get('probabilty', 0.5)) ]) elif aug_name == 'median_blur': aug = get_albu([ albu.MedianBlur(blur_limit=aug_config.get('blur_limit', 7), p=aug_config.get('probabilty', 0.5)) ]) elif aug_name == 'iso_noise': aug = get_albu([ albu.ISONoise( color_shift=(aug_config.get('min_color_shift', 0.01), aug_config.get('max_color_shift', 0.05)), intensity=(aug_config.get('min_intensity', 0.1), aug_config.get('min_intensity', 0.5)), p=aug_config.get('probabilty', 0.5)) ]) if not aug: continue aug.name, aug.p, aug.base = aug_name, aug_config[ 'probability'], self self.augmentors.append(aug) return
def aug_medium(prob=1): return aug.Compose([ aug.HorizontalFlip(p=.5), aug.OneOf([ aug.CLAHE(clip_limit=2, p=.5), aug.IAASharpen(p=.25), ], p=0.35), aug.RandomBrightnessContrast(p=.7), aug.OneOf([ aug.GaussNoise(p=.35), aug.ISONoise(p=.7), aug.ImageCompression(quality_lower=70, quality_upper=100, p=.7) ], p=.6), aug.RGBShift(p=.5), aug.HueSaturationValue(hue_shift_limit=8, sat_shift_limit=12, val_shift_limit=8, p=.5), aug.ToGray(p=.3) ], p=prob)
def hard_transforms(border_reflect=2): result = [ albu.HorizontalFlip(p=0.5), albu.VerticalFlip(p=0.5), albu.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=15, border_mode=border_reflect, p=0.5), albu.IAAPerspective(scale=(0.02, 0.05), p=0.3), albu.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.3), albu.RandomGamma(gamma_limit=(85, 115), p=0.3), albu.HueSaturationValue(p=0.3), albu.ImageCompression(quality_lower=80, p=0.5), ] return result
def get_transform(img_size: int, darknet_pretrained: bool = False): """Initializes and returns data transformation pipeline Args: img_size (int): image size darknet_pretrained (bool): if you use a pre-trained darknet model, you need to disable image normalization """ normalize = albu.Normalize(p=1) if darknet_pretrained: normalize = albu.Normalize(p=0) train_transform = albu.Compose( [ albu.RandomResizedCrop( scale=(0.9, 1.0), height=img_size, width=img_size, p=1, ), albu.ShiftScaleRotate( border_mode=cv2.BORDER_CONSTANT, rotate_limit=10, scale_limit=0, p=0.5, mask_value=255, ), albu.ImageCompression(quality_lower=60, quality_upper=100, p=0.5), normalize, ], bbox_params=albu.BboxParams(format="pascal_voc", label_fields=["category_id"]), ) test_transform = albu.Compose( [albu.Resize(height=img_size, width=img_size, p=1), normalize], bbox_params=albu.BboxParams(format="pascal_voc", label_fields=["category_id"]), ) return train_transform, test_transform
def hard_transforms(): result = [ # Random shifts, stretches and turns with a 50% probability albu.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=15, border_mode=BORDER_REFLECT, p=0.5), albu.IAAPerspective(scale=(0.02, 0.05), p=0.3), # Random brightness / contrast with a 30% probability albu.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.3), # Random gamma changes with a 30% probability albu.RandomGamma(gamma_limit=(85, 115), p=0.3), # Randomly changes the hue, saturation, and color value of the input image albu.HueSaturationValue(p=0.3), albu.ImageCompression(quality_lower=80), ] return result
def __init__(self): self.transform = None try: import albumentations as A self.transform = A.Compose([ A.Blur(p=0.01), A.MedianBlur(p=0.01), A.ToGray(p=0.01), A.CLAHE(p=0.01), A.RandomBrightnessContrast(p=0.0), A.RandomGamma(p=0.0), A.ImageCompression(quality_lower=75, p=0.0) ], bbox_params=A.BboxParams( format='pascal_voc', label_fields=['class_labels'])) except ImportError: # package not installed, skip pass except Exception as e: print('albumentations: ' + f'{e}')
def get_image_augmentation(): """ Augmentations just for input and output images (not for masks) """ image_transform = [ albu.OneOf([ albu.Blur(p=0.2, blur_limit=(3, 5)), albu.GaussNoise(p=0.2, var_limit=(10.0, 50.0)), albu.ISONoise(p=0.2, intensity=(0.1, 0.5), color_shift=(0.01, 0.05)), albu.ImageCompression(p=0.2, quality_lower=90, quality_upper=100, compression_type=0), albu.MultiplicativeNoise(p=0.2, multiplier=(0.9, 1.1), per_channel=True, elementwise=True), ], p=1), albu.OneOf([ albu.HueSaturationValue(p=0.2, hue_shift_limit=(-10, 10), sat_shift_limit=(-10, 10), val_shift_limit=(-10, 10)), albu.RandomBrightness(p=0.3, limit=(-0.1, 0.1)), albu.RandomGamma(p=0.3, gamma_limit=(80, 100), eps=1e-07), albu.ToGray(p=0.1), albu.ToSepia(p=0.1), ], p=1) ] return albu.Compose(image_transform, additional_targets={ 'image1': 'image', 'image2': 'image' })
wandb.init(project='inception_v3', group=wandb.util.generate_id()) wandb.config.width_size = width_size wandb.config.aspect_rate = 1 wandb.config.batch_size = batch_size wandb.config.accumulation_step = accumulation_step shutil.rmtree('tensorboard_runs', ignore_errors=True) writer = SummaryWriter(log_dir='tensorboard_runs', filename_suffix=str(time.time())) ranzcr_df = pd.read_csv('train_folds.csv') ranzcr_train_df = ranzcr_df[ranzcr_df['fold'] != 1] chestx_df = pd.read_csv('chestx_pseudolabeled_data_lazy_balancing.csv') train_image_transforms = alb.Compose([ alb.ImageCompression(quality_lower=65, p=0.5), alb.HorizontalFlip(p=0.5), alb.CLAHE(p=0.5), 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.7), alb.RandomResizedCrop(height=width_size, width=width_size, scale=(0.8, 1.2),
parser.add_argument('--verbose_eval', default=50, type=int) parser.add_argument('--max_size', default=300, type=int) parser.add_argument('--num_classes', default=1049, type=int) parser.add_argument('--resume', default=None, type=str) parser.add_argument('--depth', default=0, type=int) args = parser.parse_args() scaler = GradScaler() writer = SummaryWriter() fixed_seed(42) device = 'cuda' transforms_train = A.Compose([ A.HorizontalFlip(p=0.2), 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.5), A.ColorJitter(0.2, 0.2, 0.2, 0.2), A.Resize(args.max_size, args.max_size), A.OneOf([ A.RandomResizedCrop(args.max_size, args.max_size), A.Cutout( max_h_size=int(args.max_size*0.4), max_w_size=int(args.max_size*0.4), num_holes=1, p=0.3),
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, ) val_augmentations = albu.Compose( [ albu.PadIfNeeded(min_height=1024, min_width=2048,
def train_function(gpu, world_size, node_rank, gpus): import torch.multiprocessing torch.multiprocessing.set_sharing_strategy('file_system') torch.manual_seed(25) np.random.seed(25) rank = node_rank * gpus + gpu dist.init_process_group( backend='nccl', init_method='env://', world_size=world_size, rank=rank ) width_size = 512 batch_size = 32 accumulation_step = 5 device = torch.device("cuda:{}".format(gpu) if torch.cuda.is_available() else "cpu") if rank == 0: wandb.init(project='inception_v3', group=wandb.util.generate_id()) wandb.config.width_size = width_size wandb.config.aspect_rate = 1 wandb.config.batch_size = batch_size wandb.config.accumulation_step = accumulation_step shutil.rmtree('tensorboard_runs', ignore_errors=True) writer = SummaryWriter(log_dir='tensorboard_runs', filename_suffix=str(time.time())) ranzcr_df = pd.read_csv('train_folds.csv') ranzcr_train_df = ranzcr_df[ranzcr_df['fold'] != 1] chestx_df = pd.read_csv('chestx_pseudolabeled_data_lazy_balancing.csv') train_image_transforms = alb.Compose([ alb.ImageCompression(quality_lower=65, p=0.5), alb.HorizontalFlip(p=0.5), alb.CLAHE(p=0.5), 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.7 ), alb.RandomResizedCrop( height=width_size, width=width_size, scale=(0.8, 1.2), p=0.7 ), alb.RGBShift(p=0.5), alb.RandomSunFlare(p=0.5), alb.RandomFog(p=0.5), alb.RandomBrightnessContrast(p=0.5), alb.HueSaturationValue( hue_shift_limit=20, sat_shift_limit=20, val_shift_limit=20, 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(width_size / 6), max_width=int(width_size / 6), min_height=int(width_size / 6), min_width=int(width_size / 20), p=0.5 ), alb.IAAAdditiveGaussianNoise(loc=0, scale=(2.5500000000000003, 12.75), per_channel=False, p=0.5), alb.IAAAffine(scale=1.0, translate_percent=None, translate_px=None, rotate=0.0, shear=0.0, order=1, cval=0, mode='reflect', p=0.5), alb.IAAAffine(rotate=90., p=0.5), alb.IAAAffine(rotate=180., p=0.5), alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensorV2() ]) train_set = NoisyStudentDataset(ranzcr_train_df, chestx_df, train_image_transforms, '../ranzcr/train', '../data', width_size=width_size) train_sampler = DistributedSampler(train_set, num_replicas=world_size, rank=rank, shuffle=True) train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False, num_workers=4, sampler=train_sampler) ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1] valid_image_transforms = alb.Compose([ alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensorV2() ]) valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size) valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, pin_memory=False, drop_last=False) # ranzcr_valid_df = ranzcr_df[ranzcr_df['fold'] == 1] # valid_image_transforms = alb.Compose([ # alb.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), # ToTensorV2() # ]) # valid_set = ImageDataset(ranzcr_valid_df, valid_image_transforms, '../ranzcr/train', width_size=width_size) # valid_sampler = DistributedSampler(valid_set, num_replicas=world_size, rank=rank) # valid_loader = DataLoader(valid_set, batch_size=batch_size, num_workers=4, sampler=valid_sampler) checkpoints_dir_name = 'inception_v3_noisy_student_{}'.format(width_size) os.makedirs(checkpoints_dir_name, exist_ok=True) # model = EfficientNetNoisyStudent(11, pretrained_backbone=True, # mixed_precision=True, model_name='tf_efficientnet_b7_ns') model = Inception(11, pretrained_backbone=True, mixed_precision=False, model_name='inception_v3') model = SyncBatchNorm.convert_sync_batchnorm(model) model.to(device) model = DistributedDataParallel(model, device_ids=[gpu]) # class_weights = [354.625, 23.73913043478261, 2.777105767812362, 110.32608695652173, # 52.679245283018865, 9.152656621728786, 4.7851333032083145, # 8.437891632878731, 2.4620064899945917, 0.4034751151063363, 31.534942820838626] class_names = ['ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal', 'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal', 'Swan Ganz Catheter Present'] scaler = GradScaler() criterion = torch.nn.BCEWithLogitsLoss() lr_start = 1e-4 lr_end = 1e-6 weight_decay = 0 epoch_num = 20 if rank == 0: wandb.config.model_name = checkpoints_dir_name wandb.config.lr_start = lr_start wandb.config.lr_end = lr_end wandb.config.weight_decay = weight_decay wandb.config.epoch_num = epoch_num wandb.config.optimizer = 'adam' wandb.config.scheduler = 'CosineAnnealingLR' wandb.config.is_loss_weights = 'no' optimizer = Adam(model.parameters(), lr=lr_start, weight_decay=weight_decay) scheduler = CosineAnnealingLR(optimizer, T_max=epoch_num, eta_min=lr_end, last_epoch=-1) max_val_auc = 0 for epoch in range(epoch_num): train_loss, train_avg_auc, train_auc, train_rocs, train_data_pr, train_duration = one_epoch_train( model, train_loader, optimizer, criterion, device, scaler, iters_to_accumulate=accumulation_step, clip_grads=False) scheduler.step() if rank == 0: val_loss, val_avg_auc, val_auc, val_rocs, val_data_pr, val_duration = eval_model( model, valid_loader, device, criterion, scaler) wandb.log({'train_loss': train_loss, 'val_loss': val_loss, 'train_auc': train_avg_auc, 'val_auc': val_avg_auc, 'epoch': epoch}) for class_name, auc1, auc2 in zip(class_names, train_auc, val_auc): wandb.log({'{} train auc'.format(class_name): auc1, '{} val auc'.format(class_name): auc2, 'epoch': epoch}) if val_avg_auc > max_val_auc: max_val_auc = val_avg_auc wandb.run.summary["best_accuracy"] = val_avg_auc print('EPOCH %d:\tTRAIN [duration %.3f sec, loss: %.3f, avg auc: %.3f]\t\t' 'VAL [duration %.3f sec, loss: %.3f, avg auc: %.3f]\tCurrent time %s' % (epoch + 1, train_duration, train_loss, train_avg_auc, val_duration, val_loss, val_avg_auc, str(datetime.now(timezone('Europe/Moscow'))))) torch.save(model.module.state_dict(), os.path.join(checkpoints_dir_name, '{}_epoch{}_val_auc{}_loss{}_train_auc{}_loss{}.pth'.format( checkpoints_dir_name, epoch + 1, round(val_avg_auc, 3), round(val_loss, 3), round(train_avg_auc, 3), round(train_loss, 3)))) if rank == 0: wandb.finish()
def load_train_transform(transform_type, patch_size=512): if transform_type == 'basic': transform = A.Compose([ A.RandomRotate90(p=0.75), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.PadIfNeeded(patch_size, patch_size)]) elif transform_type == 'light': transform = A.Compose([ A.RandomRotate90(p=0.75), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.Rotate(limit=90, interpolation=1, border_mode=0, value=0, mask_value=0, p=0.25), A.HueSaturationValue( hue_shift_limit=10, sat_shift_limit=10, val_shift_limit=0, p=0.5), A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.15, contrast_limit=0.0, p=1.0), A.RandomBrightnessContrast(brightness_limit=0.0, contrast_limit=0.15, p=1.0), A.RandomGamma(p=.0)], p=1.0), A.PadIfNeeded(patch_size, patch_size)]) elif transform_type == 'moderate': transform = A.Compose([ A.RandomRotate90(p=0.75), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.Rotate(limit=360, interpolation=1, border_mode=0, value=0, mask_value=0, p=0.25), A.transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2, p=0.1), A.CoarseDropout(max_holes=12, max_height=12, max_width=12, fill_value=0, p=0.25), A.RandomResizedCrop(patch_size, patch_size, scale=(0.5, 1.5), ratio=(0.5, 1.5), interpolation=1, p=0.5), A.RGBShift(r_shift_limit=20, g_shift_limit=20, b_shift_limit=20, p=0.5), A.HueSaturationValue( hue_shift_limit=20, sat_shift_limit=20, val_shift_limit=5, p=0.5), A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.4, contrast_limit=0.0, p=0.75), A.RandomBrightnessContrast(brightness_limit=0.0, contrast_limit=0.2, p=0.75), A.RandomGamma(gamma_limit=(75, 125), p=0.5)], p=1.0), A.OneOf([ A.Blur(blur_limit=1, p=0.75), A.CLAHE(clip_limit=2.0, tile_grid_size=(8, 8), p=0.75), A.IAASharpen(alpha=(0.2, 0.5), lightness=(0.5, 1.0), p=0.75), A.ImageCompression(quality_lower=60, quality_upper=100, compression_type=0, p=0.75)], p=1.0), A.PadIfNeeded(patch_size, patch_size)]) elif transform_type == 'medium': transform = A.Compose([ A.RandomRotate90(p=0.75), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.Rotate(limit=90, interpolation=1, border_mode=0, value=0, mask_value=0, p=0.25), A.HueSaturationValue( hue_shift_limit=10, sat_shift_limit=10, val_shift_limit=0, p=0.5), A.OneOf([ A.RandomBrightnessContrast(brightness_limit=0.15, contrast_limit=0.0, p=1.0), A.RandomBrightnessContrast(brightness_limit=0.0, contrast_limit=0.15, p=1.0), A.RandomGamma(p=.0)], p=1.0), A.OneOf([ A.ElasticTransform( alpha=200, sigma=20, alpha_affine=20, interpolation=1, border_mode=0, value=0, mask_value=0, p=1.0), A.GridDistortion( num_steps=10, distort_limit=0.3, interpolation=1, border_mode=0, value=0, mask_value=0, p=1.0)]), A.PadIfNeeded(patch_size, patch_size)]) else: transform = A.Compose([A.PadIfNeeded(patch_size, patch_size)]) return transform
def get_config(runner, raw_uri, processed_uri, root_uri, test=False, external_model=False, external_loss=False, augment=False): debug = False train_scene_info = get_scene_info(join(processed_uri, 'train-scenes.csv')) val_scene_info = get_scene_info(join(processed_uri, 'val-scenes.csv')) log_tensorboard = True run_tensorboard = True class_config = ClassConfig(names=['no_building', 'building']) if test: debug = True train_scene_info = train_scene_info[0:1] val_scene_info = val_scene_info[0:1] def make_scene(scene_info): (raster_uri, label_uri) = scene_info raster_uri = join(raw_uri, raster_uri) label_uri = join(processed_uri, label_uri) aoi_uri = join(raw_uri, aoi_path) if test: crop_uri = join(processed_uri, 'crops', os.path.basename(raster_uri)) label_crop_uri = join(processed_uri, 'crops', os.path.basename(label_uri)) save_image_crop(raster_uri, crop_uri, label_uri=label_uri, label_crop_uri=label_crop_uri, size=600, min_features=20, class_config=class_config) raster_uri = crop_uri label_uri = label_crop_uri id = os.path.splitext(os.path.basename(raster_uri))[0] raster_source = RasterioSourceConfig(channel_order=[0, 1, 2], uris=[raster_uri]) label_source = ChipClassificationLabelSourceConfig( vector_source=GeoJSONVectorSourceConfig(uri=label_uri, default_class_id=1, ignore_crs_field=True), ioa_thresh=0.5, use_intersection_over_cell=False, pick_min_class_id=False, background_class_id=0, infer_cells=True) return SceneConfig(id=id, raster_source=raster_source, label_source=label_source, aoi_uris=[aoi_uri]) chip_sz = 200 train_scenes = [make_scene(info) for info in train_scene_info] val_scenes = [make_scene(info) for info in val_scene_info] dataset = DatasetConfig(class_config=class_config, train_scenes=train_scenes, validation_scenes=val_scenes) if external_model: model = ClassificationModelConfig(external_def=ExternalModuleConfig( github_repo='lukemelas/EfficientNet-PyTorch', # uri='s3://raster-vision-ahassan/models/EfficientNet-PyTorch.zip', name='efficient_net', entrypoint='efficientnet_b0', force_reload=False, entrypoint_kwargs={ 'num_classes': len(class_config.names), 'pretrained': 'imagenet' })) else: model = ClassificationModelConfig(backbone=Backbone.resnet50) if external_loss: external_loss_def = ExternalModuleConfig( github_repo='AdeelH/pytorch-multi-class-focal-loss', name='focal_loss', entrypoint='focal_loss', force_reload=False, entrypoint_kwargs={ 'alpha': [.75, .25], 'gamma': 2 }) else: external_loss_def = None solver = SolverConfig(lr=1e-4, num_epochs=20, test_num_epochs=4, batch_sz=32, one_cycle=True, external_loss_def=external_loss_def) if augment: mu = np.array((0.485, 0.456, 0.406)) std = np.array((0.229, 0.224, 0.225)) aug_transform = A.Compose([ A.Flip(), A.Transpose(), A.RandomRotate90(), A.ShiftScaleRotate(), A.OneOf([ A.ChannelShuffle(), A.CLAHE(), A.FancyPCA(), A.HueSaturationValue(), A.RGBShift(), A.ToGray(), A.ToSepia(), ]), A.OneOf([ A.RandomBrightness(), A.RandomGamma(), ]), A.OneOf([ A.GaussNoise(), A.ISONoise(), A.RandomFog(), ]), A.OneOf([ A.Blur(), A.MotionBlur(), A.ImageCompression(), A.Downscale(), ]), A.CoarseDropout(max_height=32, max_width=32, max_holes=5) ]) base_transform = A.Normalize(mean=mu.tolist(), std=std.tolist()) plot_transform = A.Normalize(mean=(-mu / std).tolist(), std=(1 / std).tolist(), max_pixel_value=1.) else: aug_transform = None base_transform = None plot_transform = None backend = PyTorchChipClassificationConfig( model=model, solver=solver, log_tensorboard=log_tensorboard, run_tensorboard=run_tensorboard, test_mode=test, base_transform=A.to_dict(base_transform), aug_transform=A.to_dict(aug_transform), plot_options=PlotOptions(transform=A.to_dict(plot_transform))) config = ChipClassificationConfig(root_uri=root_uri, dataset=dataset, backend=backend, train_chip_sz=chip_sz, predict_chip_sz=chip_sz) return config
def get_augmentations(name, img_size): if name == 'training_none': aug = A.Compose([ A.Resize(img_size, img_size), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) elif name == 'training_dropout': aug = A.Compose([ A.Resize(img_size, img_size), A.CoarseDropout(min_height=int(img_size * 0.05), min_width=int(img_size * 0.05), max_height=int(img_size * 0.1), max_width=int(img_size * 0.1), min_holes=1, max_holes=20, p=0), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) elif name == 'training_1': aug = A.Compose([ A.RandomResizedCrop(img_size, img_size, scale=(0.9, 1), p=1), A.ShiftScaleRotate(p=0.5), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.7), A.HueSaturationValue(hue_shift_limit=10, val_shift_limit=10, sat_shift_limit=10, p=0.7), A.CLAHE(clip_limit=(1, 4), p=0.5), A.OneOf([ A.GaussNoise(var_limit=[10, 50]), A.GaussianBlur(), A.MotionBlur(), A.MedianBlur(), ], p=0.3), A.OneOf([ A.OpticalDistortion(distort_limit=1.0), A.GridDistortion(num_steps=5, distort_limit=1.), A.ElasticTransform(alpha=3), ], p=0.3), A.OneOf([ A.ImageCompression(), A.Downscale(scale_min=0.1, scale_max=0.15), ], p=0.2), A.IAAPiecewiseAffine(p=0.2), A.IAASharpen(p=0.2), A.CoarseDropout(max_height=int(img_size * 0.1), max_width=int(img_size * 0.1), min_holes=5, max_holes=10, p=0.5), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) elif name == 'training_2': aug = A.Compose([ A.RandomResizedCrop(img_size, img_size, scale=(0.9, 1), p=1), A.ShiftScaleRotate(p=0.5), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.7), A.HueSaturationValue(hue_shift_limit=10, val_shift_limit=10, sat_shift_limit=10, p=0.7), A.CLAHE(clip_limit=(1, 4), p=0.5), A.OneOf([ A.GaussNoise(var_limit=[10, 50]), A.GaussianBlur(), A.MotionBlur(), A.MedianBlur(), ], p=0.3), A.OneOf([ A.OpticalDistortion(distort_limit=1.0), A.GridDistortion(num_steps=5, distort_limit=1.), A.ElasticTransform(alpha=3), ], p=0.3), A.OneOf([ A.ImageCompression(), A.Downscale(scale_min=0.1, scale_max=0.15), ], p=0.2), A.IAAPiecewiseAffine(p=0.2), A.IAASharpen(p=0.2), A.CoarseDropout(max_height=int(img_size * 0.1), max_width=int(img_size * 0.1), min_holes=5, max_holes=10, p=0.5), A.Normalize(), ToTensorV2() ]) elif name == 'training_2_bis': aug = A.Compose([ A.RandomResizedCrop(img_size, img_size, scale=(0.9, 1), p=1), A.ShiftScaleRotate(rotate_limit=30, p=0.5), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.7), A.HueSaturationValue(hue_shift_limit=10, val_shift_limit=10, sat_shift_limit=10, p=0.7), A.CLAHE(clip_limit=(1, 4), p=0.5), A.OneOf([ A.GaussNoise(var_limit=[10, 50]), A.GaussianBlur(), A.MotionBlur(), A.MedianBlur() ], p=0.3), #A.OneOf([A.OpticalDistortion(distort_limit=1.0), A.GridDistortion(num_steps=5, distort_limit=1.), # A.ElasticTransform(alpha=3)], p=0.3), A.OneOf([ A.ImageCompression(), A.Downscale(scale_min=0.1, scale_max=0.15) ], p=0.2), #A.IAAPiecewiseAffine(p=0.2), A.IAASharpen(p=0.2), A.CoarseDropout(max_height=int(img_size * 0.1), max_width=int(img_size * 0.1), min_holes=5, max_holes=10, p=0.5), A.Normalize(), ToTensorV2() ]) elif name == 'training_3': aug = A.Compose([ A.Rotate(limit=5), A.RandomResizedCrop(img_size, img_size, scale=(0.9, 1), p=1), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(brightness_limit=0.15, contrast_limit=0.15, p=0.5), A.CoarseDropout(min_height=int(img_size * 0.05), min_width=int(img_size * 0.05), max_height=int(img_size * 0.1), max_width=int(img_size * 0.1), min_holes=1, max_holes=10, p=0.5), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) elif name == 'training_4': aug = A.Compose([ A.Rotate(limit=5, p=1), A.RandomResizedCrop(img_size, img_size, scale=(0.9, 1), p=1), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(brightness_limit=(-0.15, +0.25), contrast_limit=(-0.15, +0.25), p=1), A.CLAHE(clip_limit=(1, 4), p=0.5), A.OneOf([ A.GaussNoise(var_limit=(10, 50)), A.GaussianBlur(), A.MotionBlur(), A.MedianBlur(), ], p=1), A.IAASharpen(p=0.3), A.CoarseDropout(min_height=int(img_size * 0.05), min_width=int(img_size * 0.05), max_height=int(img_size * 0.1), max_width=int(img_size * 0.1), min_holes=1, max_holes=20, p=0), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) elif name == 'validation': aug = A.Compose( [A.Resize(img_size, img_size), A.Normalize(), ToTensorV2()]) elif name == 'none': aug = A.Compose([A.Resize(img_size, img_size)]) else: raise ValueError(f"{name} is not a valid augmentations name") return aug
def get_transform_imagenet(use_albu_aug): if use_albu_aug: train_transform = al.Compose([ # al.Flip(p=0.5), al.Resize(256, 256, interpolation=2), al.RandomResizedCrop(224, 224, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=2), al.HorizontalFlip(), al.OneOf( [ al.OneOf( [ al.ShiftScaleRotate( border_mode=cv2.BORDER_CONSTANT, rotate_limit=30), # , p=0.05), al.OpticalDistortion( border_mode=cv2.BORDER_CONSTANT, distort_limit=5.0, shift_limit=0.1), # , p=0.05), al.GridDistortion(border_mode=cv2.BORDER_CONSTANT ), # , p=0.05), al.ElasticTransform( border_mode=cv2.BORDER_CONSTANT, alpha_affine=15), # , p=0.05), ], p=0.1), al.OneOf( [ al.RandomGamma(), # p=0.05), al.HueSaturationValue(), # p=0.05), al.RGBShift(), # p=0.05), al.CLAHE(), # p=0.05), al.ChannelShuffle(), # p=0.05), al.InvertImg(), # p=0.05), ], p=0.1), al.OneOf( [ al.RandomSnow(), # p=0.05), al.RandomRain(), # p=0.05), al.RandomFog(), # p=0.05), al.RandomSunFlare(num_flare_circles_lower=1, num_flare_circles_upper=2, src_radius=110), # p=0.05, ), al.RandomShadow(), # p=0.05), ], p=0.1), al.RandomBrightnessContrast(p=0.1), al.OneOf( [ al.GaussNoise(), # p=0.05), al.ISONoise(), # p=0.05), al.MultiplicativeNoise(), # p=0.05), ], p=0.1), al.OneOf( [ al.ToGray(), # p=0.05), al.ToSepia(), # p=0.05), al.Solarize(), # p=0.05), al.Equalize(), # p=0.05), al.Posterize(), # p=0.05), al.FancyPCA(), # p=0.05), ], p=0.1), al.OneOf( [ # al.MotionBlur(blur_limit=1), al.Blur(blur_limit=[3, 5]), al.MedianBlur(blur_limit=[3, 5]), al.GaussianBlur(blur_limit=[3, 5]), ], p=0.1), al.OneOf( [ al.CoarseDropout(), # p=0.05), al.Cutout(), # p=0.05), al.GridDropout(), # p=0.05), al.ChannelDropout(), # p=0.05), al.RandomGridShuffle(), # p=0.05), ], p=0.1), al.OneOf( [ al.Downscale(), # p=0.1), al.ImageCompression(quality_lower=60), # , p=0.1), ], p=0.1), ], p=0.5), al.Normalize(), ToTensorV2() ]) else: train_transform = transforms.Compose([ transforms.Resize(256), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ]) test_transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), ]) if use_albu_aug: train_transform = MultiDataTransformAlbu(train_transform) else: train_transform = MultiDataTransform(train_transform) return train_transform, test_transform
import os from typing import Tuple, List import albumentations as a import cv2 import numpy as np transform_distort = a.Compose([ a.ImageCompression(quality_lower=39, quality_upper=40, p=0.4), a.MultiplicativeNoise(multiplier=(0.5, 1.5), per_channel=True, p=0.2), a.RandomBrightnessContrast(p=0.3), ]) transform_beauty = a.Compose([ a.RandomBrightnessContrast(p=0.3), ]) def augment_distort(img: np.ndarray): transformed = transform_distort(image=img) transformed_image = transformed["image"] return transformed_image def augment_beauty(img: np.ndarray): transformed = transform_beauty(image=img) transformed_image = transformed["image"] return transformed_image def load_altered_dataset_beauty(
def experiment(device, args=None): """Train model. Args: device (str): device to use for training. args (dict): experiment arguments. """ if args is None: args = dict train_config = args["train"] train_augmentations = albu.Compose( [ albu.OneOf([ albu.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=35, val_shift_limit=25), albu.RandomGamma(), albu.CLAHE(), ]), albu.RandomBrightnessContrast(brightness_limit=[-0.3, 0.3], contrast_limit=[-0.3, 0.3], p=0.5), albu.OneOf([ albu.Blur(), albu.MotionBlur(), albu.GaussNoise(), albu.ImageCompression(quality_lower=75) ]), albu.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.15, rotate_limit=10, border_mode=0, p=0.5), albu.Resize(300, 300), albu.Normalize(), ToTensorV2(), ], bbox_params=albu.BboxParams( "albumentations" ), # 'albumentations' because x1, y1, x2, y2 in range [0, 1] ) train_dataset = COCOFileDataset(train_config["annotations"], train_config["images_dir"], transforms=train_augmentations) train_loader = DataLoader( train_dataset, batch_size=train_config["batch_size"], num_workers=train_config["num_workers"], shuffle=True, drop_last=True, ) logger.info("Train dataset information:") logger.info("\n" + train_dataset.info()) valid_config = args["validation"] valid_augmentations = albu.Compose( [ albu.Resize(300, 300), albu.Normalize(), ToTensorV2(), ], bbox_params=albu.BboxParams( format="albumentations" ), # 'albumentations' because x1, y1, x2, y2 in range [0, 1] ) valid_dataset = COCOFileDataset(valid_config["annotations"], valid_config["images_dir"], transforms=valid_augmentations) valid_loader = DataLoader( valid_dataset, batch_size=valid_config["batch_size"], num_workers=valid_config["num_workers"], shuffle=False, drop_last=False, ) logger.info("Validation dataset information:") logger.info("\n" + valid_dataset.info()) model_config = args["model"] num_classes = model_config["num_classes"] + 1 # +1 for background class seed_all(42) model = SSD300(model_config["backbone"], num_classes) model = model.to(device) optimizer = optim.AdamW(model.parameters(), lr=1e-3 / 2) # optimizer = optim.SGD(model.parameters(), lr=2.6e-3, momentum=0.9, weight_decay=0.0005) epoch_scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts( optimizer, args["experiment"]["num_epochs"]) batch_scheduler = None criterion = Loss(num_classes) experiment_config = args["experiment"] num_epochs = experiment_config["num_epochs"] for epoch_idx in range(1, num_epochs + 1): logger.info(f"Epoch: {epoch_idx}/{num_epochs}") train_metrics = train_fn(train_loader, model, device, criterion, optimizer, batch_scheduler, verbose=False) logger.info(f" Train: {train_metrics}") # TODO: checkpoints valid_metrics = valid_fn(valid_loader, model, device, criterion, verbose=False) logger.info(f"Validation: {valid_metrics}") epoch_scheduler.step() export_to_onnx(model, torch.randn(1, 3, 300, 300), experiment_config["onnx"]) logger.info("Exported ONNX model to '{}'".format( experiment_config["onnx"]))
def train_process(data_path, config): def _worker_init_fn_(): import random import numpy as np import torch random_seed = config.random_seed torch.manual_seed(random_seed) np.random.seed(random_seed) random.seed(random_seed) if torch.cuda.is_available(): torch.cuda.manual_seed(random_seed) input_size = (config.img_height, config.img_width) PAD_VALUE = (0, 0, 0) IGNORE_INDEX = 255 transforms = [ abm.RandomResizedCrop( scale=(0.7, 1), ratio=(1.5, 2), height=config.img_height, width=config.img_width, interpolation=cv2.INTER_NEAREST, always_apply=True, ), abm.OneOf([abm.IAAAdditiveGaussianNoise(), abm.GaussNoise()], p=0.5), abm.OneOf( [ abm.MedianBlur(blur_limit=3), abm.GaussianBlur(blur_limit=3), abm.MotionBlur(blur_limit=3), ], p=0.5, ), abm.OneOf([ abm.ShiftScaleRotate( rotate_limit=7, interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, value=PAD_VALUE, mask_value=IGNORE_INDEX, p=1.0, ), abm.ElasticTransform( interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT, alpha_affine=30, value=PAD_VALUE, mask_value=IGNORE_INDEX, p=1.0, ), abm.Perspective( scale=(0.05), interpolation=cv2.INTER_NEAREST, pad_mode=cv2.BORDER_CONSTANT, pad_val=PAD_VALUE, mask_pad_val=IGNORE_INDEX, keep_size=True, fit_output=True, p=1.0, ), ]), abm.RandomGamma(gamma_limit=(80, 120), p=0.5), abm.RandomBrightnessContrast(brightness_limit=(-0.5, 0.5), contrast_limit=(-0.5, 0.5), p=0.5), abm.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5), abm.RandomShadow(p=0.5), abm.ChannelShuffle(p=0.5), abm.ChannelDropout(p=0.5), abm.HorizontalFlip(p=0.5), abm.ImageCompression(quality_lower=50, p=0.5), abm.Cutout(num_holes=100, max_w_size=8, max_h_size=8, p=0.5), ] data_transform = DataTransformBase(transforms=transforms, input_size=input_size, normalize=True) train_dataset = EgoRailDataset(data_path=data_path, phase="train", transform=data_transform) val_dataset = EgoRailDataset(data_path=data_path, phase="val", transform=data_transform) # train_dataset.weighted_class() weighted_values = [8.90560578, 1.53155476] train_data_loader = DataLoader( train_dataset, batch_size=config.batch_size, shuffle=True, num_workers=config.num_workers, drop_last=True, worker_init_fn=_worker_init_fn_(), ) val_data_loader = DataLoader( val_dataset, batch_size=config.batch_size, shuffle=False, num_workers=config.num_workers, drop_last=True, ) data_loaders_dict = {"train": train_data_loader, "val": val_data_loader} model = BiSeNetV2(n_classes=config.num_classes) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") criterion = OHEMCELoss(thresh=config.ohem_ce_loss_thresh, weighted_values=weighted_values) base_lr_rate = config.lr_rate / (config.batch_size * config.batch_multiplier) base_weight_decay = config.weight_decay * (config.batch_size * config.batch_multiplier) def _lambda_epoch(epoch): import math max_epoch = config.num_epochs return math.pow((1 - epoch * 1.0 / max_epoch), 0.9) optimizer = torch.optim.SGD( model.parameters(), lr=base_lr_rate, momentum=config.momentum, weight_decay=base_weight_decay, ) scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=_lambda_epoch) trainer = BiSeNetV2Trainer( model=model, criterion=criterion, metric_func=None, optimizer=optimizer, data_loaders_dict=data_loaders_dict, config=config, scheduler=scheduler, device=device, ) if config.snapshot and os.path.isfile(config.snapshot): trainer.resume_checkpoint(config.snapshot) with torch.autograd.set_detect_anomaly(True): trainer.train()
from transformers import PreTrainedTokenizerFast from tqdm.auto import tqdm train_transform = alb.Compose( [ alb.Compose( [alb.ShiftScaleRotate(shift_limit=0, scale_limit=(-.15, 0), rotate_limit=1, border_mode=0, interpolation=3, value=[255, 255, 255], p=1), alb.GridDistortion(distort_limit=0.1, border_mode=0, interpolation=3, value=[255, 255, 255], p=.5)], p=.15), # alb.InvertImg(p=.15), alb.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=0.3), alb.GaussNoise(10, p=.2), alb.RandomBrightnessContrast(.05, (-.2, 0), True, p=0.2), alb.ImageCompression(95, p=.3), alb.ToGray(always_apply=True), alb.Normalize((0.7931, 0.7931, 0.7931), (0.1738, 0.1738, 0.1738)), # alb.Sharpen() ToTensorV2(), ] ) test_transform = alb.Compose( [ alb.ToGray(always_apply=True), alb.Normalize((0.7931, 0.7931, 0.7931), (0.1738, 0.1738, 0.1738)), # alb.Sharpen() ToTensorV2(), ] )
def make_transform(args): base_transform = [ A.Resize(args.img_size, args.img_size), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ] train_transform = [] if args.Blur: train_transform.append(A.Blur(p=args.Blur)) if args.Blur: train_transform.append(A.ElasticTransform(p=args.Blur)) if args.CLAHE: train_transform.append( A.CLAHE(clip_limit=(1, 4), tile_grid_size=(8, 8), p=args.CLAHE)) if args.RandomBrightnessContrast: train_transform.append( A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, brightness_by_max=True, p=args.RandomBrightnessContrast)) if args.HueSaturationValue: train_transform.append( A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=args.HueSaturationValue)) if args.RGBShift: train_transform.append( A.RGBShift(r_shift_limit=20, g_shift_limit=20, b_shift_limit=20, p=args.RGBShift)) if args.RandomGamma: train_transform.append( A.RandomGamma(gamma_limit=(80, 120), p=args.RandomGamma)) if args.HorizontalFlip: train_transform.append(A.HorizontalFlip(p=args.HorizontalFlip)) if args.VerticalFlip: train_transform.append(A.VerticalFlip(p=args.VerticalFlip)) if args.ShiftScaleRotate: train_transform.append( A.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=10, border_mode=args.ShiftScaleRotateMode, p=args.ShiftScaleRotate)) if args.GridDistortion: train_transform.append( A.GridDistortion(num_steps=5, distort_limit=(-0.3, 0.3), p=args.GridDistortion)) if args.MotionBlur: train_transform.append( A.MotionBlur(blur_limit=(3, 7), p=args.MotionBlur)) if args.RandomResizedCrop: train_transform.append( A.RandomResizedCrop(height=args.img_size, width=args.img_size, scale=(-0.4, 1.0), ratio=(0.75, 1.3333333333333333), p=args.RandomResizedCrop)) if args.ImageCompression: train_transform.append( A.ImageCompression(quality_lower=99, quality_upper=100, p=args.ImageCompression)) train_transform.extend(base_transform) train_transform = A.Compose(train_transform) test_transform = A.Compose(base_transform) return train_transform, test_transform