Training vs. Inference Decoupling
Relevant source files
The following files were used as context for generating this wiki page:
The mbbank-capcha-ocr project implements a strict decoupling between the model training environment and the production inference environment. This separation allows the training phase to utilize heavy-weight frameworks and hardware acceleration, while the inference phase remains lightweight and portable by using a cross-platform model format.
The project is structured into two primary directories: train_model/ for development and training, and mb_capcha_ocr/ for deployment and prediction. This architecture ensures that users of the OCR library do not need to install complex training dependencies like PyTorch or Matplotlib to perform character recognition.
Sources: README.md:7-10, setup.py:24-27
Architecture Overview
The system transitions from a dynamic training state to a static inference state via the ONNX (Open Neural Network Exchange) format.
The diagram shows the lifecycle of the model from training in PyTorch to deployment in ONNX Runtime.
Sources: train_model/train.py:59, mb_capcha_ocr/predict.py:16, README.md:12-32
Training Environment
The training environment is built on PyTorch and is designed for high-performance model optimization. It includes custom dataset handling and a ResNet-18 based architecture adapted for multi-label classification.
Training Components
- Dataset Management: The
DatasetLoaderclass handles image loading, label encoding (one-hot), and data splitting (90% train, 10% test). - Model Architecture: Uses a modified
resnet18where the first convolutional layer and the final fully connected layer are adjusted for grayscale CAPTCHA images (1x50x160) and a fixed output size (6 characters * character set length). - Optimization: Employs
MultiLabelSoftMarginLossand theAdamoptimizer.
Sources: train_model/core.py:44-90, train_model/train.py:44-46
Training Dependencies
The training phase requires a comprehensive stack of data science libraries:
| Dependency | Purpose |
|---|---|
torch | Core deep learning framework |
torchvision | Image transformations and pre-trained ResNet-18 |
matplotlib | Plotting training loss curves |
onnx | Model export functionality |
Sources: train_model/train_requirements.txt:1-7
Inference Environment
The inference environment is optimized for speed and minimal dependency overhead. It utilizes onnxruntime to execute the pre-compiled model.
Prediction Logic
Inference involves several preprocessing steps to ensure input consistency with the trained model:
- Grayscale Conversion: Image converted to "L" mode.
- Resizing: Image forced to 160x50 pixels.
- Normalization: Pixel values scaled to
[0, 1]. - Dimension Expansion: Reshaping to
(1, 1, 50, 160)to match the expected ONNX input tensor.
Sources: mb_capcha_ocr/predict.py:20-27
Inference Dependencies
The production environment requires significantly fewer resources:
| Dependency | Purpose |
|---|---|
onnxruntime | Efficient model execution |
Pillow | Basic image loading and manipulation |
numpy | Numerical operations on image arrays |
Sources: requirements.txt:1-3
Decoupling Mechanism: ONNX Export
The bridge between training and inference is the torch.onnx.export call. This process serializes the PyTorch model weights and computational graph into a static model.onnx file.
# train_model/train.py lines 58-59
torch_input = torch.randn(1, 1, 50, 160).to(device)
torch.onnx.export(model, (torch_input,), f"modal.onnx", input_names=['input'], output_names=['output'])
Sources: train_model/train.py:58-59
Comparison of Model Handlers
| Feature | Training (train_model/core.py) | Inference (mb_capcha_ocr/predict.py) |
|---|---|---|
| Class Name | OcrModel (inherits torch.nn.Module) | OcrModel (standalone class) |
| Engine | PyTorch | ONNX Runtime |
| Input Type | Torch Tensors | PIL Image |
| Data Processing | torchvision.transforms | numpy / PIL manual processing |
Sources: train_model/core.py:82, mb_capcha_ocr/predict.py:11
Conclusion
By decoupling training from inference, the project achieves a separation of concerns where the heavy training infrastructure (PyTorch, GPU drivers, plotting tools) is isolated from the lightweight consumption layer. This allows the mb_capcha_ocr package to be distributed as a standard Python library with a small footprint, while still leveraging advanced deep learning architectures for CAPTCHA recognition.
Sources: setup.py:24-34, README.md:34-45