def get_train_augmentors(self, input_shape, output_shape, view=False): shape_augs = [ imgaug.Affine( shear=5, # in degree scale=(0.8, 1.2), rotate_max_deg=179, translate_frac=(0.01, 0.01), interp=cv2.INTER_NEAREST, border=cv2.BORDER_CONSTANT), imgaug.Flip(vert=True), imgaug.Flip(horiz=True), imgaug.CenterCrop(input_shape), ] input_augs = self.input_augs label_augs = [] if self.model_type == 'unet' or self.model_type == 'micronet': label_augs =[GenInstanceUnetMap(crop_shape=output_shape)] if self.model_type == 'dcan': label_augs =[GenInstanceContourMap(crop_shape=output_shape)] if self.model_type == 'dist': label_augs = [GenInstanceDistance(crop_shape=output_shape, inst_norm=False)] if self.model_type == 'np_hv' or self.model_type == 'np_hv_opt': label_augs = [GenInstanceHV(crop_shape=output_shape)] if self.model_type == 'np_dist': label_augs = [GenInstanceDistance(crop_shape=output_shape, inst_norm=True)] if not self.type_classification: label_augs.append(BinarizeLabel()) if not view: label_augs.append(imgaug.CenterCrop(output_shape)) return shape_augs, input_augs, label_augs
def train_dataloader(self): # REQUIRED ds_train = PretrainedSNEMI(folder=self.hparams.data_path, train_or_valid='train', size=40000, # resize=int(self.hparams.shape), debug=DEBUG ) ag_train = [ imgaug.RotationAndCropValid(max_deg=180, interp=cv2.INTER_NEAREST), imgaug.Flip(horiz=True, vert=False), imgaug.Flip(horiz=False, vert=True), imgaug.Transpose(), imgaug.GoogleNetRandomCropAndResize(crop_area_fraction=(0.2, 0.5), aspect_ratio_range=(0.5, 2.0), interp=cv2.INTER_NEAREST, target_shape=self.hparams.shape), imgaug.ToFloat32(), ] ds_train.reset_state() # ds_train = AugmentImageComponent(ds_train, ag_train, 0) ds_train = AugmentImageComponent(ds_train, [imgaug.Albumentations(AB.RandomBrightnessContrast())], (0)) ds_train = AugmentImageComponents(ds_train, ag_train, (0, 1)) ds_train = MapData(ds_train, lambda dp: [dp[0], 255.0*(dp[1]>0)*(1-skimage.segmentation.find_boundaries(dp[1], mode='inner'))]) ds_train = BatchData(ds_train, self.hparams.batch) ds_train = PrintData(ds_train) ds_train = MapData(ds_train, lambda dp: [torch.tensor(dp[0][:,np.newaxis,:,:]), torch.tensor(dp[1][:,np.newaxis,:,:]).float()]) ds_train = MultiProcessRunner(ds_train, num_proc=32, num_prefetch=8) return ds_train
def get_data(image_ids, batch_size=1, is_train=False, shape=510): ds = ImageAndMaskFromFile(image_ids, channel=3, shuffle=True) if is_train: number_of_prefetch = 8 augs_with_label = [ imgaug.RandomCrop(shape), imgaug.Flip(horiz=True, prob=0.5), imgaug.Flip(vert=True, prob=0.5) ] augs_no_label = [ imgaug.RandomOrderAug( [imgaug.Brightness(delta=20), imgaug.Contrast((0.6, 1.4))]) ] # augmentors = [ # GoogleNetResize(), # # It's OK to remove the following augs if your CPU is not fast enough. # # Removing brightness/contrast/saturation does not have a significant effect on accuracy. # # Removing lighting leads to a tiny drop in accuracy. # imgaug.RandomOrderAug( # [imgaug.BrightnessScale((0.6, 1.4), clip=False), # imgaug.Contrast((0.6, 1.4), clip=False), # imgaug.Saturation(0.4, rgb=False), # # rgb-bgr conversion for the constants copied from fb.resnet.torch # imgaug.Lighting(0.1,— # eigval=np.asarray( # [0.2175, 0.0188, 0.0045][::-1]) * 255.0, # eigvec=np.array( # [[-0.5675, 0.7192, 0.4009], # [-0.5808, -0.0045, -0.8140], # [-0.5836, -0.6948, 0.4203]], # dtype='float32')[::-1, ::-1] # )]), # imgaug.Flip(horiz=True), # ] else: number_of_prefetch = 1 augs_with_label = [imgaug.CenterCrop(shape)] augs_no_label = [] ds = AugmentImageComponents(ds, augs_with_label, (0, 1)) ds = AugmentImageComponents(ds, augs_no_label, [0]) ds = BatchData(ds, batch_size) ds = PrefetchData(ds, 30, 1) #number_of_prefetch) return ds
def get_train_augmentors(self, input_shape, output_shape, view=False): print(input_shape, output_shape) shape_augs = [ imgaug.Affine( shear=5, # in degree scale=(0.8, 1.2), rotate_max_deg=179, translate_frac=(0.01, 0.01), interp=cv2.INTER_NEAREST, border=cv2.BORDER_CONSTANT), imgaug.Flip(vert=True), imgaug.Flip(horiz=True), imgaug.CenterCrop(input_shape), ] input_augs = [ imgaug.RandomApplyAug( imgaug.RandomChooseAug([ GaussianBlur(), MedianBlur(), imgaug.GaussianNoise(), ]), 0.5), # standard color augmentation imgaug.RandomOrderAug([ imgaug.Hue((-8, 8), rgb=True), imgaug.Saturation(0.2, rgb=True), imgaug.Brightness(26, clip=True), imgaug.Contrast((0.75, 1.25), clip=True), ]), imgaug.ToUint8(), ] label_augs = [] if self.model_type == 'unet' or self.model_type == 'micronet': label_augs = [GenInstanceUnetMap(crop_shape=output_shape)] if self.model_type == 'dcan': label_augs = [GenInstanceContourMap(crop_shape=output_shape)] if self.model_type == 'dist': label_augs = [ GenInstanceDistance(crop_shape=output_shape, inst_norm=False) ] if self.model_type == 'np_hv': label_augs = [GenInstanceHV(crop_shape=output_shape)] if self.model_type == 'np_dist': label_augs = [ GenInstanceDistance(crop_shape=output_shape, inst_norm=True) ] if not self.type_classification: label_augs.append(BinarizeLabel()) if not view: label_augs.append(imgaug.CenterCrop(output_shape)) return shape_augs, input_augs, label_augs
def get_augmentations(is_train): if is_train: augmentors = [ GoogleNetResize(crop_area_fraction=0.76, target_shape=224), # TODO : 76% or 49%? imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.6, 1.4), clip=True), imgaug.Contrast((0.6, 1.4), clip=True), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ] return augmentors
def fbresnet_augmentor(isTrain, target_shape=224): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ if isTrain: augmentors = [ GoogleNetResize(crop_area_fraction=0.32, target_shape=target_shape), # GoogleNetResize(target_shape=target_shape), imgaug. RandomOrderAug([ # imgaug.BrightnessScale((0.6, 1.4), clip=False), # imgaug.Contrast((0.6, 1.4), clip=False), # imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(int(256 / 224 * target_shape), cv2.INTER_CUBIC), imgaug.CenterCrop((target_shape, target_shape)), ] return augmentors
def get_tp_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ interpolation = cv2.INTER_CUBIC # linear seems to have more stable performance. # but we keep cubic for compatibility with old models if isTrain: augmentors = [ imgaug.GoogleNetRandomCropAndResize(interp=interpolation), # It's OK to remove the following augs if your CPU is not fast enough. # Removing brightness/contrast/saturation does not have a significant effect on accuracy. # Removing lighting leads to a tiny drop in accuracy. # imgaug.RandomOrderAug( # [imgaug.BrightnessScale((0.6, 1.4), clip=False), # imgaug.Contrast((0.6, 1.4), rgb=False, clip=False), # imgaug.Saturation(0.4, rgb=False), # # rgb-bgr conversion for the constants copied from fb.resnet.torch # imgaug.Lighting(0.1, # eigval=np.asarray( # [0.2175, 0.0188, 0.0045][::-1]) * 255.0, # eigvec=np.array( # [[-0.5675, 0.7192, 0.4009], # [-0.5808, -0.0045, -0.8140], # [-0.5836, -0.6948, 0.4203]], # dtype='float32')[::-1, ::-1] # )]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, interp=interpolation), imgaug.CenterCrop((224, 224)), ] return augmentors
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ if isTrain: augmentors = [ GoogleNetResize(), imgaug.RandomOrderAug( # Remove these augs if your CPU is not fast enough [imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting(0.1, eigval=np.asarray( [0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array( [[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1] )]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ] return augmentors
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ if isTrain: augmentors = [ GoogleNetResize(), # It's OK to remove the following augs if your CPU is not fast enough. # Removing brightness/contrast/saturation does not have a significant effect on accuracy. # Removing lighting leads to a tiny drop in accuracy. imgaug.RandomOrderAug([ # imgaug.BrightnessScale((0.6, 1.4), clip=False), # imgaug.Contrast((0.6, 1.4), clip=False), # imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((DEFAULT_IMAGE_SHAPE, DEFAULT_IMAGE_SHAPE)), ] return augmentors
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ if isTrain: """ Sec 5.1: We use scale and aspect ratio data augmentation [35] as in [12]. The network input image is a 224×224 pixel random crop from an augmented image or its horizontal flip. """ augmentors = [ GoogleNetResize(), imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ] return augmentors
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. # 残差网络增强图像 """ if isTrain: # 如果训练数据的话 augmentors = [ GoogleNetResize(), # 定义好了crop_area_fraction等参数 imgaug. RandomOrderAug( # GPU不行的话就把这部分删除Remove these augs if your CPU is not fast enough #imgaug是一个图像增强库 [ imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] else: # 如果不是训练数据的话 augmentors = [ imgaug.ResizeShortestEdge( 256, cv2.INTER_CUBIC), # 在保持纵横比的同时,将最短边的大小调整为某个数字。 imgaug.CenterCrop((224, 224)), # 在中间裁剪图像 ] return augmentors
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ if isTrain: augmentors = [ GoogleNetResize(), imgaug.RandomOrderAug([ JohnAug(), imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] else: round2pow2 = lambda x: 2**(x - 1).bit_length() augmentors = [ imgaug.ResizeShortestEdge(round2pow2(IMAGE_SIZE), cv2.INTER_CUBIC), imgaug.CenterCrop((IMAGE_SIZE, IMAGE_SIZE)), ] return augmentors
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images. """ if isTrain: augmentors = [ GoogleNetResize(), imgaug.RandomOrderAug([ imgaug.Brightness(30, clip=False), imgaug.Contrast((0.8, 1.2), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion imgaug.Lighting(0.1, eigval=[0.2175, 0.0188, 0.0045][::-1], eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Clip(), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ] return augmentors
def get_data(datadir, size=IMAGE_SIZE, isTrain=True, zmin=-1, zmax=1, batch=BATCH, shuffle_read=False): if isTrain: augs = [ imgaug.ResizeShortestEdge(int(size * 1.143)), imgaug.RandomCrop(size), imgaug.Flip(horiz=True), ] else: augs = [ imgaug.ResizeShortestEdge(int(size * 1.143)), imgaug.CenterCrop(size) ] def get_images(dir): files = glob.glob(os.path.join(dir, "*.jpg")) if shuffle_read: import random random.shuffle(files) else: files = sorted(files) image_df = ImageFromFile(files, channel=3, shuffle=isTrain) image_df = AugmentImageComponent(image_df, augs) random_df = RandomZData([size, size, 3], zmin, zmax) return JoinData([random_df, image_df]) names = ['train'] if isTrain else ['test'] df = get_images(*[os.path.join(datadir, n) for n in names]) df = BatchData(df, batch) return df
def get_train_augmentors(self, view=False): shape_augs = [ imgaug.Affine( shear=5, # in degree scale=(0.8, 1.2), rotate_max_deg=179, translate_frac=(0.01, 0.01), interp=cv2.INTER_NEAREST, border=cv2.BORDER_CONSTANT), imgaug.Flip(vert=True), imgaug.Flip(horiz=True), imgaug.CenterCrop(self.train_input_shape), ] input_augs = [ imgaug.RandomApplyAug( imgaug.RandomChooseAug([ GaussianBlur(), MedianBlur(), imgaug.GaussianNoise(), ]), 0.5), # standard color augmentation imgaug.RandomOrderAug([ imgaug.Hue((-8, 8), rgb=True), imgaug.Saturation(0.2, rgb=True), imgaug.Brightness(26, clip=True), imgaug.Contrast((0.75, 1.25), clip=True), ]), imgaug.ToUint8(), ] # default to 'xy' if self.model_mode != 'np+dst': label_augs = [GenInstanceXY(self.train_mask_shape)] else: label_augs = [GenInstanceDistance(self.train_mask_shape)] label_augs.append(BinarizeLabel()) if not view: label_augs.append(imgaug.CenterCrop(self.train_mask_shape)) return shape_augs, input_augs, label_augs
def small_augmentor(): augmentors = [ GoogleNetResize(), imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array( [[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]), imgaug.Flip(horiz=True), ] return augmentors
def normal_augmentor(isTrain): """ Normal augmentor with random crop and flip only, for BGR images in range [0,255]. """ if isTrain: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.RandomCrop((DEFAULT_IMAGE_SHAPE, DEFAULT_IMAGE_SHAPE)), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((DEFAULT_IMAGE_SHAPE, DEFAULT_IMAGE_SHAPE)), ] return augmentors
def get_data(phase): is_train = phase == "train" ds = dataset.Cifar10(phase) pp_mean = ds.get_per_pixel_mean(("train", )) if is_train: augmentors = [ imgaug.CenterPaste((40, 40)), imgaug.RandomCrop((32, 32)), imgaug.Flip(horiz=True), imgaug.MapImage(lambda x: x - pp_mean), ] else: augmentors = [imgaug.MapImage(lambda x: x - pp_mean)] ds = AugmentImageComponent(ds, augmentors) ds = BatchData(ds, BATCH_SIZE, remainder=not is_train) return ds
def get_data(datadir, size=IMAGESIZE, isTrain=True): if isTrain: augs = [ imgaug.RandomCrop(size), imgaug.Flip(horiz=True), ] else: augs = [imgaug.CenterCrop(size)] def get_images(dir): files = sorted(glob.glob(os.path.join(dir, "*.jpg"))) df = ImageFromFile(files, channel=3, shuffle=isTrain) random_df = RandomZData([size, size, 3], -1, 1) return JoinData([random_df, AugmentImageComponent(df, augs)]) names = ['train'] if isTrain else ['test'] df = get_images(*[os.path.join(datadir, n) for n in names]) df = BatchData(df, BATCH if isTrain else TEST_BATCH) return df
def fbresnet_augmentor(is_training, option): if is_training: augmentors = [ imgaug.ToFloat32(), imgaug.Resize((option.final_size + 32, option.final_size + 32)), imgaug.RandomCrop((option.final_size, option.final_size)) ] flip = [imgaug.Flip(horiz=True), imgaug.ToUint8()] augmentors.extend(flip) else: augmentors = [ imgaug.ToFloat32(), imgaug.Resize((option.final_size + 32, option.final_size + 32)), imgaug.CenterCrop((option.final_size, option.final_size)), imgaug.ToUint8() ] return augmentors
def get_ilsvrc_data_alexnet(is_train, image_size, batchsize, directory): if is_train: if not directory.startswith('/'): ds = ILSVRCTTenthTrain(directory) else: ds = ILSVRC12(directory, 'train') augs = [ imgaug.RandomApplyAug(imgaug.RandomResize((0.9, 1.2), (0.9, 1.2)), 0.7), imgaug.RandomApplyAug(imgaug.RotationAndCropValid(15), 0.7), imgaug.RandomApplyAug( imgaug.RandomChooseAug([ imgaug.SaltPepperNoise(white_prob=0.01, black_prob=0.01), imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.8, 1.2), clip=False), imgaug.Contrast((0.8, 1.2), clip=False), # imgaug.Saturation(0.4, rgb=True), ]), ]), 0.7), imgaug.Flip(horiz=True), imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.RandomCrop((224, 224)), ] ds = AugmentImageComponent(ds, augs) ds = PrefetchData(ds, 1000, multiprocessing.cpu_count()) ds = BatchData(ds, batchsize) ds = PrefetchData(ds, 10, 4) else: if not directory.startswith('/'): ds = ILSVRCTenthValid(directory) else: ds = ILSVRC12(directory, 'val') ds = AugmentImageComponent(ds, [ imgaug.ResizeShortestEdge(224, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ]) ds = PrefetchData(ds, 100, multiprocessing.cpu_count()) ds = BatchData(ds, batchsize) return ds
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ interpolation = cv2.INTER_LINEAR if isTrain: """ Sec 5.1: We use scale and aspect ratio data augmentation [35] as in [12]. The network input image is a 224×224 pixel random crop from an augmented image or its horizontal flip. """ augmentors = [ imgaug.GoogleNetRandomCropAndResize(interp=interpolation), # It's OK to remove the following augs if your CPU is not fast enough. # Removing brightness/contrast/saturation does not have a significant effect on accuracy. # Removing lighting leads to a tiny drop in accuracy. imgaug.RandomOrderAug( [imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), rgb=False, clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting(0.1, eigval=np.asarray( [0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array( [[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1] )]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, interp=interpolation), imgaug.CenterCrop((224, 224)), ] return augmentors
def get_train_augmentors(self, input_shape, output_shape, view=False): print(input_shape, output_shape) if self.model_mode == "class_rotmnist": shape_augs = [ imgaug.Affine(rotate_max_deg=359, interp=cv2.INTER_NEAREST, border=cv2.BORDER_CONSTANT), ] input_augs = [] else: shape_augs = [ imgaug.Affine( rotate_max_deg=359, translate_frac=(0.01, 0.01), interp=cv2.INTER_NEAREST, border=cv2.BORDER_REFLECT, ), imgaug.Flip(vert=True), imgaug.Flip(horiz=True), imgaug.CenterCrop(input_shape), ] input_augs = [ imgaug.RandomApplyAug( imgaug.RandomChooseAug([ GaussianBlur(), MedianBlur(), imgaug.GaussianNoise(), ]), 0.5, ), # Standard colour augmentation imgaug.RandomOrderAug([ imgaug.Hue((-8, 8), rgb=True), imgaug.Saturation(0.2, rgb=True), imgaug.Brightness(26, clip=True), imgaug.Contrast((0.75, 1.25), clip=True), ]), imgaug.ToUint8(), ] if self.model_mode == "seg_gland": label_augs = [] label_augs = [GenInstanceContourMap(mode=self.model_mode)] label_augs.append(BinarizeLabel()) if not view: label_augs.append(imgaug.CenterCrop(output_shape)) else: label_augs.append(imgaug.CenterCrop(input_shape)) return shape_augs, input_augs, label_augs elif self.model_mode == "seg_nuc": label_augs = [] label_augs = [GenInstanceMarkerMap()] label_augs.append(BinarizeLabel()) if not view: label_augs.append(imgaug.CenterCrop(output_shape)) else: label_augs.append(imgaug.CenterCrop(input_shape)) return shape_augs, input_augs, label_augs else: return shape_augs, input_augs
def fbresnet_augmentor(isTrain, crop_method, color_augmentation): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ execution_lst = [] if isTrain: augmentors = [ # 1. crop_method # a) GoogleNetResize GoogleNetResize(), # b) ShortestEdgeResize imgaug.ResizeShortestEdge(256), # c) GlobalWarp imgaug.Resize(226), # NOTE: for CAM generation imgaug.RandomCrop((224, 224)), # d) CAMCrop # (when CAMCrop is set, the output from the original DataFlow has already been cropped) # 2. color_augmentation imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] # if crop_method == 'GoogleNetResize': print( '--> perform GoogleNetResize cropping method during the training pipeline' ) execution_lst.extend([0]) elif crop_method == 'ShortestEdgeResize': print( '--> perform ShortestEdgeResize cropping method during the training pipeline' ) execution_lst.extend([1, 3]) elif crop_method == 'GlobalWarp': print( '--> perform GlobalWarp cropping method during the training pipeline' ) execution_lst.extend([2, 3]) elif crop_method == 'CAMCrop': # enable CAMCrop @ 20171124 print( '*** Perform CAMCrop to better the training dynamics and the results ***' ) if color_augmentation: print( '--> perform color augmentation during the training pipeline') execution_lst.extend([4]) else: print( '--> discard the color jittering process during the training pipeline' ) # perform mirror reflection augmentation anyway execution_lst.extend([5]) else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), imgaug.RandomCrop((224, 224)), ] if crop_method == 'RandomCrop': execution_lst.extend([0, 2]) elif crop_method == 'CenterCrop': execution_lst.extend([0, 1]) return [ item_ for id_, item_ in enumerate(augmentors) if id_ in execution_lst ]