Model Inference Execution
Relevant source files
The following files were used as context for generating this wiki page:
Model Inference Execution refers to the process of using a trained Optical Character Recognition (OCR) model to identify and extract text from CAPTCHA images. In this project, the inference system is designed to be lightweight, utilizing the ONNX Runtime for efficient execution of models trained via PyTorch.
The system encapsulates the complexity of image preprocessing, model loading, and character decoding within a unified interface. This allows developers to pass a raw image and receive the predicted string without needing to manage the underlying tensor operations or character mapping manually.
Sources: README.md:3-6, mb_capcha_ocr/predict.py:12-32
Core Components and Architecture
The inference system relies on a specialized class, OcrModel, which handles the lifecycle of the inference session. It uses a predefined character set and a serialized ONNX model file.
OcrModel Class
The OcrModel class is the primary interface for inference. It maintains a sorted list of alphanumeric characters used to map model outputs (indices) back to human-readable text.
| Component | Description |
|---|---|
chars | A sorted list containing digits and ASCII letters (0-9, a-z, A-Z). |
session | An onnxruntime.InferenceSession that executes the model logic. |
model.onnx | The default serialized model file included in the package. |
Sources: mb_capcha_ocr/predict.py:12-19, setup.py:22-24
Data Flow for Prediction
The following diagram illustrates the sequence of operations from receiving an input image to returning the predicted text string.
The inference flow ensures that the input image matches the exact dimensions and normalization expected by the ResNet-based architecture defined during training.
Sources: mb_capcha_ocr/predict.py:21-32, train_model/core.py:73-77
Implementation Details
Model Loading and Initialization
Upon initialization, the system attempts to load the ONNX model. If no specific path is provided, it defaults to model.onnx located within the package directory. The character set is sorted alphabetically to ensure consistency with the training label encoding.
def __init__(self, model_path: Optional[str]=None):
self.chars.sort()
self.session = onnxruntime.InferenceSession(model_path or f"{dir_path}/model.onnx")
Sources: mb_capcha_ocr/predict.py:17-19
Image Preprocessing
Inference requires specific preprocessing steps to transform a standard image into a 4D float32 tensor compatible with the model:
- Grayscale Conversion: The model expects a single channel (1, 1, H, W).
- Resizing: Images are forced to 160x50 pixels.
- Normalization: Pixel values (0-255) are scaled to a 0.0-1.0 range.
- Dimension Expansion: The array is expanded twice to add the batch size and channel dimensions.
Sources: mb_capcha_ocr/predict.py:22-26, train_model/train.py:25-28
Output Decoding
The model output is a tensor of shape (batch, max_captcha, chars_len). The inference execution applies an argmax operation across the character dimension (axis 2) to find the most probable character index for each position in the CAPTCHA. These indices are then joined using the chars reference list.
Requirements for Execution
To execute inference, the environment must satisfy the base dependencies which differ from the training requirements.
| Dependency | Purpose | File Source |
|---|---|---|
onnxruntime | Core execution engine for .onnx models. | requirements.txt |
Pillow | Image loading and manipulation. | requirements.txt |
numpy | Numerical operations and array reshaping. | requirements.txt |
Sources: requirements.txt:1-3, train_model/train_requirements.txt:1-7
Conclusion
The Model Inference Execution module provides a streamlined path for character recognition by abstracting the ONNX session management and tensor transformations. By utilizing a ResNet18 backbone (modified for single-channel input) and exporting to ONNX, the project achieves a balance between the high-performance training capabilities of PyTorch and the portable, fast execution of ONNX Runtime for production-ready CAPTCHA solving.
Sources: train_model/core.py:73-77, mb_capcha_ocr/predict.py:12-32