On the journey of

[CSE URP] Instance Normalization(StarGAN) (1) 본문

Experiences & Study/CSE URP' 29

[CSE URP] Instance Normalization(StarGAN) (1)

dlrpskdi 2023. 8. 29. 10:12

CSE URP라는 카테고리 안에 글 쓸 날도 얼마 안 남은 것 같다 :) 자세한 것은 나아중에 적겠지만, 우선은..! 그렇다.


StarGAN에서의 Instance Normalization에 대한 Key 질문은 아래와 같다. 

"왜 StarGAN에서는 (Image Classification에서 주로 사용되는) Batch Norm이 아닌 Instance Norm이 사용되었는가?"

이 질문의 답변에 앞서 Batch Norm과 Instance Norm에 대해서 간략하게 살펴보자.

- Batch Norm을 수식으로 표현하면 아래와 같다. 

Instance Norm을 수식으로 표현하면 다음과 같다.

(수식에 따르면) Batch Norm은 "normalize all images across batch and spatial locations"이고 Instance norm은 "normalize each batch independently across spatial locations only)이다. 이는 맨 위의 도표를 보면 조금 더 직감적으로 이해할 수 있다.

Batch Norm을 사용하려면 mini batch의 mean (평균)과 variance (분포)을 계산해야 한다. 하지만 만일 mini-batch의 사이즈가 너무 작으면, mini-batch들의 mean과 variance가 매우 noisy해질 위험이 있고 training process가 unstable할 수 있다.

 

GAN은 일반적으로 네트워크는 두 개 (즉, Generator와 Discriminator)로 이루어져 있고, Generator도 Latent space로 input image을 축소시켰다가 fake image로 reconstruct하는 구조를 가지고 있기 때문에 Generator의 크기가 왠만한 image classification 모델보다 더 크다. 이러한 이유로 GAN에서는 Batch norm을 사용하기에 mini-batch의 사이즈가 작다 (starGAN에서도 mini-batch의 사이즈가 12밖에 되지 않는다). 이러한 이유로 Batch norm을 사용하게 되면, 작은 mini-batch 사이즈 때문에 training이 unstable해진다.

 

요약:

  • Batch Norm을 사용하려면, mini batch가 충분히 커야하는데 GAN에서는 그렇지 않다.
  • 그래서 batch size와 무관한 Instance Norm을 사용해야한다

* Precise Reason은 아니기 때문에(여러 조사와 공부 내용을 기반으로 제가 이해한 내용이기에) 누구든 정확한 이유가 있다면 알려주시면 ,, 감사하겠습니다 :)

 

Style transfer에서 contrast의 중요성

stylized image의 contrast는 대부분 style image의 contrast에 의해 결정되며 content image contrast와 거의 독립적이라는 것을 아래 이미지에서 확인할 수 있다.

https://arxiv.org/abs/1607.08022

source https://arxiv.org/abs/1607.08022


 

content image를 style image의 style로 변환할 때 styleization의 결과가 일반적으로 content image의 contrast에 의존해서는 안 된다. stylized image의 contrast는 style iamge의 contrast와 비슷하기를 원하기 때문에 generator는 content image에서 contrast information을 제거해야한다.

 

아래 image를 통해 contrast normalization이 필요하다는 것을 확인할 수 있다.

batch normalization, instance normalization 비교

contrast normalization을 위해 batch normalization을 사용할 수 있다. 하지만 batch normalization을 사용했을 시 결과의 품질이 instance normalization을 사용했을 때보다 낮을 수 있다. 각 image의 channel 값 분포가 다른 비율로 존재한다면 해당 image의 분포를 기준으로 normalization되지 않고 전체 batch의 분포로 normalization되기 때문에 network의 training의 난이도가 상승하기 때문이다.

현재 소개하고 있는 contrast normalization는, batch normalization을 전체 batch가 아닌 개별 instance에서 작동하는 drop-in replacement로 사용하기 때문에 GAN에서 instance normalization라는 명칭으로 주로 사용한다. 아래 이미지를 통해 batch normalization과 instance normalization의 차이를 쉽게 이해할 수 있다. (https://becominghuman.ai/all-about-normalization-6ea79e70894b에서 발췌)

both source https://becominghuman.ai/all-about-normalization-6ea79e70894b 

 

Batch Normalization, Instance Normalization, Layer Normalization: Structural Nuances

This short post highlights the structural nuances between popular normalization techniques employed while training deep neural networks.

becominghuman.ai

In “Batch Normalization”, mean and variance are calculated for each individual channel across all samples and both spatial dimensions.

Instance Normalization

In “Instance Normalization”, mean and variance are calculated for each individual channel for each individual sample across both spatial dimensions.

추가자료

"IN (instance normalization)은 image의 feature statics를 normalization하여 style variantion을 제거하는 효과가 있으며 style transfer에서 BN (batch normalization) 또는 LN(layer normalization)보다 자주 사용된다. 그러나 image를 normalization 할 때 최근 연구에서는 IN만 사용하는 대신 adaptive instance normalization (AdaIN), conditional instance normalization, 및 batch instance normalization을 사용한다." (https://arxiv.org/abs/1907.10830에서 발췌)

요약
1)stylization이 성공적으로 수행되기 위해서 generator는 content image에서 contrast information을 contrast normalization을 통해 제거해야 한다.
2)contrast normalization을 위해 batch normalization을 사용할 때 각 image의 channel 값 분포가 다른 비율로 존재한다면, batch 단위로 normalization 수행 시 training의 난이도가 상승해 instance normalization을 사용한다.