36.md 3.6 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
# torchvision.transform

1.  [对 PIL.Image 进行变换](#transforms-on-pil-image)
2.  [对 Tensor 进行变换](#transforms-on-torch-tensor)
3.  [Conversion Transforms](#conversion-transforms)
4.  [通用变换](#generic-transforms)

* * *

变换是常用的图像变换。它们可以用`Compose`连接在一起。

```py
class torchvision.transforms.Compose(transforms) 
```

将多个 transform 组合起来使用。

transforms: 由 transform 构成的列表. 例子:

```py
transforms.Compose([
     transforms.CenterCrop(10),
    transforms.ToTensor(),
]) 
```

#### 对 PIL.Image 进行变换

* * *

```py
class torchvision.transforms.Scale(size, interpolation=2) 
```

按照规定的尺寸重新调节`PIL.Image`

**参数说明**

1.  size (sequence or int) - 期望输出尺寸。如果 size 是一个像(w, h)的序列,输出大小将按照 w,h 匹配到。如果大小是 int,则图像将匹配到这个数字。例如,如果原图的`height>width`,那么改变大小后的图片大小是`(size*height/width, size)`
2.  interpolation (int, optional) -需要添加值。默认的是`PIL.Image.BILINEAR`

```py
class torchvision.transforms.CenterCrop(size) 
```

将给定的 PIL.Image 进行中心切割,得到给定的 size,size 可以是 tuple,(target_height, target_width)。size 也可以是一个 Integer,在这种情况下,切出来的图片的形状是正方形。

```py
class torchvision.transforms.RandomCrop(size, padding=0) 
```

切割中心点的位置随机选取。size 可以是 tuple 也可以是 Integer。

```py
class torchvision.transforms.RandomHorizontalFlip 
```

随机水平翻转给定的 PIL.Image,概率为 0.5。即:一半的概率翻转,一半的概率不翻转。

```py
class torchvision.transforms.RandomSizedCrop(size, interpolation=2) 
```

先将给定的 PIL.Image 随机切,然后再 resize 成给定的 size 大小。

```py
class torchvision.transforms.Pad(padding, fill=0) 
```

将给定的 PIL.Image 的所有边用给定的 pad value 填充。 padding:要填充多少像素 fill:用什么值填充

#### 对 Tensor 进行变换

* * *

```py
class torchvision.transforms.Normalize(mean, std) 
```

给定均值:(R,G,B) 方差:(R,G,B),将会把 Tensor 正则化。即:Normalized_image=(image-mean)/std。

**参数说明:**

1.  mean (sequence) – 序列 R, G, B 的均值。
2.  std (sequence) – 序列 R, G, B 的平均标准偏差.

****call**(tensor)** 参数: tensor (Tensor) – 规范化的大小(c,h,w)的张量图像. 返回结果: 规范化的图片. 返回样式: Tensor 张量

#### 对 Conversion 进行变换

* * *

```py
class torchvision.transforms.ToTensor 
```

把一个取值范围是[0,255]的 PIL.Image 或者 shape 为(H,W,C)的 numpy.ndarray,转换成形状为[C,H,W],取值范围是[0,1.0]的 torch.FloadTensor

****call**(pic)**

1.  参数: pic (PIL.Image or numpy.ndarray) – 图片转换为张量.
2.  返回结果: 转换后的图像。
3.  返回样式: Tensor 张量

```py
class torchvision.transforms.ToPILImage 
```

将 shape 为(C,H,W)的 Tensor 或 shape 为(H,W,C)的 numpy.ndarray 转换成 PIL.Image,值不变。

****call**(pic)**

1.  参数: pic (Tensor or numpy.ndarray) – 图像转换为 pil.image。
2.  返回结果: 图像转换为 PIL.Image.
3.  返回样式: PIL.Image

#### 通用变换

* * *

```py
class torchvision.transforms.Lambda(lambd) 
```

使用 lambd 作为转换器。

参数说明: lambd (function) – Lambda/function 用于转换.

### 译者署名

| 用户名 | 头像 | 职能 | 签名 |
| --- | --- | --- | --- |
| [Song](https://ptorch.com) | ![](img/2018033000352689884.jpeg) | 翻译 | 人生总要追求点什么 |