Inference Environment Setup
Relevant source files
The following files were used as context for generating this wiki page:
The Inference Environment Setup defines the necessary dependencies, configurations, and structures required to execute the Optical Character Recognition (OCR) model for predicting text from CAPTCHA images. This environment is distinct from the training environment, focusing on lightweight execution using the ONNX Runtime for high performance and portability.
The system is encapsulated within the mb_capcha_ocr package, which provides a high-level API for loading a pre-trained model.onnx file and performing inference on image data. This setup ensures that the model can be integrated into larger Python applications with minimal overhead.
Sources: README.md:1-7, setup.py:18-22, mb_capcha_ocr/predict.py:12-16
Runtime Dependencies
To establish a functional inference environment, specific Python packages must be installed. These dependencies handle image processing, numerical array manipulations, and the execution of the serialized ONNX model.
| Dependency | Purpose |
|---|---|
onnxruntime | Core engine for executing the trained .onnx model. |
Pillow | Used for image loading, conversion (L mode), and resizing. |
numpy | Manages tensor transformations and numerical operations on image data. |
setuptools/wheel | Required for package installation and distribution. |
Sources: requirements.txt:1-5, README.md:46-50, setup.py:5-6
Package Architecture and Components
The inference logic is organized as a modular Python package. The structure ensures that the model file is bundled with the code, allowing for immediate use after installation.
Component Structure
mb_capcha_ocr/__init__.py: Exposes theOcrModelclass for external use and defines the library version.mb_capcha_ocr/predict.py: Contains theOcrModelclass which encapsulates the ONNX session and thepredictlogic.mb_capcha_ocr/model.onnx: The serialized model weights included in the package data.
The following diagram illustrates the relationship between the package components and the external interface:
Inference Logic and Data Flow
The environment expects image data to be processed through a specific pipeline before being passed to the ONNX Runtime session.
Prediction Workflow
- Image Preprocessing: The input image (typically from
PIL.Image) is converted to grayscale ("L" mode) and resized to $160 \times 50$ pixels. - Normalization: Pixel values are scaled to a range of $[0.0, 1.0]$ by dividing by $255.0$.
- Tensor Shaping: The data is reshaped into a 4D tensor with dimensions
(1, 1, 50, 160)to match the model's expected input. - ONNX Execution: The
onnxruntime.InferenceSessionruns the forward pass. - Post-processing: The output tensor is processed using
argmaxacross the character dimension, and the resulting indices are mapped back to characters using a sorted list of alphanumeric characters.
The following sequence diagram details the flow of a single prediction request:
Configuration and Customization
The inference environment allows for custom model paths if the default bundled model is not used.
model_path: An optional string passed to theOcrModelconstructor. IfNone, the environment defaults tomb_capcha_ocr/model.onnxrelative to the installation directory.chars: The character set is hardcoded as a sorted list ofstring.digits + string.ascii_letters.
Setup Snippet
To initialize the environment in a script:
from PIL import Image
from mb_capcha_ocr import OcrModel
# Initializes session with default model.onnx
model = OcrModel()
img = Image.open("captcha.png")
text = model.predict(img)
Sources: mb_capcha_ocr/predict.py:14-19, README.md:36-43
The inference environment provides a streamlined, dependency-minimal setup for deploying the MBBank CAPTCHA OCR model, leveraging ONNX for efficient cross-platform character recognition.