Deeper Learning

Neural Style Transfer 본문

AI/Deep Learning

Neural Style Transfer

Dlaiml 2020. 11. 28. 17:15

Neural Style Transfer

이미지 C와 content가 비슷하고 이미지 S와 style이 비슷한 이미지 G를 만드는 알고리즘

Cost function의 정의는 다음과 같다.

hyperparameter $\alpha$와 $\beta$에 의해 content loss, style loss에 weight가 적용된 Cost function

 

Content Cost Function

$a^{(C)}$ = input C가 hidden layer를 통과한 activations (1, n_h, n_w, n_c)

$a^{(G)}$ = input G가 hidden layer를 통과한 activations (1, n_h, n_w, n_c)

(n_h,n_w,n_c) = hidden layer의 output shape 

$\frac{1}{4 \times n_{H} \times n_{W} \times n_{C}}$ = Normalization term

$a^{(C)}$ - $a^{(G)}$는 단순 pixel distance로 측정한다. 

$a^{(C)}$ , $a^{(G)}$의 Frobenius norm의 제곱합과 같다.

두 input C와 G가 얼마나 비슷한지 단순 hidden layer output의 비교로 Content의 손실을 측정한다.

 

Style Cost Function

예시의 5개의 채널은 hidden layer에서 각각 다른 filter의 output이다. 

예로 가장 앞의 초록색 층은 이미지의 검고 동그란 모양, 노란색 층은 초록색의 물결선을 detect하는 filter를 사용하여 Convolution 연산이 이루어진 output이라고 생각할 수 있다.

hidden layer의 activation을 채널별로 Correlation을 측정하는 것이다. 총 5개의 채널에선 5 X 5 = 25 개의 Correleation value를 구할 수 있다.

 

채널별 비교를 위해 2차원으로 reshape시킨 matrix의 내적을 통해 각 채널끼리 Corrleation을 기록한 Gram Matrix를 만들 수 있다. 각 채널은 다른 filter에 의해 생성된 output으로 이는 Correlation between filters와 같다.

 

위의 그림에서 가장 좌측의 matrix의 1열은 각각 hidden layer를 통과하기전 같은 부분(좌상단)에 대한 filter가 feature를 detect한 값이다. 내적을 통해 각 filter 사이의 Correlation을 구할 수 있으며 이 방식으로 Gram Matrix가 생성된다.

Gram Matrix는 따라서 전체적인 image의 Style을 대변할 수 있다.

따라서 다음 수식으로 Style의 차이를 측정이 가능한 것이다. 

자세한 각 term의 설명은 Cost function과 같다.

하나의 hidden layer가 아닌 여러개의 hidden layer와 weight인 $\lambda$ list를 이용하여 각 층에 다른 weight를 부여하면서 전체를 더한것이 최종 Style Cost function이 된다.

Leon A. Gatys.et.al(2015).A Neural Algorithm of Artistic Style

 

Content & Style Additional explanation

Content = Image를 Neural Net이 판별할 수 있는 Feature

Style = Gram Matrix를 이용하여 전체적인 image와 상관없이 반복되는 Texture라고 할 수 있다. (질감, 패턴)

따라서 이미지 C를 인식하는데 필요한 중요 Feature와 이미지 S의 텍스쳐가 혼합된 형태로 새로운 이미지 G가 생성되는 것이다.

 

Hidden Layer & Tuning hyperparameters

Leon A. Gatys.et.al(2015).A Neural Algorithm of Artistic Style

Hidden layer의 선정은 너무 shallow하거나 deep하면 안되는데 이는 위와 같이 shallow한 hidden layer의 filter는 vertical edge과 같은 low-level feature를 탐지하기 때문에 이는 생성할 이미지 G에 작은 세로선 또는 반복되는 단순한 무늬를 만드는데 그친다.

반대로 deep layer의 activation은 많은 정보를 담고 있으며 크기가 더 큰 feature를 탐지하여 기존 Content의 Style에 의한 왜곡이 더 심해지기 때문이다. 따라서 적절한 hidden layer의 선정과 weight $\lambda\가 중요하다.

 

Gradient descent를 통해 input image G의 값들이 update되어 최종적으로 두 이미지의 content와 style이 혼합된 결과물이 나오게 되는것이다.

 

[참고] Leon A. Gatys.et.al.(2015).A Neural Algorithm of Artistic Style

'AI > Deep Learning' 카테고리의 다른 글

Gated Recurrent Units (GRU)  (0) 2020.12.03
Language Model  (0) 2020.12.01
Recurrent Neural Network  (0) 2020.12.01
Fréchet inception distance (FID)  (0) 2020.11.30
One Shot Learning  (0) 2020.11.27
Comments