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((224, 224)), ] return augmentors
def get_data(name, batch): isTrain = name == 'train' if isTrain: augmentors = [ GoogleNetResize(crop_area_fraction=0.49), 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 get_imagenet_dataflow(args.data, name, batch, augmentors)
def fbresnet_augmentor(isTrain): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ if isTrain: augmentors = [ GoogleNetResize(), imgaug.Flip(horiz=True), imgaug.ToFloat32(), 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]) ]), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_LINEAR), imgaug.CenterCrop((224, 224)), imgaug.ToFloat32(), ] return augmentors
def get_data(name, batch): isTrain = name == 'train' image_shape = 224 if isTrain: augmentors = [ # use lighter augs if model is too small GoogleNetResize( crop_area_fraction=0.49 if args.width_ratio < 1 else 0.08, target_shape=image_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), ]), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(int(image_shape * 256 / 224), cv2.INTER_CUBIC), imgaug.CenterCrop((image_shape, image_shape)), ] return get_imagenet_dataflow(args.data_dir, name, batch, augmentors, meta_dir=args.meta_dir)
def train_dataloader(self): ds_train = MultiLabelDataset(folder=self.hparams.data, is_train='train', fname='covid_train_v5.csv', types=self.hparams.types, pathology=self.hparams.pathology, resize=int(self.hparams.shape), balancing=None) ds_train.reset_state() ag_train = [ # imgaug.Albumentations( # AB.SmallestMaxSize(self.hparams.shape, p=1.0)), imgaug.ColorSpace(mode=cv2.COLOR_GRAY2RGB), # imgaug.Affine(shear=10), imgaug.RandomChooseAug([ imgaug.Albumentations(AB.Blur(blur_limit=4, p=0.25)), imgaug.Albumentations(AB.MotionBlur(blur_limit=4, p=0.25)), imgaug.Albumentations(AB.MedianBlur(blur_limit=4, p=0.25)), ]), imgaug.Albumentations(AB.CLAHE(tile_grid_size=(32, 32), p=0.5)), imgaug.RandomOrderAug([ imgaug.Affine(shear=10, border=cv2.BORDER_CONSTANT, interp=cv2.INTER_AREA), imgaug.Affine(translate_frac=(0.01, 0.02), border=cv2.BORDER_CONSTANT, interp=cv2.INTER_AREA), imgaug.Affine(scale=(0.5, 1.0), border=cv2.BORDER_CONSTANT, interp=cv2.INTER_AREA), ]), imgaug.RotationAndCropValid(max_deg=10, interp=cv2.INTER_AREA), imgaug.GoogleNetRandomCropAndResize( crop_area_fraction=(0.8, 1.0), aspect_ratio_range=(0.8, 1.2), interp=cv2.INTER_AREA, target_shape=self.hparams.shape), imgaug.ColorSpace(mode=cv2.COLOR_RGB2GRAY), imgaug.ToFloat32(), ] ds_train = AugmentImageComponent(ds_train, ag_train, 0) # Label smoothing ag_label = [ imgaug.BrightnessScale((0.8, 1.2), clip=False), ] # ds_train = AugmentImageComponent(ds_train, ag_label, 1) ds_train = BatchData(ds_train, self.hparams.batch, remainder=True) if self.hparams.debug: ds_train = FixedSizeData(ds_train, 2) ds_train = MultiProcessRunner(ds_train, num_proc=4, num_prefetch=16) ds_train = PrintData(ds_train) ds_train = MapData( ds_train, lambda dp: [ torch.tensor(np.transpose(dp[0], (0, 3, 1, 2))), torch.tensor(dp[1]).float() ]) return ds_train
def get_moco_v1_augmentor(): augmentors = [ TorchvisionCropAndResize(crop_area_fraction=(0.2, 1.)), RandomGrayScale(0.2), imgaug.ToFloat32(), imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.6, 1.4)), imgaug.Contrast((0.6, 1.4), rgb=False), imgaug.Saturation(0.4, rgb=False), # 72 = 180*0.4 imgaug.Hue(range=(-72, 72), rgb=False) ]), imgaug.ToUint8(), imgaug.Flip(horiz=True), ] return augmentors
def get_moco_v1_augmentor(): augmentors = [ imgaug.GoogleNetRandomCropAndResize(crop_area_fraction=(0.2, 1.)), imgaug.RandomApplyAug(imgaug.Grayscale(rgb=False, keepshape=True), 0.2), imgaug.ToFloat32(), imgaug.RandomOrderAug( [imgaug.BrightnessScale((0.6, 1.4)), imgaug.Contrast((0.6, 1.4), rgb=False), imgaug.Saturation(0.4, rgb=False), # 72 = 180*0.4 imgaug.Hue(range=(-72, 72), rgb=False) ]), imgaug.ToUint8(), imgaug.Flip(horiz=True), ] return augmentors
def get_resnet_augmentor(): augmentors = [ 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] )]) ] return augmentors
def get_moco_v2_augmentor(): augmentors = [ TorchvisionCropAndResize(crop_area_fraction=(0.2, 1.)), imgaug.ToFloat32(), imgaug.RandomApplyAug( imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.6, 1.4)), imgaug.Contrast((0.6, 1.4), rgb=False), imgaug.Saturation(0.4, rgb=False), # 18 = 180*0.1 imgaug.Hue(range=(-18, 18), rgb=False) ]), 0.8), RandomGrayScale(0.2), imgaug.RandomApplyAug(RandomGaussionBlur([0.1, 2.0], 0.1), 0.5), imgaug.ToUint8(), imgaug.Flip(horiz=True), ] return augmentors
def get_moco_v2_augmentor(): augmentors = [ imgaug.GoogleNetRandomCropAndResize(crop_area_fraction=(0.2, 1.)), imgaug.ToFloat32(), imgaug.RandomApplyAug( imgaug.RandomOrderAug( [imgaug.BrightnessScale((0.6, 1.4)), imgaug.Contrast((0.6, 1.4), rgb=False), imgaug.Saturation(0.4, rgb=False), # 18 = 180*0.1 imgaug.Hue(range=(-18, 18), rgb=False) ]), 0.8), imgaug.RandomApplyAug(imgaug.Grayscale(rgb=False, keepshape=True), 0.2), imgaug.RandomApplyAug( # 11 = 0.1*224//2 imgaug.GaussianBlur(size_range=(11, 12), sigma_range=[0.1, 2.0]), 0.5), imgaug.ToUint8(), imgaug.Flip(horiz=True), ] return augmentors
def fbresnet_augmentor(): # assme BGR input augmentors = [ imgaug.GoogleNetRandomCropAndResize(), 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), ] return augmentors
def get_data(is_train, batch_size, data_dir_path, input_image_size=224, resize_inv_factor=0.875): assert (resize_inv_factor > 0.0) resize_value = int(math.ceil(float(input_image_size) / resize_inv_factor)) if is_train: augmentors = [ GoogleNetResize(crop_area_fraction=0.08, target_shape=input_image_size), 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(resize_value, cv2.INTER_CUBIC), imgaug.ResizeShortestEdge(resize_value, cv2.INTER_LINEAR), imgaug.CenterCrop((input_image_size, input_image_size)) ] return get_imagenet_dataflow(datadir=data_dir_path, is_train=is_train, batch_size=batch_size, augmentors=augmentors)
def fbresnet_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), imgaug.ToFloat32(), # avoid frequent casting in each color augmentation # 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)), imgaug.Contrast((0.6, 1.4), rgb=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.ToUint8(), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, interp=interpolation), imgaug.CenterCrop((224, 224)), ] return augmentors
def get_input_imagenet(): train = dataset.ILSVRC12("/datasets/ImageNet/ILSVRC/Data/CLS-LOC", "train", dir_structure="train", shuffle=True) test = dataset.ILSVRC12("/datasets/ImageNet/ILSVRC/Data/CLS-LOC", "val", dir_structure="train", shuffle=False) # Copied from tensorpack examples: # https://github.com/tensorpack/tensorpack/blob/master/examples/ImageNetModels/imagenet_utils.py train_augmentors = imgaug.AugmentorList([ imgaug.GoogleNetRandomCropAndResize(interp=cv2.INTER_CUBIC), # 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), ]) test_augmentors = imgaug.AugmentorList([ imgaug.ResizeShortestEdge(256, interp=cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ]) def preprocess(augmentors): def apply(x): image, label = x onehot = np.zeros(1000) onehot[label] = 1.0 image = augmentors.augment(image) return image, onehot return apply parallel = min(40, multiprocessing.cpu_count() // 2) # assuming hyperthreading train = MapData(train, preprocess(train_augmentors)) train = PrefetchDataZMQ(train, parallel) test = MultiThreadMapData(test, parallel, preprocess(test_augmentors), strict=True) test = PrefetchDataZMQ(test, 1) return train, test, ((224, 224, 3), (1000, ))