コード例 #1
0
ファイル: main.py プロジェクト: alainray/age_gender_detection
def init_datasets(x, y, cfg):
    # We assume IMDB, FairFace
    print("Loading Datasets...")
    transforms_train = transforms.Compose([
        Resize(224),
        RandomHorizontalFlip(0.5),
        RandomCrop(224),
        RandomGrayscale(0.5),
        ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
    transforms_val = transforms.Compose([
        Resize(224),
        RandomHorizontalFlip(0.5),
        CenterCrop(224),
        RandomGrayscale(0.5),
        ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    datasets = {k: [] for k in ['train', 'val', 'test']}

    for i, (x_val, y_val) in enumerate(zip(x, y)):
        #print(x_val,y_val)
        X_train, X_test, y_train, y_test = train_test_split(
            x_val,
            y_val,
            test_size=(1 - cfg.TRAIN.TRAIN_RATIO),
            random_state=42)
        X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                          y_train,
                                                          test_size=0.15,
                                                          random_state=42)
        datasets['train'].append(
            IMDBDataset(X_train,
                        y_train,
                        root_dir=cfg.DATASET.DATA_FOLDER[i],
                        transform=transforms_train))
        datasets['val'].append(
            IMDBDataset(X_val,
                        y_val,
                        root_dir=cfg.DATASET.DATA_FOLDER[i],
                        transform=transforms_val))
        datasets['test'].append(
            IMDBDataset(X_test,
                        y_test,
                        root_dir=cfg.DATASET.DATA_FOLDER[i],
                        transform=transforms_val))

    for k in ['train', 'val', 'test']:
        datasets[k] = ConcatDataset(datasets[k])
    return datasets
コード例 #2
0
    def __init__(self,
                 image_folder,
                 max_images=False,
                 image_size=(512, 512),
                 add_random_masks=False):
        super(ImageInpaintingData, self).__init__()

        if isinstance(image_folder, str):
            self.images = glob.glob(os.path.join(image_folder, "clean/*"))
        else:
            self.images = list(
                chain.from_iterable([
                    glob.glob(os.path.join(i, "clean/*")) for i in image_folder
                ]))
        assert len(self.images) > 0

        if max_images:
            self.images = random.choices(self.images, k=max_images)
        print(f"Find {len(self.images)} images.")

        self.img_size = image_size

        self.transformer = Compose([
            RandomGrayscale(p=0.4),
            # ColorJitter(brightness=0.2, contrast=0.2, saturation=0, hue=0),
            ToTensor(),
            # Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        self.add_random_masks = add_random_masks
コード例 #3
0
ファイル: dataset.py プロジェクト: ekorman/neurve
 def __init__(self,
              base_dataset,
              img_size=(32, 32),
              color_dist_strength=0.5):
     """
     Parameters
     ----------
     base_dataset : Dataset
     img_size : tuple
     color_dist_strength : float
     """
     self.base_dataset = base_dataset
     # get color distortion random transform, Color distortion (see Pytorch
     # pseudocode in arXiv:2002.05709)
     color_jitter = ColorJitter(
         0.8 * color_dist_strength,
         0.8 * color_dist_strength,
         0.8 * color_dist_strength,
         0.2 * color_dist_strength,
     )
     rnd_color_jitter = RandomApply([color_jitter], p=0.8)
     rnd_gray = RandomGrayscale(p=0.2)
     self.transform = Compose([
         RandomResizedCrop(img_size), rnd_color_jitter, rnd_gray,
         ToTensor()
     ])
コード例 #4
0
ファイル: kather_dataset.py プロジェクト: stegmuel/DANN_py3
 def get_image_transformer(self):
     transformations = [
         ColorJitter(brightness=0.3, contrast=0.3, saturation=0.1, hue=0.1),
         RandomGrayscale(p=0.5),
         RandomRotation(degrees=[0., 45.]),
         RandomResizedCrop(size=[224, 224], scale=(0.3, 1.0))
     ]
     return Compose([RandomApply(transformations, p=0.7), ToTensor()])
コード例 #5
0
 def transformer(mean, std):
     m = Compose([RandomGrayscale(p=0.2),
                  # RandomHorizontalFlip(p=0.2), don't use them since label locations are not available
                  # RandomVerticalFlip(p=0.2),
                  ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
                  ToTensor(),
                  Normalize(mean, std)])
     return m
コード例 #6
0
 def __init__(self):
     self.random_resized_crop = RandomResizedCrop(range=(0.85,1.0))
     self.random_horizontal_flip = RandomHorizontalFlip(p=1.0)
     self.other_transform = Compose([
         RandomApply([ColorJitter(0.8, 0.8, 0.2)], p=0.3),
         RandomGrayscale(p=0.3),
         RandomGaussianBlur(p=0.3),
         ToTensor()
     ])
コード例 #7
0
 def transformer(mean, std):
     m = Compose([
         RandomGrayscale(p=0.2),
         # RandomHorizontalFlip(p=0.2),
         # RandomVerticalFlip(p=0.2),
         ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
         ToTensor(),
         Normalize(mean, std)
     ])
     return m
コード例 #8
0
ファイル: data.py プロジェクト: ryanlindeborg/Sequoia
 def __init__(self, hp: HParams):
     s = hp.colour_distortion
     self.simclr_augment = Compose([
         RandomResizedCrop(hp.image_size),
         RandomHorizontalFlip(),
         RandomApply([ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s)],
                     p=0.8),
         RandomGrayscale(p=0.2),
         ToTensor(),
     ])
コード例 #9
0
ファイル: transforms.py プロジェクト: adbugger/FewShot
def ColorDistort(options):
    s = options.jitter_strength
    # No need to normalize after color jitter?
    # Or normalize before color jitter?
    return Compose([
        RandomApply([
            ColorJitter(0.8*s, 0.8*s, 0.8*s, 0.2*s)
        ], p=0.8),
        RandomGrayscale(p=0.2),
        ToTensor(),
        Normalize(options.image_mean, options.image_std),
    ])
コード例 #10
0
ファイル: camvid.py プロジェクト: liuguoyou/PixelPick
    def _photometric_augmentations(self, x):
        if self.photometric_augmentations["random_color_jitter"]:
            color_jitter = ColorJitter(brightness=0.8, contrast=0.8, saturation=0.8, hue=0.2)
            x = RandomApply([color_jitter], p=0.8)(x)

        if self.photometric_augmentations["random_grayscale"]:
            x = RandomGrayscale(0.2)(x)

        if self.photometric_augmentations["random_gaussian_blur"]:
            w, h = x.size
            x = GaussianBlur(kernel_size=int((0.1 * min(w, h) // 2 * 2) + 1))(x)
        return x
コード例 #11
0
    def __init__(self, opts, setname, input_dim):
        self.dataroot = opts.dataroot
        images = os.listdir(os.path.join(self.dataroot, opts.phase + setname))
        self.img = [
            os.path.join(self.dataroot, opts.phase + setname, x)
            for x in images
        ]
        self.size = len(self.img)
        self.input_dim = input_dim

        # setup image transformation
        normalize = Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
        if args.aug_plus:
            # MoCo v2's aug: similar to SimCLR https://arxiv.org/abs/2002.05709
            augmentation = [
                #transforms.RandomResizedCrop(224, scale=(0.2, 1.)),
                Resize((opts.resize_size, opts.resize_size), Image.BICUBIC),
                CenterCrop(opts.crop_size),
                RandomApply([ColorJitter(0.4, 0.4, 0.4, 0.1)], p=0.8),
                RandomGrayscale(p=0.2),
                RandomApply([moco.loader.GaussianBlur([.1, 2.])], p=0.5),
                RandomHorizontalFlip(),
                ToTensor(),
                normalize
            ]
        else:
            # MoCo v1's aug: the same as InstDisc https://arxiv.org/abs/1805.01978
            augmentation = [
                #RandomResizedCrop(224, scale=(0.2, 1.)),
                Resize((opts.resize_size, opts.resize_size), Image.BICUBIC),
                CenterCrop(opts.crop_size),
                RandomGrayscale(p=0.2),
                ColorJitter(0.4, 0.4, 0.4, 0.4),
                RandomHorizontalFlip(),
                ToTensor(),
                normalize
            ]
        self.transforms = Compose(augmentation)
        print('%s: %d images' % (setname, self.size))
        return
コード例 #12
0
ファイル: lapsrn_data.py プロジェクト: wuwuwujy/TryCode
def SR4x_transform(subset):
    try:
        ts = Compose([
            RandomCrop(
                (subset,
                 subset)),  #, pad_if_needed=False, padding_mode='reflect'),
            RandomHorizontalFlip(),
            RandomVerticalFlip(),
            ColorJitter(hue=.05, saturation=.05),
            RandomAffine(30),
            RandomGrayscale(),
        ])
    except:
        ts = Compose([
            Resize(subset),
            RandomHorizontalFlip(),
            RandomVerticalFlip(),
            ColorJitter(hue=.05, saturation=.05),
            RandomAffine(30),
            RandomGrayscale(),
        ])
    return ts
コード例 #13
0
def get_transform(
    target_size=(512, 512),
    transform_list='horizontal_flip',  # random_crop | keep_aspect
    augment_ratio=0.5,
    is_train=True,
):
    transform = list()
    transform_list = transform_list.split(', ')
    augments = list()

    for transform_name in transform_list:
        if transform_name == 'random_crop':
            scale = (0.6, 1.0) if is_train else (0.8, 1.0)
            transform.append(RandomResizedCrop(target_size, scale=scale))
        # elif transform_name == 'resize':
        #     transform.append(Resize(target_size))
        elif transform_name == 'keep_aspect':
            transform.append(KeepAsepctResize(target_size))
        elif transform_name == 'Affine':
            augments.append(
                RandomAffine(degrees=(-180, 180),
                             scale=(0.8889, 1.0),
                             shear=(-36, 36)))
        elif transform_name == 'centor_crop':
            augments.append(CenterCrop(target_size))
        elif transform_name == 'horizontal_flip':
            augments.append(RandomHorizontalFlip())
        elif transform_name == 'vertical_flip':
            augments.append(RandomVerticalFlip())
        elif transform == 'random_grayscale':
            p = 0.5 if is_train else 0.25
            transform.append(RandomGrayscale(p))
        elif transform_name == 'random_rotate':
            augments.append(RandomRotation(180))
        elif transform_name == 'color_jitter':
            brightness = 0.1 if is_train else 0.05
            contrast = 0.1 if is_train else 0.05
            augments.append(
                ColorJitter(
                    brightness=brightness,
                    contrast=contrast,
                    saturation=0,
                    hue=0,
                ))

    transform.append(RandomApply(augments, p=augment_ratio))
    transform.append(ToTensor())
    transform.append(
        Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
    return Compose(transform)
コード例 #14
0
class Transforms(Enum):
    """ Enum of possible transforms. 

    By having this as an Enum, we can choose which transforms to use from the
    command-line.
    This also makes it easier to check for identity, e.g. to check wether a
    particular transform was used.  

    TODO: Add the SimCLR/MOCO/etc transforms from  https://pytorch-lightning-bolts.readthedocs.io/en/latest/transforms.html
    TODO: Figure out a way to let people customize the arguments to the transforms?
    """
    three_channels = ThreeChannels()
    to_tensor = ToTensor()
    random_grayscale = RandomGrayscale()
    channels_first = ChannelsFirst()
    channels_first_if_needed = ChannelsFirstIfNeeded()
    channels_last = ChannelsLast()
    channels_last_if_needed = ChannelsLastIfNeeded()
    resize_64x64 = Resize((64, 64))
    resize_32x32 = Resize((32, 32))

    # simclr = Simclr

    def __call__(self, x):
        return self.value(x)

    @classmethod
    def _missing_(cls, value: Any):
        # called whenever performing something like Transforms[<something>]
        # with <something> not being one of the enum values.
        for e in cls:
            if e.name == value:
                return e
            elif type(e.value) == type(value):
                return e
        return super()._missing_(value)

    def shape_change(
            self, input_shape: Union[Tuple[int, ...],
                                     torch.Size]) -> Tuple[int, ...]:
        raise NotImplementedError(f"TODO: Add shape (tuple) support to {self}")
        if isinstance(self.value, Transform):
            return self.value.shape_change(input_shape)

    def space_change(self, input_space: gym.Space) -> gym.Space:
        raise NotImplementedError(f"TODO: Add space support to {self}")
        if isinstance(self.value, Transform):
            return self.value.space_change(input_space)
コード例 #15
0
    def get_transform(self, trns_mode):
        transform_list = []
        transform_list.append(Resize((256, 256)))
        if trns_mode == 'train':
            transform_list.append(RandomCrop((224, 224)))
            transform_list.append(RandomGrayscale())
            transform_list.append(RandomOrder(
                [RandomHorizontalFlip(), ColorJitter(), RandomRotation(20)]))
        else:
            transform_list.append(CenterCrop((224, 224)))
        transform_list.append(ToTensor())
        transform_list.append(Normalize(self.mean, self.std))
        if trns_mode == 'train':
            transform_list.append(RandomErasing(value='random'))

        return Compose(transform_list)
コード例 #16
0
    def __init__(self, opts):
        self.dataroot = opts.dataroot
        # A
        images_A = os.listdir(os.path.join(self.dataroot, opts.phase + 'A'))
        self.A = [
            os.path.join(self.dataroot, opts.phase + 'A', x) for x in images_A
        ]

        # B
        images_B = os.listdir(os.path.join(self.dataroot, opts.phase + 'B'))
        self.B = [
            os.path.join(self.dataroot, opts.phase + 'B', x) for x in images_B
        ]

        self.A_size = len(self.A)
        self.B_size = len(self.B)
        self.dataset_size = max(self.A_size, self.B_size)
        self.input_dim_A = opts.input_dim_a
        self.input_dim_B = opts.input_dim_b

        # setup image transformation
        transforms = [
            Resize((opts.resize_size, opts.resize_size), Image.BICUBIC),
            CenterCrop(opts.crop_size),
            RandomApply([ColorJitter(0.4, 0.4, 0.4, 0.1)], p=0.8),
            RandomGrayscale(p=0.2),
            RandomApply([moco.loader.GaussianBlur([.1, 2.])], p=0.5),
            RandomHorizontalFlip(),
        ]
        if opts.phase == 'train':
            transforms.append(RandomCrop(opts.crop_size))
        else:
            #ALERT: what to do!
            transforms.append(CenterCrop(opts.crop_size))
        if not opts.no_flip:
            transforms.append(RandomHorizontalFlip())
        transforms.append(ToTensor())
        transforms.append(Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]))
        self.transforms = Compose(transforms)
        print('A: %d, B: %d images' % (self.A_size, self.B_size))
        return
コード例 #17
0
    def __init__(self,
                 size: tuple = (32, 32),
                 mode: str = 'pretrain',
                 **kwargs):
        if mode == 'pretrain':
            self.transforms = Compose([
                RandomResizedCrop(size=size),
                RandomHorizontalFlip(p=0.5),
                RandomApply([ColorJitter(0.4, 0.4, 0.4, 0.1)], p=0.8),
                RandomGrayscale(p=0.2),
                ToTensor(),
                Normalize(mean=self.mean, std=self.std)
            ])
        elif mode == 'test':
            self.transforms = Compose(
                [ToTensor(),
                 Normalize(mean=self.mean, std=self.std)])
        else:
            raise NotImplementedError

        self.size = size
        self.mode = mode
コード例 #18
0
ファイル: config.py プロジェクト: BenJourdan/BikeML
#Baseline_1b
Baseline_1b_config = dict(
    exp_name='Margin tuning: 0.9',
    model=BaselineModel_1b,
    dataloader=BikeDataLoader,
    criterion=SupervisedCosineContrastiveLoss,
    project_path="./baseline_1b",
    input_shape=(dataloader_params["batch_size"], 3, base_config["image_dim"],
                 base_config["image_dim"]),
    mlp_layers=4,
    embedding_dimension=128,
    margin=0.9,
    transforms=torchvision.transforms.Compose([
        SquareCrop((base_config["image_dim"], base_config["image_dim"])),
        RandomHorizontalFlip(p=0.5),
        ColorJitter(0.8, 0.8, 0.8, 0.2),
        RandomGrayscale(p=0.2),
        ToTensor()
    ]),
    tiny_transforms=torchvision.transforms.Compose([
        SquareCrop((base_config["image_dim"], base_config["image_dim"])),
        ColorJitter(0.8, 0.8, 0.8, 0.2),
        RandomGrayscale(p=0.2),
        ToTensor()
    ]))

hyperparameters = base_config
hyperparameters["dataloader_params"] = dataloader_params
hyperparameters.update(Baseline_1a_config)
コード例 #19
0
ファイル: train.py プロジェクト: daniel-j-h/visiont-pytorch
def main(args):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    if torch.cuda.is_available():
        torch.backends.cudnn.benchmark = True

    # ImageNet stats for now
    mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]

    transform = Compose([
        ToImageMode("RGB"),
        Resize(1024),
        RandomCrop(224),
        RandomHorizontalFlip(p=0.5),
        RandomApply([
            ColorJitter(brightness=0.8, contrast=0.8, saturation=0.8, hue=0.2)
        ],
                    p=0.8),
        RandomApply([GaussianBlur((3, 3), (1.5, 1.5))], p=0.1),
        RandomGrayscale(p=0.2),
        ToTensor(),
        Normalize(mean=mean, std=std),
        ToPatches(16)
    ])

    # Applying the same transform twice will give us
    # different transformations on the same image,
    # because the transformation's rng state changes.
    dataset = ImageDirectory(args.dataset, transform, transform)

    # TODO: hard coded for now, works on my 2x Titan RTX machine
    loader = DataLoader(dataset,
                        batch_size=144,
                        num_workers=40,
                        shuffle=True,
                        pin_memory=True,
                        drop_last=True)

    # We will chop off the final layer anyway,
    # therefore num_classes doesn't matter here.
    online = VisionTransformer(num_classes=1, C=3, H=224, W=224, P=16)
    target = VisionTransformer(num_classes=1, C=3, H=224, W=224, P=16)

    # Projection heads for both networks
    #online.final = mlp(768, 4096, 256)
    #target.final = mlp(768, 4096, 256)
    online.final = nn.Identity()
    target.final = nn.Identity()

    # Target network does not learn on its own.
    # Gets average of online network's weights.

    online.train()
    target.eval()

    for param in target.parameters():
        param.requires_grad = False

    def update_target():
        update(target, online, 0.99)

    # In addition to projection heads,
    # The online network has predictor.
    #predictor = mlp(256, 4096, 256)
    predictor = mlp(768, 4096, 768)

    # Move everything to devices

    online = online.to(device)
    online = nn.DataParallel(online)

    predictor = predictor.to(device)
    predictor = nn.DataParallel(predictor)

    target = target.to(device)
    target = nn.DataParallel(target)

    def criterion(x, y):
        x = nn.functional.normalize(x, dim=-1)
        y = nn.functional.normalize(y, dim=-1)
        return 2 - 2 * (x * y).sum(dim=-1)

    # Online and predictor learns, target gets assigned moving average of online network's weights.

    lr = 0.1
    epochs = 15

    optimizer = torch.optim.SGD(list(online.parameters()) +
                                list(predictor.parameters()),
                                lr=lr,
                                momentum=0.9)
    scheduler = torch.optim.lr_scheduler.OneCycleLR(
        optimizer, max_lr=lr, steps_per_epoch=len(loader), epochs=epochs)

    scaler = torch.cuda.amp.GradScaler()

    step = 0
    running = 0

    for epoch in range(epochs):
        progress = tqdm(loader, desc=f"Epoch {epoch+1}", unit="batch")

        for inputs1, inputs2 in progress:
            assert inputs1.size() == inputs2.size()

            # Overlap data transfers to gpus, pinned memory
            inputs1 = inputs1.to(device, non_blocking=True)
            inputs2 = inputs2.to(device, non_blocking=True)

            optimizer.zero_grad()

            with torch.cuda.amp.autocast():
                # Target network is in eval mode and does not
                # require grads, forward no grad ctx to be sure
                with torch.no_grad():
                    labels1 = target(inputs1).detach()
                    labels2 = target(inputs2).detach()

                outputs1 = predictor(online(inputs1))
                outputs2 = predictor(online(inputs2))

                # Symmetrize the loss, both transformations
                # go through both networks, one at a time
                loss = criterion(outputs1, labels2) + criterion(
                    outputs2, labels1)
                loss = loss.mean()

            scaler.scale(loss).backward()

            # Transformers need their nails clipped
            scaler.unscale_(optimizer)
            nn.utils.clip_grad_norm_(online.parameters(), 1)
            nn.utils.clip_grad_norm_(predictor.parameters(), 1)

            scaler.step(optimizer)
            scaler.update()

            scheduler.step()

            # After training the online network, we transfer
            # a weighted average of the weights to the target
            update_target()

            running += loss.item() * inputs1.size(0)

            if step % 100 == 0:
                progress.write(f"loss: {running / 100}")
                running = 0

            step += 1

        torch.save(online.state_dict(), f"vt-{epoch + 1:03d}.pth")
コード例 #20
0
        #plt.plot( testErrsTotal, '-', label = "test total", color = (0.5,0.8,0) )

        plt.yscale('log')
        plt.grid(True)
        plt.legend()
        plt.savefig("./errors")

    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    img_size = (224, 224)
    composed = Compose([
        ToPILImage(),
        Resize(img_size),
        RandomHorizontalFlip(),
        RandomGrayscale(p=0.5),
        RandomRotation(degrees=30, center=None),
        ToTensor(), normalize
    ])

    train_dataset = HumpbackWhaleDataset(csv_file='./train.csv',
                                         root_dir="./train",
                                         transform=composed)
    #test_dataset = TitanicDataset(csvFile = 'test.csv')

    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=100,
                                               shuffle=True,
                                               num_workers=4)
    #test_loader = torch.utils.data.DataLoader(dataset = test_dataset,
    #                                           batch_size = 50,
コード例 #21
0
def build_transforms(height,
                     width,
                     transforms='random_flip',
                     norm_mean=[0.485, 0.456, 0.406],
                     norm_std=[0.229, 0.224, 0.225],
                     **kwargs):
    """Builds train and test transform functions.

    Args:
        height (int): target image height.
        width (int): target image width.
        transforms (str or list of str, optional): transformations applied to model training.
            Default is 'random_flip'.
        norm_mean (list or None, optional): normalization mean values. Default is ImageNet means.
        norm_std (list or None, optional): normalization standard deviation values. Default is
            ImageNet standard deviation values.
    """
    if transforms is None:
        transforms = []

    if isinstance(transforms, str):
        transforms = [transforms]

    if not isinstance(transforms, list):
        raise ValueError(
            'transforms must be a list of strings, but found to be {}'.format(
                type(transforms)))

    if len(transforms) > 0:
        transforms = [t.lower() for t in transforms]

    if norm_mean is None or norm_std is None:
        #norm_mean, norm_std = compute_mean_std.main()
        norm_mean = [0.485, 0.456, 0.406]  # imagenet mean
        norm_std = [0.229, 0.224, 0.225]  # imagenet std
    normalize = Normalize(mean=norm_mean, std=norm_std)

    print('Building train transforms ...')
    transform_tr = []

    print('+ resize to {}x{}'.format(height, width))
    transform_tr += [Resize((height, width))]

    if 'random_flip' in transforms:
        print('+ random flip')
        transform_tr += [RandomHorizontalFlip()]

    if 'random_crop' in transforms:
        print('+ random crop (enlarge to {}x{} and '
              'crop {}x{})'.format(int(round(height * 1.125)),
                                   int(round(width * 1.125)), height, width))
        transform_tr += [Random2DTranslation(height, width)]

    if 'random_patch' in transforms:
        print('+ random patch')
        transform_tr += [RandomPatch()]

    if 'color_jitter' in transforms:
        print('+ color jitter')
        transform_tr += [
            ColorJitter(brightness=0.2, contrast=0.15, saturation=0, hue=0)
        ]

    print('+ to torch tensor of range [0, 1]')
    transform_tr += [ToTensor()]

    print('+ normalization (mean={}, std={})'.format(norm_mean, norm_std))
    transform_tr += [normalize]

    if 'random_erase' in transforms:
        print('+ random erase')
        transform_tr += [RandomErasing(mean=norm_mean)]

    if 'gray_scale' in transforms:
        print('+ gray scale')
        transform_tr += [RandomGrayscale(p=0.2)]

    transform_tr = Compose(transform_tr)

    print('Building test transforms ...')
    print('+ resize to {}x{}'.format(height, width))
    print('+ to torch tensor of range [0, 1]')
    print('+ normalization (mean={}, std={})'.format(norm_mean, norm_std))

    transform_te = Compose([
        Resize((height, width)),
        ToTensor(),
        normalize,
    ])

    return transform_tr, transform_te
コード例 #22
0
import sys
import time

NUM_CHANNELS = 3
NUM_CLASSES = 22

to_tensor=transforms.ToTensor()
color_transform = Colorize()
image_transform = ToPILImage()
to_img=transforms.ToPILImage()

 
input_transform = Compose([ 
    
    RandomGrayscale(0.02),
    CenterCrop((512,512)),
    ToTensor(),
    Normalize([.485, .456, .406], [.229, .224, .225]),
    ])

target_transform = Compose([
    CenterCrop((512,512)), 
    ToLabel(),
    Relabel(255, 22),
    ])

)

    
def train(args, model):
コード例 #23
0
def get_transforms(transforms_list,
                   width,
                   height,
                   is_train):
    transforms = []
    for transform in transforms_list:
        if transform == 'random_resized_crop':
            scale = (0.8, 1.2) if is_train else (1.0, 1.0)
            ratio = (1.0, 1.0) if is_train else (1.0, 1.0)
            transforms.append(
                RandomResizedCrop(
                    (width, height),
                    scale=scale,
                    ratio=ratio,
                )
                
            )
        elif transform == 'center_crop' :
            transforms.append(
                CenterCrop((700, 700))
            )
        elif transform == 'resize':
            transforms.append(
                Resize(
                    (width, height)
                )
            )
        elif transform == 'resize':
            transforms.append(
                Resize(
                    (width, height)
                )
            )
        elif transform == 'crop_black': # crop_black은 첫번째로 넣어줘야함.
            p = 1.0 if is_train else 1.0
            transforms.append(CropBlack(p))
        elif transform == 'random_rotate':
            p = 0.5 if is_train else 0.25
            transforms.append(RandomRotate(p))
        elif transform == 'random_vertical_flip':
            p = 0.5 if is_train else 0.25
            transforms.append(RandomVerticalFlip(p))
        elif transform == 'random_horizontal_flip':
            p = 0.5 if is_train else 0.25
            transforms.append(RandomHorizontalFlip(p))
        elif transform == 'random_color_jitter':
            brightness = 0.1 if is_train else 0.0
            contrast = 0.1 if is_train else 0.0
            transforms.append(ColorJitter(
                brightness=brightness,
                contrast=contrast,
                saturation=0,
                hue=0,
            ))
        elif transform == 'random_grayscale':
            p = 0.5 if is_train else 0.25
            transforms.append(RandomGrayscale(p))
        elif transform == 'ben_graham':
            p = 1 if is_train else 1
            transforms.append(BenGrahamAug(p))
        elif transform == 'imagenet_poilcy':
            transforms.append(ImageNetPolicy())
        elif transform == 'cifar_policy':
            transforms.append(CIFAR10Policy())
        elif transform == 'svhn_policy':
            transform.append(SVHNPolicy())
        else:
            print(transform)
            raise NotImplementedError
    return transforms
コード例 #24
0
def _build_transform_train(cfg, choices, expected_size, normalize):
    print('Building transform_train')
    tfm_train = []

    print('+ resize to {}'.format(expected_size))
    tfm_train += [Resize(cfg.INPUT.SIZE)]

    if 'random_flip' in choices:
        print('+ random flip')
        tfm_train += [RandomHorizontalFlip()]

    if 'random_translation' in choices:
        print('+ random translation')
        tfm_train += [
            Random2DTranslation(cfg.INPUT.SIZE[0], cfg.INPUT.SIZE[1])
        ]

    if 'random_crop' in choices:
        crop_padding = cfg.INPUT.CROP_PADDING
        print('+ random crop (padding = {})'.format(crop_padding))
        tfm_train += [RandomCrop(cfg.INPUT.SIZE, padding=crop_padding)]

    if 'random_resized_crop' in choices:
        print('+ random resized crop')
        tfm_train += [RandomResizedCrop(cfg.INPUT.SIZE)]

    if 'center_crop' in choices:
        print('+ center crop (on 1.125x enlarged input)')
        enlarged_size = [int(x * 1.125) for x in cfg.INPUT.SIZE]
        tfm_train += [Resize(enlarged_size)]
        tfm_train += [CenterCrop(cfg.INPUT.SIZE)]

    if 'imagenet_policy' in choices:
        print('+ imagenet policy')
        tfm_train += [ImageNetPolicy()]

    if 'cifar10_policy' in choices:
        print('+ cifar10 policy')
        tfm_train += [CIFAR10Policy()]

    if 'svhn_policy' in choices:
        print('+ svhn policy')
        tfm_train += [SVHNPolicy()]

    if 'randaugment' in choices:
        n_ = cfg.INPUT.RANDAUGMENT_N
        m_ = cfg.INPUT.RANDAUGMENT_M
        print('+ randaugment (n={}, m={})'.format(n_, m_))
        tfm_train += [RandAugment(n_, m_)]

    if 'randaugment_fixmatch' in choices:
        n_ = cfg.INPUT.RANDAUGMENT_N
        print('+ randaugment_fixmatch (n={})'.format(n_))
        tfm_train += [RandAugmentFixMatch(n_)]

    if 'randaugment2' in choices:
        n_ = cfg.INPUT.RANDAUGMENT_N
        print('+ randaugment2 (n={})'.format(n_))
        tfm_train += [RandAugment2(n_)]

    if 'colorjitter' in choices:
        print('+ color jitter')
        tfm_train += [
            ColorJitter(brightness=cfg.INPUT.COLORJITTER_B,
                        contrast=cfg.INPUT.COLORJITTER_C,
                        saturation=cfg.INPUT.COLORJITTER_S,
                        hue=cfg.INPUT.COLORJITTER_H)
        ]

    if 'randomgrayscale' in choices:
        print('+ random gray scale')
        tfm_train += [RandomGrayscale(p=cfg.INPUT.RGS_P)]

    if 'gaussian_blur' in choices:
        print(f'+ gaussian blur (kernel={cfg.INPUT.GB_K})')
        tfm_train += [
            RandomApply([GaussianBlur(cfg.INPUT.GB_K)], p=cfg.INPUT.GB_P)
        ]

    print('+ to torch tensor of range [0, 1]')
    tfm_train += [ToTensor()]

    if 'cutout' in choices:
        cutout_n = cfg.INPUT.CUTOUT_N
        cutout_len = cfg.INPUT.CUTOUT_LEN
        print('+ cutout (n_holes={}, length={})'.format(cutout_n, cutout_len))
        tfm_train += [Cutout(cutout_n, cutout_len)]

    if 'normalize' in choices:
        print('+ normalization (mean={}, '
              'std={})'.format(cfg.INPUT.PIXEL_MEAN, cfg.INPUT.PIXEL_STD))
        tfm_train += [normalize]

    if 'gaussian_noise' in choices:
        print('+ gaussian noise (mean={}, std={})'.format(
            cfg.INPUT.GN_MEAN, cfg.INPUT.GN_STD))
        tfm_train += [GaussianNoise(cfg.INPUT.GN_MEAN, cfg.INPUT.GN_STD)]

    if 'instance_norm' in choices:
        print('+ instance normalization')
        tfm_train += [InstanceNormalization()]

    tfm_train = Compose(tfm_train)

    return tfm_train