That's a classic symptom of issues in the autoregressive decoding process of the language model part of your captioning system. While CLIP provides the image understanding, a separate language model (like a GPT-style decoder, BART, etc.) is typically responsible for generating the text sequence.
Here are the most likely causes:
-
End-of-Sequence (EOS) Token Problems:
- Model not predicting EOS: The language model might not have learned to predict the EOS token with high enough probability. Instead, it finds that repeating the last plausible word is the "safest" (highest probability) continuation, especially if the context becomes uninformative.
- EOS token missing or mishandled in generation: The generation script might not be correctly looking for or terminating upon the generation of an EOS token.
- Low EOS probability: Even if the model can predict EOS, its probability might be consistently lower than repeating the last word in certain contexts.
-
Decoding Strategy Issues:
- Greedy Decoding: If you're using greedy decoding (always picking the highest probability next token), it's highly susceptible to getting stuck in repetitive loops once it starts.
- Beam Search without adequate penalties: Beam search can also fall into this trap if all beams converge on the same repetitive sequence. Often, repetition penalties or n-gram blocking are needed.
- Low Temperature Sampling: If using sampling, a very low temperature makes the output more deterministic and closer to greedy decoding, increasing the chance of repetition.
-
Repetition Penalties Not Applied or Misconfigured:
- Most modern text generation libraries (like Hugging Face Transformers) offer parameters like
repetition_penalty (to penalize tokens that have already appeared) or no_repeat_ngram_size (to prevent n-grams from repeating). If these are not used or are set inappropriately, repetition is common.
-
Model Training Issues:
- Poorly trained language model: The language model might not have been trained sufficiently or on diverse enough data to learn robust sentence structures and when to stop.
- Overfitting to certain patterns: While unlikely to cause this specific error, poor training can lead to degenerate outputs.
- Context length limitations: If the model has a short effective context window, it might "forget" it just said a word and say it again.
-
Issues with Conditioning on CLIP Embeddings:
- If the language model isn't effectively using the image information from CLIP, it might default to generic or repetitive language when it runs out of strong signal from the image. The image features might "fade" in influence as the caption gets longer.
-
Dataset Artifacts (Less Likely for this specific pattern):
- If the training data for the language model part somehow contained examples with such repetitions, the model might learn it. This is generally unlikely for standard captioning datasets.
How to Investigate and Fix:
-
Check your generation script:
- Are you correctly handling the EOS token?
- What decoding strategy are you using? Try switching from greedy to beam search or sampling.
- Implement or tune repetition penalties (e.g.,
repetition_penalty > 1.0 in Hugging Face, or set no_repeat_ngram_size to 2 or 3).
- If using sampling, experiment with
temperature, top_k, and top_p settings.
-
Examine the model's vocabulary: Ensure the EOS token is part of the vocabulary and is handled correctly.
-
Inspect probabilities: If possible, look at the probability distribution the model assigns at the step where it starts repeating. Is the EOS token's probability very low? Is the repeated word's probability very high?
-
Try a different pre-trained language model head: If you're using a common architecture (e.g., CLIP image encoder + GPT-2 decoder), try a different pre-trained GPT-2 checkpoint to see if the issue is specific to the LM weights you're using.
Most often, the culprit is a combination of greedy decoding (or near-greedy sampling) and a lack of explicit mechanisms (like EOS handling or repetition penalties) to prevent the model from falling into simple, high-probability loops.