본문 바로가기
pytorch

ToTensor - torchvision.transforms.ToTensor

by Back2Baek 2024. 6. 22.

 

torchvision.transforms는 파이토치의 이미지 전처리 및 증강을 위한 모듈.

그 중에서 ToTensor는 PIL image나 numpy array를 tensor화 시켜주는 클래스.

변환 후 Channel x Height x Width로 변경

 

ToTensor — Torchvision main documentation (pytorch.org)

 

 

하단의 이미지를 바탕으로 ToTensor의 적용 후 결과를 확인.

 

https://commons.wikimedia.org/wiki/File:Cloud_with_long_silver_lining.jpg#/media/File:Cloud_with_long_silver_lining.jpg\

 

File:Cloud with long silver lining.jpg - Wikimedia Commons

File:Cloud with long silver lining.jpg From Wikimedia Commons, the free media repository Jump to navigation Jump to search

commons.wikimedia.org

 

 

from torchvision.transforms import ToTensor
from PIL import Image
import numpy as np

img  = Image.open("/content/silver_lining.jpg")
img_array  = np.array(img)

transform = ToTensor()

# ToTensor : turn an img to tensor, 픽셀 값 0~255 -> 0~1으로 변환
img_tensor = transform(img)
print(img_tensor) 
print(img_tensor.size()) # Channel x Height x Width
print(img_tensor.min(), img_tensor.max()) # 0~1


'''
tensor([[[0.4235, 0.4235, 0.4235,  ..., 0.2627, 0.2667, 0.2667],
         [0.4275, 0.4275, 0.4275,  ..., 0.2627, 0.2667, 0.2667],
         [0.4275, 0.4275, 0.4275,  ..., 0.2667, 0.2667, 0.2667],
         ...,
         [0.0157, 0.0118, 0.0118,  ..., 0.0941, 0.0902, 0.0863],
         [0.0196, 0.0157, 0.0118,  ..., 0.0902, 0.0902, 0.0902],
         [0.0235, 0.0196, 0.0157,  ..., 0.0980, 0.0980, 0.0980]],

        [[0.5922, 0.5922, 0.5922,  ..., 0.4275, 0.4314, 0.4314],
         [0.5922, 0.5922, 0.5922,  ..., 0.4275, 0.4314, 0.4314],
         [0.5922, 0.5922, 0.5922,  ..., 0.4314, 0.4314, 0.4314],
         ...,
         [0.0078, 0.0039, 0.0039,  ..., 0.1373, 0.1333, 0.1294],
         [0.0118, 0.0078, 0.0039,  ..., 0.1333, 0.1333, 0.1333],
         [0.0157, 0.0118, 0.0078,  ..., 0.1294, 0.1294, 0.1294]],

        [[0.7569, 0.7569, 0.7569,  ..., 0.6235, 0.6275, 0.6275],
         [0.7569, 0.7569, 0.7569,  ..., 0.6235, 0.6275, 0.6275],
         [0.7569, 0.7569, 0.7569,  ..., 0.6275, 0.6275, 0.6275],
         ...,
         [0.0118, 0.0078, 0.0078,  ..., 0.2078, 0.2039, 0.2000],
         [0.0157, 0.0118, 0.0078,  ..., 0.2039, 0.2039, 0.2039],
         [0.0196, 0.0157, 0.0118,  ..., 0.2039, 0.2039, 0.2039]]])
채널 x 높이 x 너비 -> torch.Size([3, 4016, 6016]) 
min: tensor(0.) max: tensor(1.)
'''