Reading notes: Using Reinforcement Leanring Algorithm to Generate Image captions
I have been a while not reading pure image captioning paper. One day I checked the coco leaderboard, and found the first two are both quite recent result. So I checked their paper, and found that they are both using policy gradient. Intersting.
So some basic background for image captioning: the standard way to train a image caption is to maximize . Using chain rule and take log, you can basically train the model by minimizing the cross entropy loss of each word.
However, there are two shortcomings for this kind of strategy:

The evaluation metric is different from the training loss. This is an common issue in many problems, like minimizing loglikelihood for segmentation but evaluating using mean IOU. Not only we want to get better evaluation metric, but also the evaluation metric has perceptual mearning. You can think of log loss as putting the same weight on all the words in the sentence, however, if it’s human to judge the captions, human may focus more on if the captions are informative or not. So, presumably optimizing directly on the metric can get better caption.

The exposure bias. During the training, the input of each time step of the model is from the true captions. However, when generating captions, the input of next time step is previous work generated by the model. Once the model generate a “bad” word that the model has never seen before, the error will propogate and result in bad caption. Schedule sampling is motivated by this problem.
So the obvious idea is to match the training stage and test stage. Since we want better CIDER, so let’s optimizer CIDER. Since we sampled words in test stage, then we sampled word in training stage too.
However, the sampling operation is not differentiable, so you can’t use normal backpropogation to get the gradients. Here comes the Reinforcement Learning part. The RL is to some gamelike task. Say the game is Flappy bird; it’s about what position(state) to jump(action) can achieve the highest score(reward). RL is to train a model that can choose a right action given a state so that the model can finally can get high reward.
In the case of image captioning, the state is the image and previously generated words, the action is choosing next word, and the reward is CIDER or other metrics.
Policy Gradient is a very fundamental algorithm in RL. You can check Karparthy’s blog to learn the basic PG.
PS: The bay area deep learning school has a tutorial on reinforcement learning which covers PG and another popular algorithm in RL, Q learning. Highly recommended.
It’s not the first work applying PG to image captioning. FAIR proposed MIXER. After that, this paper applies actorcritic (an improved PG algorithm) to machine translation. However, these two recent papers are still using PG, not knowing if the next stateoftheart will be AC or not.
The first paper is Selfcritical Sequence Training for Image Captioning from IBM; it’s now the best on coco leader board. The second is Optimization of image description metrics using policy gradient methods, which is now the second on leader board.
First, let me introduce the basic Policy gradient:
The objective is to maximize expected reward:
where is the sampled caption, which can be seperated to to . is our model parameterized by .
By simple derivation, we can get the gradient of with respect to .
To this formula, we make a small change:
The ‘b’ represents baseline. As long as the b is not dependent on , this change won’t change the gradient value. The proof is as follows:
In practice, we use a sampled caption to estimate the gradient. (As GD to SGD)
(We will discuss the baseline after a while.) By chain rule, we can get
Here, is a vocabulary size vector, the score of each word at time t. (The input of softmax layer)
For sampled each , the later term is always less than 0. In this case, if the previous term is greater than 0, then the gradient is negative. If we do gradient descent, then the scores of sampled words will be pushed up.
The purpose of using baseline is to reduce the variance of the gradient. Say the reward is always between 100 and 120; if we don’t have b, all the sampled results will be pushed in each SGD update. The difference is, the sampled results with higher reward will be pushed up to a larger extent. However, even if the reward is 120, the scale of pushup is not much more than reward 100. You can imageine that the update is very unstable.
However, if we have a baseline 110 now, the result with reward 100 will be pusheddown, because the gradient is positive. Another possible baseline is , aka the average reward. When you sample a caption, you can know if it’s above average or below average. If it’s above average, push it up, and vice versa.
The first paper define the baseline in a quite interesting way. They use the reward of generated by greedy decoding as baseline. This method avoid training a baseline function which will involve choosing hyperparameters. However, this method is much simpler and they claim that this would reduce variance. And the name of selfcritical comes from this design.
So, if the sampled result is worse then greedy decoding, the result will be pusheddown, and vice versa.
In the paper, they propose that they can use other , like nbest list or beam search.
No matter what result they get. This choice of baseline is a very interesting idea, but no that intuitive. In general, the sampled result will mostly be worse than greedy decoding result, so the first term of the gradient are mostly negative. To improve, improving is not the only way; you can also decrease . An extreme case is when you have a random model, the rewards you get from sampled and greedy search could be very close, similarly bad. However, the paper shows that this case doesn’t happen. One possible reason is they pretrain the model using cross entropy loss which avoid the model diverge to really bad result.
Another thing is, the geration process in training and testing is coherent. However, during training, the output is sampled, and during testing, the output is generated by greedy decoding or beam search.
Some implementation details:
They are using show attend tell model. The vision feature is the output of the last convolutional layer of resnet101. They didn’t crop or rescale the image, but input the original image, and then use spatial adaptive average pooling to resize it to 14x14x2048. (I omit the nonattention model in that paper.)
They also tried different metric as reweard. However, only optimizing on CIDER can gain improvements on all the other metrics compared to crossentropy loss, which implicitly proves CIDER is better than other metrics.
The second paper is quite similar to seqGAN. This paper defines Q, which is the expected reward of choosing given the previously generated t1 words and the image.
The purpose of using Q is to split the contributions of each words, kind of like giving each word a reward.
In practice, the Q is estimated by rollouts. K here is chosen to be 3. (Recall that AlphaGo also uses rollouts to estimate values),
In this way, the gradient becomes:
(To be consistent with the first paper, I slightly change the expressions in the second paper.) The difference here is, the same reward became different Ws, the same b become a parametric function B. The b here is a trainable function of the hidden vector at each time step. It’s the same as what’s in MIXER’s code.
The only thing I’m curious about is why not use the Q value of the previous time step? Then, is the contribution of .
The paper also uses other tircks, which I’m not covering here. The caption model they use is the show tell model from google. The CNN is inception v3.
Conclusion:
These two papers are two divergent algorithm based on the same motivation. However, it’s hard to compare them in parallel, because their cnnrnn network architecture is different. There are a lot of possible future works, like what’s mentioned in the conclusion of the second paper. For example, use discriminator score as reward like what’s in seqGAN instead of these metrics.