chore: Syntax Highlighting

This commit is contained in:
camera-2018
2023-08-15 01:03:42 +08:00
parent 9351ddc524
commit 4ce9a285d8
9 changed files with 48 additions and 43 deletions

View File

@@ -27,7 +27,7 @@ Torchvision 库中的 torchvision.transforms 包中提供了常用的图像操
我们来看一个具体的例子加深理解。将图片进行一下数据类型的相互转换。具体代码如下:
```
```python
from PIL import Image
from torchvision import transforms
@@ -68,7 +68,7 @@ torchvision.transforms 提供了丰富的图像变换方法,例如:改变尺
将输入的 PIL Image 或 Tensor 尺寸调整为给定的尺寸,具体定义为:
```
```python
torchvision.transforms.Resize(size, interpolation=2)
```
@@ -81,7 +81,7 @@ torchvision.transforms.Resize(size, interpolation=2)
在 resize 之后呢,一般会接一个 crop 操作crop 到指定的大小。对于高与宽接近的图片来说,这么做问题不大,但是高与宽的差距较大时,就会 crop 掉很多有用的信息。关于这一点,我们在后续的图像分类部分还会遇到,到时我在详细展开。
```
```python
from PIL import Image
from torchvision import transforms
@@ -105,7 +105,7 @@ torchvision.transforms 提供了多种剪裁方法,例如中心剪裁、随机
先说中心剪裁,在中心裁剪指定的 PIL Image 或 Tensor其定义如下
```
```python
torchvision.transforms.CenterCrop(size)
```
@@ -113,7 +113,7 @@ torchvision.transforms.CenterCrop(size)
然后是随机剪裁,在一个随机位置剪裁指定的 PIL Image 或 Tensor定义如下
```
```python
torchvision.transforms.RandomCrop(size, padding=None)
```
@@ -121,13 +121,13 @@ torchvision.transforms.RandomCrop(size, padding=None)
最后要说的是 FiveCrop我们将给定的 PIL Image 或 Tensor ,分别从四角和中心进行剪裁,共剪裁成五块,定义如下:
```
```python
torchvision.transforms.FiveCrop(size)
```
size 可以是 int 或 tuple用法同上。掌握了各种剪裁的定义和参数用法以后我们来看一下这些剪裁操作具体如何调用代码如下
```
```python
from PIL import Image
from torchvision import transforms
@@ -158,13 +158,13 @@ for img in imgs:
以概率 p 随机水平翻转图像,定义如下:
```
```python
torchvision.transforms.RandomHorizontalFlip(p=0.5)
```
以概率 p 随机垂直翻转图像,定义如下:
```
```python
torchvision.transforms.RandomVerticalFlip(p=0.5)
```
@@ -172,7 +172,7 @@ torchvision.transforms.RandomVerticalFlip(p=0.5)
这里的随机翻转,是为数据增强提供方便。如果想要必须执行翻转操作的话,将 p 设置为 1 即可。图片翻转代码如下:
```
```python
from PIL import Image
from torchvision import transforms
@@ -212,7 +212,7 @@ display(img2)
torchvision.transforms 提供了对 Tensor 进行标准化的函数,定义如下:
```
```python
torchvision.transforms.Normalize(mean, std, inplace=False)
```
@@ -224,7 +224,7 @@ torchvision.transforms.Normalize(mean, std, inplace=False)
我们来看看以 (R, G, B) 均值和标准差均为 (0.5, 0.5, 0.5) 来标准化图片后,是什么效果:
```
```python
from PIL import Image
from torchvision import transforms
@@ -254,7 +254,7 @@ display(img_norm)
Compose 类是将多个变换组合到一起,它的定义如下:
```
```python
torchvision.transforms.Compose(transforms)
```
@@ -262,7 +262,7 @@ torchvision.transforms.Compose(transforms)
我们还是结合例子动手试试,如果我们想要将图片变为 200*200 像素大小,并且随机裁切成 80 像素的正方形。那么我们可以组合 Resize 和 RandomCrop 变换,具体代码如下所示:
```
```python
from PIL import Image
from IPython.display import display
from torchvision import transforms
@@ -292,7 +292,7 @@ Compose 类是未来我们在实际项目中经常要使用到的类,结合 to
我们还是以读取 MNIST 数据集为例,看下如何在读取数据的同时,完成数据预处理等操作。具体代码如下:
```
```python
from torchvision import transforms
from torchvision import datasets
@@ -320,7 +320,7 @@ print(type(item[0]))
我们下面先来看看,在图像分类实战中使用的 transform可以感受一下实际使用的 transforms 是什么样子:
```
```python
transform = transforms.Compose([
transforms.RandomResizedCrop(dest_image_size),
transforms.RandomHorizontalFlip(),