-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn_transform.py
61 lines (49 loc) · 1.51 KB
/
learn_transform.py
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
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
"""
类似与一个工具箱 将数据类型装换成tensor类型
图片-->工具--->结果
tensor 数据类型 包装了神经网络需要的类型
`
输入 * PIL * Image.open()
输出 * tensor * ToTensor
作用 * narrays * cv.imread()
`
"""
"""
利用transforms.ToTensor去看两个问题
"""
img_path = "/home/wf/learn_torch/data/train/ants_image/0013035.jpg"
img = Image.open(img_path)
#ToTensor
tensor_tran = transforms.ToTensor()
tensor_img = tensor_tran(img)
#print(tensor_img)
# Normalize
print(tensor_img[0][0][0])
trans_normal = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_normal = trans_normal(tensor_img)
print(img_normal[0][0][0])
#Resize
print(img.size)
trans_resize = transforms.Resize((512,512))
img_resize = trans_resize(img)
print(type(img_resize))
img_resize = tensor_tran(img)
print(type(img_resize))
#Compose -resize
trains_resize_2 = transforms.Resize(512)
trains_compose = transforms.Compose([trains_resize_2,tensor_tran])
trains_resize_2 = trains_compose(img)
#RandomCrop
tran_random = transforms.RandomResizedCrop(250)
tran_compose = transforms.Compose([tran_random,tensor_tran])
tran_random = tran_compose(img)
writer = SummaryWriter("logs")
writer.add_image("Tensor",tensor_img)
writer.add_image("Tensor",img_normal,2)
writer.add_image("Tensor",img_resize,3)
writer.add_image("Tensor",trains_resize_2,4)
writer.add_image("Tensor",tran_random,5)
writer.close()