本文整理汇总了Python中torchvision.transforms.RandomResizedCrop方法的典型用法代码示例。如果您正苦于以下问题:Python transforms.RandomResizedCrop方法的具体用法?Python transforms.RandomResizedCrop怎么用?Python transforms.RandomResizedCrop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.transforms
的用法示例。
在下文中一共展示了transforms.RandomResizedCrop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_data
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def load_data(root_path, dir, batch_size, phase):
transform_dict = {
'src': transforms.Compose(
[transforms.RandomResizedCrop(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),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])}
data = datasets.ImageFolder(root=root_path + dir, 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
示例2: load_train
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def load_train(root_path, dir, batch_size, phase):
transform_dict = {
'src': transforms.Compose(
[transforms.RandomResizedCrop(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),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])}
data = datasets.ImageFolder(root=root_path + dir, 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
示例3: __init__
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def __init__(
self,
crop_size: int = ImagenetConstants.CROP_SIZE,
mean: List[float] = ImagenetConstants.MEAN,
std: List[float] = ImagenetConstants.STD,
):
"""The constructor method of ImagenetAugmentTransform class.
Args:
crop_size: expected output size per dimension after random cropping
mean: a 3-tuple denoting the pixel RGB mean
std: a 3-tuple denoting the pixel RGB standard deviation
"""
self.transform = transforms.Compose(
[
transforms.RandomResizedCrop(crop_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std),
]
)
示例4: make
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def make(sz_resize = 256, sz_crop = 227, mean = [104, 117, 128],
std = [1, 1, 1], rgb_to_bgr = True, is_train = True,
intensity_scale = None):
return transforms.Compose([
RGBToBGR() if rgb_to_bgr else Identity(),
transforms.RandomResizedCrop(sz_crop) if is_train else Identity(),
transforms.Resize(sz_resize) if not is_train else Identity(),
transforms.CenterCrop(sz_crop) if not is_train else Identity(),
transforms.RandomHorizontalFlip() if is_train else Identity(),
transforms.ToTensor(),
ScaleIntensities(
*intensity_scale) if intensity_scale is not None else Identity(),
transforms.Normalize(
mean=mean,
std=std,
)
])
示例5: get_jig_train_transformers
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def get_jig_train_transformers(args):
size = args.img_transform.random_resize_crop.size
scale = args.img_transform.random_resize_crop.scale
img_tr = [transforms.RandomResizedCrop((int(size[0]), int(size[1])), (scale[0], scale[1]))]
if args.img_transform.random_horiz_flip > 0.0:
img_tr.append(transforms.RandomHorizontalFlip(args.img_transform.random_horiz_flip))
if args.img_transform.jitter > 0.0:
img_tr.append(transforms.ColorJitter(
brightness=args.img_transform.jitter, contrast=args.img_transform.jitter,
saturation=args.jitter, hue=min(0.5, args.jitter)))
tile_tr = []
if args.jig_transform.tile_random_grayscale:
tile_tr.append(transforms.RandomGrayscale(args.jig_transform.tile_random_grayscale))
mean = args.normalize.mean
std = args.normalize.std
tile_tr = tile_tr + [transforms.ToTensor(), transforms.Normalize(mean=mean, std=std)]
return transforms.Compose(img_tr), transforms.Compose(tile_tr)
示例6: get_rot_train_transformers
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def get_rot_train_transformers(args):
size = args.img_transform.random_resize_crop.size
scale = args.img_transform.random_resize_crop.scale
img_tr = [transforms.RandomResizedCrop((int(size[0]), int(size[1])), (scale[0], scale[1]))]
if args.img_transform.random_horiz_flip > 0.0:
img_tr.append(transforms.RandomHorizontalFlip(args.img_transform.random_horiz_flip))
if args.img_transform.jitter > 0.0:
img_tr.append(transforms.ColorJitter(
brightness=args.img_transform.jitter, contrast=args.img_transform.jitter,
saturation=args.jitter, hue=min(0.5, args.jitter)))
mean = args.normalize.mean
std = args.normalize.std
img_tr += [transforms.ToTensor(), transforms.Normalize(mean=mean, std=std)]
return transforms.Compose(img_tr)
示例7: preprocess
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def preprocess(self):
if self.train:
return transforms.Compose([
transforms.RandomResizedCrop(self.image_size),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.2),
transforms.ToTensor(),
transforms.Normalize(self.mean, self.std),
])
else:
return transforms.Compose([
transforms.Resize((int(self.image_size / 0.875), int(self.image_size / 0.875))),
transforms.CenterCrop(self.image_size),
transforms.ToTensor(),
transforms.Normalize(self.mean, self.std),
])
示例8: __init__
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def __init__(self, path, classes, stage='train'):
self.data = []
for i, c in enumerate(classes):
cls_path = osp.join(path, c)
images = os.listdir(cls_path)
for image in images:
self.data.append((osp.join(cls_path, image), i))
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
if stage == 'train':
self.transforms = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize])
if stage == 'test':
self.transforms = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize])
示例9: get_img_tensor
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def get_img_tensor(img_path, use_cuda, get_size=False):
img = Image.open(img_path)
original_w, original_h = img.size
img_size = (224, 224) # crop image to (224, 224)
img.thumbnail(img_size, Image.ANTIALIAS)
img = img.convert('RGB')
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
transform = transforms.Compose([
transforms.RandomResizedCrop(img_size[0]),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
img_tensor = transform(img)
img_tensor = torch.unsqueeze(img_tensor, 0)
if use_cuda:
img_tensor = img_tensor.cuda()
if get_size:
return img_tensor, original_w, original_w
else:
return img_tensor
示例10: get_pytorch_train_loader
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def get_pytorch_train_loader(data_path, batch_size, workers=5, _worker_init_fn=None, input_size=224):
traindir = os.path.join(data_path, 'train')
train_dataset = datasets.ImageFolder(
traindir,
transforms.Compose([
transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
]))
if torch.distributed.is_initialized():
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
else:
train_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=(train_sampler is None),
num_workers=workers, worker_init_fn=_worker_init_fn, pin_memory=True, sampler=train_sampler, collate_fn=fast_collate)
return PrefetchedWrapper(train_loader), len(train_loader)
示例11: get_loaders
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def get_loaders(traindir, valdir, sz, bs, fp16=True, val_bs=None, workers=8, rect_val=False, min_scale=0.08, distributed=False):
val_bs = val_bs or bs
train_tfms = [
transforms.RandomResizedCrop(sz, scale=(min_scale, 1.0)),
transforms.RandomHorizontalFlip()
]
train_dataset = datasets.ImageFolder(traindir, transforms.Compose(train_tfms))
train_sampler = (DistributedSampler(train_dataset, num_replicas=env_world_size(), rank=env_rank()) if distributed else None)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=bs, shuffle=(train_sampler is None),
num_workers=workers, pin_memory=True, collate_fn=fast_collate,
sampler=train_sampler)
val_dataset, val_sampler = create_validation_set(valdir, val_bs, sz, rect_val=rect_val, distributed=distributed)
val_loader = torch.utils.data.DataLoader(
val_dataset,
num_workers=workers, pin_memory=True, collate_fn=fast_collate,
batch_sampler=val_sampler)
train_loader = BatchTransformDataLoader(train_loader, fp16=fp16)
val_loader = BatchTransformDataLoader(val_loader, fp16=fp16)
return train_loader, val_loader, train_sampler, val_sampler
示例12: make_transform
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def make_transform(sz_resize = 256, sz_crop = 227, mean = [104, 117, 128],
std = [1, 1, 1], rgb_to_bgr = True, is_train = True,
intensity_scale = None):
return transforms.Compose([
RGBToBGR() if rgb_to_bgr else Identity(),
transforms.RandomResizedCrop(sz_crop) if is_train else Identity(),
transforms.Resize(sz_resize) if not is_train else Identity(),
transforms.CenterCrop(sz_crop) if not is_train else Identity(),
transforms.RandomHorizontalFlip() if is_train else Identity(),
transforms.ToTensor(),
ScaleIntensities(
*intensity_scale) if intensity_scale is not None else Identity(),
transforms.Normalize(
mean=mean,
std=std,
)
])
示例13: imgnet_transform
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def imgnet_transform(is_training=True):
if is_training:
transform_list = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.5,
contrast=0.5,
saturation=0.3),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
else:
transform_list = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
return transform_list
示例14: __init__
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def __init__(self, data_dir, image_size, is_train=True, **kwargs):
self.image_size = image_size
self.image_paths = []
self.image_labels = []
self.classes = sorted(os.listdir(data_dir))
for idx, cls_ in enumerate(self.classes):
self.image_paths += glob.glob(os.path.join(data_dir, cls_, '*.*'))
self.image_labels += [idx] * len(glob.glob(os.path.join(data_dir, cls_, '*.*')))
self.indexes = list(range(len(self.image_paths)))
if is_train:
random.shuffle(self.indexes)
self.transform = transforms.Compose([transforms.RandomResizedCrop(image_size),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=1, contrast=1, saturation=0.5, hue=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
else:
self.transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
示例15: build_train_transform
# 需要导入模块: from torchvision import transforms [as 别名]
# 或者: from torchvision.transforms import RandomResizedCrop [as 别名]
def build_train_transform(self, distort_color, resize_scale):
print('Color jitter: %s' % distort_color)
if distort_color == 'strong':
color_transform = transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1)
elif distort_color == 'normal':
color_transform = transforms.ColorJitter(brightness=32. / 255., saturation=0.5)
else:
color_transform = None
if color_transform is None:
train_transforms = transforms.Compose([
transforms.RandomResizedCrop(self.image_size, scale=(resize_scale, 1.0)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
self.normalize,
])
else:
train_transforms = transforms.Compose([
transforms.RandomResizedCrop(self.image_size, scale=(resize_scale, 1.0)),
transforms.RandomHorizontalFlip(),
color_transform,
transforms.ToTensor(),
self.normalize,
])
return train_transforms