0
0
PyTorchml~5 mins

YOLO concept in PyTorch

Choose your learning style9 modes available
Introduction

YOLO helps computers find and recognize many objects in pictures quickly and all at once.

When you want to detect multiple objects in a photo or video in real-time, like spotting cars on the road.
When building a security camera system that alerts you if a person or animal appears.
When creating apps that need to understand scenes, like counting people in a room.
When you want fast object detection on devices like phones or drones.
When you need to track objects moving in videos, such as players in a sports game.
Syntax
PyTorch
import torch
from ultralytics import YOLO

model = YOLO("yolov8n.pt")

outputs = model(images)

YOLO models take images and output boxes with object labels and confidence scores.

Pretrained models are ready to detect common objects without extra training.

Examples
Load a pretrained YOLOv8 model and run it on one image tensor.
PyTorch
model = YOLO("yolov8n.pt")
outputs = model([image_tensor])
Run YOLOv8 on a batch of two images for faster processing.
PyTorch
model = YOLO("yolov8n.pt")
outputs = model([image1, image2])
Sample Model

This code loads a sample image, runs YOLOv8 to detect objects, and prints the bounding boxes, labels, and confidence scores.

PyTorch
import torch
from ultralytics import YOLO
from torchvision.transforms import functional as F
from PIL import Image

# Load image and convert to tensor
image = Image.open('sample.jpg').convert('RGB')
image_tensor = F.to_tensor(image)

# Load pretrained YOLOv8 model
model = YOLO("yolov8n.pt")

# Run model on image
with torch.no_grad():
    outputs = model([image_tensor])

# Extract detections
boxes = outputs[0].boxes.xyxy.cpu()
labels = outputs[0].boxes.cls.cpu()
scores = outputs[0].boxes.conf.cpu()

# Print detected objects and scores
for box, label, score in zip(boxes, labels, scores):
    print(f"Box coordinates: {box.tolist()}")
    print(f"Label: {int(label.item())}, Confidence: {score.item():.2f}")
OutputSuccess
Important Notes

YOLO stands for 'You Only Look Once' because it detects all objects in one pass.

YOLO models are fast and good for real-time applications.

Labels are numbers that map to object names like 'person' or 'car'.

Summary

YOLO detects many objects in images quickly and all at once.

It outputs boxes, labels, and confidence scores for each object.

Pretrained YOLO models can be used easily with PyTorch for fast object detection.