def get_transforms_aug2(size_input): transforms_aug = transforms.Compose([ mtrans.ToResize((size_input, size_input), resize_mode='square', padding_mode=cv2.BORDER_REFLECT_101), #mtrans.ToResize( (size_input+20, size_input+20), resize_mode='asp' ) , #mtrans.RandomCrop( (size_input, size_input), limit=0, padding_mode=cv2.BORDER_REFLECT_101 ) , mtrans.RandomScale(factor=0.2, padding_mode=cv2.BORDER_REFLECT_101), mtrans.RandomGeometricalTransform(angle=45, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT_101), mtrans.ToRandomTransform(mtrans.HFlip(), prob=0.5), #------------------------------------------------------------------ #mtrans.RandomRGBPermutation(), #mtrans.ToRandomChoiceTransform( [ # mtrans.RandomBrightness( factor=0.15 ), # mtrans.RandomContrast( factor=0.15 ), # #mtrans.RandomSaturation( factor=0.15 ), # mtrans.RandomHueSaturation( hue_shift_limit=(-5, 5), sat_shift_limit=(-11, 11), val_shift_limit=(-11, 11) ), # mtrans.RandomGamma( factor=0.30 ), # mtrans.ToRandomTransform(mtrans.ToGrayscale(), prob=0.15 ), # ]), #mtrans.ToRandomTransform(mtrans.ToGaussianBlur( sigma=0.0001), prob=0.15 ), #------------------------------------------------------------------ mtrans.ToTensor(), normalize, ]) return transforms_aug
def get_transforms_fer_aug( size_input ): return transforms.Compose([ #------------------------------------------------------------------ #Resize input mtrans.ToResize( (48, 48), resize_mode='square', padding_mode=cv2.BORDER_REFLECT ), #------------------------------------------------------------------ #Geometric mtrans.RandomScale(factor=0.2, padding_mode=cv2.BORDER_REPLICATE ), mtrans.ToRandomTransform( mtrans.RandomGeometricalTransform( angle=30, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT ), prob=0.5 ), mtrans.ToRandomTransform( mtrans.VFlip(), prob=0.5 ), #mtrans.ToRandomTransform( mtrans.HFlip(), prob=0.5 ), #------------------------------------------------------------------ #Colors mtrans.ToRandomTransform( mtrans.RandomBrightness( factor=0.25 ), prob=0.50 ), mtrans.ToRandomTransform( mtrans.RandomContrast( factor=0.25 ), prob=0.50 ), mtrans.ToRandomTransform( mtrans.RandomGamma( factor=0.25 ), prob=0.50 ), mtrans.ToRandomTransform( mtrans.RandomRGBPermutation(), prob=0.50 ), mtrans.ToRandomTransform( mtrans.CLAHE(), prob=0.25 ), mtrans.ToRandomTransform( mtrans.ToGaussianBlur( sigma=0.05 ), prob=0.25 ), #------------------------------------------------------------------ #Resize mtrans.ToResize( (size_input+5, size_input+5), resize_mode='square', padding_mode=cv2.BORDER_REFLECT ), mtrans.RandomCrop( (size_input, size_input), limit=2, padding_mode=cv2.BORDER_REFLECT ), #------------------------------------------------------------------ mtrans.ToGrayscale(), mtrans.ToTensor(), normalize, ])
def get_transforms_geom_color(): #7 return transforms.Compose([ #------------------------------------------------------------------ #Resize # #mtrans.CenterCrop((1008, 1008)), #mtrans.RandomCrop( (size_crop, size_crop), limit=10, padding_mode=cv2.BORDER_REFLECT_101 ), #mtrans.ToResize( (size_input, size_input), resize_mode='square', padding_mode=cv2.BORDER_REFLECT_101 ), #mtrans.ToPad( pad, pad, padding_mode=cv2.BORDER_CONSTANT ), #------------------------------------------------------------------ #Geometric mtrans.ToRandomTransform(mtrans.VFlip(), prob=0.5), mtrans.ToRandomTransform(mtrans.HFlip(), prob=0.5), mtrans.RandomScale(factor=0.3, padding_mode=cv2.BORDER_REFLECT_101), mtrans.ToRandomTransform(mtrans.RandomGeometricalTransform( angle=45, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT_101), prob=0.5), mtrans.ToRandomTransform(mtrans.RandomElasticDistort( size_grid=32, deform=None, padding_mode=cv2.BORDER_REFLECT_101), prob=0.5), mtrans.ToRandomTransform(mtrans.RandomErased(p_grid=0.2, p_img=0.5), prob=0.1), mtrans.ToRandomTransform(mtrans.RandomLines(n_lines=5), prob=0.1), #mtrans.ToResizeUNetFoV(imsize, cv2.BORDER_REFLECT_101), #------------------------------------------------------------------ #Colors mtrans.ToRandomTransform(mtrans.RandomBrightness(factor=0.25), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomContrast(factor=0.25), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomGamma(factor=0.25), prob=0.50), #mtrans.ToRandomTransform( mtrans.RandomRGBPermutation(), prob=0.50 ), #mtrans.ToRandomTransform( mtrans.CLAHE(), prob=0.25 ), mtrans.ToRandomTransform(mtrans.ToGaussianBlur(sigma=0.05), prob=0.1), #------------------------------------------------------------------ mtrans.ToTensor(), normalize, ])
def get_transforms_aug(size_input=256, size_crop=512): return transforms.Compose([ #------------------------------------------------------------------ #Resize # mtrans.RandomCrop((size_crop, size_crop), limit=10, padding_mode=cv2.BORDER_REFLECT_101), mtrans.ToResize((size_input, size_input), resize_mode='square', padding_mode=cv2.BORDER_REFLECT_101), #mtrans.ToPad( 5 , 5, padding_mode=cv2.BORDER_REFLECT_101 ), #------------------------------------------------------------------ #Geometric mtrans.ToRandomTransform(mtrans.VFlip(), prob=0.5), mtrans.ToRandomTransform(mtrans.HFlip(), prob=0.5), mtrans.RandomScale(factor=0.3, padding_mode=cv2.BORDER_REFLECT_101), mtrans.ToRandomTransform(mtrans.RandomGeometricalTransform( angle=45, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT_101), prob=0.5), mtrans.ToRandomTransform(mtrans.RandomElasticDistort( size_grid=32, deform=12, padding_mode=cv2.BORDER_REFLECT_101), prob=0.5), #mtrans.ToResizeUNetFoV(imsize, cv2.BORDER_REFLECT_101), #------------------------------------------------------------------ #Colors mtrans.ToRandomTransform(mtrans.RandomBrightness(factor=0.25), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomContrast(factor=0.25), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomGamma(factor=0.25), prob=0.50), #mtrans.ToRandomTransform( mtrans.RandomRGBPermutation(), prob=0.50 ), #mtrans.ToRandomTransform( mtrans.CLAHE(), prob=0.25 ), #mtrans.ToRandomTransform(mtrans.ToGaussianBlur( sigma=0.05 ), prob=0.25 ), #------------------------------------------------------------------ mtrans.ToTensor(), normalize, ])
def get_transforms_aug(size_input): return transforms.Compose([ #------------------------------------------------------------------ #Resize mtrans.ToResize((size_input, size_input), resize_mode='square', padding_mode=cv2.BORDER_REFLECT_101), #mtrans.ToResize( (size_input+10, size_input+10), resize_mode='square', padding_mode=cv2.BORDER_REFLECT_101 ) , #mtrans.RandomCrop( (size_input, size_input), limit=2, padding_mode=cv2.BORDER_REFLECT_101 ) , #------------------------------------------------------------------ #Geometric mtrans.RandomScale(factor=0.2, padding_mode=cv2.BORDER_REFLECT_101), mtrans.ToRandomTransform(mtrans.RandomGeometricalTransform( angle=45, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT_101), prob=0.5), mtrans.ToRandomTransform(mtrans.VFlip(), prob=0.5), mtrans.ToRandomTransform(mtrans.HFlip(), prob=0.5), #mtrans.ToRandomTransform( mtrans.RandomElasticDistort( size_grid=32, deform=12, padding_mode=cv2.BORDER_REFLECT_101 ), prob=0.5 ), #------------------------------------------------------------------ #Colors mtrans.ToRandomTransform(mtrans.RandomBrightness(factor=0.25), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomContrast(factor=0.25), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomGamma(factor=0.25), prob=0.50), #mtrans.ToRandomTransform( mtrans.RandomHueSaturation( hue_shift_limit=(-5, 5), sat_shift_limit=(-11, 11), val_shift_limit=(-11, 11) ), prob=0.30 ), #mtrans.ToRandomTransform( mtrans.RandomRGBPermutation(), prob=0.50 ), #mtrans.ToRandomTransform( mtrans.CLAHE(), prob=0.25 ), #mtrans.ToRandomTransform(mtrans.ToGaussianBlur( sigma=0.05 ), prob=0.25 ), #------------------------------------------------------------------ mtrans.ToTensor(), normalize, ])
def main(): # parameters parser = arg_parser() args = parser.parse_args() imsize = args.image_size parallel = args.parallel num_classes = 2 num_channels = 3 view_freq = 2 network = SegmentationNeuralNet( patchproject=args.project, nameproject=args.name, no_cuda=args.no_cuda, parallel=parallel, seed=args.seed, print_freq=args.print_freq, gpu=args.gpu, view_freq=view_freq, ) network.create(arch=args.arch, num_output_channels=num_classes, num_input_channels=num_channels, loss=args.loss, lr=args.lr, momentum=args.momentum, optimizer=args.opt, lrsch=args.scheduler, pretrained=args.finetuning, size_input=imsize) # resume network.resume(os.path.join(network.pathmodels, args.resume)) cudnn.benchmark = True # datasets # training dataset train_data = tgsdata.TGSDataset( args.data, tgsdata.train, count=16000, num_channels=num_channels, transform=transforms.Compose([ mtrans.ToRandomTransform(mtrans.HFlip(), prob=0.5), mtrans.ToRandomTransform(mtrans.VFlip(), prob=0.5), mtrans.ToResize((300, 300), resize_mode='squash', padding_mode=cv2.BORDER_REFLECT_101), mtrans.RandomCrop((256, 256), limit=10, padding_mode=cv2.BORDER_REFLECT_101), mtrans.RandomScale(factor=0.2, padding_mode=cv2.BORDER_REFLECT_101), mtrans.RandomGeometricalTransform( angle=30, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT_101), #mtrans.ToResizeUNetFoV(imsize, cv2.BORDER_REFLECT_101), mtrans.ToRandomTransform(mtrans.RandomBrightness(factor=0.15), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomContrast(factor=0.15), prob=0.50), mtrans.ToRandomTransform(mtrans.RandomGamma(factor=0.15), prob=0.50), mtrans.ToRandomTransform(mtrans.ToGaussianBlur(), prob=0.15), mtrans.ToTensor(), mtrans.ToMeanNormalization( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ) #mtrans.ToNormalization(), ])) train_loader = DataLoader(train_data, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=network.cuda, drop_last=True) # validate dataset val_data = tgsdata.TGSDataset( args.data, tgsdata.test, count=4000, num_channels=num_channels, transform=transforms.Compose([ mtrans.ToResize((256, 256), resize_mode='squash'), #mtrans.RandomCrop( (255,255), limit=50, padding_mode=cv2.BORDER_CONSTANT ), #mtrans.ToResizeUNetFoV(imsize, cv2.BORDER_REFLECT_101), mtrans.ToTensor(), mtrans.ToMeanNormalization( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ) #mtrans.ToNormalization(), ])) val_loader = DataLoader(val_data, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=network.cuda, drop_last=True) # print neural net class print('SEG-Torch: {}'.format(datetime.datetime.now())) print(network) # training neural net network.fit(train_loader, val_loader, args.epochs, args.snapshot) print("Optimization Finished!") print("DONE!!!")
def test_ellipse(): data = SyntethicCircleDataset( count=300, generate=SyntethicCircleDataset.generate_image_mask_and_weight, imsize=(512, 612), sigma=0.01, bdraw_grid=True, transform=transforms.Compose([ ## resize and crop mtrans.ToResize((400, 400), resize_mode='square', padding_mode=cv2.BORDER_REFLECT_101), #mtrans.CenterCrop( (200,200) ), #mtrans.RandomCrop( (255,255), limit=50, padding_mode=cv2.BORDER_REFLECT_101 ), #mtrans.ToResizeUNetFoV(388, cv2.BORDER_REFLECT_101), ## color mtrans.ToRandomChoiceTransform([ mtrans.RandomSaturation(), mtrans.RandomHueSaturationShift(), mtrans.RandomHueSaturation(), #mtrans.RandomRGBShift(), #mtrans.ToNegative(), #mtrans.RandomRGBPermutation(), #mtrans.ToRandomTransform( mtrans.ToGrayscale(), prob=0.5 ), mtrans.ToGrayscale(), ]), ## blur #mtrans.ToRandomTransform( mtrans.ToLinealMotionBlur( lmax=1 ), prob=0.5 ), #mtrans.ToRandomTransform( mtrans.ToMotionBlur( ), prob=0.5 ), mtrans.ToRandomTransform(mtrans.ToGaussianBlur(), prob=0.75), ## geometrical #mtrans.ToRandomTransform( mtrans.HFlip(), prob=0.5 ) #mtrans.ToRandomTransform( mtrans.VFlip(), prob=0.5 ) mtrans.RandomScale(factor=0.2, padding_mode=cv2.BORDER_REFLECT101), #mtrans.RandomGeometricalTransform( angle=360, translation=0.2, warp=0.02, padding_mode=cv2.BORDER_REFLECT101), #mtrans.RandomElasticDistort( size_grid=50, padding_mode=cv2.BORDER_REFLECT101 ), ## tensor mtrans.ToTensor(), mtrans.RandomElasticTensorDistort(size_grid=10, deform=0.05), ## normalization mtrans.ToNormalization(), #mtrans.ToWhiteNormalization(), #mtrans.ToMeanNormalization( # mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225] # ), ])) dataloader = DataLoader(data, batch_size=3, shuffle=True, num_workers=1) label_batched = [] for i_batch, sample_batched in enumerate(dataloader): print(i_batch, sample_batched['image'].size(), sample_batched['label'].size(), sample_batched['weight'].size()) image = sample_batched['image'] label = sample_batched['label'] weight = sample_batched['weight'] print(torch.min(image), torch.max(image), image.shape) print(torch.min(label), torch.max(label), image.shape) print(torch.min(weight), torch.max(weight), image.shape) print(image.shape) print(np.unique(label)) print(image.min(), image.max()) image = image.permute(2, 3, 1, 0)[:, :, :, 0].squeeze() label = label.permute(2, 3, 1, 0)[:, :, :, 0].squeeze() weight = weight.permute(2, 3, 1, 0)[:, :, :, 0].squeeze() plt.figure(figsize=(16, 4)) plt.subplot(131) plt.imshow(image) plt.title('image + [grid]') plt.axis('off') plt.ioff() plt.subplot(132) plt.imshow(label) plt.title('gt') plt.axis('off') plt.ioff() plt.subplot(133) plt.imshow(weight) plt.title('weight map') plt.axis('off') plt.ioff() #print('save figure ...') #plt.savefig('../out/image_{}.png'.format(i_batch) ) plt.show() # observe 4th batch and stop. if i_batch == 1: break