image normalize, inverse normalize

2021. 9. 16. 23:13·기술/Computer Vision

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
'기술/Computer Vision' 카테고리의 다른 글
  • conv1d, conv2d, conv3d
  • 랜덤 샘플 이미지
  • tensor image to numpy image, numpy image to tensor image
  • 컴퓨터비전 방법론들
용나리
용나리
  • 용나리
    티스토리 블로그
    용나리
  • 전체
    오늘
    어제
    • 분류 전체보기 (333)
      • 과거의 것들 (93)
        • AI Tech boostcamp (92)
      • 생각정리(고찰) (2)
      • 기술 글 (0)
      • 코딩테스트 (4)
        • C++ (0)
        • Python (4)
      • CS (121)
        • 컴퓨터 시스템 (4)
        • 코틀린 인 액션 (13)
        • 김영한 스프링 강의 (104)
      • 일지 남기기용 (11)
        • 운동 (10)
      • 개발 배포 해보기 (1)
      • 프로그래밍 언어 및 기타 (32)
        • Spring Boot (9)
        • Python (9)
        • Kotlin (1)
        • Flutter (2)
        • SQL (4)
        • Docker (3)
        • 공통 (4)
      • os (4)
        • Linux (4)
      • 기술 (17)
        • PyTorch (6)
        • Computer Vision (6)
        • NLP (1)
        • 기타 (4)
      • 제품 후기 (0)
      • 게임 (0)
        • Human Resource Machine (0)
      • 강의 (26)
        • fullstackdeeplearning_sprin.. (9)
        • 부캠 안드로이드 학습정리 (17)
      • 개인 메모 (10)
      • IT 기타 (5)
      • 논문 읽기 연습 (5)
        • Computer Vision (1)
        • NLP (0)
        • 공통 (2)
        • 그냥 메모 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    pip install killed
    파이썬 실행경로
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
용나리
image normalize, inverse normalize
상단으로

티스토리툴바