本文整理汇总了Python中torchvision.transforms.RandomCrop方法的典型用法代码示例。如果您正苦于以下问题:Python transforms.RandomCrop方法的具体用法?Python transforms.RandomCrop怎么用?Python transforms.RandomCrop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块torchvision.transforms的用法示例。
在下文中一共展示了transforms.RandomCrop方法的26个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: load_data
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def load_data(data_folder, batch_size, phase='train', train_val_split=True, train_ratio=.8):
transform_dict = {
'train': transforms.Compose(
[transforms.Resize(256),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
'test': transforms.Compose(
[transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])}
data = datasets.ImageFolder(root=data_folder, transform=transform_dict[phase])
if phase == 'train':
if train_val_split:
train_size = int(train_ratio * len(data))
test_size = len(data) - train_size
data_train, data_val = torch.utils.data.random_split(data, [train_size, test_size])
train_loader = torch.utils.data.DataLoader(data_train, batch_size=batch_size, shuffle=True, drop_last=True,
num_workers=4)
val_loader = torch.utils.data.DataLoader(data_val, batch_size=batch_size, shuffle=False, drop_last=False,
num_workers=4)
return [train_loader, val_loader]
else:
train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True,
num_workers=4)
return train_loader
else:
test_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=False, drop_last=False,
num_workers=4)
return test_loader
## Below are for ImageCLEF datasets
开发者ID:jindongwang,项目名称:transferlearning,代码行数:40,
示例2: load_imageclef_train
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def load_imageclef_train(root_path, domain, batch_size, phase):
transform_dict = {
'src': transforms.Compose(
[transforms.Resize((256, 256)),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
'tar': transforms.Compose(
[transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])}
data = ImageCLEF(root_dir=root_path, domain=domain, transform=transform_dict[phase])
train_size = int(0.8 * len(data))
test_size = len(data) - train_size
data_train, data_val = torch.utils.data.random_split(data, [train_size, test_size])
train_loader = torch.utils.data.DataLoader(data_train, batch_size=batch_size, shuffle=True, drop_last=False,
num_workers=4)
val_loader = torch.utils.data.DataLoader(data_val, batch_size=batch_size, shuffle=True, drop_last=False,
num_workers=4)
return train_loader, val_loader
开发者ID:jindongwang,项目名称:transferlearning,代码行数:27,
示例3: load_imageclef_test
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def load_imageclef_test(root_path, domain, batch_size, phase):
transform_dict = {
'src': transforms.Compose(
[transforms.Resize((256,256)),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
'tar': transforms.Compose(
[transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])}
data = ImageCLEF(root_dir=root_path, domain=domain, transform=transform_dict[phase])
data_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=False, num_workers=4)
return data_loader
开发者ID:jindongwang,项目名称:transferlearning,代码行数:21,
示例4: load_training
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def load_training(root_path, dir, batch_size, kwargs):
transform = transforms.Compose(
[transforms.Resize([256, 256]),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()])
data = datasets.ImageFolder(root=root_path + dir, transform=transform)
train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True, **kwargs)
return train_loader
开发者ID:jindongwang,项目名称:transferlearning,代码行数:12,
示例5: main
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def main():
best_acc = 0
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('==> Preparing data..')
transforms_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
dataset_train = CIFAR10(root='../data', train=True, download=True,
transform=transforms_train)
train_loader = DataLoader(dataset_train, batch_size=args.batch_size,
shuffle=True, num_workers=args.num_worker)
# there are 10 classes so the dataset name is cifar-10
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
print('==> Making model..')
net = pyramidnet()
net = nn.DataParallel(net)
net = net.to(device)
num_params = sum(p.numel() for p in net.parameters() if p.requires_grad)
print('The number of parameters of model is', num_params)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=args.lr)
# optimizer = optim.SGD(net.parameters(), lr=args.lr,
# momentum=0.9, weight_decay=1e-4)
train(net, criterion, optimizer, train_loader, device)
开发者ID:dnddnjs,项目名称:pytorch-multigpu,代码行数:38,
示例6: cifar10
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def cifar10():
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = MyCIFAR10.CIFAR10(root='./data', train=True, download=True, transform=transform_train, seed=0)
valset = MyCIFAR10.CIFAR10(root='./data', train=True, download=True, transform=transform_test, seed=0)
testset = MyCIFAR10.CIFAR10(root='./data', train=False, download=True, transform=transform_test, seed=0)
net_func = MyNet.CifarAE
return net_func, trainset, valset, testset
开发者ID:sato9hara,项目名称:sgd-influence,代码行数:18,
示例7: cifar10
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def cifar10():
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = MyCIFAR10.CIFAR10(root='./data', train=True, download=True, transform=transform_train, seed=0)
valset = MyCIFAR10.CIFAR10(root='./data', train=True, download=True, transform=transform_test, seed=0)
testset = MyCIFAR10.CIFAR10(root='./data', train=False, download=True, transform=transform_test, seed=0)
net_func = MyNet.CifarNet
return net_func, trainset, valset, testset
开发者ID:sato9hara,项目名称:sgd-influence,代码行数:18,
示例8: get_transform
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def get_transform(opt):
transform_list = []
if opt.resize_or_crop == 'resize_and_crop':
osize = [opt.loadSize, opt.loadSize]
transform_list.append(transforms.Scale(osize, Image.BICUBIC))
transform_list.append(transforms.RandomCrop(opt.fineSize))
elif opt.resize_or_crop == 'crop':
transform_list.append(transforms.RandomCrop(opt.fineSize))
elif opt.resize_or_crop == 'scale_width':
transform_list.append(transforms.Lambda(
lambda img: __scale_width(img, opt.fineSize)))
elif opt.resize_or_crop == 'scale_width_and_crop':
transform_list.append(transforms.Lambda(
lambda img: __scale_width(img, opt.loadSize)))
transform_list.append(transforms.RandomCrop(opt.fineSize))
if opt.isTrain and not opt.no_flip:
transform_list.append(transforms.RandomHorizontalFlip())
transform_list += [transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]
return transforms.Compose(transform_list)
开发者ID:aayushbansal,项目名称:Recycle-GAN,代码行数:25,
示例9: transform
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def transform(is_train=True, normalize=True):
"""
Returns a transform object
"""
filters = []
filters.append(Scale(256))
if is_train:
filters.append(RandomCrop(224))
else:
filters.append(CenterCrop(224))
if is_train:
filters.append(RandomHorizontalFlip())
filters.append(ToTensor())
if normalize:
filters.append(Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]))
return Compose(filters)
开发者ID:uwnlp,项目名称:verb-attributes,代码行数:22,
示例10: initialize_dataset
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def initialize_dataset(clevr_dir, dictionaries, state_description=True):
if not state_description:
train_transforms = transforms.Compose([transforms.Resize((128, 128)),
transforms.Pad(8),
transforms.RandomCrop((128, 128)),
transforms.RandomRotation(2.8), # .05 rad
transforms.ToTensor()])
test_transforms = transforms.Compose([transforms.Resize((128, 128)),
transforms.ToTensor()])
clevr_dataset_train = ClevrDataset(clevr_dir, True, dictionaries, train_transforms)
clevr_dataset_test = ClevrDataset(clevr_dir, False, dictionaries, test_transforms)
else:
clevr_dataset_train = ClevrDatasetStateDescription(clevr_dir, True, dictionaries)
clevr_dataset_test = ClevrDatasetStateDescription(clevr_dir, False, dictionaries)
return clevr_dataset_train, clevr_dataset_test
开发者ID:mesnico,项目名称:RelationNetworks-CLEVR,代码行数:20,
示例11: cifar10_train_transform
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def cifar10_train_transform(ds_metainfo,
mean_rgb=(0.4914, 0.4822, 0.4465),
std_rgb=(0.2023, 0.1994, 0.2010),
jitter_param=0.4):
assert (ds_metainfo is not None)
assert (ds_metainfo.input_image_size[0] == 32)
return transforms.Compose([
transforms.RandomCrop(
size=32,
padding=4),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(
brightness=jitter_param,
contrast=jitter_param,
saturation=jitter_param),
transforms.ToTensor(),
transforms.Normalize(
mean=mean_rgb,
std=std_rgb)
])
开发者ID:osmr,项目名称:imgclsmob,代码行数:22,
示例12: init_dataset
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def init_dataset():
transform_train = transforms.Compose(
[transforms.Resize(256),
transforms.RandomCrop(227),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
transform_test = transforms.Compose(
[transforms.Resize(227),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
trainset = datasets.CIFAR10(root='./data', train=True, download=True,
transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,
shuffle=True, num_workers=0)
testset = datasets.CIFAR10(root='./data', train=False, download=True,
transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100,
shuffle=True, num_workers=0)
return trainloader, testloader
开发者ID:flyingpot,项目名称:pytorch_deephash,代码行数:23,
示例13: _data_transforms_cifar10
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def _data_transforms_cifar10(cutout_size):
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
if cutout_size is not None:
train_transform.transforms.append(Cutout(cutout_size))
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
return train_transform, valid_transform
开发者ID:renqianluo,项目名称:NAO_pytorch,代码行数:20,
示例14: __init__
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def __init__(self, noisy_dir, crop_size, upscale_factor=4, cropped=False, flips=False, rotations=False, **kwargs):
super(TrainDataset, self).__init__()
# get all directories used for training
if isinstance(noisy_dir, str):
noisy_dir = [noisy_dir]
self.files = []
for n_dir in noisy_dir:
self.files += [join(n_dir, x) for x in listdir(n_dir) if utils.is_image_file(x)]
# intitialize image transformations and variables
self.input_transform = T.Compose([
T.RandomVerticalFlip(0.5 if flips else 0.0),
T.RandomHorizontalFlip(0.5 if flips else 0.0),
T.RandomCrop(crop_size)
])
self.crop_transform = T.RandomCrop(crop_size // upscale_factor)
self.upscale_factor = upscale_factor
self.cropped = cropped
self.rotations = rotations
开发者ID:ManuelFritsche,项目名称:real-world-sr,代码行数:20,
示例15: _data_transforms_cifar10
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def _data_transforms_cifar10(cutout=False, cutout_length=16):
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
if cutout:
train_transform.transforms.append(Cutout(cutout_length))
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
return train_transform, valid_transform
开发者ID:shirakawas,项目名称:ASNG-NAS,代码行数:20,
示例16: cifar_loaders
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def cifar_loaders(batch_size, shuffle_test=False):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.225, 0.225, 0.225])
train = datasets.CIFAR10('./data', train=True, download=True,
transform=transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, 4),
transforms.ToTensor(),
normalize,
]))
test = datasets.CIFAR10('./data', train=False,
transform=transforms.Compose([transforms.ToTensor(), normalize]))
train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size,
shuffle=True, pin_memory=True)
test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size,
shuffle=shuffle_test, pin_memory=True)
return train_loader, test_loader
开发者ID:locuslab,项目名称:convex_adversarial,代码行数:19,
示例17: init_learning
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def init_learning(self, model, criterion):
if self._state('train_transform') is None:
self.state['train_transform'] = transforms.Compose([
Warp(self.state['image_size'] + 30),
transforms.RandomCrop(self.state['image_size']),
transforms.RandomHorizontalFlip(),
lambda x: torch.from_numpy(np.array(x)).permute(2, 0, 1).float(),
lambda x: x.index_select(0, torch.LongTensor([2,1,0])),
lambda x: x - torch.Tensor(model.image_normalization_mean).view(3, 1, 1),
])
if self._state('val_transform') is None:
self.state['val_transform'] = transforms.Compose([
Warp(self.state['image_size']),
lambda x: torch.from_numpy(np.array(x)).permute(2, 0, 1).float(),
lambda x: x.index_select(0, torch.LongTensor([2,1,0])),
lambda x: x - torch.Tensor(model.image_normalization_mean).view(3, 1, 1),
])
self.state['best_score'] = 0
开发者ID:yeezhu,项目名称:SPN.pytorch,代码行数:22,
示例18: __init__
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def __init__(self, opt):
self.image_path = opt.dataroot
self.is_train = opt.is_train
self.d_num = opt.n_attribute
print ('Start preprocessing dataset..!')
random.seed(1234)
self.preprocess()
print ('Finished preprocessing dataset..!')
if self.is_train:
trs = [transforms.Resize(opt.load_size, interpolation=Image.ANTIALIAS), transforms.RandomCrop(opt.fine_size)]
else:
trs = [transforms.Resize(opt.load_size, interpolation=Image.ANTIALIAS), transforms.CenterCrop(opt.fine_size)]
if opt.is_flip:
trs.append(transforms.RandomHorizontalFlip())
self.transform = transforms.Compose(trs)
self.norm = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
self.num_data = max(self.num)
开发者ID:Xiaoming-Yu,项目名称:DMIT,代码行数:21,
示例19: __init__
点赞 6
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def __init__(self, opt):
'''Initialize this dataset class.
We need to specific the path of the dataset and the domain label of each image.
'''
self.image_list = []
self.label_list = []
if opt.is_train:
trs = [transforms.Resize(opt.load_size, interpolation=Image.ANTIALIAS), transforms.RandomCrop(opt.fine_size)]
else:
trs = [transforms.Resize(opt.load_size, interpolation=Image.ANTIALIAS), transforms.CenterCrop(opt.fine_size)]
if opt.is_flip:
trs.append(transforms.RandomHorizontalFlip())
trs.append(transforms.ToTensor())
trs.append(transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)))
self.transform = transforms.Compose(trs)
self.num_data = len(self.image_list)
开发者ID:Xiaoming-Yu,项目名称:DMIT,代码行数:18,
示例20: load_training
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def load_training(root_path, dir, batch_size, kwargs):
transform = transforms.Compose(
[transforms.Resize([256, 256]),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()])
data = datasets.ImageFolder(root=root_path + dir, transform=transform)
train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True, **kwargs)
return train_loader
开发者ID:jindongwang,项目名称:transferlearning,代码行数:11,
示例21: scale_random_crop
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
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)
开发者ID:JiaRenChang,项目名称:PSMNet,代码行数:12,
示例22: pad_random_crop
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
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),
])
开发者ID:JiaRenChang,项目名称:PSMNet,代码行数:10,
示例23: _get_dataloaders
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def _get_dataloaders(self, num_workers, shuffle_train=True):
assert self.config_dl.train_imgs_glob is not None
print('Cropping to {}'.format(self.config_dl.crop_size))
to_tensor_transform = transforms.Compose(
[transforms.RandomCrop(self.config_dl.crop_size),
transforms.RandomHorizontalFlip(),
images_loader.IndexImagesDataset.to_tensor_uint8_transform()])
# NOTE: if there are images in your training set with dimensions <128, training will abort at some point,
# because the cropper failes. See REAME, section about data preparation.
min_size = self.config_dl.crop_size
ds_train = images_loader.IndexImagesDataset(
images=images_loader.ImagesCached(
self.config_dl.train_imgs_glob,
self.config_dl.image_cache_pkl,
min_size=min_size),
to_tensor_transform=to_tensor_transform)
dl_train = DataLoader(ds_train, self.config_dl.batchsize_train, shuffle=shuffle_train,
num_workers=num_workers)
print('Created DataLoader [train] {} batches -> {} imgs'.format(
len(dl_train), self.config_dl.batchsize_train * len(dl_train)))
ds_val = self._get_ds_val(
self.config_dl.val_glob,
crop=self.config_dl.crop_size,
truncate=self.config_dl.num_val_batches * self.config_dl.batchsize_val)
dl_val = DataLoader(
ds_val, self.config_dl.batchsize_val, shuffle=False,
num_workers=num_workers, drop_last=True)
print('Created DataLoader [val] {} batches -> {} imgs'.format(
len(dl_val), self.config_dl.batchsize_train * len(dl_val)))
return dl_train, dl_val
开发者ID:fab-jul,项目名称:L3C-PyTorch,代码行数:35,
示例24: main
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def main():
best_acc = 0
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('==> Preparing data..')
transforms_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
dataset_train = CIFAR10(root='../data', train=True, download=True,
transform=transforms_train)
train_loader = DataLoader(dataset_train, batch_size=args.batch_size,
shuffle=True, num_workers=args.num_worker)
# there are 10 classes so the dataset name is cifar-10
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
print('==> Making model..')
net = pyramidnet()
net = net.to(device)
num_params = sum(p.numel() for p in net.parameters() if p.requires_grad)
print('The number of parameters of model is', num_params)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=args.lr,
momentum=0.9, weight_decay=1e-4)
train(net, criterion, optimizer, train_loader, device)
开发者ID:dnddnjs,项目名称:pytorch-multigpu,代码行数:36,
示例25: check_dataset
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def check_dataset(opt):
normalize_transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))])
train_large_transform = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip()])
val_large_transform = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224)])
train_small_transform = transforms.Compose([transforms.Pad(4),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip()])
splits = check_split(opt)
if opt.dataset in ['cub200', 'indoor', 'stanford40', 'dog']:
train, val = 'train', 'test'
train_transform = transforms.Compose([train_large_transform, normalize_transform])
val_transform = transforms.Compose([val_large_transform, normalize_transform])
sets = [dset.ImageFolder(root=os.path.join(opt.dataroot, train), transform=train_transform),
dset.ImageFolder(root=os.path.join(opt.dataroot, train), transform=val_transform),
dset.ImageFolder(root=os.path.join(opt.dataroot, val), transform=val_transform)]
sets = [FolderSubset(dataset, *split) for dataset, split in zip(sets, splits)]
opt.num_classes = len(splits[0][0])
else:
raise Exception('Unknown dataset')
loaders = [torch.utils.data.DataLoader(dataset,
batch_size=opt.batchSize,
shuffle=True,
num_workers=0) for dataset in sets]
return loaders
开发者ID:alinlab,项目名称:L2T-ww,代码行数:35,
示例26: mnist
点赞 5
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomCrop [as 别名]
def mnist():
transform_train = transforms.Compose([
transforms.RandomCrop(28, padding=2),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
trainset = MyMNIST.MNIST(root='./data', train=True, download=True, transform=transform_train, seed=0)
valset = MyMNIST.MNIST(root='./data', train=True, download=True, transform=transform_test, seed=0)
testset = MyMNIST.MNIST(root='./data', train=False, download=True, transform=transform_test, seed=0)
net_func = MyNet.MnistAE
return net_func, trainset, valset, testset
开发者ID:sato9hara,项目名称:sgd-influence,代码行数:17,
注:本文中的torchvision.transforms.RandomCrop方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。