TrOCR processor cannot be loaded from AutoProcessor
Author: LysandreJik
Pull request: no

The following works on current `master`:

```py
>>> from transformers import TrOCRProcessor
>>> processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
Downloading: 100%|██████████| 878k/878k [00:00<00:00, 1.61MB/s]
Downloading: 100%|██████████| 446k/446k [00:00<00:00, 1.03MB/s]
Downloading: 100%|██████████| 772/772 [00:00<00:00, 1.18MB/s]
Downloading: 100%|██████████| 1.28k/1.28k [00:00<00:00, 1.70MB/s]
```

The following does not:

```
>>> from transformers import AutoProcessor
>>> processor = AutoProcessor.from_pretrained("microsoft/trocr-base-printed")
Downloading: 100%|██████████| 228/228 [00:00<00:00, 404kB/s]
Downloading: 100%|██████████| 4.03k/4.03k [00:00<00:00, 6.88MB/s]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/lysandre/Workspaces/Python/transformers/src/transformers/models/auto/processing_auto.py", line 171, in from_pretrained
    return PROCESSOR_MAPPING[type(config)].from_pretrained(pretrained_model_name_or_path, **kwargs)
  File "/home/lysandre/Workspaces/Python/transformers/src/transformers/models/auto/auto_factory.py", line 559, in __getitem__
    raise KeyError(key)
KeyError: <class 'transformers.models.vision_encoder_decoder.configuration_vision_encoder_decoder.VisionEncoderDecoderConfig'
```

cc @NielsRogge 

Comments:
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

Currently looking into this

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

Reopening this issue since I'm getting a new error
```python
>>> from transformers import TrOCRProcessor
>>> processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-stage1")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-14-3c6b951b36db>](https://localhost:8080/#) in <module>()
      1 from transformers import TrOCRProcessor
      2 
----> 3 processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-stage1")
      4 train_dataset = IAMDataset(root_dir=data_path,
      5                            df=train_df,

6 frames
[/usr/local/lib/python3.7/dist-packages/transformers/processing_utils.py](https://localhost:8080/#) in from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
    184                 [`~tokenization_utils_base.PreTrainedTokenizer.from_pretrained`].
    185         """
--> 186         args = cls._get_arguments_from_pretrained(pretrained_model_name_or_path, **kwargs)
    187         return cls(*args)
    188 

[/usr/local/lib/python3.7/dist-packages/transformers/processing_utils.py](https://localhost:8080/#) in _get_arguments_from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
    228                 attribute_class = getattr(transformers_module, class_name)
    229 
--> 230             args.append(attribute_class.from_pretrained(pretrained_model_name_or_path, **kwargs))
    231         return args
    232 

[/usr/local/lib/python3.7/dist-packages/transformers/models/auto/tokenization_auto.py](https://localhost:8080/#) in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
    526                     f"Tokenizer class {tokenizer_class_candidate} does not exist or is not currently imported."
    527                 )
--> 528             return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
    529 
    530         # Otherwise we have to be creative.

[/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_base.py](https://localhost:8080/#) in from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs)
   1785             use_auth_token=use_auth_token,
   1786             cache_dir=cache_dir,
-> 1787             **kwargs,
   1788         )
   1789 

[/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_base.py](https://localhost:8080/#) in _from_pretrained(cls, resolved_vocab_files, pretrained_model_name_or_path, init_configuration, use_auth_token, cache_dir, *init_inputs, **kwargs)
   1913         # Instantiate tokenizer.
   1914         try:
-> 1915             tokenizer = cls(*init_inputs, **init_kwargs)
   1916         except OSError:
   1917             raise OSError(

[/usr/local/lib/python3.7/dist-packages/transformers/models/xlm_roberta/tokenization_xlm_roberta_fast.py](https://localhost:8080/#) in __init__(self, vocab_file, tokenizer_file, bos_token, eos_token, sep_token, cls_token, unk_token, pad_token, mask_token, **kwargs)
    147             pad_token=pad_token,
    148             mask_token=mask_token,
--> 149             **kwargs,
    150         )
    151 

[/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py](https://localhost:8080/#) in __init__(self, *args, **kwargs)
    117         else:
    118             raise ValueError(
--> 119                 "Couldn't instantiate the backend tokenizer from one of: \n"
    120                 "(1) a `tokenizers` library serialization file, \n"
    121                 "(2) a slow tokenizer instance to convert or \n"

ValueError: Couldn't instantiate the backend tokenizer from one of: 
(1) a `tokenizers` library serialization file, 
(2) a slow tokenizer instance to convert or 
(3) an equivalent slow tokenizer class to instantiate and convert. 
You need to have sentencepiece installed to convert a slow tokenizer to a fast one.
```

edit: Already fixed in this issue  https://github.com/huggingface/transformers/issues/9750#issuecomment-766862107 , my bad.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

I verified with v4.19, and there is no issue. @NouamaneTazi Could you try the new version?

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

It's working now. Thanks for your help! 🤗

loaded_model = VisionEncoderDecoderModel.from_pretrained('/content/drive/MyDrive/ocr_pth/checkpoint-5000')
processor = TrOCRProcessor.from_pretrained("/content/drive/MyDrive/ocr_pth/checkpoint-5000")

KeyError                                  Traceback (most recent call last)
[<ipython-input-12-a5659b723d72>](https://localhost:8080/#) in <module>
      1 # loaded_preprocessor = TrOCRProcessor.from_pretrained('/content/drive/MyDrive/ocr_pth/checkpoint-5000')
      2 loaded_model = VisionEncoderDecoderModel.from_pretrained('/content/drive/MyDrive/ocr_pth/checkpoint-5000')
----> 3 processor = TrOCRProcessor.from_pretrained("/content/drive/MyDrive/ocr_pth/checkpoint-5000")

3 frames
[/usr/local/lib/python3.7/dist-packages/transformers/models/auto/auto_factory.py](https://localhost:8080/#) in __getitem__(self, key)
    570                 model_name = self._model_mapping[mtype]
    571                 return self._load_attr_from_module(mtype, model_name)
--> 572         raise KeyError(key)
    573 
    574     def _load_attr_from_module(self, model_type, attr):

KeyError: <class 'transformers.models.vision_encoder_decoder.configuration_vision_encoder_decoder.VisionEncoderDecoderConfig'>

kindly guide me how resolve this problem.Thanks

Hi @zainali60 What's your `transformers` version? Could you try the latest one? If the same error still occurs, could you open a new issue with a small but self-contained code snippet to reproduce the issue? Thank you!

Thanks for reply @ydshieh  i resolve my issue and get results
if you have implementation scratch ocr transformers model kindly share me.thanks

Hi, I'm getting same error.. I tried v4.19, v4.21.3, v4.22.1 which is the last version so far.

```
# LOAD PRE_PROCESSOR
from transformers import AdamW, TrOCRProcessor, VisionEncoderDecoderModel, get_scheduler
def load_processor() -> TrOCRProcessor:
    return TrOCRProcessor.from_pretrained('gagan3012/TrOCR-Ar-Small')

load_processor()
```

@HebaGamalElDin I can reproduce the issue with the checkpoint `gagan3012/TrOCR-Ar-Small`, but not `microsoft/trocr-base-printed`. By looking the files in these 2 model repositories, I believe it is because `gagan3012/TrOCR-Ar-Small` doesn't contain the tokenizer files.

I think you can get the tokenizer from [microsoft/trocr-small-stage1](https://huggingface.co/microsoft/trocr-small-stage1). But it would be nice if you can leave a message or even open a PR in [gagan3012/TrOCR-Ar-Small](https://huggingface.co/gagan3012/TrOCR-Ar-Small) to upload the tokenizer files :-)

Hi @ydshieh and @NielsRogge  Can we find the accuracy of the OCR model along with the character error rate CER?  and how to do it. Thank you