iMTE

Show and Tell: A Neural Image Caption Generator 본문

Deep learning/Keras

Show and Tell: A Neural Image Caption Generator

Wonju Seo 2018. 7. 26. 19:58

Show and Tell: A Neural Image Caption Generator

Vinyals, Oriol, et al. "Show and tell: A neural image caption generator." Proceedings of the IEEE conference on computer vision and pattern recognition. 2015.

Image-to-sentence translation은 sentence-to-sentence translation을 기초로, 기존에 Encoder로 사용된 RNN 대신에 CNN을 사용해서 이미지를 encoding을 하였다. Image-to-sentence 작업은 기존의 object detection과 object classification과 달리, 물체를 인식하고 이 물체가 무엇을 하는지 Natural language로 translation을 할 수 있어야하므로, 어려운일이다.

위와 같이 이미지가 주어지면, pre-trained된 CNN에 의해서 추출된 feature들이 (Encoding) Decoder인 RNN에 입력으로 사용되어 이미지를 설명하는 문장을 생성해낸다. 기존의 machine translation은 다음과 같은 확률을 maximize하는게 목표인데, 이를 이미지를 넣었을 때의 sentence의 확률을 maximize하는 것으로 변환하였다.

이 논문에서는 위의 theta를 계산하기 위해서 다음을 recurrent neural network로 모델링을 하였다.

"with a recurrent neural network (RNN), where the variable number of words we condition upon up to t-1 is expressed by a fixed length hidden state or memory h_{t}"

RNN이 잘 학습하기 위해서, LSTM을 사용했는데, LSTM은 vanishing or exploding gradient를 해결할 수 있는 것으로 알려져있다. LSTM은 memory cell C를 3개의 gate (input, forget, output)으로 조절하면서 학습을 진행한다. 기존 RNN에서 사용된 backpopagtion에서의 chain rule에 의한 곱셈이 덧셈으로 변환되면서, vanishing or exploding gradient를 해결할 뿐만 아니라, RNN에 비해 긴 메모리를 저장할 수 있는 장점이 있다.

위의 그림은 이 논문에서 NIC (Neural Image Captioning)을 학습시킨 방법으로, pre-trained된 (이 논문에서는 InceptionV3를 사용했다.) CNN classifier를 사용해서 x(-1)에 넣는다. 먼저 S0는 "<Start>" 단어로 시작해서, 문장의 시작을 알리고 이 입력을 embedding한 결과와 CNN classifier의 x(-1) 값으로 output을 계산한다. 계산된 output에서 argmax를 취해서 가장 확률이 높은 값을 선택해서 다음 입력으로 집어넣는다. "<end>"가 나올 때까지 반복하거나, max_len을 초과할 때 까지 예측을 한다.

Inference에서 1. Sampling 방법과 2. Beam search 방법이 사용되었다. 다음을 잘 읽어보자!

"There are multiple approaches that can be used to generate a sentence given an image, with NIC. the first one is 'Sampling' where we just sample the first word according to p1, then provide the corresponding embedding as input and sample p2, continuing like this until we sample the sepcial end-of-sentence token or some maximum length."

"The second one is 'BeamSearch' : iteratively consider the set of the k best sentences up to time t as candidates to generate sentences of size t+1, and keep only the resulting best k of them."

이 논문에서는 Pascal VOC2008, Flickr8k, Flickr30k, MSCOCO, SBU data를 사용하였다. 결과만 보면, 다음과 같이 해당 이미지에 description이 generate을 확인할 수 있다.

맨 왼쪽의 경우 거의 에러가 없는 것을 확인할 수 있으나 맨 오른쪽은 부정확한 description이 generate되었다. 마지막으로 embedding된 결과를 보면, 비슷한 단어끼리 모여있다는 것을 보여주고 있다. 또한, 이런 관계들은 CNN이 비슷한 종류의 사물의 feature를 추출 할 수 있도록 도움을 줄 수 있는 장점이 있다.

"Indeed, having "horse", "pony", and "donkey" close to each otehr will encourage the CNN to extract features that are relevant to horse-looking animals"

Show and Tell을 Keras로 구현해본 결과 다음과 같았다. (성능은 47 %의 accuracy.) code: https://github.com/sachin-kmr/Neural-Image-Captioning

(위 코드에서 오류인 부분이 조금 있습니다. 수정을 해야합니다.)

단순 sampling 방법에서는, 학습된 모델이 잘 예측을 못하지만, beam search의 index가 5이상에서는 단순 sampling 방법보다는 이미지를 잘 설명하고 있다는 것을 보여주고 있다.

좀 더 학습을 시키고, 같은 사진을 해석하도록 해보았다. (accuracy 70 %)

이전과 달리 snow대신 street을 보고 있지만, 'red'라는 색깔을 추출했다는 것을 확인할 수 있다. 하지만 beam search의 결과 값은 만족스럽지 못하다.

(스탠포드에서 찍었던 사진중에, 사진을 찍는 사람들이 있어서 이 사진으로 검증을 해봤다.) 검증 결과, red라는 색깔은 검출했지만 구체적으로 어떤 행동을 하고 있는지는 모델이 제대로 예측을 하고 있지 않고 있다. 좀 더 학습을 시켜야겠다.


'Deep learning > Keras' 카테고리의 다른 글

You Only Look Once : Unified Real-Time Object Detection  (0) 2018.07.27
Variational Auto-Encoder (VAE)  (0) 2018.07.11
Generative Adversarial Networks  (0) 2018.06.10
Comments