LayoutLMv3 transformers.onnx

Hi, I’m trying to export a given LayoutLMv3 fine tuned model to onnx format following this guide.
The actual model I’m trying to export is this one from @nielsr .

My current configuration is the following:

  • transformers version: 4.21.3
  • Platform: Windows-10-10.0.22000-SP0
  • Python version: 3.10.4
  • Huggingface_hub version: 0.9.1
  • PyTorch version (GPU?): 1.12.0+cpu (False)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: No
  • Using distributed or parallel set-up in script?: -

When I’m trying to run the command from the tutorial (python -m transformers.onnx --model=nielsr/layoutlmv3-finetuned-cord onnx/) I get this error:

Some weights of the model checkpoint at nielsr/layoutlmv3-finetuned-cord were not used when initializing LayoutLMv3Model: [‘classifier.out_proj.bias’, ‘classifier.dense.bias’, ‘classifier.dense.weight’, ‘classifier.out_proj.weight’]

  • This IS expected if you are initializing LayoutLMv3Model from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
  • This IS NOT expected if you are initializing LayoutLMv3Model from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
    Using framework PyTorch: 1.12.0+cpu
    Traceback (most recent call last):
    File “C:\Users\nk-alex\miniconda3\envs\sparrow\lib\”, line 196, in _run_module_as_main
    return run_code(code, main_globals, None,
    File “C:\Users\nk-alex\miniconda3\envs\sparrow\lib\”, line 86, in run_code
    exec(code, run_globals)
    File "C:\Users\nk-alex\miniconda3\envs\sparrow\lib\site-packages\transformers\onnx_main
    .py", line 107, in
    File "C:\Users\nk-alex\miniconda3\envs\sparrow\lib\site-packages\transformers\onnx_main
    .py", line 89, in main
    onnx_inputs, onnx_outputs = export(
    File “C:\Users\nk-alex\miniconda3\envs\sparrow\lib\site-packages\transformers\onnx\”, line 336, in export
    return export_pytorch(preprocessor, model, config, opset, output, tokenizer=tokenizer, device=device)
    File “C:\Users\nk-alex\miniconda3\envs\sparrow\lib\site-packages\transformers\onnx\”, line 143, in export_pytorch
    model_inputs = config.generate_dummy_inputs(preprocessor, framework=TensorType.PYTORCH)
    File “C:\Users\nk-alex\miniconda3\envs\sparrow\lib\site-packages\transformers\models\layoutlmv3\”, line 264, in generate_dummy_inputs
    setattr(processor.feature_extractor, “apply_ocr”, False)
    AttributeError: ‘RobertaTokenizerFast’ object has no attribute ‘feature_extractor’

Am I doing something wrong?


Thanks for your interest in that model :slight_smile: the reason was that the repo on the hub didn’t contain a preprocessor_config.json file. I’ve uploaded it to the repo.

Also note that, in order to convert the model that includes the token classification head, you need to provide --feature token-classification to the script. Without this, it would just convert the base LayoutLMv3Model, not LayoutLMv3ForTokenClassification:

!pip install -q transformers[onnx]
!python -m transformers.onnx --model=nielsr/layoutlmv3-finetuned-cord onnx/ --feature token-classification
1 Like