本文整理汇总了Python中transforms.ToTensor方法的典型用法代码示例。如果您正苦于以下问题:Python transforms.ToTensor方法的具体用法?Python transforms.ToTensor怎么用?Python transforms.ToTensor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transforms
的用法示例。
在下文中一共展示了transforms.ToTensor方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_image
# 需要导入模块: import transforms [as 别名]
# 或者: from transforms import ToTensor [as 别名]
def read_image(img_path):
"""Keep reading image until succeed.
This can avoid IOError incurred by heavy IO process."""
got_img = False
if not osp.exists(img_path):
raise IOError("{} does not exist".format(img_path))
while not got_img:
try:
img = Image.open(img_path).convert('RGB')
got_img = True
except IOError:
print("IOError incurred when reading '{}'. Will redo. Don't worry. Just chill.".format(img_path))
pass
transform_test = T.Compose([
T.Resize((args.height, args.width)),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
img = transform_test(img)
return img
示例2: get_transform
# 需要导入模块: import transforms [as 别名]
# 或者: from transforms import ToTensor [as 别名]
def get_transform(mode,base_size):
#base_size = 520
#crop_size = 480
crop_size=int(480*base_size/520)
min_size = int((0.5 if mode=='train' else 1.0) * base_size)
max_size = int((2.0 if mode=='train' else 1.0) * base_size)
transforms = []
transforms.append(T.RandomResize(min_size, max_size))
if mode=='train':
transforms.append(T.RandomHorizontalFlip(0.5))
transforms.append(T.RandomCrop(crop_size))
transforms.append(T.ToTensor())
transforms.append(T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]))
return T.Compose(transforms)
示例3: __init__
# 需要导入模块: import transforms [as 别名]
# 或者: from transforms import ToTensor [as 别名]
def __init__(self, batch_size, use_gpu, num_workers):
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
pin_memory = True if use_gpu else False
trainset = torchvision.datasets.MNIST(root='./data/mnist', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=pin_memory,
)
testset = torchvision.datasets.MNIST(root='./data/mnist', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(
testset, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=pin_memory,
)
self.trainloader = trainloader
self.testloader = testloader
self.num_classes = 10