Build a Computer Vision Pipeline with Detectron2, MMDetection
A computer vision pipeline turns raw images into detections a system can act on. Here is how to build one with Detectron2 or MMDetection, from data to production.
On this page
Short answer
A computer vision pipeline turns raw images into detections through four stages: data preparation, model training, evaluation and inference. Detectron2 and MMDetection both provide pretrained detectors and training loops for this work; the choice depends on whether your team prefers code-first customisation or config-first model comparison.
What is a computer vision pipeline?
A computer vision pipeline is a sequence of stages that turns raw images or video into a decision a system can act on. Images are collected and labelled, a model is trained to find what matters in them, and the trained model runs on new images to produce detections, masks or counts that feed a product or a workflow.
The stages are usually data preparation, model training, evaluation and inference. Each stage has its own tools and failure modes, and a weakness in one stage shows up as a problem in the next: poor labels produce a model that cannot learn, and a model that is never evaluated on real data fails quietly in production.
Detectron2 and MMDetection are the two most widely used open source frameworks for building such a pipeline when the task is detection or segmentation. Both ship with pretrained models, standard datasets and training loops, so a team can start from a working baseline instead of writing the whole pipeline from scratch. The choice between them shapes the config style and the model zoo, but the pipeline stages stay the same.
Detectron2 or MMDetection: which fits your project?
Detectron2 and MMDetection solve the same problem in different ways, so the choice comes down to how your team likes to work and how much you plan to customise the model.
| Aspect | Detectron2 | MMDetection |
|---|---|---|
| Origin | Meta AI Research, PyTorch based | OpenMMLab, PyTorch based |
| Configuration | Python config files and code | Python config files, modular parts |
| Model range | Faster R-CNN, Mask R-CNN, RetinaNet | Dozens of detectors in one toolbox |
| Customisation | Subclass and override in code | Swap registry components by name |
| Best for | Research-style projects, custom models | Comparing many models quickly |
| Learning curve | Steeper, code-first | Moderate, config-first |
Pick Detectron2 when the team is comfortable reading framework code and plans to change the model itself. Pick MMDetection when the team wants to try many published detectors on the same dataset and compare them with as little code as possible.
Both can train the same standard architectures, and both read the COCO annotation format, so switching later is work but not a rewrite. For a comparison of the wider field, see best open source computer vision platforms.
How do you prepare the dataset?
A detection pipeline is only as good as its dataset, and dataset preparation usually takes more time than training. The work is collecting images, labelling them consistently, splitting them into train and test sets, and fixing the problems the labels reveal.
Labels for detection are boxes around each object, plus a class name; for segmentation they are pixel masks. The standard format is COCO JSON, which both frameworks read directly. Label with a written guide that defines each class, the edge cases and what to do when an object is partly hidden, because two labellers who disagree produce a model that cannot learn a clean boundary.
Split the data by scene or by day, not at random, so the test set contains images the model has never seen in any form. Check the class balance: if one class has 10,000 examples and another has 200, the model will favour the common class unless you balance the sampling or add more examples of the rare one.
Augmentation stretches a small dataset further. Flips, crops, brightness changes and small rotations teach the model that an object is the same object under different conditions. Augmentation helps, but it cannot replace real examples of the hard cases, such as objects in poor light or partly hidden behind other objects.
How do you configure and train the model?
You configure and train the model in a short sequence: pick a starting config, point it at your data, set the training schedule, then train and watch the loss.
- Install the framework and a GPU build of PyTorch. Detection training on a CPU is too slow for real datasets; a single GPU is enough for a first pipeline.
- Pick a pretrained config. Start from a config that matches your task, such as Faster R-CNN with a ResNet backbone, and load its pretrained weights.
- Register your dataset. Point the framework at your COCO files, set the class names and confirm the number of images and boxes it sees.
- Set the schedule. Choose a learning rate, a batch size that fits the GPU memory and a number of iterations, then train in short runs while you tune.
- Watch the loss and the samples. A falling loss with rising validation accuracy means the model is learning; a loss that stalls early usually means the learning rate is wrong.
- Save checkpoints. Keep the best checkpoint by validation score, not the last one, so you can roll back if a later run gets worse.
Fine-tuning from pretrained weights needs far fewer images than training from scratch. A few hundred labelled images per class can be enough for a narrow task, though more data helps the model handle unusual cases. How this fits into a wider vision project is covered in how to implement computer vision.
How do you evaluate the pipeline?
You evaluate a detection pipeline with measures that separate finding objects from finding them correctly. The headline measure is mean average precision, usually written mAP, which averages precision across classes and overlap thresholds.
Precision answers how many of the model's detections were real objects, while recall answers how many real objects the model found. A model can have high precision and low recall if it only reports the easy cases, or high recall and low precision if it reports everything. Report both, per class, because a single average hides a class the model handles badly.
Look at the errors, not just the scores. Sort the test images by the model's mistakes and group them: missed small objects, confused classes, double detections on one object, false alarms on background. Each group points at a different fix, such as more training data for a class, a higher resolution input, or a different overlap threshold.
Also measure speed. A model that scores well but processes two frames a second is useless for a live camera feed, so record frames per second on the hardware you will actually use, alongside the accuracy numbers.
How do you run inference in production?
Running inference in production means turning the trained model into a service that takes images, returns detections and keeps working when the input changes. The simplest setup is a small API that loads the model once and scores each image on demand.
Batch images where you can, because a GPU works far more efficiently on a batch than on single images. For video, process every second or third frame and track objects between frames instead of detecting from scratch each time. Export the model to a faster format, such as TorchScript or ONNX, when latency matters, and test the exported model against the original to confirm the scores match.
Monitor the pipeline after launch. Log a sample of inputs and outputs, watch for drift in the image distribution, and set an alert for a sudden rise in low-confidence detections, which usually means the camera moved, the lighting changed or a new object type appeared. Keep a human review path for the cases the model is unsure about, and use those reviewed cases as new training data.
The infrastructure around the model matters as much as the model. Version the weights, the config and the dataset together, so any result can be traced back to the exact inputs that produced it. Computer vision development for production systems usually pairs the model with this kind of versioning and monitoring from the start, which is where computer vision development support helps most.
Example (hypothetical): a warehouse damage-check pipeline
Example (hypothetical): a warehouse team builds a pipeline to spot damaged boxes on a conveyor. It labels 900 photos a day for two weeks, trains a Faster R-CNN in Detectron2, and tests it on 900 fresh photos that contain 120 damaged boxes.
The model flags 120 boxes as damaged. Of those, 96 are truly damaged, so its precision is 96 out of 120 flagged boxes, or 80 in every 100. It misses 24 damaged boxes, so it finds 96 of the 120 damaged boxes in total, which is a recall of 80 in every 100 as well.
Checking the 120 flagged boxes by hand takes 120 times 3 minutes, or 6 hours. Checking all 900 photos the same way would take 900 times 3 minutes, or 45 hours. The pipeline saves 39 hours a day of manual checking while catching 4 in 5 damaged boxes, and the 24 it misses are the reason a human still samples the unflagged photos each shift.
What are the common mistakes?
The most common computer vision pipeline mistakes are labelling without a guide, splitting data at random, and shipping a model that was never measured on real images.
- No labelling guide. Labellers who guess at edge cases produce inconsistent boxes, and the model learns the inconsistency.
- Random splits. Splitting by image instead of by scene lets near-identical images land in both train and test, which inflates the score.
- Training on clean data only. A model trained on bright, centred photos fails on the dark, tilted images the camera actually produces.
- Judging by one number. A good mAP can hide a class the model never finds. Always check per-class precision and recall.
- No speed test. A model that is accurate but too slow for the camera feed is not a working pipeline.
- No monitoring. Cameras move, seasons change and new objects appear. A pipeline with no drift checks degrades silently.
Teams that avoid these mistakes usually find the framework choice matters less than the data and the evaluation. Detectron2 and MMDetection both work well; the dataset, the split and the error analysis decide whether the pipeline survives production. For the techniques behind the models themselves, computer vision techniques covers the main approaches.
Key takeaways
- A pipeline has four stages: data, training, evaluation and inference, and a weak stage shows up in the next one
- Detectron2 suits code-first customisation; MMDetection suits comparing many detectors by config
- Label with a written guide and split by scene, not at random, or the test score lies
- Judge a detector by per-class precision and recall plus speed, not by one average score
- Version weights, config and dataset together and monitor for drift once the pipeline is live
Frequently asked questions
Do I need a GPU to build a computer vision pipeline?
For training, yes in practice. Detection training on a CPU is too slow for real datasets, though a single GPU is enough for a first pipeline. Inference can run on a CPU for small images or low frame rates, but a GPU is usually needed for live video.
How many labelled images do I need?
A few hundred labelled images per class can be enough when you fine-tune from pretrained weights on a narrow task. More data helps with unusual cases such as poor light or partly hidden objects, and the label quality matters more than the raw count.
Can I use Detectron2 and MMDetection in the same project?
Yes, since both read COCO annotations and train similar architectures. Teams sometimes prototype in MMDetection to compare models, then implement the chosen model in Detectron2 for customisation. Keeping one annotation format makes that switch far easier.
What is mAP in detection evaluation?
Mean average precision averages precision across classes and across overlap thresholds between predicted and true boxes. It is the standard headline measure for detectors, but it should always be reported alongside per-class precision and recall so weak classes are visible.
How do I handle class imbalance in detection data?
Balance the sampling so rare classes appear more often during training, add more real examples of the rare class, or use augmentation on those examples. Check per-class recall after each change, since a single average score hides whether the rare class improved.
What format should I export the model in for production?
TorchScript and ONNX are the common choices for serving PyTorch detectors outside training. Both run faster than raw PyTorch in many setups, but always test the exported model against the original on the same images to confirm the scores match.