normalize는 torchvision의 transforms를 쓰든, albumentations를 사용하든 하면 되고, 중요한건 안에 Normalize로 주는 means, std 매개변수들만 바꿔주면 됨.
import albumentations as A
from albumentations.pytorch import ToTensorV2
from torchvision import transforms
self.transform = A.Compose([
A.Normalize(mean=(0.548, 0.504, 0.479), std=(0.237, 0.247, 0.246)),
ToTensorV2(),
])
invTrans = transforms.Compose([transforms.Normalize(mean=[0., 0., 0.],
std=[1 / 0.229, 1 / 0.224, 1 / 0.225]),
transforms.Normalize(mean=[-0.485, -0.456, -0.406],
std=[1., 1., 1.]),
])
ax1.imshow(invTrans(temp_images[i]).permute([1, 2, 0]))
지금보니 쓸떼없이 2번 사용하고 안에 숫자도 좀 이상한데, 어쨌든 저런식으로 하면 된다~
permute하는건 https://tistory-nari.tistory.com/57
tensor image to numpy image, numpy image to tensor image
여기서 말하는 tensor는 pytorch tensor임. 변환방법 1. 바닐라 방법(제일 좋은듯) def image_tensor_to_numpy(tensor_image): # If this is already a numpy image, just return it if type(tensor_image) == np...
tistory-nari.tistory.com
1. inverse normalize 직접 바꾸기
답답해서 직접 만든다 아오..
https://pytorch.org/vision/main/generated/torchvision.transforms.Normalize.html
Normalize — Torchvision main documentation
Shortcuts
pytorch.org
torchvision normalize 수식 보면 이래 설명됨.
주의할건 우리가 알고있는 이미지는 r,g,b로 [0~255] 이지만 torchvision의 input 이미지가 애초에 rgb [0~1]로 가정하고 만들어진 듯.
그래서 저 수식 반대로 하면 된다.
def inverse_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
"""
:param img: numpy array. shape (height, width, channel). [-1~1]
:return: numpy array. shape (height, width, channel). [0~1]
"""
img[:,:,0] = ((img[:,:,0]) * std[0]) + mean[0]
img[:,:,1] = ((img[:,:,1]) * std[1]) + mean[1]
img[:,:,2] = ((img[:,:,2]) * std[2]) + mean[2]
return img
'기술 > Computer Vision' 카테고리의 다른 글
self-supervised learning (0) | 2022.01.17 |
---|---|
conv1d, conv2d, conv3d (0) | 2021.11.01 |
랜덤 샘플 이미지 (0) | 2021.10.06 |
tensor image to numpy image, numpy image to tensor image (0) | 2021.09.16 |
컴퓨터비전 방법론들 (0) | 2021.08.01 |