trocr model performs worse than unilm version
Author: konradgrosser
Pull request: no
Labels: name: bug

### System Info

```shell
transformers 4.19.2, python 3.7
```


### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction


![MicrosoftTeams-image (2)](https://user-images.githubusercontent.com/107130731/172656279-c9a56b80-d19c-4edd-aaa9-77fbb55c4594.png)


use this image for recognition.

You can use the following code to execute microsofts unilm trocr on it:


 ```
import task
import deit
import deit_models
import torch
import fairseq
from fairseq import utils
from fairseq_cli import generate
from PIL import Image
import torchvision.transforms as transforms


def init(model_path, beam=5):
    model, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task(
        [model_path],
        arg_overrides={"beam": beam,
                       "task": "text_recognition",
                       "data": "", "fp16": False})

    device = "cuda" if torch.cuda.is_available() else "cpu"
    model[0].to(device)
    model[0].eval()

    img_transform = transforms.Compose([
        transforms.Resize((384, 384), interpolation=3),
        transforms.ToTensor(),
        transforms.Normalize(0.5, 0.5)
    ])

    generator = task.build_generator(
        model, cfg.generation, extra_gen_cls_kwargs={'lm_model': None, 'lm_weight': None}
    )
    generator.eval()

    bpe = task.build_bpe(cfg.bpe)

    return model, cfg, task, generator, bpe, img_transform, device


def preprocess(img_path, img_transform):
    im = Image.open(img_path).convert('RGB')
    im = img_transform(im).unsqueeze(0).to(device).float()

    sample = {
        'net_input': {"imgs": im},
    }

    return sample


@torch.no_grad()
def get_text(cfg, generator, model, sample, bpe):
    decoder_output = task.inference_step(generator, model, sample, prefix_tokens=None, constraints=None)
    decoder_output = decoder_output[0][0]       #top1

    hypo_tokens, hypo_str, alignment = utils.post_process_prediction(
        hypo_tokens=decoder_output["tokens"].int().cpu(),
        src_str="",
        alignment=decoder_output["alignment"],
        align_dict=None,
        tgt_dict=model[0].decoder.dictionary,
        remove_bpe=cfg.common_eval.post_process,
        extra_symbols_to_ignore=generate.get_symbols_to_strip_from_output(generator),
    )

    detok_hypo_str = bpe.decode(hypo_str)

    return detok_hypo_str


if __name__ == '__main__':
    model_path = 'path/to/models/microsoft-large-handwritten.pt'
    jpg_path = "Long_sentence.png"
    beam = 5

    model, cfg, task, generator, bpe, img_transform, device = init(model_path, beam)

    sample = preprocess(jpg_path, img_transform)

    text = get_text(cfg, generator, model, sample, bpe)

    print('Raw text')
    print(text)
    print('done')
```
    
 This results in the recognition:
"Zu Dyonis den Tyrannen schlich Damon den Dolchen im Gewande . Ihn schlugen die Verfolger in Bande. 3.1415926 ."

Trying the same with huggingface:
"Zu Dyonis den Tyrannen schlich Damon den Dolchen im Gewande . "

As you can see, the huggingface implementation is missing half the line.

Huggingface code:
```
import torch.cuda
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image


if __name__ == "__main__":
    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
    processor = TrOCRProcessor.from_pretrained('models/microsoft-trocr-large-handwritten')
    model = VisionEncoderDecoderModel.from_pretrained('models/microsoft-trocr-large-handwritten', pad_token_id=processor.tokenizer.eos_token_id).to(device)

    path = "path/to/Long_sentence.png"
    image = Image.open(path).convert("RGB")

    pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)

    generated_ids = model.generate(pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
    for txt in generated_text:
        print(txt)
```


### Expected behavior

```shell
When executing the script we expect the whole sequence to be recognized. It is recognized with the microsoft code in https://github.com/microsoft/unilm/tree/master/trocr

With this repo however we only recognize part of the sequence, which seems like an implementation error.
```


Comments:
TrOCR use autoregressive generation method, so the text are generated progressively from left to right. This process is time consuming ,thus there is a default cutoff length.

To increase the length, you can add max_length=N argument inn `generated_ids = model.generate(pixel_values)`.

Hi, indeed, as mentioned above, the `generate` method has a `max_length` argument which you can set appropriately.

Details here: https://huggingface.co/docs/transformers/v4.19.4/en/main_classes/text_generation#transformers.generation_utils.GenerationMixin.generate

Closing this issue.