Computer Vision voi PyTorch nam 2026: CNN, Transfer Learning va Cau hoi Phong van
Computer vision voi PyTorch: xay dung CNN tu dau, ap dung transfer learning voi model pretrained, va chuan bi cho phong van ky thuat.

Computer vision voi PyTorch da tro thanh phuong phap chu dao cho phan loai hinh anh, phat hien doi tuong va cac tac vu nhan dang hinh anh nam 2026. Bai huong dan nay trinh bay cach xay dung CNN tu dau, ap dung transfer learning voi cac model pretrained, va chuan bi cho cac cau hoi phong van ky thuat.
PyTorch 2.4 gioi thieu torch.compile() mac dinh cho cac phep toan CNN, mang lai toc do tang 30-50% tren GPU hien dai ma khong can thay doi ma nguon. Tat ca cac vi du trong bai viet nay tuong thich voi PyTorch 2.4+.
Tim hieu Convolutional Neural Networks cho Phan loai Hinh anh
Convolutional Neural Networks trich xuat cac dac trung phan cap tu hinh anh thong qua cac bo loc co the hoc duoc. Khac voi mang fully connected, CNN bao toan moi quan he khong gian giua cac pixel, khien chung ly tuong cho cac tac vu thi giac. Kien truc bao gom cac lop tich chap phat hien canh va mau, cac lop pooling giam chieu, va cac lop fully connected thuc hien phan loai.
Phep toan tich chap truot mot bo loc nho (thuong la 3x3 hoac 5x5) qua hinh anh dau vao, tinh tich vo huong tai moi vi tri. Qua trinh nay tao ra cac feature map lam noi bat cac mau cu the nhu canh, ket cau hoac hinh dang. Cac lop sau ket hop cac dac trung cap thap nay thanh cac bieu dien phuc tap—khuon mat, doi tuong hoac canh quan.
# cnn_architecture.py
import torch
import torch.nn as nn
class SimpleCNN(nn.Module):
"""Basic CNN for CIFAR-10 classification (32x32 RGB images, 10 classes)"""
def __init__(self, num_classes: int = 10):
super().__init__()
# First conv block: 3 input channels (RGB) -> 32 feature maps
self.conv1 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1), # Output: 32x32x32
nn.BatchNorm2d(32), # Normalize activations for stable training
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2) # Output: 16x16x32
)
# Second conv block: increase depth for richer features
self.conv2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1), # Output: 16x16x64
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2) # Output: 8x8x64
)
# Third conv block: capture high-level patterns
self.conv3 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1), # Output: 8x8x128
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2) # Output: 4x4x128
)
# Classifier head
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(128 * 4 * 4, 256),
nn.ReLU(inplace=True),
nn.Dropout(0.5), # Prevent overfitting
nn.Linear(256, num_classes)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
return self.classifier(x)Kien truc nay tang dan so luong bo loc (32 → 64 → 128) dong thoi giam kich thuoc khong gian thong qua pooling. BatchNorm on dinh qua trinh huan luyen bang cach chuan hoa dau ra cua lop, trong khi Dropout ngan classifier hoc thuoc cac mau huan luyen.
Huan luyen CNN voi PyTorch DataLoaders
Tai du lieu hieu qua rat quan trong de tan dung GPU trong qua trinh huan luyen. DataLoader cua PyTorch xu ly batching, shuffling va tai du lieu song song tu dong. Tang cuong du lieu phu hop—random crop, flip va color jitter—cai thien dang ke kha nang tong quat hoa tren hinh anh chua tung thay.
# train_cnn.py
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
# Data augmentation for training (reduces overfitting)
train_transform = transforms.Compose([
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomCrop(32, padding=4), # Random crop with padding
transforms.ColorJitter(brightness=0.2, contrast=0.2),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), # CIFAR-10 mean
(0.2470, 0.2435, 0.2616)) # CIFAR-10 std
])
# No augmentation for validation (deterministic evaluation)
val_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2470, 0.2435, 0.2616))
])
def train_model(model: nn.Module, epochs: int = 20) -> dict:
"""Train CNN with standard best practices"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
# Load CIFAR-10 dataset
train_data = datasets.CIFAR10(root="./data", train=True,
download=True, transform=train_transform)
val_data = datasets.CIFAR10(root="./data", train=False,
transform=val_transform)
# DataLoaders with num_workers for parallel loading
train_loader = DataLoader(train_data, batch_size=128, shuffle=True,
num_workers=4, pin_memory=True)
val_loader = DataLoader(val_data, batch_size=256, shuffle=False,
num_workers=4, pin_memory=True)
criterion = nn.CrossEntropyLoss()
optimizer = optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4)
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs)
best_acc = 0.0
for epoch in range(epochs):
model.train()
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
scheduler.step()
# Validation
model.eval()
correct, total = 0, 0
with torch.no_grad():
for images, labels in val_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = outputs.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
acc = 100. * correct / total
best_acc = max(best_acc, acc)
print(f"Epoch {epoch+1}/{epochs} - Val Acc: {acc:.2f}%")
return {"best_accuracy": best_acc}Cac toi uu hoa huan luyen chinh bao gom: pin_memory=True de chuyen du lieu CPU-sang-GPU nhanh hon, num_workers de tai du lieu song song, optimizer AdamW voi weight decay de dieu chinh, va lich trinh learning rate cosine annealing de hoi tu muot ma.
Transfer Learning voi Pretrained ResNet va EfficientNet
Transfer learning tan dung cac model da duoc huan luyen tren ImageNet (1.2 trieu hinh anh, 1000 lop) va thich ung chung voi tap du lieu tuy chinh. Phuong phap nay dat duoc do chinh xac cao hon voi it du lieu huan luyen va thoi gian tinh toan hon. torchvision.models cua PyTorch cung cap cac kien truc tien tien nhat voi trong so pretrained.
Phuong phap tieu chuan dong bang cac lop tich chap ban dau (phat hien cac dac trung pho bien nhu canh) va tinh chinh cac lop sau cung them phan dau phan loai moi cho tac vu muc tieu.
# transfer_learning.py
import torch
import torch.nn as nn
from torchvision import models
from torchvision.models import ResNet50_Weights, EfficientNet_B0_Weights
def create_transfer_model(
model_name: str = "resnet50",
num_classes: int = 10,
freeze_backbone: bool = True
) -> nn.Module:
"""Create a pretrained model with custom classification head"""
if model_name == "resnet50":
# Load ResNet50 with ImageNet weights
model = models.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
# Replace final fully connected layer
in_features = model.fc.in_features # 2048 for ResNet50
model.fc = nn.Sequential(
nn.Dropout(0.3),
nn.Linear(in_features, num_classes)
)
elif model_name == "efficientnet_b0":
# EfficientNet: better accuracy/compute tradeoff
model = models.efficientnet_b0(weights=EfficientNet_B0_Weights.IMAGENET1K_V1)
in_features = model.classifier[1].in_features # 1280 for B0
model.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(in_features, num_classes)
)
if freeze_backbone:
# Freeze all layers except classifier
for name, param in model.named_parameters():
if "fc" not in name and "classifier" not in name:
param.requires_grad = False
return model
def count_trainable_params(model: nn.Module) -> int:
"""Count parameters that will be updated during training"""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
# Example usage
model = create_transfer_model("resnet50", num_classes=5, freeze_backbone=True)
print(f"Trainable parameters: {count_trainable_params(model):,}")
# Output: ~10,245 (only classifier) vs ~25 million (full ResNet50)Dong bang backbone giam cac tham so co the huan luyen tu 25 trieu xuong khoang 10.000, cho phep huan luyen tren tap du lieu nho ma khong bi overfitting. Voi tap du lieu lon hon (10.000+ hinh anh), dan dan mo dong cac lop sau su dung differential learning rates—ty le thap hon cho cac lop pretrained, ty le cao hon cho classifier moi.
Sẵn sàng chinh phục phỏng vấn Data Science & ML?
Luyện tập với mô phỏng tương tác, flashcards và bài kiểm tra kỹ thuật.
Chien luoc Tang cuong Du lieu cho Computer Vision
Tang cuong du lieu mo rong nhan tao tap huan luyen bang cach ap dung cac phep bien doi bao toan noi dung ngu nghia. Tang cuong hien dai vuot ra ngoai flip va xoay co ban—cac ky thuat nhu MixUp va CutOut tao cac vi du huan luyen tong hop giup cai thien do ben cua model.
# augmentation_strategies.py
import torch
import torchvision.transforms.v2 as T
from torchvision.transforms.v2 import functional as F
# Modern augmentation pipeline using torchvision v2 transforms
advanced_augmentation = T.Compose([
T.RandomResizedCrop(224, scale=(0.8, 1.0)), # Random crop and resize
T.RandomHorizontalFlip(p=0.5),
T.RandomVerticalFlip(p=0.1), # Less common but useful for some domains
T.RandomRotation(degrees=15),
T.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.1),
T.RandomAffine(degrees=0, translate=(0.1, 0.1)), # Small translations
T.GaussianBlur(kernel_size=3, sigma=(0.1, 2.0)),
T.ToImage(),
T.ToDtype(torch.float32, scale=True),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
class CutOut:
"""Randomly mask out square regions of the image"""
def __init__(self, n_holes: int = 1, length: int = 16):
self.n_holes = n_holes
self.length = length
def __call__(self, img: torch.Tensor) -> torch.Tensor:
h, w = img.shape[1], img.shape[2]
mask = torch.ones_like(img)
for _ in range(self.n_holes):
y = torch.randint(0, h, (1,)).item()
x = torch.randint(0, w, (1,)).item()
y1 = max(0, y - self.length // 2)
y2 = min(h, y + self.length // 2)
x1 = max(0, x - self.length // 2)
x2 = min(w, x + self.length // 2)
mask[:, y1:y2, x1:x2] = 0
return img * mask
def mixup_data(
x: torch.Tensor,
y: torch.Tensor,
alpha: float = 0.4
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""MixUp: blend two images and their labels"""
lam = torch.distributions.Beta(alpha, alpha).sample().item()
batch_size = x.size(0)
index = torch.randperm(batch_size)
mixed_x = lam * x + (1 - lam) * x[index]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lamCutOut buoc model phai dua vao nhieu dau moi thi giac thay vi mot vung phan biet duy nhat. MixUp tao cac ket hop loi cua cac cap huan luyen, tao ra ranh gioi quyet dinh muot ma hon. Cac ky thuat nay lien tuc cai thien do chinh xac 1-3% tren cac benchmark tieu chuan.
Cau hoi Phong van ve CNN va Computer Vision
Cac cuoc phong van ky thuat cho vai tro computer vision kiem tra ca hieu biet ly thuyet va ky nang thuc hanh. Chuan bi cho cac cau hoi ve thiet ke kien truc, thach thuc toi uu hoa va cac can nhac trien khai thuc te.
Nguoi phong van thuong hoi ve tinh toan receptive field, muc dich cua batch normalization, tai sao tich chap hoat dong voi hinh anh, va cach chan doan overfitting trong cac model vision.
H: Receptive field la gi va tai sao no quan trong?
Receptive field la vung cua hinh anh dau vao anh huong den mot vi tri feature map cu the. Cac lop sau co receptive field lon hon, cho phep chung nam bat ngu canh rong hon. Voi mang co n lop tich chap su dung kernel 3x3, receptive field tang theo (2n + 1) x (2n + 1). Thiet ke kien truc doi hoi can bang giua kich thuoc receptive field va chi phi tinh toan—dilated convolution mo rong receptive field ma khong tang tham so.
H: Tai sao su dung batch normalization trong CNN?
Batch normalization chuan hoa dau vao lop ve mean bang khong va phuong sai don vi, giai quyet internal covariate shift. Loi ich bao gom: hoi tu nhanh hon (learning rate cao hon tro nen on dinh), hieu ung dieu chinh (giam nhu cau dropout), va cai thien dong gradient (ngan vanishing gradient trong mang sau). Trong qua trinh suy luan, running statistics thay the batch statistics de dau ra xac dinh.
H: Giai thich su khac biet giua same va valid padding.
Same padding them cac so khong xung quanh dau vao de bao toan kich thuoc khong gian sau tich chap—dau vao 32x32 voi kernel 3x3 tao ra dau ra 32x32. Valid padding khong ap dung padding, giam kich thuoc—cung phep toan tao ra dau ra 30x30. Same padding duoc ua chuong trong mang sau de tranh giam kich thuoc khong gian qua nhanh.
H: Lam the nao de xu ly mat can bang lop trong phan loai hinh anh?
Cac chien luoc bao gom: weighted cross-entropy loss (trong so cao hon cho cac lop thieu so), oversampling cac lop thieu so trong qua trinh huan luyen, tang cuong du lieu tap trung vao cac lop it duoc dai dien, va focal loss giam trong so cac vi du de. Voi mat can bang nghiem trong (>100:1), ket hop nhieu ky thuat va xem xet cac chi so ngoai do chinh xac—F1 score, precision-recall AUC, hoac phan tich confusion matrix.
De chuan bi phong van deep learning them, kham pha cac module cau hoi CNN & Image Classification va Deep Learning Fundamentals.
Trien khai Model Vision PyTorch voi TorchScript
Trien khai san xuat yeu cau chuyen doi model PyTorch sang dinh dang toi uu. TorchScript bien dich model thanh bieu dien di dong chay ma khong can Python, cho phep trien khai trong ung dung C++, thiet bi di dong hoac moi truong serverless.
# deployment_export.py
import torch
from torchvision import models
from torchvision.models import ResNet18_Weights
def export_for_production(model: torch.nn.Module, save_path: str) -> None:
"""Export model to TorchScript for production deployment"""
model.eval() # Set to evaluation mode (disables dropout, uses running stats)
# Create example input matching expected dimensions
example_input = torch.randn(1, 3, 224, 224)
# Method 1: Tracing (for models without control flow)
traced_model = torch.jit.trace(model, example_input)
traced_model.save(save_path.replace(".pt", "_traced.pt"))
# Method 2: Scripting (handles if/else, loops)
scripted_model = torch.jit.script(model)
scripted_model.save(save_path.replace(".pt", "_scripted.pt"))
# Verify outputs match
with torch.no_grad():
original_out = model(example_input)
traced_out = traced_model(example_input)
scripted_out = scripted_model(example_input)
assert torch.allclose(original_out, traced_out, atol=1e-5)
assert torch.allclose(original_out, scripted_out, atol=1e-5)
print(f"Model exported successfully to {save_path}")
def optimize_for_inference(model_path: str) -> torch.jit.ScriptModule:
"""Load and optimize TorchScript model for inference"""
model = torch.jit.load(model_path)
# Optimize for inference (fuses operations, removes dropout)
optimized = torch.jit.optimize_for_inference(model)
return optimized
# Export ResNet18
model = models.resnet18(weights=ResNet18_Weights.IMAGENET1K_V1)
export_for_production(model, "resnet18_production.pt")De co toc do suy luan toi da, xem xet xuat ONNX de tuong thich da framework hoac TensorRT de trien khai GPU NVIDIA. So sanh PyTorch vs TensorFlow trinh bay cac danh doi trien khai giua cac framework.
Ket luan
Computer vision voi PyTorch ket hop cac tru tuong manh me voi cac cong cu san sang cho san xuat:
- Kien truc CNN trich xuat cac dac trung phan cap thong qua cac lop tich chap, pooling va chuan hoa
- Transfer learning voi cac model pretrained (ResNet, EfficientNet) dat do chinh xac cao tren du lieu han che
- Tang cuong du lieu (MixUp, CutOut, bien doi hinh hoc) cai thien kha nang tong quat hoa va ngan overfitting
- Phong van ky thuat kiem tra receptive field, batch normalization, chien luoc padding va xu ly mat can bang lop
- TorchScript cho phep trien khai san xuat ma khong phu thuoc Python
Bắt đầu luyện tập!
Kiểm tra kiến thức với mô phỏng phỏng vấn và bài kiểm tra kỹ thuật.

Viết bởi
Anthony Fillion-MailletLập trình viên fullstack, người sáng lập SharpSkill
Lập trình viên fullstack hơn 10 năm. Anh điều hành SharpSkill và chịu trách nhiệm về mọi nội dung đăng tại đây.
Cập nhật ngày 14 tháng 8, 2026
Chia sẻ
Bài viết liên quan

LangChain cho Data Scientist 2026: LLM, Agent và Câu hỏi Phỏng vấn
Làm chủ LangChain 0.3 cho data science: LCEL chain, mẫu RAG, ReAct agent, hệ thống bộ nhớ, và câu hỏi phỏng vấn cho vị trí ML engineering.

MLOps năm 2026: MLflow, Model Registry và Câu Hỏi Phỏng Vấn Kỹ Thuật
Các câu hỏi phỏng vấn MLOps bao quát vòng đời ML, theo dõi thí nghiệm với MLflow, thăng cấp model registry, các mẫu triển khai, giám sát drift và thiết kế hệ thống cho năm 2026, kèm mã Python và câu trả lời.

RAG và LLM năm 2026: Retrieval-Augmented Generation cho phỏng vấn Data Science
Retrieval-Augmented Generation (RAG) giải thích cho phỏng vấn data science năm 2026. Bao gồm cơ sở dữ liệu vector, chiến lược chunking, mô hình embedding, agentic RAG, Graph RAG và kiến trúc pipeline sẵn sàng cho sản xuất.