torch

from PIL import Image
import numpy as np
import torch
import torchvision.transforms as T

1.From Tensor to Numpy:

>>> a=torch.Tensor([[1,2],[3,2]])
>>> a
tensor([[1., 2.],
        [3., 2.]])
>>> a.numpy()
array([[1., 2.],
       [3., 2.]], dtype=float32)

2. From Numpy to Tensor

>>> b=a.numpy()
>>> b
array([[1., 2.],
       [3., 2.]], dtype=float32)
>>> torch.from_numpy(b)
tensor([[1., 2.],
        [3., 2.]])

3.圖片分割

1.載入圖片

>>> os.getcwd()
'/home/hushch'
>>> p1=os.getcwd()
>>> p2 = '圖片/th.jpeg'
>>> pf=p1+p2
>>> pf  # 錯誤的
'/home/hushch圖片/th.jpeg'
>>> pf=os.path.join(p1,p2)
>>> pf
'/home/hushch/圖片/th.jpeg'
>>> img=Image.open(fp=pf, mode='r')
>>> img.show(title='th', command=None)  # 輸出圖片

2.圖片分割:

>>> img_np = np.asarray(img)  # from image to array
>>> img_np.shape
(168, 300, 3)
>>> x_start=0
>>> y_start=0
>>> x_end=128
>>> y_end=128
>>> crop_img = img.crop(box=[x_start, y_start, x_end, y_end])  # 設定範圍
>>> crop_img.show()
>>> np.asarray(crop_img).shape
(128, 128, 3)

3.其他圖片操作

>>> img.transpose(method=Image.FLIP_LEFT_RIGHT)  # 左右翻轉
<PIL.Image.Image image mode=RGB size=300x168 at 0x7F485080A908>

4. Torchvision.transforms

1.RandomHorizontalFlip

>>> transform = T.Compose(transforms=[T.RandomHorizontalFlip(p=0.5)])  # 水平翻轉的概率爲0.5
>>> transform
Compose(
    RandomHorizontalFlip(p=0.5)
)
>>> new_img=transform(img)  # 調用transform
>>> new_img
<PIL.Image.Image image mode=RGB size=300x168 at 0x7F48061B3240>
>>> new_img.show()

2.RandomCrop

>>> transform=T.Compose([T.RandomCrop(size=cropsize, padding=0, pad_if_needed=False)])  
# 隨機剪下cropsize * cropsize大小的圖
>>> new_img=transform(img)
>>> new_img.show()

3.ToTensor

>>> transform = T.Compose([T.ToTensor()])
>>> transform
Compose(
    ToTensor()
)
>>> new_img=transform(img)
>>> new_img.show()
Traceback (most recent call last):
  File "<pyshell#173>", line 1, in <module>
    new_img.show()
AttributeError: 'Tensor' object has no attribute 'show'
>>> type(new_img)
<class 'torch.Tensor'>
>>> new_img
tensor([[[0.9020, 0.8863, 0.8784,  ..., 0.2980, 0.2706, 0.2353],
         [0.8824, 0.8667, 0.8549,  ..., 0.2667, 0.2118, 0.2784],
         [0.8667, 0.8510, 0.8431,  ..., 0.2471, 0.3843, 0.5569],
         ...,
         [0.6118, 0.6000, 0.5882,  ..., 0.1294, 0.1176, 0.1176],
         [0.6118, 0.6157, 0.6078,  ..., 0.2667, 0.2627, 0.2706],
         [0.6118, 0.6275, 0.6157,  ..., 0.3020, 0.3294, 0.3529]],

        [[0.9137, 0.8980, 0.8863,  ..., 0.5490, 0.5216, 0.4863],
         [0.8941, 0.8784, 0.8627,  ..., 0.5176, 0.4510, 0.5216],
         [0.8784, 0.8627, 0.8510,  ..., 0.4863, 0.6196, 0.7922],
         ...,
         [0.6627, 0.6510, 0.6392,  ..., 0.3647, 0.3412, 0.3529],
         [0.6549, 0.6588, 0.6510,  ..., 0.5098, 0.4980, 0.5137],
         [0.6549, 0.6706, 0.6588,  ..., 0.5451, 0.5725, 0.5961]],

        [[0.8863, 0.8706, 0.8667,  ..., 0.6588, 0.6275, 0.5922],
         [0.8667, 0.8510, 0.8431,  ..., 0.6235, 0.5608, 0.6196],
         [0.8510, 0.8353, 0.8314,  ..., 0.5961, 0.7216, 0.8863],
         ...,
         [0.6941, 0.6824, 0.6627,  ..., 0.4667, 0.4471, 0.4549],
         [0.6784, 0.6824, 0.6745,  ..., 0.6000, 0.6000, 0.6118],
         [0.6784, 0.6941, 0.6824,  ..., 0.6353, 0.6706, 0.6941]]])
>>> new_img=np.asarray(new_img)
>>> type(new_image)
Traceback (most recent call last):
  File "<pyshell#177>", line 1, in <module>
    type(new_image)
NameError: name 'new_image' is not defined
>>> type(new_img)
<class 'numpy.ndarray'>
>>> new_img=Image.fromarray(new_img)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2428, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 300), '<f4')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#179>", line 1, in <module>
    new_img=Image.fromarray(new_img)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2431, in fromarray
    raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type
>>> new_img
array([[[0.9019608 , 0.8862745 , 0.8784314 , ..., 0.29803923,
         0.27058825, 0.23529412],
        [0.88235295, 0.8666667 , 0.85490197, ..., 0.26666668,
         0.21176471, 0.2784314 ],
        [0.8666667 , 0.8509804 , 0.84313726, ..., 0.24705882,
         0.38431373, 0.5568628 ],
        ...,
        [0.6117647 , 0.6       , 0.5882353 , ..., 0.12941177,
         0.11764706, 0.11764706],
        [0.6117647 , 0.6156863 , 0.60784316, ..., 0.26666668,
         0.2627451 , 0.27058825],
        [0.6117647 , 0.627451  , 0.6156863 , ..., 0.3019608 ,
         0.32941177, 0.3529412 ]],

       [[0.9137255 , 0.8980392 , 0.8862745 , ..., 0.54901963,
         0.52156866, 0.4862745 ],
        [0.89411765, 0.8784314 , 0.8627451 , ..., 0.5176471 ,
         0.4509804 , 0.52156866],
        [0.8784314 , 0.8627451 , 0.8509804 , ..., 0.4862745 ,
         0.61960787, 0.7921569 ],
        ...,
        [0.6627451 , 0.6509804 , 0.6392157 , ..., 0.3647059 ,
         0.34117648, 0.3529412 ],
        [0.654902  , 0.65882355, 0.6509804 , ..., 0.50980395,
         0.49803922, 0.5137255 ],
        [0.654902  , 0.67058825, 0.65882355, ..., 0.54509807,
         0.57254905, 0.59607846]],

       [[0.8862745 , 0.87058824, 0.8666667 , ..., 0.65882355,
         0.627451  , 0.5921569 ],
        [0.8666667 , 0.8509804 , 0.84313726, ..., 0.62352943,
         0.56078434, 0.61960787],
        [0.8509804 , 0.8352941 , 0.83137256, ..., 0.59607846,
         0.72156864, 0.8862745 ],
        ...,
        [0.69411767, 0.68235296, 0.6627451 , ..., 0.46666667,
         0.44705883, 0.45490196],
        [0.6784314 , 0.68235296, 0.6745098 , ..., 0.6       ,
         0.6       , 0.6117647 ],
        [0.6784314 , 0.69411767, 0.68235296, ..., 0.63529414,
         0.67058825, 0.69411767]]], dtype=float32)
>>> 

變成Tensor後,取值都變了,歸一化了,即使變成矩陣,也不能變成圖像了。

4.

transform_augment = T.Compose([
    T.RandomHorizontalFlip(),
    T.RandomCrop(32, padding=4)])
transform_normalize = T.Compose([
    T.ToTensor(),
    T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

transtrain = T.Compose([transform_augment, transform_normalize])

功能上和,之前的Image.transpose相似。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章