--- library_name: pytorch license: mit pipeline_tag: image-to-text tags: - android --- ![](https://qaihub-public-assets.s3.us-west-2.amazonaws.com/qai-hub-models/models/trocr/web-assets/model_demo.png) # TrOCR: Optimized for Mobile Deployment ## Transformer based model for state-of-the-art optical character recognition (OCR) on both printed and handwritten text End-to-end text recognition approach with pre-trained image transformer and text transformer models for both image understanding and wordpiece-level text generation. This model is an implementation of TrOCR found [here](https://huggingface.co/microsoft/trocr-small-stage1). This repository provides scripts to run TrOCR on Qualcomm® devices. More details on model performance across various devices, can be found [here](https://aihub.qualcomm.com/models/trocr). ### Model Details - **Model Type:** Image to text - **Model Stats:** - Model checkpoint: trocr-small-stage1 - Input resolution: 320x320 - Number of parameters (TrOCREncoder): 23.0M - Model size (TrOCREncoder): 87.8 MB - Number of parameters (TrOCRDecoder): 38.3M - Model size (TrOCRDecoder): 146 MB | Model | Device | Chipset | Target Runtime | Inference Time (ms) | Peak Memory Range (MB) | Precision | Primary Compute Unit | Target Model |---|---|---|---|---|---|---|---|---| | TrOCRDecoder | Samsung Galaxy S23 | Snapdragon® 8 Gen 2 | TFLITE | 2.193 ms | 0 - 351 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | Samsung Galaxy S23 | Snapdragon® 8 Gen 2 | QNN | 2.388 ms | 2 - 72 MB | FP16 | NPU | [TrOCR.so](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.so) | | TrOCRDecoder | Samsung Galaxy S23 | Snapdragon® 8 Gen 2 | ONNX | 2.904 ms | 0 - 179 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.onnx) | | TrOCRDecoder | Samsung Galaxy S24 | Snapdragon® 8 Gen 3 | TFLITE | 1.554 ms | 0 - 52 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | Samsung Galaxy S24 | Snapdragon® 8 Gen 3 | QNN | 1.796 ms | 0 - 53 MB | FP16 | NPU | [TrOCR.so](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.so) | | TrOCRDecoder | Samsung Galaxy S24 | Snapdragon® 8 Gen 3 | ONNX | 2.453 ms | 0 - 62 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.onnx) | | TrOCRDecoder | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | TFLITE | 1.515 ms | 0 - 47 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | QNN | 1.603 ms | 1 - 49 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | ONNX | 1.733 ms | 1 - 48 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.onnx) | | TrOCRDecoder | QCS8550 (Proxy) | QCS8550 Proxy | TFLITE | 2.227 ms | 0 - 120 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | QCS8550 (Proxy) | QCS8550 Proxy | QNN | 2.279 ms | 2 - 5 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | SA7255P ADP | SA7255P | TFLITE | 12.101 ms | 0 - 44 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | SA7255P ADP | SA7255P | QNN | 12.355 ms | 7 - 15 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | SA8255 (Proxy) | SA8255P Proxy | TFLITE | 2.18 ms | 0 - 364 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | SA8255 (Proxy) | SA8255P Proxy | QNN | 2.29 ms | 2 - 4 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | SA8295P ADP | SA8295P | TFLITE | 3.149 ms | 0 - 44 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | SA8295P ADP | SA8295P | QNN | 3.287 ms | 7 - 21 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | SA8650 (Proxy) | SA8650P Proxy | TFLITE | 2.285 ms | 0 - 348 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | SA8650 (Proxy) | SA8650P Proxy | QNN | 2.288 ms | 1 - 4 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | SA8775P ADP | SA8775P | TFLITE | 3.394 ms | 0 - 44 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | SA8775P ADP | SA8775P | QNN | 3.511 ms | 7 - 17 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | QCS8450 (Proxy) | QCS8450 Proxy | TFLITE | 2.702 ms | 0 - 47 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.tflite) | | TrOCRDecoder | QCS8450 (Proxy) | QCS8450 Proxy | QNN | 2.793 ms | 5 - 54 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | Snapdragon X Elite CRD | Snapdragon® X Elite | QNN | 2.483 ms | 7 - 7 MB | FP16 | NPU | Use Export Script | | TrOCRDecoder | Snapdragon X Elite CRD | Snapdragon® X Elite | ONNX | 2.688 ms | 68 - 68 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCRDecoder.onnx) | | TrOCREncoder | Samsung Galaxy S23 | Snapdragon® 8 Gen 2 | TFLITE | 50.556 ms | 7 - 31 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | Samsung Galaxy S23 | Snapdragon® 8 Gen 2 | QNN | 52.318 ms | 2 - 21 MB | FP16 | NPU | [TrOCR.so](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.so) | | TrOCREncoder | Samsung Galaxy S23 | Snapdragon® 8 Gen 2 | ONNX | 37.972 ms | 14 - 158 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.onnx) | | TrOCREncoder | Samsung Galaxy S24 | Snapdragon® 8 Gen 3 | TFLITE | 39.483 ms | 6 - 69 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | Samsung Galaxy S24 | Snapdragon® 8 Gen 3 | QNN | 40.727 ms | 2 - 65 MB | FP16 | NPU | [TrOCR.so](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.so) | | TrOCREncoder | Samsung Galaxy S24 | Snapdragon® 8 Gen 3 | ONNX | 32.375 ms | 16 - 77 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.onnx) | | TrOCREncoder | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | TFLITE | 35.24 ms | 5 - 69 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | QNN | 33.856 ms | 2 - 67 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | Snapdragon 8 Elite QRD | Snapdragon® 8 Elite | ONNX | 27.536 ms | 16 - 78 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.onnx) | | TrOCREncoder | QCS8550 (Proxy) | QCS8550 Proxy | TFLITE | 50.422 ms | 7 - 31 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | QCS8550 (Proxy) | QCS8550 Proxy | QNN | 37.115 ms | 2 - 5 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | SA7255P ADP | SA7255P | TFLITE | 266.414 ms | 7 - 69 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | SA7255P ADP | SA7255P | QNN | 247.903 ms | 2 - 11 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | SA8255 (Proxy) | SA8255P Proxy | TFLITE | 50.495 ms | 7 - 28 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | SA8255 (Proxy) | SA8255P Proxy | QNN | 37.4 ms | 2 - 4 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | SA8295P ADP | SA8295P | TFLITE | 65.319 ms | 4 - 64 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | SA8295P ADP | SA8295P | QNN | 51.358 ms | 2 - 16 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | SA8650 (Proxy) | SA8650P Proxy | TFLITE | 50.622 ms | 7 - 34 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | SA8650 (Proxy) | SA8650P Proxy | QNN | 37.215 ms | 2 - 5 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | SA8775P ADP | SA8775P | TFLITE | 59.798 ms | 7 - 69 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | SA8775P ADP | SA8775P | QNN | 42.33 ms | 2 - 12 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | QCS8450 (Proxy) | QCS8450 Proxy | TFLITE | 60.378 ms | 7 - 67 MB | FP16 | NPU | [TrOCR.tflite](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.tflite) | | TrOCREncoder | QCS8450 (Proxy) | QCS8450 Proxy | QNN | 64.062 ms | 2 - 64 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | Snapdragon X Elite CRD | Snapdragon® X Elite | QNN | 34.073 ms | 2 - 2 MB | FP16 | NPU | Use Export Script | | TrOCREncoder | Snapdragon X Elite CRD | Snapdragon® X Elite | ONNX | 36.772 ms | 51 - 51 MB | FP16 | NPU | [TrOCR.onnx](https://huggingface.co/qualcomm/TrOCR/blob/main/TrOCREncoder.onnx) | ## Installation Install the package via pip: ```bash pip install "qai-hub-models[trocr]" ``` ## Configure Qualcomm® AI Hub to run this model on a cloud-hosted device Sign-in to [Qualcomm® AI Hub](https://app.aihub.qualcomm.com/) with your Qualcomm® ID. Once signed in navigate to `Account -> Settings -> API Token`. With this API token, you can configure your client to run models on the cloud hosted devices. ```bash qai-hub configure --api_token API_TOKEN ``` Navigate to [docs](https://app.aihub.qualcomm.com/docs/) for more information. ## Demo off target The package contains a simple end-to-end demo that downloads pre-trained weights and runs this model on a sample input. ```bash python -m qai_hub_models.models.trocr.demo ``` The above demo runs a reference implementation of pre-processing, model inference, and post processing. **NOTE**: If you want running in a Jupyter Notebook or Google Colab like environment, please add the following to your cell (instead of the above). ``` %run -m qai_hub_models.models.trocr.demo ``` ### Run model on a cloud-hosted device In addition to the demo, you can also run the model on a cloud-hosted Qualcomm® device. This script does the following: * Performance check on-device on a cloud-hosted device * Downloads compiled assets that can be deployed on-device for Android. * Accuracy check between PyTorch and on-device outputs. ```bash python -m qai_hub_models.models.trocr.export ``` ``` Profiling Results ------------------------------------------------------------ TrOCRDecoder Device : Samsung Galaxy S23 (13) Runtime : TFLITE Estimated inference time (ms) : 2.2 Estimated peak memory usage (MB): [0, 351] Total # Ops : 399 Compute Unit(s) : NPU (399 ops) ------------------------------------------------------------ TrOCREncoder Device : Samsung Galaxy S23 (13) Runtime : TFLITE Estimated inference time (ms) : 50.6 Estimated peak memory usage (MB): [7, 31] Total # Ops : 591 Compute Unit(s) : NPU (591 ops) ``` ## How does this work? This [export script](https://aihub.qualcomm.com/models/trocr/qai_hub_models/models/TrOCR/export.py) leverages [Qualcomm® AI Hub](https://aihub.qualcomm.com/) to optimize, validate, and deploy this model on-device. Lets go through each step below in detail: Step 1: **Compile model for on-device deployment** To compile a PyTorch model for on-device deployment, we first trace the model in memory using the `jit.trace` and then call the `submit_compile_job` API. ```python import torch import qai_hub as hub from qai_hub_models.models.trocr import Model # Load the model model = Model.from_pretrained() decoder_model = model.decoder encoder_model = model.encoder # Device device = hub.Device("Samsung Galaxy S23") # Trace model decoder_input_shape = decoder_model.get_input_spec() decoder_sample_inputs = decoder_model.sample_inputs() traced_decoder_model = torch.jit.trace(decoder_model, [torch.tensor(data[0]) for _, data in decoder_sample_inputs.items()]) # Compile model on a specific device decoder_compile_job = hub.submit_compile_job( model=traced_decoder_model , device=device, input_specs=decoder_model.get_input_spec(), ) # Get target model to run on-device decoder_target_model = decoder_compile_job.get_target_model() # Trace model encoder_input_shape = encoder_model.get_input_spec() encoder_sample_inputs = encoder_model.sample_inputs() traced_encoder_model = torch.jit.trace(encoder_model, [torch.tensor(data[0]) for _, data in encoder_sample_inputs.items()]) # Compile model on a specific device encoder_compile_job = hub.submit_compile_job( model=traced_encoder_model , device=device, input_specs=encoder_model.get_input_spec(), ) # Get target model to run on-device encoder_target_model = encoder_compile_job.get_target_model() ``` Step 2: **Performance profiling on cloud-hosted device** After compiling models from step 1. Models can be profiled model on-device using the `target_model`. Note that this scripts runs the model on a device automatically provisioned in the cloud. Once the job is submitted, you can navigate to a provided job URL to view a variety of on-device performance metrics. ```python decoder_profile_job = hub.submit_profile_job( model=decoder_target_model, device=device, ) encoder_profile_job = hub.submit_profile_job( model=encoder_target_model, device=device, ) ``` Step 3: **Verify on-device accuracy** To verify the accuracy of the model on-device, you can run on-device inference on sample input data on the same cloud hosted device. ```python decoder_input_data = decoder_model.sample_inputs() decoder_inference_job = hub.submit_inference_job( model=decoder_target_model, device=device, inputs=decoder_input_data, ) decoder_inference_job.download_output_data() encoder_input_data = encoder_model.sample_inputs() encoder_inference_job = hub.submit_inference_job( model=encoder_target_model, device=device, inputs=encoder_input_data, ) encoder_inference_job.download_output_data() ``` With the output of the model, you can compute like PSNR, relative errors or spot check the output with expected output. **Note**: This on-device profiling and inference requires access to Qualcomm® AI Hub. [Sign up for access](https://myaccount.qualcomm.com/signup). ## Deploying compiled model to Android The models can be deployed using multiple runtimes: - TensorFlow Lite (`.tflite` export): [This tutorial](https://www.tensorflow.org/lite/android/quickstart) provides a guide to deploy the .tflite model in an Android application. - QNN (`.so` export ): This [sample app](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/sample_app.html) provides instructions on how to use the `.so` shared library in an Android application. ## View on Qualcomm® AI Hub Get more details on TrOCR's performance across various devices [here](https://aihub.qualcomm.com/models/trocr). Explore all available models on [Qualcomm® AI Hub](https://aihub.qualcomm.com/) ## License * The license for the original implementation of TrOCR can be found [here](https://github.com/microsoft/unilm/blob/master/LICENSE). * The license for the compiled assets for on-device deployment can be found [here](https://qaihub-public-assets.s3.us-west-2.amazonaws.com/qai-hub-models/Qualcomm+AI+Hub+Proprietary+License.pdf) ## References * [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) * [Source Model Implementation](https://huggingface.co/microsoft/trocr-small-stage1) ## Community * Join [our AI Hub Slack community](https://aihub.qualcomm.com/community/slack) to collaborate, post questions and learn more about on-device AI. * For questions or feedback please [reach out to us](mailto:ai-hub-support@qti.qualcomm.com).