λ°μν
# PIL
- RGB νμ μΌλ‘ μ΄λ―Έμ§ μ½μ
- torchvision.transforms λͺ¨λμμ μ§μνλ λ°μ΄ν° νμ μ΄ PILμ Image array μ΄λ―λ‘ pytorch νλ μμν¬ μ¬μ©μ PIL λ‘ μ΄λ―Έμ§λ₯Ό λ‘λ©νλ λ°©μμ λ§μ΄ μ¬μ©
- numpy array μΈλ±μ± λΆκ°λ₯ → μν©μ λ°λΌ numpy arrayλ‘ λ°κΎΈκΈ° μν μμ νμ
- μ¬μ΄μ¦ νμΈμ .size() λ©μλλ‘ νμΈ κ°λ₯νλ°, (W,H)λ‘ μΆλ ₯λ¨. νμ§λ§, numpy array λ‘ λ³νμ (H,W,C)λ‘ λμ€λκΉ μ£Όμ. μ¦, .size()λ‘ νμΈ μμλ§ (W,H)λ‘ λμ€λ κ².
from PIL import Image
image = Image.open("image.jpg").convert('RGB')
image.show()
image.save("saved_image.jpg")
# OpenCV
- BGR νμ μΌλ‘ μ΄λ―Έμ§ μ½μ
- PILμ λΉν΄ λ λ§μ κΈ°λ₯(ν¨μ) μ§μ
- cv2 ν¨μμ numpy array λ₯Ό inputμΌλ‘ μ¬μ© κ°λ₯ - numpy array μμ νΈνμ± μ’μ
- numpy array μΈλ±μ± μ¬μ© κ°λ₯
- torchvision κ³Όμ νΈνμ±μ μ’μ§ μμ
import cv2
BGR_image = cv2.imread("image.jpg")
RGB_image = cv2.cvtColor(BGR_image, cv2.COLOR_BGR2RGB)
gray_image = cv2.imread("image.jpg",0)
cv2.imshow('color image', BGR_image)
cv2.waitkey(0)
cv2.imshow('gray image', gray_image)
cv2.waitkey(0)
cv2.imwrite('saved_image.jpg', color_image)
# Matplotlib
import matplotlib.pyplot as plt
image = plt.imread('image.jpg')
plt.imshow(image)
plt.show()
# PyTorch image tensor
- C x H x W ꡬ쑰
- ν½μ λ°κΈ° κ°μ΄ 0~1 λ²μ
# νμ λ³ν
μ΄λ―Έμ§ μ μ²λ¦¬λ₯Ό νκ±°λ λ₯λ¬λ νλ μμν¬λ₯Ό μ¬μ©νκΈ° μν΄μλ λ°μ΄ν° νμ λ³νμ μμ£Ό μ¬μ©νκ² λλλ° λͺκ°μ§ μ£Όμμ¬νμ΄ μλ€.
- μΌλ°μ μΈ μ΄λ―Έμ§ λ°μ΄ν°λ (H,W,C) μμ 0~255 ν½μ κ°μΌλ‘ μ μ₯μ΄ λλλ°, PyTorch Tensor μ κ²½μ° (C,H,W) μμ 0~1λ‘ μ κ·νλμ΄ μ¬μ©
- PIL image / Numpy array νμ μ PyTorch Tensor λ‘ λ³κ²½ν΄μ£Όλ torchvision.transforms.ToTensor() ν¨μμ κ²½μ° (H,W,C), 0~255 ν½μ κ°μ μλμΌλ‘ (C,H,W), 0~1 ν½μ κ°μΌλ‘ λ³νν΄μ£Όκ³ , λ€μ PyTorch Tensor λ₯Ό PIL image νμ μΌλ‘ λ³κ²½ν΄μ£Όλ torchvision.transforms.ToPILImage() ν¨μ λν (C,H,W), 0~1 ν½μ κ°μ (H,W,C), 0~255 λ‘ λ³ν
- νμ§λ§, κΈ°μ‘΄ λ©λͺ¨λ¦¬λ₯Ό κ·Έλλ‘ μμλ°μ numpy arrayλ₯Ό PyTorch Tensor λ‘ λ³νν΄μ£Όλ torch.from_numpy() ν¨μμ κ²½μ° numpy arrayμ μΈλ±μ€ μμμ ν½μ κ°μ΄ μ μ§λκΈ° λλ¬Έμ μ¬μ μ μ±λμΆκ³Ό ν½μ κ°μ μ‘°μ νλ κ³Όμ μ΄ νμ
*μ 리
- PIL image / Numpy array → PyTorch Tensor (μ±λ μΆ, ν½μ κ° λ²μ λ³κ²½ O) : torchvision.transforms.ToTensor()
- PyTorch Tensor → PIL image (μ±λ μΆ, ν½μ κ° λ²μ λ³κ²½ O) : torchvision.transforms.ToPILImage()
- Numpy array → PyTorch Tensor (μ±λ μΆ, ν½μ κ° λ²μ λ³κ²½ X) : torch.from_numpy()
- PIL image / OpneCV image / PyTorch Tensor → Numpy array : np.array(input_data)
from PIL import Image
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
from torchvision.transforms import ToTensor, ToPILImage
image_PIL = Image.open("image.jpg")
image_PIL = image_PIL.convert('RGB')
image_plt = plt.imread('image.jpg')
image_numpy = np.array(image_PIL)
image_numpy_to_PIL = Image.fromarray(image_numpy)
image_cv_BGR = cv2.imread("image.jpg")
image_cv_RGB = cv2.cvtColor(image_cv_BGR, cv2.COLOR_BGR2RGB)
ToPILImage_ = ToPILImage()
ToTensor_ = ToTensor()
# torchvision.transforms.ToTensor() : PIL/numpy -> PyTorch Tensor
PIL_ToTensor_tensor = ToTensor_(image_PIL) # H,W,C, 0~255 -> C,H,W, 0~1
Numpy_ToTensor_tensor = ToTensor_(image_numpy) # H,W,C, 0~255 -> C,H,W, 0~1
# torch.from_numpy() : numpy array -> PyTorch Tensor λ‘ λ³ν μ κΈ°μ‘΄ λ©λͺ¨λ¦¬ μμ
Numpy_to_tensor_from_numpy = torch.from_numpy(image_numpy)
# PyTorch Tensor to numpy array
PIL_ToTensor_tensor_to_Numpy = np.array(PIL_ToTensor_tensor)
Numpy_ToTensor_tensor_to_Numpy = np.array(Numpy_ToTensor_tensor)
# torchvision.transforms.ToPILImage() : PyTorch Tensor -> PIL
img_PIL_from_Tensor = ToPILImage_(PIL_ToTensor_tensor) # C,H,W, 0~1 -> H,W,C, 0~255
* https://github.com/ethereon/lycon μ benchmark λ₯Ό 보면 μ¬λ¬ ν¨μμ μ²λ¦¬ μλκ° opencv κ° μλ±ν μ’μ κ²μ λ³Ό μ μμ.
λ°μν