import matplotlib
.pyplot
as plt
import numpy
as np
import torchvision
.transforms
as T
from PIL
import Image
img
= Image
.open('0703.jpg')
plt
.imshow
(np
.array
(img
))
plt
.savefig
("原图.png")
plt
.show
()
"""
正常张量转PIL
"""
def tensor2img(ts
):
array
= np
.array
(ts
)
if ts
.ndim
==4:
imgs
= [array
[i
] for i
in range(array
.shape
[0])]
else:
raise ValueError
("没有批次维度")
for item
in imgs
:
img
= np
.transpose
(item
,(1,2,0))*256
img
= img
.astype
(np
.int)
plt
.imshow
(img
)
plt
.savefig
()
plt
.show
()
"""
为下面量身定做的转PIL
"""
def T2I(ts
):
array
= np
.array
(ts
)
img
= np
.transpose
(array
, (1, 2, 0)) * 256
img
= img
.astype
(np
.int)
plt
.imshow
(img
)
plt
.savefig
("裁剪,补零,垂直翻转,旋转.png")
plt
.show
()
transform
= T
.Compose
([
T
.Resize
((32,32)),
T
.RandomCrop
(32),
T
.Pad
(4),
T
.RandomVerticalFlip
(),
T
.RandomRotation
(10),
T
.ToTensor
(),
])
result
= transform
(img
)
img
= T2I
(result
)
如果没有在transform里转成张量,就不用调用张量转PIL,后面注释掉的还要。代码是一块一块进行的。
“squeezenet“
转载请注明原文地址:https://ipadbbs.8miu.com/read-62464.html