# TrOCR repeated generation

**URL:** <https://discuss.huggingface.co/t/trocr-repeated-generation/12361>\
**Category:** Beginners\
**Created:** [November 29, 2021, 11:19am UTC](https://discuss.huggingface.co/t/trocr-repeated-generation/12361 "2021-11-29T11:19:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kforcode](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@Kforcode](https://discuss.huggingface.co/u/Kforcode)\
**Post date:** [November 29, 2021, 11:19am UTC](https://discuss.huggingface.co/t/trocr-repeated-generation/12361/1 "2021-11-29T11:19:13Z")

</div>

@nielsr I am using microsoft/trocr-large-printed  
there is a slight issue, the model generates repeated predictions on my dataset.

if you see the left is ground\_truth and the right is the model prediction  
 ![repeat](https://us1.discourse-cdn.com/hellohellohello/original/2X/c/c8d55b1a7de854ac7cd849ff046de04c1f41db8f.png)

after generating the right text, it does not stop and goes on repeating the same.  
Do you know what might be the issue? Am I missing any param in generate function  
My decoding code looks like this

```auto
for batch in tqdm(test_dataloader):
    # predict using generate
    pixel_values = batch["pixel_values"].to(device)
    outputs = model.generate(pixel_values, output_scores=True, return_dict_in_generate=True, max_length=22)
    
    # decode
    pred_str = processor.batch_decode(outputs.sequences, skip_special_tokens=True)

```

thanks once again

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [November 29, 2021, 1:24pm UTC](https://discuss.huggingface.co/t/trocr-repeated-generation/12361/2 "2021-11-29T13:24:11Z")

</div>

Hi,

Thanks for reporting. This has been reported before (see [this](https://github.com/NielsRogge/Transformers-Tutorials/issues/53)).This probably has to do with the settings of the [`generate()` method](https://huggingface.co/transformers/main_classes/model.html#transformers.generation_utils.GenerationMixin.generate), which uses greedy decoding by default. Note that the [original implementation](https://github.com/microsoft/unilm/blob/master/trocr/pic_inference.py) uses beam search.

Greedy decoding usually does a lot of repetition, so for tasks like TrOCR one should consider beam-search. And to avoid repetition, one could use the `no_repeat_ngram_size` argument. One should also set the right `max_length` and `min_length`.

I’ll investigate this a bit. Feel free to experiment with the settings of the generate method.

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [November 29, 2021, 5:19pm UTC](https://discuss.huggingface.co/t/trocr-repeated-generation/12361/3 "2021-11-29T17:19:45Z")

</div>

Hi,

After investigation, it turns out the generate() method currently does not take into account `config.decoder.eos_token_id`, only `config.eos_token_id`.

You can fix it by setting `model.config.eos_token_id = 2`.

We will fix this soon.

---

<div class="post-metadata">

**Author:** ![Kforcode](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@Kforcode](https://discuss.huggingface.co/u/Kforcode)\
**Post date:** [November 30, 2021, 3:28pm UTC](https://discuss.huggingface.co/t/trocr-repeated-generation/12361/4 "2021-11-30T15:28:40Z")

</div>

awesome it worked. Thank you
