torchvision.transforms

本文介绍了torchvision库中用于图像预处理的各种变换方法,包括组合变换、尺寸缩放、裁剪、翻转等操作,并提供了如何将PIL图像转换为Tensor及归一化的详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

torchvision.transforms

classtorchvision.transforms.Compose(transforms)

Composes several transforms together.

Parameters: transforms (List[Transform]) – list of transforms to compose.

Example

>>> transforms.Compose([
>>>     transforms.CenterCrop(10),
>>>     transforms.ToTensor(),
>>> ])

Transforms on PIL.Image

classtorchvision.transforms.Scale(sizeinterpolation=2)

Rescales the input PIL.Image to the given ‘size’. ‘size’ will be the size of the smaller edge. For example, if height > width, then image will be rescaled to (size * height / width, size) size: size of the smaller edge interpolation: Default: PIL.Image.BILINEAR

classtorchvision.transforms.CenterCrop(size)

Crops the given PIL.Image at the center to have a region of the given size. size can be a tuple (target_height, target_width) or an integer, in which case the target will be of a square shape (size, size)

classtorchvision.transforms.RandomCrop(sizepadding=0)

Crops the given PIL.Image at a random location to have a region of the given size. size can be a tuple (target_height, target_width) or an integer, in which case the target will be of a square shape (size, size)

classtorchvision.transforms.RandomHorizontalFlip

Randomly horizontally flips the given PIL.Image with a probability of 0.5

classtorchvision.transforms.RandomSizedCrop(sizeinterpolation=2)

Random crop the given PIL.Image to a random size of (0.08 to 1.0) of the original size and and a random aspect ratio of 3/4 to 4/3 of the original aspect ratio This is popularly used to train the Inception networks size: size of the smaller edge interpolation: Default: PIL.Image.BILINEAR

classtorchvision.transforms.Pad(paddingfill=0)

Pads the given PIL.Image on all sides with the given “pad” value

Transforms on torch.*Tensor

classtorchvision.transforms.Normalize(meanstd)

Given mean: (R, G, B) and std: (R, G, B), will normalize each channel of the torch.*Tensor, i.e. channel = (channel - mean) / std

Conversion Transforms

classtorchvision.transforms.ToTensor

Converts a PIL.Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].

classtorchvision.transforms.ToPILImage

Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL.Image while preserving value range.

Generic Transforms

classtorchvision.transforms.Lambda(lambd)

Applies a lambda as a transform.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值