Пример #1
0
def get_data(name, batch):
    isTrain = name == 'train'

    if isTrain:
        augmentors = [
            # use lighter augs if model is too small
            imgaug.GoogleNetRandomCropAndResize(crop_area_fraction=(0.49 if args.ratio < 1 else 0.08, 1.)),
            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)
Пример #2
0
def fbresnet_augmentor(isTrain):
    """
    Augmentor used in fb.resnet.torch, for BGR images in range [0,255].
    """
    if isTrain:
        augmentors = [
            imgaug.GoogleNetRandomCropAndResize(),
            # 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 fbresnet_augmentor(isTrain):
    """
    Augmentor used in fb.resnet.torch, for BGR images in range [0,255].
    """
    if isTrain:
        augmentors = [
            imgaug.GoogleNetRandomCropAndResize(),
            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
Пример #4
0
    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
Пример #5
0
def get_basic_augmentor(isTrain):
    interpolation = cv2.INTER_LINEAR
    if isTrain:
        augmentors = [
            imgaug.GoogleNetRandomCropAndResize(),
            imgaug.Flip(horiz=True),
        ]
    else:
        augmentors = [
            imgaug.ResizeShortestEdge(256, interp=interpolation),
            imgaug.CenterCrop((224, 224)),
        ]
    return augmentors
Пример #6
0
def resizeOnly_augmentor():
    # assme BGR input
    augmentors = [
        imgaug.GoogleNetRandomCropAndResize(),
        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
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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
Пример #10
0
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
Пример #11
0
    def train_dataloader(self):
        """Summary

        Returns:
            TYPE: Description
        """
        ds_train = CustomDataSet(folder=self.hparams.data,
                                 train_or_valid='train',
                                 size=np.inf,
                                 hparams=self.hparams)
        ds_train.reset_state()
        ag_train = [
            imgaug.Affine(shear=10, interp=cv2.INTER_NEAREST),
            imgaug.Affine(translate_frac=(0.01, 0.02),
                          interp=cv2.INTER_NEAREST),
            imgaug.Affine(scale=(0.25, 1.0), interp=cv2.INTER_NEAREST),
            imgaug.RotationAndCropValid(max_deg=10, interp=cv2.INTER_NEAREST),
            imgaug.GoogleNetRandomCropAndResize(
                crop_area_fraction=(0.8, 1.0),
                aspect_ratio_range=(0.8, 1.2),
                interp=cv2.INTER_NEAREST,
                target_shape=self.hparams.shape),
            imgaug.Resize(self.hparams.shape, interp=cv2.INTER_NEAREST),
            imgaug.Flip(horiz=True, vert=False, prob=0.5),
            imgaug.Flip(horiz=False, vert=True, prob=0.5),
            imgaug.Transpose(prob=0.5),
            imgaug.Albumentations(AB.RandomRotate90(p=1)),
            imgaug.ToFloat32(),
        ]
        ds_train = AugmentImageComponent(
            ds_train,
            [
                # imgaug.Float32(),
                # imgaug.RandomChooseAug([
                #     imgaug.Albumentations(AB.IAAAdditiveGaussianNoise(p=0.25)),
                #     imgaug.Albumentations(AB.GaussNoise(p=0.25)),
                #     ]),
                # imgaug.ToUint8(),
                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.RandomChooseAug([
                    # imgaug.Albumentations(AB.IAASharpen(p=0.5)),
                    # imgaug.Albumentations(AB.IAAEmboss(p=0.5)),
                    imgaug.Albumentations(AB.RandomBrightnessContrast(p=0.5)),
                ]),
                imgaug.ToUint8(),
                imgaug.Albumentations(AB.CLAHE(tile_grid_size=(32, 32),
                                               p=0.5)),
            ],
            0)
        ds_train = AugmentImageComponents(ds_train, ag_train, [0, 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(dp[0][:, np.newaxis, :, :]).float(),
                torch.tensor(dp[1][:, np.newaxis, :, :]).float(),
            ])
        return ds_train
Пример #12
0
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, ))
Пример #13
0
def get_input_visualwakewords():
    # VISUALWAKEWORDS_CONFIG = {
    #     "train_instances": "/home/el/Datasets/COCO14/visualwakewords/instances_visualwakewords_train2014.json",
    #     "train_images": "/home/el/Datasets/COCO14/train2014",
    #     "val_instances": "/home/el/Datasets/COCO14/visualwakewords/instances_visualwakewords_val2014.json",
    #     "val_images": "/home/el/Datasets/COCO14/val2014",
    #     "filter_list": "/home/el/Datasets/COCO14/visualwakewords/mscoco_minival_ids.txt"
    # }

    target_side_size = 128
    train = VisualWakeWords(VISUALWAKEWORDS_CONFIG["train_instances"],
                            VISUALWAKEWORDS_CONFIG["train_images"],
                            shuffle=True)
    test = VisualWakeWords(VISUALWAKEWORDS_CONFIG["val_instances"],
                           VISUALWAKEWORDS_CONFIG["val_images"],
                           filter_list=VISUALWAKEWORDS_CONFIG["filter_list"],
                           shuffle=False)

    train_augmentors = imgaug.AugmentorList([
        imgaug.GoogleNetRandomCropAndResize(crop_area_fraction=(0.25, 1.),
                                            target_shape=target_side_size,
                                            interp=cv2.INTER_CUBIC),
        imgaug.Flip(horiz=True),
    ])

    autoaugment_policy = ImageNetPolicy()

    test_augmentors = imgaug.AugmentorList([
        imgaug.ResizeShortestEdge(int(target_side_size * 1.2),
                                  interp=cv2.INTER_CUBIC),
        imgaug.CenterCrop((target_side_size, target_side_size)),
    ])

    def preprocess(train):
        def apply(x):
            image, label = x
            onehot = np.zeros(2)
            onehot[label] = 1.0
            augmentors = train_augmentors if train else test_augmentors
            image = augmentors.augment(image)
            if train:
                image = np.array(autoaugment_policy(Image.fromarray(image)))
            return image, onehot
            # mean = [0.4767, 0.4488, 0.4074]
            # std = [0.2363, 0.2313, 0.2330]
            # return (image / 255.0 - mean) / std, onehot

        return apply

    parallel = min(18,
                   multiprocessing.cpu_count() // 2)  # assuming hyperthreading
    train = MapData(train, preprocess(train=True))
    train = PrefetchDataZMQ(train, parallel)

    test = MultiThreadMapData(test,
                              parallel,
                              preprocess(train=False),
                              strict=True)
    test = PrefetchDataZMQ(test, 1)

    return train, test, ((target_side_size, target_side_size, 3), (2, ))