Python torchvision.transforms.RandomSizedCrop() Examples

The following are 30 code examples of torchvision.transforms.RandomSizedCrop(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torchvision.transforms , or try the search function .
Example #1
Source File: test_modules.py    From CAMP_iccv19 with Apache License 2.0 6 votes vote down vote up
def test_f30k_dataloader():
    data_name = "f30k"
    data_path = "./data/f30k"
    vocab_path = "./vocab/"

    vocab = pickle.load(open(os.path.join(vocab_path,
            '%s_vocab.pkl' % data_name), 'rb'))
    roots, ids = data.get_paths(data_path, data_name, False)
    transform = transforms.Compose([transforms.RandomSizedCrop(224),
                                    transforms.ToTensor()])
    print (roots, ids)
    train_loader = data.get_loader_single(data_name, "train", # !!!
                                     roots["train"]["img"],
                                     roots["train"]["cap"],
                                     vocab, transform, ids=ids["train"],
                                     batch_size=16, shuffle=False,
                                     num_workers=1,
                                     collate_fn=data.collate_fn,
                                     distributed=False)
    print ("f30k dataloader output:", train_loader.dataset.img_num)
    #for (id, x) in enumerate(train_loader):
        #if id > 0 : break
        #print (id, x) 
Example #2
Source File: data.py    From CAMP_iccv19 with Apache License 2.0 6 votes vote down vote up
def get_transform(data_name, split_name, opt):
    normalizer = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                      std=[0.229, 0.224, 0.225])
    t_list = []
    if split_name == "train":
        t_list = [transforms.RandomSizedCrop(opt.crop_size),
                  transforms.RandomHorizontalFlip()]
    elif split_name == "val":
        t_list = [transforms.Resize(256), transforms.CenterCrop(224)]
        #t_list = [transforms.Resize((224, 224))]
    elif split_name == "test":
        t_list = [transforms.Resize(256), transforms.CenterCrop(224)]
        #t_list = [transforms.Resize((224, 224))]

    """if "CUHK" in data_name:
        t_end = [transforms.ToTensor()]
    else:"""
    t_end = [transforms.ToTensor(), normalizer]
    
    transform = transforms.Compose(t_list + t_end)
    return transform 
Example #3
Source File: amnet.py    From AMNet with MIT License 6 votes vote down vote up
def init_transformations(self):

        if self.hps.torchvision_version_major == 0 and self.hps.torchvision_version_minor < 2:
            _resize = transforms.Scale
            _rnd_resize_crop = transforms.RandomSizedCrop
        else:
            _resize = transforms.Resize
            _rnd_resize_crop = transforms.RandomResizedCrop

        self.train_transform = transforms.Compose([
            _resize([264, 264]),
            _rnd_resize_crop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize(mean=self.hps.img_mean, std=self.hps.img_std)
        ])

        # Test
        self.test_transform = transforms.Compose([
            _resize([224, 224]),
            transforms.ToTensor(),
            transforms.Normalize(mean=self.hps.img_mean, std=self.hps.img_std)
        ])

        return 
Example #4
Source File: ImageNet12.py    From quantized_distillation with MIT License 6 votes vote down vote up
def getTrainLoader(self, batch_size, shuffle=True, num_workers=4):

        # first we define the training transform we will apply to the dataset
        list_of_transforms = []
        list_of_transforms.append(vision_transforms.RandomSizedCrop(self.size_images))
        list_of_transforms.append(vision_transforms.RandomHorizontalFlip())

        if self.type_of_data_augmentation == 'extended':
            list_of_transforms.append(vision_transforms.ColorJitter(brightness=0.4,
                                                                             contrast=0.4,
                                                                             saturation=0.4))
        list_of_transforms.append(vision_transforms.ToTensor())
        if self.type_of_data_augmentation == 'extended':
            list_of_transforms.append(vision_transforms_extension.Lighting(alphastd=0.1,
                                                                          eigval=self.pca['eigval'],
                                                                          eigvec=self.pca['eigvec']))

        list_of_transforms.append(vision_transforms.Normalize(mean=self.meanstd['mean'],
                                                             std=self.meanstd['std']))
        train_transform = vision_transforms.Compose(list_of_transforms)
        train_set = torchvision.datasets.ImageFolder(self.trainFolder, train_transform)
        train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=shuffle,
                                        num_workers=num_workers, pin_memory=self.pin_memory)

        return train_loader 
Example #5
Source File: imagenet_models.py    From imagenet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_label(self, train_dir):
		# Normalize on RGB Value
		normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
										 std=[0.229, 0.224, 0.225])
		# Train -> Preprocessing -> Tensor
		train_dataset = datasets.ImageFolder(
			train_dir,
			transforms.Compose([
				transforms.RandomSizedCrop(self._size[0]), #224 , 299
				transforms.RandomHorizontalFlip(),
				transforms.ToTensor(),
				normalize,
			]))

		# Get number of labels
		return train_dataset.classes 
Example #6
Source File: modular_transforms.py    From dlupi-heteroscedastic-dropout with MIT License 5 votes vote down vote up
def get_reproducible_rand_transform(opt):
    """ Image data and side info can be transformed identically. """
    return [
        reproducible_transforms.RandomSizedCrop(opt.image_size),
        reproducible_transforms.RandomHorizontalFlip(),
        reproducible_transforms.ToTensor(),
        reproducible_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                          std=[0.229, 0.224, 0.225])
    ] 
Example #7
Source File: preprocess.py    From DSGN with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #8
Source File: preprocess.py    From DSGN with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #9
Source File: preprocess.py    From DeepLiDAR with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #10
Source File: preprocess.py    From DeepLiDAR with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #11
Source File: preprocess.py    From DeepLiDAR with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #12
Source File: preprocess.py    From DeepLiDAR with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #13
Source File: preprocess.py    From DSGN with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #14
Source File: modular_transforms.py    From dlupi-heteroscedastic-dropout with MIT License 5 votes vote down vote up
def get_nonreproducible_rand_transform(opt):
    return transforms.Compose([
        transforms.RandomSizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ]) 
Example #15
Source File: datasets.py    From Deep_Openset_Recognition_through_Uncertainty with MIT License 5 votes vote down vote up
def __get_transforms(self, patch_size):
        train_transforms = transforms.Compose([
            transforms.Resize(patch_size + 32),
            transforms.RandomSizedCrop(patch_size),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
        ])

        val_transforms = transforms.Compose([
            transforms.Resize(patch_size + 32),
            transforms.CenterCrop(patch_size),
            transforms.ToTensor(),
        ])

        return train_transforms, val_transforms 
Example #16
Source File: preprocess.py    From AnyNet with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #17
Source File: preprocess.py    From AnyNet with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #18
Source File: preprocess.py    From AnyNet with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #19
Source File: preprocess.py    From AnyNet with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #20
Source File: preprocess.py    From 360SD-Net with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #21
Source File: sun_dataset.py    From dogTorch with MIT License 5 votes vote down vote up
def __init__(self, args, train=True):
        self.root_dir = args.data
        root_dir = self.root_dir
        if train:
            self.data_set_list = os.path.join(root_dir,
                                              args.trainset_image_list)
        else:
            self.data_set_list = os.path.join(root_dir, args.testset_image_list)

        self.categ_dict = get_class_names(
            os.path.join(root_dir, 'ClassName.txt'))

        self.data_set_list = parse_file(self.data_set_list, self.categ_dict)

        self.args = args
        self.read_features = args.read_features

        self.features_dir = args.features_dir
        if train:
            self.transform = transforms.Compose([
                transforms.RandomSizedCrop(args.image_size),
                transforms.RandomHorizontalFlip(),
                transforms.Scale((args.image_size, args.image_size)),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225]),
            ])
        else:
            self.transform = transforms.Compose([
                transforms.Scale((args.image_size, args.image_size)),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225]),
            ]) 
Example #22
Source File: preprocess.py    From PSMNet with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #23
Source File: preprocess.py    From PSMNet with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #24
Source File: preprocess.py    From PSMNet with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ]) 
Example #25
Source File: preprocess.py    From PSMNet with MIT License 5 votes vote down vote up
def inception_color_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        #transforms.RandomSizedCrop(input_size),
        #transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        ColorJitter(
            brightness=0.4,
            contrast=0.4,
            saturation=0.4,
        ),
        Lighting(0.1, __imagenet_pca['eigval'], __imagenet_pca['eigvec']),
        transforms.Normalize(**normalize)
    ]) 
Example #26
Source File: __init__.py    From DSMnet with Apache License 2.0 5 votes vote down vote up
def Imagenet_train():
    return transforms.Compose([
                transforms.Scale(256),             # 重新改变大小为size=(w, h) 或 (size, size)
                transforms.RandomSizedCrop(224),   # 随机剪切并resize成给定的size大小
                transforms.RandomHorizontalFlip(),  # 概率为0.5,随机水平翻转。
                transforms.ToTensor(),              # 转化为tensor数据
                ColorJitter(Jitter=0.4, group=1, same_group=False),
                Lighting(alphastd=0.1, group=1, same_group=False),
                Normalize_Imagenet(), 
                ]) 
Example #27
Source File: test_first_block.py    From kinetics_i3d_pytorch with MIT License 5 votes vote down vote up
def test_input_block():
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    dataset = datasets.ImageFolder('/sequoia/data1/yhasson/datasets/test-dataset',
            transforms.Compose([
            transforms.RandomSizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))

    densenet = torchvision.models.densenet121(pretrained=True)
    features = densenet.features
    seq2d = torch.nn.Sequential(
        features.conv0, features.norm0, features.relu0, features.pool0)
    seq3d = torch.nn.Sequential(
        inflate.inflate_conv(features.conv0, 3),
        inflate.inflate_batch_norm(features.norm0),
        features.relu0,
        inflate.inflate_pool(features.pool0, 1))

    loader = torch.utils.data.DataLoader(dataset, batch_size=2, shuffle=False)
    frame_nb = 4
    for i, (input_2d, target) in enumerate(loader):
        target = target.cuda()
        target_var = torch.autograd.Variable(target)
        input_2d_var = torch.autograd.Variable(input_2d)
        out2d = seq2d(input_2d_var)
        time_pad = torch.nn.ReplicationPad3d((0, 0, 0, 0, 1, 1))
        input_3d = input_2d.unsqueeze(2).repeat(1, 1, frame_nb, 1, 1)
        input_3d_var = time_pad(input_3d) 
        out3d = seq3d(input_3d_var)
        expected_out_3d = out2d.data.unsqueeze(2).repeat(1, 1, frame_nb, 1, 1)
        out_diff = expected_out_3d - out3d.data
        print(out_diff.max())
        assert(out_diff.max() < 0.0001) 
Example #28
Source File: preprocess.py    From 360SD-Net with MIT License 5 votes vote down vote up
def scale_crop(input_size, scale_size=None, normalize=__imagenet_stats):
    t_list = [
        transforms.ToTensor(),
        transforms.Normalize(**normalize),
    ]
    #if scale_size != input_size:
    #t_list = [transforms.Scale((960,540))] + t_list

    return transforms.Compose(t_list)


# def scale_random_crop(input_size, scale_size=None, normalize=__imagenet_stats):
#     t_list = [
#         transforms.RandomCrop(input_size),
#         transforms.ToTensor(),
#         transforms.Normalize(**normalize),
#     ]
#     if scale_size != input_size:
#         t_list = [transforms.Scale(scale_size)] + t_list
# 
#     transforms.Compose(t_list)
# 
# 
# def pad_random_crop(input_size, scale_size=None, normalize=__imagenet_stats):
#     padding = int((scale_size - input_size) / 2)
#     return transforms.Compose([
#         transforms.RandomCrop(input_size, padding=padding),
#         transforms.RandomHorizontalFlip(),
#         transforms.ToTensor(),
#         transforms.Normalize(**normalize),
#     ])
# 
# 
# def inception_preproccess(input_size, normalize=__imagenet_stats):
#     return transforms.Compose([
#         transforms.RandomSizedCrop(input_size),
#         transforms.RandomHorizontalFlip(),
#         transforms.ToTensor(),
#         transforms.Normalize(**normalize)
#     ]) 
Example #29
Source File: data.py    From VSE-C with MIT License 5 votes vote down vote up
def get_transform(data_name, split_name, opt):
    normalizer = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                      std=[0.229, 0.224, 0.225])
    t_list = []
    if split_name == 'train':
        t_list = [transforms.RandomSizedCrop(opt.crop_size),
                  transforms.RandomHorizontalFlip()]
    elif split_name == 'val':
        t_list = [transforms.Scale(256), transforms.CenterCrop(224)]
    elif split_name == 'test':
        t_list = [transforms.Scale(256), transforms.CenterCrop(224)]

    t_end = [transforms.ToTensor(), normalizer]
    transform = transforms.Compose(t_list + t_end)
    return transform 
Example #30
Source File: preprocess.py    From StereoNet-ActiveStereoNet with MIT License 5 votes vote down vote up
def inception_preproccess(input_size, normalize=__imagenet_stats):
    return transforms.Compose([
        transforms.RandomSizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(**normalize)
    ])