訓練影像分類器

Intermediate Deep Learning with PyTorch

Michal Oleszak

Machine Learning Engineer

重談資料增強

一張貓的圖片。

Intermediate Deep Learning with PyTorch

重談資料增強

一張水平翻轉並旋轉的貓圖片。

Intermediate Deep Learning with PyTorch

不該做的增強

黃色檸檬與綠色萊姆外觀相近,只差在顏色。

Intermediate Deep Learning with PyTorch

不該做的增強

黃色檸檬與綠色萊姆外觀相近,只差在顏色。

Intermediate Deep Learning with PyTorch

不該做的增強

字母「W」垂直翻轉後看起來像字母「M」。

  • 增強可能改變標籤
  • 是否造成混淆取決於任務
  • 選擇增強時要考量資料與任務!
Intermediate Deep Learning with PyTorch

雲朵分類的增強策略

雲朵圖片樣本。

  • 隨機旋轉:讓模型看到不同雲形角度
  • 水平翻轉:模擬不同天空視角
  • 自動對比調整:模擬不同光照條件
train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(45),
    transforms.RandomAutocontrast(),
    transforms.ToTensor(),
    transforms.Resize((128, 128))
])
Intermediate Deep Learning with PyTorch

Cross-Entropy 損失

  • 二元分類:binary cross-entropy(BCE)損失
  • 多類別分類:cross-entropy 損失
  • criterion = nn.CrossEntropyLoss()
Intermediate Deep Learning with PyTorch

影像分類器訓練迴圈

net = Net(num_classes=7)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)


for epoch in range(10): for images, labels in dataloader_train: optimizer.zero_grad() outputs = net(images) loss = criterion(outputs, labels) loss.backward() optimizer.step()
Intermediate Deep Learning with PyTorch

一起來練習吧!

Intermediate Deep Learning with PyTorch

Preparing Video For Download...