def augment_data(files, save_path, augment=True): """ Performing data augmentation. """ # original_size = (960, 540) 640x360 size = (640, 360) crop_size = (640, 360) files_name = ["instrument_instances.png", "raw.png"] for idx, path in tqdm(enumerate(files), total=len(files)): f = os.listdir(path) f.sort() len_f = len(f) ## Reading Image and Mask image = os.path.join(path, files_name[1]) mask = os.path.join(path, files_name[0]) image_name = "_".join([d for d in image.split("..")[-1].split("/")]) image_name = image_name.split("_ml_dataset_")[-1].split(".")[0] mask_name = "_".join([d for d in mask.split("..")[-1].split("/")]) mask_name = mask_name.split("_ml_dataset_")[-1].split(".")[0] x = cv2.imread(image, cv2.IMREAD_COLOR) if len_f == 1: y = np.zeros((540, 960, 3)) else: y = cv2.imread(mask, cv2.IMREAD_COLOR) * 255.0 h, w, c = x.shape if augment == True: ## Crop x_min = 0 y_min = 0 x_max = x_min + size[0] y_max = y_min + size[1] aug = Crop(p=1, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) augmented = aug(image=x, mask=y) x1 = augmented['image'] y1 = augmented['mask'] ## Random Rotate 90 degree aug = RandomRotate90(p=1) augmented = aug(image=x, mask=y) x2 = augmented['image'] y2 = augmented['mask'] ## ElasticTransform aug = ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03) augmented = aug(image=x, mask=y) x3 = augmented['image'] y3 = augmented['mask'] ## Grid Distortion aug = GridDistortion(p=1) augmented = aug(image=x, mask=y) x4 = augmented['image'] y4 = augmented['mask'] ## Optical Distortion aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5) augmented = aug(image=x, mask=y) x5 = augmented['image'] y5 = augmented['mask'] ## Vertical Flip aug = VerticalFlip(p=1) augmented = aug(image=x, mask=y) x6 = augmented['image'] y6 = augmented['mask'] ## Horizontal Flip aug = HorizontalFlip(p=1) augmented = aug(image=x, mask=y) x7 = augmented['image'] y7 = augmented['mask'] ## Grayscale x8 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY) y8 = y ## Grayscale Vertical Flip aug = VerticalFlip(p=1) augmented = aug(image=x8, mask=y8) x9 = augmented['image'] y9 = augmented['mask'] ## Grayscale Horizontal Flip aug = HorizontalFlip(p=1) augmented = aug(image=x8, mask=y8) x10 = augmented['image'] y10 = augmented['mask'] ## # aug = RandomBrightnessContrast(p=1) # augmented = aug(image=x, mask=y) # x11 = augmented['image'] # y11 = augmented['mask'] # # aug = RandomGamma(p=1) # augmented = aug(image=x, mask=y) # x12 = augmented['image'] # y12 = augmented['mask'] # # aug = HueSaturationValue(p=1) # augmented = aug(image=x, mask=y) # x13 = augmented['image'] # y13 = augmented['mask'] # # aug = RGBShift(p=1) # augmented = aug(image=x, mask=y) # x14 = augmented['image'] # y14 = augmented['mask'] # # aug = RandomBrightness(p=1) # augmented = aug(image=x, mask=y) # x15 = augmented['image'] # y15 = augmented['mask'] # # aug = RandomContrast(p=1) # augmented = aug(image=x, mask=y) # x16 = augmented['image'] # y16 = augmented['mask'] # # aug = ChannelShuffle(p=1) # augmented = aug(image=x, mask=y) # x17 = augmented['image'] # y17 = augmented['mask'] aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32) augmented = aug(image=x, mask=y) x18 = augmented['image'] y18 = augmented['mask'] # aug = GaussNoise(p=1) # augmented = aug(image=x, mask=y) # x19 = augmented['image'] # y19 = augmented['mask'] # # aug = MotionBlur(p=1, blur_limit=7) # augmented = aug(image=x, mask=y) # x20 = augmented['image'] # y20 = augmented['mask'] # # aug = MedianBlur(p=1, blur_limit=10) # augmented = aug(image=x, mask=y) # x21 = augmented['image'] # y21 = augmented['mask'] # # aug = GaussianBlur(p=1, blur_limit=10) # augmented = aug(image=x, mask=y) # x22 = augmented['image'] # y22 = augmented['mask'] images = [ x, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, #x11, x12, x14, x15, x16, x17, x18 # x19, x20, x21, x22 ] masks = [ y, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, # y10, y11, y12, y14, y15, y16, y17, y18 # y19, y20, y21, y22 ] else: images = [x] masks = [y] idx = 0 for i, m in zip(images, masks): i = cv2.resize(i, size) m = cv2.resize(m, size) tmp_image_name = f"{image_name}_{idx}.jpg" tmp_mask_name = f"{mask_name}_{idx}.jpg" image_path = os.path.join(save_path, "image/", tmp_image_name) mask_path = os.path.join(save_path, "mask/", tmp_mask_name) cv2.imwrite(image_path, i) cv2.imwrite(mask_path, m) idx += 1
# GaussNoise(p=1), RandomBrightnessContrast(p=1), RandomGamma(p=1), ]), # OneOf([ # ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), # ShiftScaleRotate(p=1), # ]), # ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), # # ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), OneOf([ ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), GridDistortion(p=1), # ShiftScaleRotate(p=1), OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5) ]), RandomCrop(256, 256, p=1) ]) seg_aug_2 = Compose([ Resize(256, 256, p=1), ]) # class JointLoader(Dataset): # def __init__(self, seg_dir, simu_dir, mode, data_transform): # seg_datasets = [SegLoader(os.path.join(seg_dir, i), mode, transform=data_transform) for i in # os.listdir(seg_dir)] # self.seg_dataset = ConcatDataset(seg_datasets)
augmented = self.augmentation(image=img) img = augmented['image'] X[i, :, :, :] = img/255.0 if not self.is_test: y[i, :] = img_2_ohe_vector[image_name] return X, y def get_labels(self): if self.shuffle: images_current = self.images_list[:self.len*self.batch_size] labels = [img_2_ohe_vector[img] for img in images_current] else: labels = self.labels return np.array(labels) albumentations_train = Compose([ VerticalFlip(), HorizontalFlip(), Rotate(limit=30), GridDistortion() ], p=1) data_generator_train = DataGenenerator(train_imgs, augmentation=albumentations_train) data_generator_train_eval = DataGenenerator(train_imgs, shuffle=False) data_generator_val = DataGenenerator(val_imgs, shuffle=False) class PrAucCallback(Callback): def __init__(self, data_generator, num_workers=num_cores, early_stopping_patience=5, plateau_patience=3, reduction_rate=0.5, stage='train', checkpoints_path='checkpoints/'): super(Callback, self).__init__() self.data_generator = data_generator self.num_workers = num_workers self.class_names = ['Fish', 'Flower', 'Sugar', 'Gravel'] self.history = [[] for _ in range(len(self.class_names) + 1)] # to store per each class and also mean PR AUC self.early_stopping_patience = early_stopping_patience
def __getitem__(self, idx): if len(self.channels) < 2: raise Exception('You have to specify at least two channels.') data_info_row = self.df.iloc[idx] instance_name = '_'.join( [data_info_row['name'], str(data_info_row['position'])]) images_array, masks_array = [], [] for k in range(1, self.num_images + 1): image_path = get_filepath(self.dataset_path, data_info_row['name'], self.images_folder, instance_name + f'_{k}', file_type=self.image_type) img = filter_by_channels(read_tensor(image_path), self.channels, 1) images_array.append(img) mask_path = get_filepath(self.dataset_path, data_info_row['name'], self.masks_folder, instance_name, file_type=self.mask_type) masks_array = read_tensor(mask_path) if self.phase == 'train': aug = Compose([ RandomRotate90(), Flip(), OneOf( [ RandomSizedCrop(min_max_height=(int( self.image_size * 0.7), self.image_size), height=self.image_size, width=self.image_size), RandomBrightnessContrast(brightness_limit=0.15, contrast_limit=0.15), #MedianBlur(blur_limit=3, p=0.2), MaskDropout(p=0.6), ElasticTransform(alpha=15, sigma=5, alpha_affine=5), GridDistortion(p=0.6) ], p=0.8), ToTensor() ]) else: aug = ToTensor() ''' keys = ['image'] values = [images_array[0]] for k in range(self.num_images-1): keys.append(f'image{k}') values.append(images_array[k+1]) keys.append('mask') values.append(masks_array) #{"image" : images_array[0], "image2" : images_array[1], ..., "mask": masks_array, ...} aug_input = { keys[i] : values[i] for i in range(len(keys)) } augmented = aug(**aug_input) augmented_images = [augmented['image']] for k in range(self.num_images-1): augmented_images.append(np.transpose(augmented[f'image{k}'], ( 2, 0, 1))/255) augmented_masks = [augmented['mask']] return {'features': augmented_images, 'targets': augmented_masks, 'name': data_info_row['name'], 'position': data_info_row['position']} ''' augmented = aug(image=np.concatenate( (images_array[0], images_array[1]), axis=-1), mask=masks_array) augmented_images = [ augmented['image'][:count_channels(self.channels), :, :], augmented['image'][count_channels(self.channels):, :, :] ] augmented_masks = [augmented['mask']] return { 'features': augmented_images, 'targets': augmented_masks, 'name': data_info_row['name'], 'position': data_info_row['position'] }
labels = self.labels return np.array(labels) # 将图片划分为训练集以及测试集 train_imgs, val_imgs = train_test_split( train_df['Image'].values, test_size=0.125, stratify=train_df['Class'].map(lambda x: str(sorted(list(x)))), random_state=2019) # 数据增强 albumentations_train = Compose( [VerticalFlip(), HorizontalFlip(), Rotate(limit=20), GridDistortion()], p=1) # 定义训练数据以及验证数据集生成器 data_generator_train = DataGenenerator(train_imgs, augmentation=albumentations_train) data_generator_train_eval = DataGenenerator(train_imgs, shuffle=False) data_generator_val = DataGenenerator(val_imgs, shuffle=False) train_metric_callback = PrAucCallback(data_generator_train_eval) val_callback = PrAucCallback(data_generator_val, stage='val') def get_threshold_for_recall(y_true, y_pred, class_i, recall_threshold=0.94, precision_threshold=0.90,
def compose_augmentations(img_height, img_width, flip_p=0.5, translate_p=0.5, distort_p=0.5, color_p=0.5, overlays_p=0.15, blur_p=0.25, noise_p=0.25): # Resize resize_p = 1 if img_height != 1024 else 0 # Random sized crop if img_height == 1024: min_max_height = (896, 960) elif img_height == 512: min_max_height = (448, 480) elif img_height == 256: min_max_height = (224, 240) else: raise NotImplementedError return Compose([ Resize(p=resize_p, height=img_height, width=img_width), OneOf([ HorizontalFlip(p=0.5), VerticalFlip(p=0.5), Transpose(p=0.5), RandomRotate90(p=0.5), ], p=flip_p), OneOf([ Rotate(p=0.25, limit=10), RandomSizedCrop(p=0.5, min_max_height=min_max_height, height=img_height, width=img_width), OneOrOther(IAAAffine(p=0.1, translate_percent=0.05), IAAPerspective(p=0.1)), ], p=translate_p), OneOf([ ElasticTransform(p=0.5, alpha=10, sigma=img_height * 0.05, alpha_affine=img_height * 0.03, approximate=True), GridDistortion(p=0.5), OpticalDistortion(p=0.5), IAAPiecewiseAffine(p=0.25, scale=(0.01, 0.03)), ], p=distort_p), OneOrOther( OneOf([ CLAHE(p=0.5), RandomGamma(p=0.5), RandomContrast(p=0.5), RandomBrightness(p=0.5), RandomBrightnessContrast(p=0.5), ], p=color_p), OneOf([IAAEmboss(p=0.1), IAASharpen(p=0.1), IAASuperpixels(p=0)], p=overlays_p)), OneOrOther( OneOf([ Blur(p=0.2), MedianBlur(p=0.1), MotionBlur(p=0.1), GaussianBlur(p=0.1), ], p=blur_p), OneOf([GaussNoise(p=0.2), IAAAdditiveGaussianNoise(p=0.1)], p=noise_p)), Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensor(sigmoid=False), ])
def augment_data(images, masks, save_path, augment=True): """ Performing data augmentation. """ crop_size = (192 - 32, 256 - 32) size = (256, 192) for image, mask in tqdm(zip(images, masks), total=len(images)): image_name = image.split("/")[-1].split(".")[0] mask_name = mask.split("/")[-1].split(".")[0] x, y = read_data(image, mask) try: h, w, c = x.shape except Exception as e: image = image[:-1] x, y = read_data(image, mask) h, w, c = x.shape if augment == True: ## Center Crop aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1]) augmented = aug(image=x, mask=y) x1 = augmented['image'] y1 = augmented['mask'] ## Crop x_min = 0 y_min = 0 x_max = x_min + size[0] y_max = y_min + size[1] aug = Crop(p=1, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) augmented = aug(image=x, mask=y) x2 = augmented['image'] y2 = augmented['mask'] ## Random Rotate 90 degree aug = RandomRotate90(p=1) augmented = aug(image=x, mask=y) x3 = augmented['image'] y3 = augmented['mask'] ## Transpose aug = Transpose(p=1) augmented = aug(image=x, mask=y) x4 = augmented['image'] y4 = augmented['mask'] ## ElasticTransform aug = ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03) augmented = aug(image=x, mask=y) x5 = augmented['image'] y5 = augmented['mask'] ## Grid Distortion aug = GridDistortion(p=1) augmented = aug(image=x, mask=y) x6 = augmented['image'] y6 = augmented['mask'] ## Optical Distortion aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5) augmented = aug(image=x, mask=y) x7 = augmented['image'] y7 = augmented['mask'] ## Vertical Flip aug = VerticalFlip(p=1) augmented = aug(image=x, mask=y) x8 = augmented['image'] y8 = augmented['mask'] ## Horizontal Flip aug = HorizontalFlip(p=1) augmented = aug(image=x, mask=y) x9 = augmented['image'] y9 = augmented['mask'] ## Grayscale x10 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY) y10 = y ## Grayscale Vertical Flip aug = VerticalFlip(p=1) augmented = aug(image=x10, mask=y10) x11 = augmented['image'] y11 = augmented['mask'] ## Grayscale Horizontal Flip aug = HorizontalFlip(p=1) augmented = aug(image=x10, mask=y10) x12 = augmented['image'] y12 = augmented['mask'] ## Grayscale Center Crop aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1]) augmented = aug(image=x10, mask=y10) x13 = augmented['image'] y13 = augmented['mask'] ## aug = RandomBrightnessContrast(p=1) augmented = aug(image=x, mask=y) x14 = augmented['image'] y14 = augmented['mask'] aug = RandomGamma(p=1) augmented = aug(image=x, mask=y) x15 = augmented['image'] y15 = augmented['mask'] aug = HueSaturationValue(p=1) augmented = aug(image=x, mask=y) x16 = augmented['image'] y16 = augmented['mask'] aug = RGBShift(p=1) augmented = aug(image=x, mask=y) x17 = augmented['image'] y17 = augmented['mask'] aug = RandomBrightness(p=1) augmented = aug(image=x, mask=y) x18 = augmented['image'] y18 = augmented['mask'] aug = RandomContrast(p=1) augmented = aug(image=x, mask=y) x19 = augmented['image'] y19 = augmented['mask'] aug = MotionBlur(p=1, blur_limit=7) augmented = aug(image=x, mask=y) x20 = augmented['image'] y20 = augmented['mask'] aug = MedianBlur(p=1, blur_limit=11) augmented = aug(image=x, mask=y) x21 = augmented['image'] y21 = augmented['mask'] aug = GaussianBlur(p=1, blur_limit=11) augmented = aug(image=x, mask=y) x22 = augmented['image'] y22 = augmented['mask'] aug = GaussNoise(p=1) augmented = aug(image=x, mask=y) x23 = augmented['image'] y23 = augmented['mask'] aug = ChannelShuffle(p=1) augmented = aug(image=x, mask=y) x24 = augmented['image'] y24 = augmented['mask'] aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32) augmented = aug(image=x, mask=y) x25 = augmented['image'] y25 = augmented['mask'] images = [ x, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25 ] masks = [ y, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20, y21, y22, y23, y24, y25 ] else: images = [x] masks = [y] idx = 0 for i, m in zip(images, masks): i = cv2.resize(i, size) m = cv2.resize(m, size) tmp_image_name = f"{image_name}_{idx}.bmp" tmp_mask_name = f"{mask_name}_{idx}.bmp" image_path = os.path.join(save_path, "image/", tmp_image_name) mask_path = os.path.join(save_path, "mask/", tmp_mask_name) cv2.imwrite(image_path, i) cv2.imwrite(mask_path, m) idx += 1
def train_transform(self): # albumentations transforms cfg at_cfg = self.cfg.abtfs height, width = self.img_size transforms_list = [Resize(height, width), Flip()] # random_grid_shuffle if at_cfg.random_grid_shuffle.enable: grid = at_cfg.random_grid_shuffle.grid grid = (grid, grid) transforms_list.append(RandomGridShuffle((grid))) # channel_shuffle if at_cfg.channel_shuffle.enable: transforms_list.append(ChannelShuffle(p=1)) # channel_dropout if at_cfg.channel_dropout.enable: drop_range = at_cfg.channel_dropout.drop_range fill_value = at_cfg.channel_dropout.fill_value transforms_list.append(ChannelDropout(drop_range, fill_value, p=1)) # noise if at_cfg.noise.enable: transforms_list.append( OneOf([ IAAAdditiveGaussianNoise(), GaussNoise(), ], p=1)) # blur if at_cfg.blur.enable: transforms_list.append( OneOf([ MotionBlur(), Blur(blur_limit=3, ), ], p=1)) # rotate if at_cfg.rotate.enable: transforms_list.append( ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=1)) # distortion if at_cfg.distortion.enable: transforms_list.append( OneOf([ OpticalDistortion(p=0.3), GridDistortion(p=.3), ], p=1)) # bright if at_cfg.bright.enable: transforms_list.append( OneOf([ CLAHE(clip_limit=2), RandomBrightnessContrast(p=0.8), ], p=1)) # hue color if at_cfg.hue.enable: transforms_list.append(HueSaturationValue(p=0.3)) # cutout if at_cfg.cutout.enable: num_holes = at_cfg.cutout.num_holes size = at_cfg.cutout.size fill_value = at_cfg.cutout.fill_value transforms_list.append(Cutout(num_holes, size, size, fill_value, 1)) transforms_list.append(self.normalize) transforms_list.append(ToTensor()) return Compose(transforms_list)
def imgClassificationfolder(self, img_path, model_list=None, tfms=True, advance_augmentation=False, size=224, bs=16, epochs=1, test_size=0.3): """ RETURNS MODEL model_list: list of names of all models that need to be trained Call Model_Names() function to get the list of available models img_path: path to the images root folder containing the individual image folders tfms: augmentation transforms. Possible methods are True or False advance_augmentation: Should we apply advance data augmentation? size: individual image size in dataloader. Default size set to 224 bs:batch size epochs:number of epochs for which the individual models need to be trained test_size:test size for ranking the models """ dictionary = {} dictionary["models"] = [ resnet18, resnet34, resnet50, resnet101, resnet152, densenet121, densenet169, densenet201, densenet161, vgg16_bn, vgg19_bn ] dictionary["model_names"] = [ 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'densenet121', 'densenet169', 'densenet201', 'densenet161', 'vgg16_bn', 'vgg19_bn' ] dictionary["efficientnets"] = [ "efficientnet-b{}".format(i) for i in range(0, 8) ] accuracy = [] roc_auc = [] list_names = [] path = '' if model_list is None: model_list = dictionary["model_names"] if tfms == True: if advance_augmentation: tfms = get_transforms(do_flip=True, max_lighting=0.2, max_zoom=1.1, max_warp=0.15, max_rotate=45, xtra_tfms=[ alb_tfm2fastai(ElasticTransform()), alb_tfm2fastai(GridDistortion()), alb_tfm2fastai(OpticalDistortion()), alb_tfm2fastai(MedianBlur()), alb_tfm2fastai(ToGray()), alb_tfm2fastai(JpegCompression()), alb_tfm2fastai(Transpose()), alb_tfm2fastai(ShiftScaleRotate()), alb_tfm2fastai(Normalize()), alb_tfm2fastai(Blur()), alb_tfm2fastai(CLAHE()), alb_tfm2fastai(RGBShift()), alb_tfm2fastai(ChannelShuffle()), alb_tfm2fastai(RandomContrast()), alb_tfm2fastai(RandomBrightness()) ]) else: tfms = get_transforms(do_flip=True, max_lighting=0.2, max_zoom=1.1, max_warp=0.15, max_rotate=45) else: tfms = False data = ImageDataBunch.from_folder( img_path, valid_pct=test_size, ds_tfms=tfms, size=224, num_workers=4).normalize(imagenet_stats) print("Loaded dataset\n") print("Labels classified from the source are {}\n".format( data.classes)) max_score = 0 for model in model_list: if model in dictionary["model_names"]: print("Started training {}\n".format(model)) acc, auroc, names, model = self.res_dense_vgg( data, eval(model), epochs) accuracy.append(acc) roc_auc.append(auroc) list_names.append(names) if model in dictionary['efficientnets']: print("Started training {}\n".format(model)) acc, auroc, names, model = self.eff(data, model, epochs) accuracy.append(acc) roc_auc.append(auroc) list_names.append(names) if acc > max_score: best_model = model max_score = acc else: del model df = pd.DataFrame(list(zip(model_list, accuracy, roc_auc)), columns=['Model', 'Accuracy', 'ROAUC']) df.sort_values('Accuracy', inplace=True, ascending=False) self.plot_table(df) return best_model
def augment_data(images, masks, augment=True): """ Performing data augmentation. """ crop_size = (192 - 32, 256 - 32) size = (512, 384) list_of_img = [] list_of_mask = [] for image, mask in zip(images, masks): image_name = image.split("/")[-1].split(".")[0] mask_name = mask.split("/")[-1].split(".")[0] x, y = read_data(image, mask) try: h, w, c = x.shape except Exception as e: image = image[:-1] x, y = read_data(image, mask) h, w, c = x.shape if augment == True: # Center Crop aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1]) augmented = aug(image=x, mask=y) x1 = augmented['image'] y1 = augmented['mask'] # Crop x_min = 0 y_min = 0 x_max = x_min + size[0] y_max = y_min + size[1] aug = Crop(p=1, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) augmented = aug(image=x, mask=y) x2 = augmented['image'] y2 = augmented['mask'] # Random Rotate 90 degree aug = RandomRotate90(p=1) augmented = aug(image=x, mask=y) x3 = augmented['image'] y3 = augmented['mask'] # Transpose aug = Transpose(p=1) augmented = aug(image=x, mask=y) x4 = augmented['image'] y4 = augmented['mask'] # ElasticTransform aug = ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03) augmented = aug(image=x, mask=y) x5 = augmented['image'] y5 = augmented['mask'] # Grid Distortion aug = GridDistortion(p=1) augmented = aug(image=x, mask=y) x6 = augmented['image'] y6 = augmented['mask'] # Optical Distortion aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5) augmented = aug(image=x, mask=y) x7 = augmented['image'] y7 = augmented['mask'] # Vertical Flip aug = VerticalFlip(p=1) augmented = aug(image=x, mask=y) x8 = augmented['image'] y8 = augmented['mask'] # Horizontal Flip aug = HorizontalFlip(p=1) augmented = aug(image=x, mask=y) x9 = augmented['image'] y9 = augmented['mask'] """## Grayscale x10 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY) y10 = y ## Grayscale Vertical Flip aug = VerticalFlip(p=1) augmented = aug(image=x10, mask=y10) x11 = augmented['image'] y11 = augmented['mask'] ## Grayscale Horizontal Flip aug = HorizontalFlip(p=1) augmented = aug(image=x10, mask=y10) x12 = augmented['image'] y12 = augmented['mask'] ## Grayscale Center Crop aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[1]) augmented = aug(image=x10, mask=y10) x13 = augmented['image'] y13 = augmented['mask'] """ ## aug = RandomBrightnessContrast(p=1) augmented = aug(image=x, mask=y) x14 = augmented['image'] y14 = augmented['mask'] aug = RandomGamma(p=1) augmented = aug(image=x, mask=y) x15 = augmented['image'] y15 = augmented['mask'] aug = HueSaturationValue(p=1) augmented = aug(image=x, mask=y) x16 = augmented['image'] y16 = augmented['mask'] aug = RGBShift(p=1) augmented = aug(image=x, mask=y) x17 = augmented['image'] y17 = augmented['mask'] aug = RandomBrightness(p=1) augmented = aug(image=x, mask=y) x18 = augmented['image'] y18 = augmented['mask'] aug = RandomContrast(p=1) augmented = aug(image=x, mask=y) x19 = augmented['image'] y19 = augmented['mask'] aug = MotionBlur(p=1, blur_limit=7) augmented = aug(image=x, mask=y) x20 = augmented['image'] y20 = augmented['mask'] aug = MedianBlur(p=1, blur_limit=9) augmented = aug(image=x, mask=y) x21 = augmented['image'] y21 = augmented['mask'] aug = GaussianBlur(p=1, blur_limit=9) augmented = aug(image=x, mask=y) x22 = augmented['image'] y22 = augmented['mask'] aug = GaussNoise(p=1) augmented = aug(image=x, mask=y) x23 = augmented['image'] y23 = augmented['mask'] aug = ChannelShuffle(p=1) augmented = aug(image=x, mask=y) x24 = augmented['image'] y24 = augmented['mask'] aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32) augmented = aug(image=x, mask=y) x25 = augmented['image'] y25 = augmented['mask'] images = [ x, x1, x2, x3, x4, x5, x6, x7, x8, x9, # x10,x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25 ] masks = [ y, y1, y2, y3, y4, y5, y6, y7, y8, y9, # y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20, y21, y22, y23, y24, y25 ] images = list(map(lambda a: cv2.resize(a, size), images)) masks = list(map(lambda a: cv2.resize(a, size), masks)) else: images = [cv2.resize(x, size)] masks = [cv2.resize(y, size)] list_of_img = list_of_img + images list_of_mask = list_of_mask + masks return list_of_img, list_of_mask
def _get_data_loader(imgs, trn_df, vld_df): from albumentations import ( Rotate, HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90, Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue, IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, RandomBrightnessContrast, IAAPiecewiseAffine, IAASharpen, IAAEmboss, Flip, OneOf, Compose) from albumentations.pytorch import ToTensor, ToTensorV2 train_transforms = Compose([ Rotate(20), OneOf([ IAAAdditiveGaussianNoise(), GaussNoise(), ], p=0.2), OneOf([ MotionBlur(p=.2), MedianBlur(blur_limit=3, p=0.1), Blur(blur_limit=3, p=0.1), ], p=0.2), ShiftScaleRotate( shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2), OneOf([ OpticalDistortion(p=0.3), GridDistortion(p=.1), IAAPiecewiseAffine(p=0.3), ], p=0.2), OneOf([ CLAHE(clip_limit=2), IAASharpen(), IAAEmboss(), RandomBrightnessContrast(), ], p=0.3), HueSaturationValue(p=0.3), ToTensor() ], p=1.0) valid_transforms = Compose([ToTensor()]) from torch.utils.data import Dataset, DataLoader trn_dataset = BangaliDataset(imgs=imgs, label_df=trn_df, transform=train_transforms) vld_dataset = BangaliDataset(imgs=imgs, label_df=vld_df, transform=valid_transforms) trn_loader = DataLoader(trn_dataset, shuffle=True, num_workers=NUM_WORKERS, batch_size=BATCH_SIZE) vld_loader = DataLoader(vld_dataset, shuffle=False, num_workers=NUM_WORKERS, batch_size=BATCH_SIZE) return trn_loader, vld_loader
# mix = batch1 * masks + batch2 * (1 - masks) # image = torch.cat((soft_masks, masks, batch1, batch2, mix), 0) # save_image(image, 'fmix_example.png', nrow=NUM_IMAGES, pad_value=1) # # plt.figure(figsize=(NUM_IMAGES, 5)) # plt.imshow(make_grid(image, nrow=NUM_IMAGES, pad_value=5).permute(1, 2, 0).numpy()) # plt.show() # _ = plt.axis('off') # data, target = fmix(batch, target, alpha=1., decay_power=3., shape=(260, 260)) # idx = np.random.randint(0, len(data)) # img_org = batch[idx] # new_img = data[idx] # plt.subplot(121) # plt.imshow(img_org.permute(1, 2, 0)) # plt.subplot(122) # plt.imshow(new_img.permute(1, 2, 0)) # plt.show() img = read_image(os.path.join(DATA_PATH, DataID, 'image/1.jpg')) train_transformation = Compose([ # RandomRotate90(), GridDistortion(p=1.), # ElasticTransform(alpha=1, sigma=25, alpha_affine=50, p=1.), ]) img2 = train_transformation(image=img)['image'] plt.subplot(121) plt.imshow(img) plt.subplot(122) plt.imshow(img2) plt.show()
def aug_test(): def get_bb_points(msk): h, w = msk.shape x0 = 0 x1 = msk.shape[1] y0 = 0 y1 = msk.shape[0] for i in range(w): if msk[:, i].max() > 200: x0 = i break for i in range(w): if msk[:, msk.shape[1] - i - 1].max() > 200: x1 = msk.shape[1] - i - 1 break for i in range(h): if msk[i, :].max() > 200: y0 = i break for i in range(h): if msk[msk.shape[0] - i - 1, :].max() > 200: y1 = msk.shape[0] - i - 1 break return (x0, y0), (x1, y1) image_name = '7aea0b3e2.jpg' p1, p2 = (12, 84), (391, 248) img = imread(f'../DATA/aug_test/src/{image_name}') h = 300 alpha, sigma, alpha_affine = h * 2, h * 0.08, h * 0.08 augs = { '1_IAAAdditiveGaussianNoise': IAAAdditiveGaussianNoise(scale=(0.01 * 255, 0.05 * 255), p=1.0), '1_GaussNoise': GaussNoise(var_limit=(20, 120), p=1.0), '1_RandomGamma': RandomGamma(gamma_limit=(80, 120), p=1.0), '2_RandomBrightnessContrast': RandomBrightnessContrast(p=1.0), '2_MotionBlur': MotionBlur(p=1.0), '2_MedianBlur': MedianBlur(blur_limit=6, p=1.0), '2_Blur': Blur(blur_limit=9, p=1.0), '2_IAASharpen': IAASharpen(p=1.0), '2_IAAEmboss': IAAEmboss(p=1.0), '2_IAASuperpixels': IAASuperpixels(n_segments=50, p_replace=0.05, p=1.0), '3_CLAHE': CLAHE(clip_limit=8, p=1.0), '3_RGBShift': RGBShift(p=1.0), '3_ChannelShuffle': ChannelShuffle(p=1.0), '3_HueSaturationValue': HueSaturationValue(p=1.0), '3_ToGray': ToGray(p=1.0), '4_OpticalDistortion': OpticalDistortion(border_mode=cv2.BORDER_CONSTANT, p=1.0), '4_GridDistortion': GridDistortion(border_mode=cv2.BORDER_CONSTANT, p=1.0), '4_IAAPiecewiseAffine': IAAPiecewiseAffine(nb_rows=4, nb_cols=4, p=1.0), '4_IAAPerspective': IAAPerspective(p=1.0), '4_IAAAffine': IAAAffine(mode='constant', p=1.0), '4_ElasticTransform': ElasticTransform(alpha=alpha, sigma=sigma, alpha_affine=alpha_affine, border_mode=cv2.BORDER_CONSTANT, p=1.0) } # im_merge.shape[1] * 2, im_merge.shape[1] * 0.08, im_merge.shape[1] * 0.08 for aug in augs: mask = np.zeros(img.shape[:2], dtype=np.uint8) cv2.rectangle(mask, p1, p2, 255, 2) data = {"image": img.copy(), 'mask': mask} augmented = augs[aug](**data) augimg = augmented['image'] draw_shadow_text(augimg, f'{aug}', (5, 15), 0.5, (255, 255, 255), 1) ap1, ap2 = get_bb_points(augmented['mask']) cv2.rectangle(augimg, ap1, ap2, (0, 255, 0), 2) imsave(f'../DATA/aug_test/aug/{aug}-{image_name}', augimg)
def __init__(self, args): self.args = args self.args.start_epoch = 0 self.args.cuda = True # data transforms input_transform = transform.Compose([ transform.ToTensor(), transform.Normalize([.490, .490, .490], [.247, .247, .247]) ]) # TODO: change mean and std # dataset train_chain = Compose([ HorizontalFlip(p=0.5), OneOf([ ElasticTransform( alpha=300, sigma=300 * 0.05, alpha_affine=300 * 0.03), GridDistortion(), OpticalDistortion(distort_limit=2, shift_limit=0.5), ], p=0.3), RandomSizedCrop( min_max_height=(900, 1024), height=1024, width=1024, p=0.5), ShiftScaleRotate(rotate_limit=20, p=0.5), Resize(self.args.size, self.args.size) ], p=1) val_chain = Compose([Resize(self.args.size, self.args.size)], p=1) num_fold = self.args.num_fold df_train = pd.read_csv(os.path.join(args.imagelist_path, 'train.csv')) df_val = pd.read_csv(os.path.join(args.imagelist_path, 'val.csv')) df_full = pd.concat((df_train, df_val), ignore_index=True, axis=0) df_full['lbl'] = (df_full['mask_name'].astype(str) == '-1').astype(int) skf = StratifiedKFold(8, shuffle=True, random_state=777) train_ids, val_ids = list( skf.split(df_full['mask_name'], df_full['lbl']))[num_fold] df_test = pd.read_csv( os.path.join(args.imagelist_path, 'test_true.csv')) df_new_train = pd.concat((df_full.iloc[train_ids], df_test), ignore_index=True, axis=0, sort=False) df_new_val = df_full.iloc[val_ids] df_new_train.to_csv(f'/tmp/train_new_pneumo_{num_fold}.csv') df_new_val.to_csv(f'/tmp/val_new_pneumo_{num_fold}.csv') trainset = SegmentationDataset(f'/tmp/train_new_pneumo_{num_fold}.csv', args.image_path, args.masks_path, input_transform=input_transform, transform_chain=train_chain, base_size=1024) testset = SegmentationDataset(f'/tmp/val_new_pneumo_{num_fold}.csv', args.image_path, args.masks_path, input_transform=input_transform, transform_chain=val_chain, base_size=1024) imgs = trainset.mask_img_map[:, [0, 3]] weights = make_weights_for_balanced_classes(imgs, 2) weights = torch.DoubleTensor(weights) train_sampler = (torch.utils.data.sampler.WeightedRandomSampler( weights, len(weights))) # dataloader kwargs = {'num_workers': args.workers, 'pin_memory': True} self.trainloader = data.DataLoader( trainset, batch_size=args.batch_size, drop_last=True, sampler=train_sampler, #shuffle=True, **kwargs) self.valloader = data.DataLoader(testset, batch_size=args.batch_size, drop_last=False, shuffle=False, **kwargs) self.nclass = 1 if self.args.model == 'unet': model = UNet(n_classes=self.nclass, norm_layer=SyncBatchNorm) params_list = [ { 'params': model.parameters(), 'lr': args.lr }, ] elif self.args.model == 'encnet': model = EncNet( nclass=self.nclass, backbone=args.backbone, aux=args.aux, se_loss=args.se_loss, norm_layer=SyncBatchNorm #nn.BatchNorm2d ) # optimizer using different LR params_list = [ { 'params': model.pretrained.parameters(), 'lr': args.lr }, ] if hasattr(model, 'head'): params_list.append({ 'params': model.head.parameters(), 'lr': args.lr * 10 }) if hasattr(model, 'auxlayer'): params_list.append({ 'params': model.auxlayer.parameters(), 'lr': args.lr * 10 }) print(model) optimizer = torch.optim.SGD(params_list, lr=args.lr, momentum=0.9, weight_decay=args.wd) # criterions if self.nclass == 1: self.criterion = SegmentationLossesBCE(se_loss=args.se_loss, aux=args.aux, nclass=self.nclass, se_weight=args.se_weight, aux_weight=args.aux_weight, use_dice=args.use_dice) else: self.criterion = SegmentationLosses( se_loss=args.se_loss, aux=args.aux, nclass=self.nclass, se_weight=args.se_weight, aux_weight=args.aux_weight, ) self.model, self.optimizer = model, optimizer self.best_pred = 0.0 self.model = DataParallelModel(self.model).cuda() self.criterion = DataParallelCriterion(self.criterion).cuda() # resuming checkpoint if args.resume is not None: if not os.path.isfile(args.resume): raise RuntimeError("=> no checkpoint found at '{}'".format( args.resume)) checkpoint = torch.load(args.resume) #, map_location='cpu') self.args.start_epoch = checkpoint['epoch'] state_dict = {k: v for k, v in checkpoint['state_dict'].items()} self.model.load_state_dict(state_dict) self.optimizer.load_state_dict(checkpoint['optimizer']) for g in self.optimizer.param_groups: g['lr'] = args.lr self.best_pred = checkpoint['best_pred'] print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) print(f'Best dice: {checkpoint["best_pred"]}') print(f'LR: {get_lr(self.optimizer):.5f}') self.scheduler = ReduceLROnPlateau(self.optimizer, mode='min', factor=0.8, patience=4, threshold=0.001, threshold_mode='abs', min_lr=0.00001) self.logger = Logger(args.logger_dir) self.step_train = 0 self.best_loss = 20 self.step_val = 0
# ### Augmentations # For testing the 1 image, don't worry about augmentations. # In[17]: #aug = () # In[105]: aug = Compose([ OneOf([ ElasticTransform( p=1, alpha=200, sigma=200 * 0.05, alpha_affine=200 * 0.03), GridDistortion(p=1, border_mode=0, value=5) ], p=0.8), HorizontalFlip(p=0.5), RandomGamma(p=0.8) ]) # ### Training Data Generator - Train & Val # Define function # In[101]: # import os # os.environ["CUDA_VISIBLE_DEVICES"] = '1' # import numpy as np # linear algebra
) rgb_file_path = f"{args.root}/train_rgb.csv" nir_file_path = f"{args.root}/train_b08.csv" additional_targets = { f"image{n}": "image" for n, _ in enumerate(dates[:-1]) } data_transform = Compose( [ RandomCrop(128, 128), VerticalFlip(p=0.3), Transpose(p=0.3), ShiftScaleRotate(p=0.3), RandomRotate90(p=0.3), GridDistortion(p=0.3), ElasticTransform(p=0.3), Normalize(), ], additional_targets=additional_targets, ) val_transform = Compose( [RandomCrop(128, 128), Normalize()], additional_targets=additional_targets) #fold_sets = [[(0, 1, 2), (0, 1, 2)]] fold_sets = [[(1, 2), (0, )], [(0, 2), (1, )], [(0, 1), (2, )]] # # KOSTIL' ALERT for i, fold_set in enumerate(fold_sets): logdir = f"./logs/3d_resnet_random_pad/fold{i}"
def albumentations_transforms( crop_size, shorter_side, low_scale, high_scale, img_mean, img_std, img_scale, ignore_label, num_stages, dataset_type, ): from albumentations import ( Normalize, HorizontalFlip, RandomRotate90, # my addition RandomBrightnessContrast, # my addition CLAHE, # my addition RandomGamma, # my addition ElasticTransform, # my addition GridDistortion, # my addition MotionBlur, # my addition RandomCrop, PadIfNeeded, RandomScale, LongestMaxSize, SmallestMaxSize, OneOf, ) from albumentations.pytorch import ToTensorV2 as ToTensor from densetorch.data import albumentations2densetorch if dataset_type == "densetorch": wrapper = albumentations2densetorch elif dataset_type == "torchvision": wrapper = albumentations2torchvision else: raise ValueError(f"Unknown dataset type: {dataset_type}") common_transformations = [ Normalize(max_pixel_value=1.0 / img_scale, mean=img_mean, std=img_std), ToTensor(), ] train_transforms = [] for stage in range(num_stages): train_transforms.append( wrapper([ ChangeBackground("../backgrounds", p=0.5), # my addition MotionBlur(p=0.5), OneOf([ RandomScale(scale_limit=(low_scale[stage], high_scale[stage])), LongestMaxSize(max_size=shorter_side[stage]), SmallestMaxSize(max_size=shorter_side[stage]), ]), PadIfNeeded( min_height=crop_size[stage], min_width=crop_size[stage], border_mode=cv2.BORDER_CONSTANT, value=np.array(img_mean) / img_scale, mask_value=ignore_label, ), HorizontalFlip(p=0.5, ), RandomRotate90(p=0.5), RandomBrightnessContrast( p=.8), # only applies to images, not masks RandomGamma(p=0.8), # only applies to images OneOf( [ ElasticTransform(p=0.5, alpha=120, sigma=500 * 0.05, alpha_affine=500 * 0.03), GridDistortion(p=0.5), # A.OpticalDistortion(distort_limit=1, shift_limit=0.5, p=1), ], p=.5), RandomCrop( height=crop_size[stage], width=crop_size[stage], ), ] + common_transformations)) val_transforms = wrapper(common_transformations) return train_transforms, val_transforms
def train_transform(self): height, width = self.img_size transforms_list = [Resize(height, width), Flip()] # random_grid_shuffle if self.random_grid_shuffle['enable']: grid = self.random_grid_shuffle['grid'] grid = (grid, grid) transforms_list.append(RandomGridShuffle((grid))) # channel_shuffle if self.channel_shuffle['enable']: transforms_list.append(ChannelShuffle(p=1)) # channel_dropout if self.channel_dropout['enable']: drop_range = self.channel_dropout.drop_range fill_value = self.channel_dropout.fill_value transforms_list.append(ChannelDropout(drop_range, fill_value, p=1)) # noise if self.noise['enable']: transforms_list.append( OneOf([ IAAAdditiveGaussianNoise(), GaussNoise(), ], p=1)) # blur if self.blur['enable']: transforms_list.append( OneOf([ MotionBlur(), Blur(blur_limit=3, ), ], p=1)) # rotate if self.rotate['enable']: params = { key: value for key, value in self.rotate.items() if key != 'enable' } transforms_list.append(ShiftScaleRotate(**params)) # distortion if self.distortion['enable']: transforms_list.append( OneOf([ OpticalDistortion(p=0.3), GridDistortion(p=.3), ], p=1)) # bright if self.bright['enable']: transforms_list.append( OneOf([ CLAHE(clip_limit=self.bright['clip_limit']), RandomBrightnessContrast(p=0.8), ], p=1)) # hue color if self.hue['enable']: transforms_list.append(HueSaturationValue(p=0.3)) # cutout if self.cutout['enable']: num_holes = self.cutout['num_holes'] size = self.cutout['size'] fill_value = self.cutout['fill_value'] transforms_list.append(Cutout(num_holes, size, size, fill_value, 1)) transforms_list.append(self.normalize) transforms_list.append(ToTensor()) return Compose(transforms_list)
transform = iaa.CropAndPad(percent=(-0.25, 0.25)) transformed_image = transform(image=image) elif augmentation == 'random_resized_crop': transform = RandomResizedCrop(always_apply=True, width=100, height=100) transformed_image = transform(image=image)['image'] elif augmentation == 'random_sized_crop': transform = RandomSizedCrop(always_apply=True, height=500, width=500, min_max_height=[200, 200]) transformed_image = transform(image=image)['image'] ## Distortion elif augmentation == 'grid_distortion': transform = GridDistortion(always_apply=True, distort_limit=0.5) transformed_image = transform(image=image)['image'] elif augmentation == 'optical_distortion': transform = OpticalDistortion(always_apply=True, distort_limit=0.5) transformed_image = transform(image=image)['image'] elif augmentation == 'random_grid_shuffle': transform = RandomGridShuffle(always_apply=True, grid=(5, 5)) transformed_image = transform(image=image)['image'] elif augmentation == 'elastic_transformation': transform = iaa.ElasticTransformation(alpha=(0, 10.0), sigma=0.25) transformed_image = transform(image=image) elif augmentation == 'elastic_transform':
def get_augs(): light_augs = Compose([ HorizontalFlip(p=1), Blur(blur_limit=3, p=1), RandomContrast(limit=0.3, p=0.5), RandomGamma(gamma_limit=(50, 100), p=0.5), RandomBrightness(limit=0.5, p=0.5), HueSaturationValue(hue_shift_limit=3, sat_shift_limit=10, val_shift_limit=5, p=.9), CLAHE(p=1.0, clip_limit=2.0) ]) heavy_augs = Compose([ OneOf([ ElasticTransform( p=0.5, alpha=40, sigma=120 * 0.05, alpha_affine=120 * 0.03), GridDistortion(p=0.5), OpticalDistortion(p=0.5, distort_limit=0.2, shift_limit=0.5) ], p=0.8), OneOf([ RandomSizedCrop( min_max_height=(200, 240), height=256, width=256, p=0.5), PadIfNeeded(min_height=256, min_width=256, p=0.5) ], p=1), HorizontalFlip(p=1), Blur(blur_limit=3, p=1), RandomContrast(limit=0.3, p=0.5), RandomGamma(gamma_limit=(50, 100), p=0.5), RandomBrightness(limit=0.5, p=0.5), HueSaturationValue(hue_shift_limit=3, sat_shift_limit=10, val_shift_limit=5, p=.9), CLAHE(p=1.0, clip_limit=2.0) ]) psn_augs = Compose([ OneOf([ ElasticTransform( p=0.5, alpha=40, sigma=120 * 0.05, alpha_affine=120 * 0.03), GridDistortion(p=0.5), OpticalDistortion(p=0.5, distort_limit=0.2, shift_limit=0.5) ], p=0.8), OneOf([ RandomSizedCrop( min_max_height=(200, 240), height=240, width=240, p=0.5), PadIfNeeded(min_height=240, min_width=240, p=0.5) ], p=1), HorizontalFlip(p=1), Blur(blur_limit=3, p=1), RandomContrast(limit=0.3, p=0.5), RandomGamma(gamma_limit=(50, 100), p=0.5), RandomBrightness(limit=0.5, p=0.5), HueSaturationValue(hue_shift_limit=3, sat_shift_limit=10, val_shift_limit=5, p=.9), CLAHE(p=1.0, clip_limit=2.0) ]) return light_augs, heavy_augs, psn_augs
df = pd.read_csv('class_map_corrected.csv') # df = pd.read_csv('class_map.csv') graphemes_df = df.loc[df['component_type'] == 'grapheme_root'].reset_index() vowel_df = df.loc[df['component_type'] == 'vowel_diacritic'].reset_index() consonant_df = df.loc[df['component_type'] == 'consonant_diacritic'].reset_index() AUGMENTATIONS = Compose([ ShiftScaleRotate(shift_limit=(-0.01, 0.01), scale_limit=(-0.001, 0.001), rotate_limit=(-5, 5), p=0.8), GridDistortion(always_apply=False, p=1.0, num_steps=4, distort_limit=(-0.2, 0.2)), ElasticTransform(always_apply=False, p=0.2, alpha=0.3, sigma=10, alpha_affine=5), ]) df_new = pd.DataFrame(columns=list(df_train.columns)) idx = 200840 n_augs = 10 font_1 = 'Nikosh.ttf' font_2 = 'Siyamrupali_1.ttf' font_3 = 'kalpurush-2.ttf' font_4 = 'Bangla.ttf'
mask_transposed = augmented['mask'] visualize(image_transposed, mask_transposed, original_image=image, original_mask=mask) #%% aug = ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03) augmented = aug(image=image, mask=mask) image_elastic = augmented['image'] mask_elastic = augmented['mask'] visualize(image_elastic, mask_elastic, original_image=image, original_mask=mask) #%% aug = GridDistortion(p=1) augmented = aug(image=image, mask=mask) image_grid = augmented['image'] mask_grid = augmented['mask'] visualize(image_grid, mask_grid, original_image=image, original_mask=mask) #%% aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5) augmented = aug(image=image, mask=mask) image_optical = augmented['image'] mask_optical = augmented['mask']
def __getitem__(self, idx): if len(self.channels) < 2: raise Exception('You have to specify at least two channels.') data_info_row = self.df.iloc[idx] instance_name = '_'.join( [data_info_row['name'], str(data_info_row['position'])]) images_array, masks_array = [], [] #for k in range(1,self.num_images+1): for k in range(self.num_images, 0, -1): image_path = get_filepath(self.dataset_path, data_info_row['dataset_folder'], self.images_folder, instance_name + f'_{k}', file_type=self.image_type) img = filter_by_channels(read_tensor(image_path), self.channels, 1) images_array.append(img) mask_path = get_filepath(self.dataset_path, data_info_row['dataset_folder'], self.masks_folder, instance_name + f'_{k}', file_type=self.mask_type) msk = read_tensor(mask_path) masks_array.append(np.expand_dims(msk, axis=-1)) aug = Compose([ RandomRotate90(), Flip(), OneOf([ RandomSizedCrop(min_max_height=(int( self.image_size * 0.7), self.image_size), height=self.image_size, width=self.image_size), RandomBrightnessContrast(brightness_limit=0.15, contrast_limit=0.15), ElasticTransform(alpha=15, sigma=5, alpha_affine=5), GridDistortion(p=0.6) ], p=0.8), ToTensor() ]) augmented = aug(image=np.concatenate(images_array, axis=-1), mask=np.concatenate(masks_array, axis=-1)) augmented_images = torch.stack([ augmented['image'][num_img * count_channels(self.channels):(num_img + 1) * count_channels(self.channels), :, :] for num_img in range(self.num_images) ]) if self.all_masks: augmented_masks = torch.stack([ augmented['mask'][:, :, :, i] for i in range(augmented['mask'].shape[-1]) ]).squeeze() else: augmented_masks = torch.stack([augmented['mask'][:, :, :, -1]]) return { 'features': augmented_images, 'targets': augmented_masks, 'name': data_info_row['name'], 'position': data_info_row['position'] }
OpticalDistortion, RandomSizedCrop, OneOf, CLAHE, RandomContrast, RandomGamma, RandomBrightness, ShiftScaleRotate ) aug = Compose( [ VerticalFlip(p=0.5), HorizontalFlip(p=0.5), ShiftScaleRotate(shift_limit=0.0625,scale_limit=0.5,rotate_limit=45,p=0.5), GridDistortion(p=0.5), # RandomRotate90(p=0.5), # ], p=0.8), # RandomContrast(p=0.5), # RandomBrightness(p=0.5), # RandomGamma(p=0.5), ], p=0.5) def augment_flips_color(p=.5): return Compose([ VerticalFlip(p=0.5), HorizontalFlip(p=0.5), #GridDistortion(p=0.5), ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=10, p=0.5), # RandomRotate90(p=0.5),
OpticalDistortion, OneOf, CLAHE, RandomContrast, RandomGamma, RandomBrightness, Resize) torch.manual_seed(42) np.random.seed(42) MODEL_NAME = 'unet_128' original_height = 101 original_width = 101 aug = Compose([ HorizontalFlip(p=0.7), RandomGamma(p=0.7), # RandomBrightness(p=0.7), Resize(width=128, height=128, interpolation=cv2.INTER_LANCZOS4), GridDistortion(p=0.6), OpticalDistortion(p=0.6), ElasticTransform(p=0.6), ]) # aug = strong_aug() # aug = Compose([ # VerticalFlip(p=0.1), # HorizontalFlip(p=0.5), # RandomGamma(p=0.3)]) if __name__ == '__main__': directory = get_directory() n_fold = 8 depths = pd.read_csv(os.path.join(directory, 'depths.csv'))
def get_transforms(phase_config): list_transforms = [] if phase_config.Resize.p > 0: list_transforms.append( Resize(phase_config.Resize.height, phase_config.Resize.width, p=1)) if phase_config.HorizontalFlip: list_transforms.append(HorizontalFlip()) if phase_config.VerticalFlip: list_transforms.append(VerticalFlip()) if phase_config.RandomCropScale: if phase_config.Resize.p > 0: height = phase_config.Resize.height width = phase_config.Resize.width else: height = HEIGHT width = WIDTH list_transforms.append( RandomSizedCrop(min_max_height=(int(height * 0.90), height), height=height, width=width, w2h_ratio=width / height)) if phase_config.ShiftScaleRotate: list_transforms.append(ShiftScaleRotate(p=1)) if phase_config.RandomCrop.p > 0: list_transforms.append( RandomCrop(phase_config.RandomCrop.height, phase_config.RandomCrop.width, p=1)) if phase_config.Noise: list_transforms.append( OneOf([ GaussNoise(), IAAAdditiveGaussianNoise(), ], p=0.5), ) if phase_config.Contrast: list_transforms.append( OneOf([ RandomContrast(0.5), RandomGamma(), RandomBrightness(), ], p=0.5), ) if phase_config.Blur: list_transforms.append( OneOf([ MotionBlur(p=.2), MedianBlur(blur_limit=3, p=0.1), Blur(blur_limit=3, p=0.1), ], p=0.5)) if phase_config.Distort: list_transforms.append( OneOf([ OpticalDistortion(p=0.3), GridDistortion(p=.1), IAAPiecewiseAffine(p=0.3), ], p=0.5)) if phase_config.Cutout.num_holes > 0: num_holes = phase_config.Cutout.num_holes hole_size = phase_config.Cutout.hole_size list_transforms.append(Cutout(num_holes, hole_size)) list_transforms.extend([ Normalize(mean=phase_config.mean, std=phase_config.std, p=1), ToTensor(), ]) return Compose(list_transforms)
#data augmentation 수행 # aug = albu.Compose( # [ # albu.HorizontalFlip(p=.25), # albu.VerticalFlip(p=.25), # albu.ShiftScaleRotate(shift_limit=.1, scale_limit=.1, rotate_limit=20, p=.25) # ] # ) aug = albu.Compose([ albu.RandomRotate90(), albu.Flip(), albu.Transpose(), albu.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2), albu.OneOf([ OpticalDistortion(p=0.3), GridDistortion(p=.1), IAAPiecewiseAffine(p=0.3), ], p=0.2), #HueSaturationValue(p=0.3), ]) #train data train_datagen = DataGenPanda( imgs_path=DATA_PATH+'/test_image', df=X_train, batch_size=BATCH_SIZE, mode='fit', shuffle=True, aug=aug, seq_len=SEQ_LEN,
import cv2 from albumentations import Compose, OneOf, Normalize, ShiftScaleRotate from albumentations import GaussNoise, OpticalDistortion, GridDistortion from albumentations import Cutout, Rotate, ElasticTransform, IAAAffine, IAAPerspective augmix_transform = [ ShiftScaleRotate(rotate_limit=15, p=1), OpticalDistortion(p=1), Cutout(max_h_size=8, max_w_size=8, p=1), # GridDistortion(p=1), Rotate(limit=15, p=1) ] faa_transform = Compose([ # GaussNoise(p=0.2), GridDistortion(num_steps=6, distort_limit=0.046, p=0.02), # OneOf([ # OpticalDistortion(p=1), # ElasticTransform(p=1), # ], p=0.2), Cutout(num_holes=7, max_h_size=18, max_w_size=2, p=0.05), ]) train_transform = Compose([ ShiftScaleRotate(rotate_limit=15, p=0.5), # GaussNoise(p=0.2), # GridDistortion(p=0.5), # OneOf([ # OpticalDistortion(p=1), # ElasticTransform(p=1), # ], p=0.2),
# ElasticTransform default aug = ElasticTransform() augmented = aug(image=image, mask=mask) image_elastic = augmented["image"] mask_elastic = augmented["mask"] visualize( image_elastic, mask_elastic, original_image=image, original_mask=mask ) # %% # GridDistortion aug = GridDistortion(distort_limit=0.3, p=1) augmented = aug(image=image, mask=mask) image_grid = augmented["image"] mask_grid = augmented["mask"] visualize(image_grid, mask_grid, original_image=image, original_mask=mask) # %% # Optical Distortion aug = OpticalDistortion(p=1, distort_limit=1, shift_limit=0.3) augmented = aug(image=image, mask=mask) image_optical = augmented["image"]
def train( train_df, valid_df, model, multi_model, model_name, input_size, epochs, batch_size, save_weights_path, save_logs_path, train_dir, valid_dir, ): if not os.path.exists(save_weights_path): os.makedirs(save_weights_path) if not os.path.exists(save_logs_path): os.makedirs(save_logs_path) AUGMENTATIONS_TRAIN = Compose([ HorizontalFlip(p=0.25), RandomSizedCrop(min_max_height=(int(input_size * 0.75), input_size), height=input_size, width=input_size, p=0.25), OneOf([ ShiftScaleRotate(rotate_limit=25), ElasticTransform( alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03), GridDistortion(), OpticalDistortion(distort_limit=2, shift_limit=0.5), ], p=0.5), OneOf([RandomContrast(), RandomGamma(), RandomBrightness()], p=0.5), OneOf( [Blur(), MedianBlur(), GaussNoise(), GaussianBlur()], p=0.5) ], p=0.5) #Generator Parmas params_train = { 'list_IDs': list(train_df.Image), 'labels': list(train_df.Labels), 'dim': (input_size, input_size), 'data_dir': train_dir, 'batch_size': batch_size, 'n_channels': 3, 'n_classees': PARAMS_JSON['CLASS_NUM'], 'aug': AUGMENTATIONS_TRAIN, 'model_name': model_name, 'preprocess_input': preprocess_input, 'to_categori': to_categori, 'shuffle': True } params_val = { 'list_IDs': list(valid_df.Image), 'labels': list(valid_df.Labels), 'dim': (input_size, input_size), 'data_dir': valid_dir, 'batch_size': batch_size, 'n_channels': 3, 'n_classees': PARAMS_JSON['CLASS_NUM'], 'aug': None, 'model_name': model_name, 'preprocess_input': preprocess_input, 'to_categori': to_categori, 'shuffle': True } #Create Generator train_generator = DataGenerator(**params_train) validation_generator = DataGenerator(**params_val) #get class weight class_weight_dict = CalculateClassWeight(train_df, PARAMS_JSON['CLASS_NUM'], to_categori) #model check point model_path = os.path.join(save_weights_path, '{}.h5') # check_point = ModelCheckpoint(filepath=model_path, monitor='val_auc',verbose=1,mode=max) #lr schedule reduceLR = ReduceLROnPlateau(monitor='val_loss', factor=0.5, mode=min, patience=5) #callbacks of CsvLogger logs_path = os.path.join(save_logs_path, "log.csv") csvlogger = CSVLogger(logs_path) cbk = MyCbk(model, model_path) callbacks = [csvlogger, reduceLR, cbk] #Train multi_model.fit_generator(generator=train_generator, epochs=epochs, validation_data=validation_generator, callbacks=callbacks, class_weight=class_weight_dict)