YOLO — এক ধাপে detection
এই পাঠে যা শিখবেন
- YOLOv1-এর single-pass detection idea
- YOLO loss function components
- YOLO version evolution
- Ultralytics-এ practical fine-tune
১ · YOLOv1 — single-pass insight
Joseph Redmon (২০১৬, CVPR) — "You Only Look Once: Unified, Real-Time Object Detection". R-CNN-এর প্রায় opposite philosophy.
Idea: ছবিকে $7 \times 7$ grid-এ ভাগ। Each cell — predict $B$ bounding box (typical 2) + class probability.
- Each cell output: $B \times 5 + C$ value.
- 5 = $(x, y, w, h, \text{confidence})$।
- $C$ = class probabilities (PASCAL VOC = 20).
- Total output: $7 \times 7 \times 30 = 1470$।
Detection = pixel-to-grid regression. কোনো proposal, কোনো region pooling — just look at the image once. এটাই YOLO-র revolutionary contribution.
২ · YOLOv1 architecture
- 24 conv layer + 2 FC layer (GoogLeNet-inspired).
- Input 448×448 → output 7×7×30.
- Final FC reshape করে grid output.
- ~63M parameter, 45 FPS.
৩ · YOLO loss function
Multi-component loss — bbox + objectness + class.
$$\mathcal{L} = \lambda_{\text{coord}} \mathcal{L}_{\text{box}} + \mathcal{L}_{\text{obj}} + \lambda_{\text{noobj}} \mathcal{L}_{\text{noobj}} + \mathcal{L}_{\text{cls}}$$
- $\lambda_{\text{coord}} = 5$ — box localization weight high.
- $\lambda_{\text{noobj}} = 0.5$ — empty cell-এ confidence loss less.
- Width/height-এ square root — small box-এ relative error matter.
৪ · YOLO evolution timeline
- YOLOv1 (২০১৬): 7×7 grid, 2 box per cell, FC layer.
- YOLOv2 (২০১৭, YOLO9000): anchor box, batch norm, multi-scale, 9000+ class.
- YOLOv3 (২০১৮): Darknet-53 backbone, FPN-style multi-scale, 3 detection head.
- YOLOv4 (২০২০, Bochkovskiy): CSPDarknet, PAN, mosaic augmentation.
- YOLOv5 (২০২০, Ultralytics): PyTorch-native, easy training.
- YOLOv6 (২০২২, Meituan): industry-focused, RepVGG block.
- YOLOv7 (২০২২): E-ELAN, SOTA.
- YOLOv8 (২০২৩, Ultralytics): anchor-free, modern, segmentation included.
- YOLOv9, v10 (২০২৪): programmable gradient information.
- YOLO11 (২০২৪-২০২৫): latest Ultralytics.
৫ · YOLOv8 architecture (modern)
- Backbone: CSPDarknet — efficient feature extraction.
- Neck: PAN-FPN — multi-scale aggregation.
- Head: decoupled (separate cls + reg branches).
- Anchor-free: v8 থেকে — direct center prediction.
- Variants: n (nano) → x (extra-large).
৬ · YOLOv8 size variants
| Variant | Params | mAP@COCO | FPS (T4) |
|---|---|---|---|
| YOLOv8n | 3.2M | 37.3 | 280 |
| YOLOv8s | 11.2M | 44.9 | 170 |
| YOLOv8m | 25.9M | 50.2 | 90 |
| YOLOv8l | 43.7M | 52.9 | 60 |
| YOLOv8x | 68.2M | 53.9 | 40 |
৭ · Ultralytics — practical YOLO
# Install: pip install ultralytics
from ultralytics import YOLO
# Pretrained YOLOv8 nano
model = YOLO('yolov8n.pt')
# Inference on image
results = model('path/to/image.jpg')
# Or on video
# results = model('video.mp4', save=True)
# Each result
for r in results:
print("Boxes:", r.boxes.xyxy) # (N, 4) tensor
print("Conf:", r.boxes.conf) # (N,)
print("Cls:", r.boxes.cls) # (N,) class index
print("Names:", r.names) # dict {idx: name}
# Train on custom dataset (YOLO format)
# model.train(data='custom.yaml', epochs=100, imgsz=640)
৮ · YOLO custom training
Dataset format (YOLO):
# custom.yaml
path: /path/to/dataset
train: images/train
val: images/val
names:
0: rickshaw
1: cng
2: bus
3: pedestrian
Each label file (one per image, same name):
# images/train/IMG001.txt
0 0.5 0.6 0.2 0.3 # class x_center y_center width height (normalized)
2 0.8 0.5 0.1 0.4
৯ · YOLO-এর strength ও weakness
Strengths:
- Realtime inference.
- End-to-end training simple.
- Mobile-friendly variants.
- Big ecosystem (Ultralytics, Darknet).
Weaknesses:
- Small object detection — historical weakness (improved in v3+).
- Crowded scene — overlap struggle.
- Custom annotation type — flexibility কম।
১০ · Modern competitors
- RT-DETR (২০২৩): realtime DETR, NMS-free, similar speed.
- YOLOX: anchor-free YOLO variant.
- EfficientDet: Google-এর efficient detector.
- D-FINE (২০২৪): latest realtime SOTA.
ভাবনার প্রশ্ন
প্র ০১ YOLOv1-এর paper — Joseph Redmon ২০২০-এ research থেকে retire (military application concern). এই story-র ethical implication?
এটি AI ethics-এর সবচেয়ে personal case study.
Background:
- Joseph Redmon — University of Washington PhD.
- YOLOv1, v2, v3 author.
- Darknet framework creator.
- ২০২০ February — Twitter announcement: stopping CV research.
Reasons cited:
- Military application — drone-based surveillance, autonomous weapon.
- Privacy concerns — face recognition mass deployment.
- "Can't stop people misusing my work".
- YOLO real-time speed makes harmful application practical.
YOLOv4 (২০২০) onwards:
- Different authors took over — Bochkovskiy, Wang.
- YOLOv5 — Ultralytics company.
- Redmon's name not on subsequent versions.
Ethical questions raised:
- Researcher responsibility for downstream use?
- Open-source vs restricted model?
- Dual-use technology (civilian + military)?
- Individual choice vs community progress?
Industry parallels:
- OpenAI not releasing GPT-2 fully (২০১৯) — initially.
- Anthropic Constitutional AI — alignment focus.
- Stable Diffusion controversies — celebrity face generation.
- Face recognition — Boston, San Francisco bans.
Counter-arguments:
- If not Redmon, others would develop.
- Civilian applications save lives (medical, accessibility).
- Open research → safety research enabled.
- Restricted research → undocumented harm.
Bangladesh context:
- YOLO traffic management — civic value.
- Border surveillance — political concern.
- Privacy law immature.
- Researcher voice in policy needed.
Personal ethics framework:
- Dual-use awareness essential.
- Avoid contributing to harmful military/surveillance specifically.
- Civilian-focused application emphasize.
- Public discourse engage.
মূল উপলব্ধি: Researcher = decision maker. Technology not neutral. Redmon's stance — moral leadership-এর rare example. Personal ethics + AI development = inseparable.
প্র ০২ YOLOv1-এ একটি grid cell দু'টি object আছে এমন situation handle কীভাবে? পরবর্তী version-এ কী fix?
এটি YOLOv1-এর fundamental limitation. Famous "two-cat-in-one-grid" problem.
YOLOv1 limitation:
- Each grid cell predict 2 box, but same class.
- Two different class object same cell-এ → only one detected.
- Crowded scene — bird flock — many missed.
- "YOLO weakness" — small densely packed objects.
YOLOv2 fixes:
- Anchor box concept (Faster R-CNN-inspired).
- 5 anchor per cell (different size/ratio).
- Each anchor own class prediction.
- Now 5 different objects/classes per cell possible.
YOLOv3 multi-scale:
- 3 detection head (different feature map scale).
- Small object — high-resolution head.
- Large object — low-resolution head.
- 3 anchor per scale × 3 scale = 9 anchor effective.
YOLOv4-v7 refinements:
- PANet, FPN improvements.
- Attention mechanism integration.
- Better anchor matching (TaskAlignedAssigner).
YOLOv8 anchor-free:
- Each cell predict 1 box directly (no anchor).
- Distance from center — width/height (DFL distribution).
- Crowded scene handling depends on grid density.
Modern crowded-scene approaches:
- Soft-NMS: overlapping detection partially keep.
- CrowdDet, RepPoints: custom head.
- DETR: set prediction natively crowded scene OK.
Bangladesh use case:
- Dhaka rush hour traffic — extremely crowded.
- Vehicle count benchmark — important.
- YOLOv8 + Soft-NMS pragmatic.
- RT-DETR alternative — set prediction better.
Test:
- OBB (Oriented Bounding Box) — rotated objects.
- YOLOv8-OBB exists.
- Aerial images, document scan — useful.
মূল উপলব্ধি: Architecture limitation — gradually solved through evolution. YOLOv1's bug → YOLOv8's solution. ML iteration discipline.
প্র ০৩ Ultralytics YOLOv8 — open source কিন্তু AGPL license. Bangladesh startup-এর জন্য কী মানে? Alternative কী?
Open-source AI-র legal landscape — দেখতে friendly, actually nuanced.
AGPL-3.0 explained:
- Affero GPL — strictest open-source license.
- "Network use" trigger — SaaS-এ source code expose obligation.
- Modification public-এ ছাড়তে হয়।
- Commercial use — যদি আপনার product source release করতে রাজি — free.
- Closed-source SaaS — Ultralytics paid license চাই।
Pricing (Ultralytics):
- Free for: research, education, open-source project.
- Enterprise license — custom pricing (থেকে $1000/year-ও বেশি)।
- Bangladesh startup-এর জন্য — significant cost.
Implications:
- Internal use OK (no service to external user).
- SaaS deployment — license obligation.
- API exposed to customer — likely AGPL trigger.
- Mobile app distribute — ambiguous, often safer to license.
Permissive alternatives:
- YOLOv5 (পুরোনো): historically GPL, community fork MIT exist.
- YOLOX (Apache 2.0): Megvii/Ultralytics-independent.
- YOLO-NAS (Apache 2.0): Deci AI.
- RT-DETR (Apache 2.0): Baidu.
- D-FINE (Apache 2.0): latest.
- torchvision detection (BSD): Faster R-CNN, FCOS.
- MMDetection (Apache 2.0): OpenMMLab — vast collection.
Migration path for startup:
- Prototype: YOLOv8 — quick development.
- Pre-launch: migrate to RT-DETR বা YOLOX.
- Train custom: any architecture, weight your own.
- ONNX deployment: framework-agnostic inference.
Bangladesh-specific:
- Many startup unaware of license.
- Audit existing code.
- Lawyer consult before commercial deploy.
- Government project — Apache/MIT preferable.
Best practice:
- License check first day of selecting library.
- Track license changes (libraries update license).
- Document open-source compliance.
- SBOM (Software Bill of Materials) maintain.
মূল উপলব্ধি: "Open source" ≠ "free for any use". License awareness — engineer-এর responsibility. Bangladesh tech ecosystem — license literacy critical for sustainable business.
প্র ০৪ YOLO mobile-এ deploy করতে quantization, pruning, ONNX. Pipeline কী? Bangladesh-এর low-end Android-এ কোন config?
Mobile YOLO deployment — production engineering-এর core challenge.
Pipeline:
- Train YOLO PyTorch.
- Export ONNX format.
- Quantize INT8 (post-training).
- Convert TFLite/CoreML/NCNN.
- Mobile SDK integrate.
- Benchmark device-specific.
Step 1 — ONNX export:
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
model.export(format='onnx', imgsz=320) # 320 mobile-friendly
Step 2 — Quantization:
- FP32 → INT8 — 4x size reduction, 2-3x speedup.
- Calibration set — 100-1000 representative images.
- Accuracy drop 1-3% typical.
Step 3 — Format conversion:
- TFLite (Android): Google standard, NPU support.
- CoreML (iOS): Apple Neural Engine optimize.
- NCNN (Tencent): ARM CPU + GPU.
- MNN (Alibaba): alternative.
- SNPE (Qualcomm): Hexagon DSP.
Image size choice:
- 640 — desktop default.
- 416 — mobile high-end.
- 320 — mid-range mobile.
- 224 — low-end (accuracy drop).
Model variant for Bangladesh devices:
- iPhone 12+: YOLOv8s, 416, FP16.
- Mid-range Android (Snapdragon 7-series): YOLOv8n, 416, INT8.
- Walton/Symphony low-end: YOLOv8n, 320, INT8 + heavy pruning.
- Embedded (Raspberry Pi 4): YOLOv8n, 320, INT8.
Latency targets:
- Realtime (30 FPS): <33 ms.
- Acceptable interactive: <100 ms.
- Batch processing: any.
Pruning:
- Magnitude pruning — small weight zero.
- Structured pruning — entire channel/filter remove.
- 2-5x speedup possible, 5-10% accuracy cost.
Knowledge distillation:
- Teacher (YOLOv8x) → Student (YOLOv8n).
- Teacher's soft prediction guide student.
- Student accuracy bump (~2-3%).
Bangladesh-specific challenges:
- Battery life — power-efficient inference.
- Background CPU usage tax.
- Network — model download size.
- Update strategy — over-the-air model.
Real example:
- Pathao bike helmet detection — YOLOv8n quantized.
- 10 Minute School OCR — YOLO + Tesseract pipeline.
- Praava Health — medical sign detection mobile.
মূল উপলব্ধি: Mobile deployment — model architecture-এর সমান optimization-এ চাই। Bangladesh device diversity — multi-tier strategy. Engineer-এর full-stack skill — research থেকে edge.
অনুশীলন
-
Output dimension: YOLOv1-এর 7×7 grid, 2 box, 20 class — output shape কী?
$7 \times 7 \times (2 \times 5 + 20) = 7 \times 7 \times 30$।
-
Train YOLOv8: custom Bangla traffic dataset-এ YOLOv8n train, 100 epoch — কোডটি লিখুন।
from ultralytics import YOLO model = YOLO('yolov8n.pt') results = model.train( data='traffic.yaml', epochs=100, imgsz=640, batch=16, device=0, # GPU project='runs/traffic', name='exp1' ) -
ভাবুন: YOLOv8 vs Faster R-CNN — accuracy gap shrunk. YOLO কেন এখনো dominant production-এ?
(1) Speed — realtime essential many use case. (2) Ecosystem — Ultralytics tooling. (3) Mobile-friendly variants. (4) Easy fine-tune. Faster R-CNN — research/medical/specialized.
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ২০ · Anchor box ও NMS পরবর্তী পাঠ Detection-এর foundation building block.
- পাঠ ১৮ · R-CNN family আগের পাঠ Two-stage counterpart.
- পাঠ ৩৩ · Project: realtime detection এগিয়ে YOLO-এর hands-on project.
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।