from albumentations import * import time IMG_SIZE = (224,224) '''Use case from https://www.kaggle.com/alexanderliao/image-augmentation-demo-with-albumentation/''' def albaugment(aug0, img): return aug0(image=img)['image'] idx=8 image1=x_test[idx] '''1. Rotate or Flip''' aug1 = OneOf([Rotate(p=0.99, limit=160, border_mode=0,value=0), Flip(p=0.5)],p=1) '''2. Adjust Brightness or Contrast''' aug2 = RandomBrightnessContrast(brightness_limit=0.45, contrast_limit=0.45,p=1) h_min=np.round(IMG_SIZE[1]*0.72).astype(int) h_max= np.round(IMG_SIZE[1]*0.9).astype(int) print(h_min,h_max) '''3. Random Crop and then Resize''' #w2h_ratio = aspect ratio of cropping aug3 = RandomSizedCrop((h_min, h_max),IMG_SIZE[1],IMG_SIZE[0], w2h_ratio=IMG_SIZE[0]/IMG_SIZE[1],p=1) '''4. CutOut Augmentation''' max_hole_size = int(IMG_SIZE[1]/10) aug4 = Cutout(p=1,max_h_size=max_hole_size,max_w_size=max_hole_size,num_holes=8 )#default num_holes=8 '''5. SunFlare Augmentation''' aug5 = RandomSunFlare(src_radius=max_hole_size,num_flare_circles_lower=10,num_flare_circles_upper=20,p=1) '''6. Ultimate Augmentation -- combine everything''' final_aug = Compose([aug1,aug2,aug3,aug4,aug5],p=1) img1 = albaugment(aug1,image1) img2 = albaugment(aug1,image1) print('Rotate or Flip') show_Nimages([image1,img1,img2],scale=2) # time.sleep(1) img1 = albaugment(aug2,image1) img2 = albaugment(aug2,image1) img3 = albaugment(aug2,image1) print('Brightness or Contrast') show_Nimages([img3,img1,img2],scale=2) # time.sleep(1) img1 = albaugment(aug3,image1) img2 = albaugment(aug3,image1) img3 = albaugment(aug3,image1) print('Rotate and Resize') show_Nimages([img3,img1,img2],scale=2) print(img1.shape,img2.shape) # time.sleep(1) img1 = albaugment(aug4,image1) img2 = albaugment(aug4,image1) img3 = albaugment(aug4,image1) print('CutOut') show_Nimages([img3,img1,img2],scale=2) # time.sleep(1) img1 = albaugment(aug5,image1) img2 = albaugment(aug5,image1) img3 = albaugment(aug5,image1) print('Sun Flare') show_Nimages([img3,img1,img2],scale=2) # time.sleep(1) img1 = albaugment(final_aug,image1) img2 = albaugment(final_aug,image1) img3 = albaugment(final_aug,image1) print('All above combined') show_Nimages([img3,img1,img2],scale=2) print(img1.shape,img2.shape)
aug_list = [aug5, aug2, aug3, aug4, aug1, final_aug] aug_name = ['SunFlare', 'brightness or contrast', 'crop and resized', 'CutOut', 'rotate or flip', 'Everything Combined'] idx=8 layer_name = 'relu' for i in range(len(aug_list)): path=f"F:\kaggleDataSet\diabeticRetinopathy\resized test 19\"+str(row["id_code"])+".jpg" input_img = np.empty((1,224, 224, 3), dtype=np.uint8) input_img[0,:,:,:] = preprocess_image(path) aug_img = albaugment(aug_list[i],input_img[0,:,:,:]) ben_img = transform_image_ben(aug_img) print('test pic no.%d -- augmentation: %s' % (i+1, aug_name[i])) _ = gen_heatmap_img(aug_img,model, layer_name=layer_name,viz_img=ben_img,orig_img=input_img[0])