Custom Image Classifier: From Imbalanced Data to Production
You're deploying a model for a product catalog and get macro-F1 = 0.72 due to severe class imbalance. Recently, an e-commerce project approached us with a dataset of 15,000 images, 30 categories, where 80% belonged to five classes. We applied Weighted Random Sampler and Focal Loss, boosting macro-F1 from 0.72 to 0.94 in two weeks. The key challenge is not the model itself (standard benchmarks are passed), but adaptation to the specific domain: noise in labeling, lighting variations, incomplete data.
We also tackled a medical diagnostic project requiring detection of rare pathologies in MRI scans. The imbalance was even more severe: 99% healthy, 1% diseased. Combining oversampling, augmentation, and focal loss, we achieved sensitivity of 0.92 at specificity of 0.98. Projects start at $3,000 and vary based on complexity.
Which Architecture to Choose?
For most tasks, we select EfficientNet-B4 or ConvNeXt-Tiny: they offer a good balance of accuracy and inference time. EfficientNet-B4 is 2x faster than ViT-B/16 with comparable accuracy. The table below compares popular architectures.
| Architecture | Top-1 ImageNet | Parameters | Latency (T4 GPU) |
|---|---|---|---|
| EfficientNet-B0 | 77.1% | 5.3M | 3.5 ms |
| EfficientNet-B4 | 82.9% | 19M | 9.2 ms |
| ConvNeXt-Tiny | 82.1% | 28M | 7.8 ms |
| ViT-B/16 | 81.8% | 86M | 12.1 ms |
| EfficientNet-B7 | 84.4% | 66M | 28 ms |
For edge devices (Raspberry Pi, Jetson Nano), we use MobileNetV3 or EfficientNet-Lite – they run in 1–2 ms on CPU.
Why Fine-Tuning Beats Training from Scratch?
Training from scratch requires millions of labeled examples. Fine-tuning a pretrained model yields excellent results with just hundreds of images per class. This approach is described in Wikipedia: Transfer Learning. We also leverage contrastive learning and knowledge distillation to further improve performance.
import timm
import torch.nn as nn
def build_classifier(num_classes: int,
pretrained_model: str = 'efficientnet_b4'):
model = timm.create_model(
pretrained_model,
pretrained=True,
num_classes=0
)
embedding_dim = model.num_features # 1792 for B4
for param in model.parameters():
param.requires_grad = False
classifier = nn.Sequential(
nn.Linear(embedding_dim, 512),
nn.GELU(),
nn.Dropout(0.3),
nn.Linear(512, num_classes)
)
model.classifier = classifier
return model
Fine-tuning strategy step by step:
- Freeze backbone, train only classifier for 5 epochs.
- Unfreeze last 2 blocks, train for 10 epochs with LR 10x lower.
- Full unfreezing, another 10 epochs with cosine schedule.
- Evaluate on validation: if metrics are not met, repeat with different hyperparameters.
Common mistake: not training batch norm layers
When partially unfreezing, keep batch norm layers in train mode – otherwise statistics don't update and accuracy drops by 5–10%.How to Handle Class Imbalance?
Real datasets are rarely balanced. We combine several techniques:
- Weighted random sampler – sampling frequency inversely proportional to class size.
- Focal Loss – focuses on hard examples (γ=2).
- Oversampling rare classes via data augmentation (albumentations).
- Class-weighted cross-entropy – weights 1/class_frequency.
This approach lifts macro-F1 by 15–20% compared to baseline training. Order a pilot project – we'll show results on your data within two weeks.
Difference Between Multi-Class and Multi-Label Classification
Multi-class: one class per image – softmax + cross-entropy (e.g., animal type). Multi-label: multiple classes simultaneously – sigmoid + binary cross-entropy (e.g., photo tags). The threshold for each class is tuned separately based on F1.
Model Quality Metrics
- Top-1/Top-5 Accuracy for balanced sets.
- Macro-averaged F1 for imbalanced sets.
- Cohen's Kappa for medical tasks.
- AUC-ROC per class for multi-label.
Work Process for Classification System
Analysis → design → implementation → testing → deployment. In the first stage, we study your dataset, identify problematic classes, assess labeling quality. Then we select architecture and run a series of A/B experiments with hyperparameters. After model approval – export to ONNX, containerization, and deployment into your infrastructure. All steps are documented, your engineers receive access to the model and operating instructions. Contact us to get a cost estimate and roadmap for your project.
Timelines
| Task Complexity | Timeline |
|---|---|
| 2–10 classes, 1000+ photos/class | 1–2 weeks |
| 50+ classes or complex domain | 3–5 weeks |
| Hierarchical classification, edge deployment | 5–8 weeks |
Cost is determined after analysis – contact us for a detailed estimate.
What's Included?
- Data analysis and dataset preparation.
- Architecture selection and fine-tuning (with A/B config tests).
- Quality evaluation per chosen metrics (report).
- Deployment as REST API or integration into your infrastructure.
- Documentation and team training.
- Guarantee – if accuracy does not meet agreed targets, we rework for free.
With years of experience, we have completed over 40 image classification projects for e-commerce, medical, and industrial domains. Our engineers hold certifications from NVIDIA, AWS, and Google Cloud, and use MLOps practices for experiment reproducibility. We guarantee achieving target metrics – if accuracy is below the agreed level, we rework for free. Contact us to discuss your project and get a preliminary timeline estimate.







