我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用torchvision.transforms.Resize()。
def imagenet(): channel_stats = dict(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) train_transformation = data.TransformTwice(transforms.Compose([ transforms.RandomRotation(10), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1), transforms.ToTensor(), transforms.Normalize(**channel_stats) ])) eval_transformation = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(**channel_stats) ]) return { 'train_transformation': train_transformation, 'eval_transformation': eval_transformation, 'datadir': 'data-local/images/ilsvrc2012/', 'num_classes': 1000 }
def get_transform(resize_crop='resize_and_crop', flip=True, loadSize=286, fineSize=256): transform_list = [] if resize_crop == 'resize_and_crop': osize = [loadSize, loadSize] transform_list.append(transforms.Resize(osize, Image.BICUBIC)) transform_list.append(transforms.RandomCrop(fineSize)) elif resize_crop == 'crop': transform_list.append(transforms.RandomCrop(fineSize)) elif resize_crop == 'scale_width': transform_list.append(transforms.Lambda( lambda img: __scale_width(img, fineSize))) elif resize_crop == 'scale_width_and_crop': transform_list.append(transforms.Lambda( lambda img: __scale_width(img, loadSize))) transform_list.append(transforms.RandomCrop(fineSize)) if 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)
def get_transformer(): normalize = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) transformer = transforms.Compose([ transforms.Resize(128), transforms.ToTensor(), normalize ]) return transformer
def __init__(self, args): normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) transform_train = transforms.Compose([ transforms.Resize(256), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ColorJitter(0.4,0.4,0.4), transforms.ToTensor(), Lighting(0.1, _imagenet_pca['eigval'], _imagenet_pca['eigvec']), normalize, ]) transform_test = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize, ]) trainset = MINCDataloder(root=os.path.expanduser('~/data/minc-2500/'), train=True, transform=transform_train) testset = MINCDataloder(root=os.path.expanduser('~/data/minc-2500/'), train=False, transform=transform_test) kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {} trainloader = torch.utils.data.DataLoader(trainset, batch_size= args.batch_size, shuffle=True, **kwargs) testloader = torch.utils.data.DataLoader(testset, batch_size= args.test_batch_size, shuffle=False, **kwargs) self.trainloader = trainloader self.testloader = testloader