การประเมินตัวจำแนกภาพ

Deep Learning ระดับกลางด้วย PyTorch

Michal Oleszak

Machine Learning Engineer

Data augmentation ในช่วงทดสอบ

Data augmentation สำหรับข้อมูลฝึกสอน:

train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(45),
    transforms.RandomAutocontrast(),
    transforms.ToTensor(),
    transforms.Resize((64, 64)),
])

dataset_train = ImageFolder(
  "clouds_train", 
  transform=train_transforms,
)

Data augmentation สำหรับข้อมูลทดสอบ:

test_transforms = transforms.Compose([
    #
    # NO DATA AUGMENTATION AT TEST TIME
    #
    transforms.ToTensor(),
    transforms.Resize((64, 64)),
])

dataset_test = ImageFolder(
  "clouds_test", 
  transform=test_transforms,
)
Deep Learning ระดับกลางด้วย PyTorch

Precision & Recall: การจำแนกแบบไบนารี

ในการจำแนกแบบไบนารี:

  • Precision: สัดส่วนของการทำนายบวกที่ถูกต้อง
  • Recall: สัดส่วนของตัวอย่างบวกทั้งหมดที่ทำนายได้ถูกต้อง

Confusion matrix แบบ 2x2 โดยแต่ละช่องมีสีต่างกัน พร้อมสูตร recall และ precision ที่แสดงในรูปของรหัสสี

Deep Learning ระดับกลางด้วย PyTorch

Precision & Recall: การจำแนกหลายคลาส

ในการจำแนกหลายคลาส: คำนวณ precision และ recall แยกตามแต่ละคลาส

  • Precision: สัดส่วนของการทำนาย cumulus ที่ถูกต้อง
  • Recall: สัดส่วนของตัวอย่าง cumulus ทั้งหมดที่ทำนายได้ถูกต้อง

 

ภาพเมฆ cumulus

Deep Learning ระดับกลางด้วย PyTorch

การเฉลี่ยเมตริกหลายคลาส

  • เมื่อมี 7 คลาส จะได้ค่า precision 7 ค่า และ recall 7 ค่า
  • วิเคราะห์แบบรายคลาส หรือจะรวมค่าได้ดังนี้:
    • Micro average: คำนวณแบบรวมทั้งหมด
    • Macro average: ค่าเฉลี่ยของเมตริกรายคลาส
    • Weighted average: ค่าเฉลี่ยถ่วงน้ำหนักของเมตริกรายคลาส
Deep Learning ระดับกลางด้วย PyTorch

การเฉลี่ยเมตริกหลายคลาส

from torchmetrics import Recall

recall_per_class = Recall(task="multiclass", num_classes=7, average=None)
recall_micro = Recall(task="multiclass", num_classes=7, average="micro")
recall_macro = Recall(task="multiclass", num_classes=7, average="macro")
recall_weighted = Recall(task="multiclass", num_classes=7, average="weighted")

ควรใช้แบบไหน:

  • Micro: ชุดข้อมูลที่ไม่สมดุล
  • Macro: ให้ความสำคัญกับประสิทธิภาพบนคลาสขนาดเล็ก
  • Weighted: ถือว่าข้อผิดพลาดในคลาสขนาดใหญ่มีความสำคัญมากกว่า
Deep Learning ระดับกลางด้วย PyTorch

ลูปการประเมินผล

from torchmetrics import Precision, Recall

metric_precision = Precision(
  task="multiclass", num_classes=7, average="macro"
)
metric_recall = Recall(
  task="multiclass", num_classes=7, average="macro"
)

net.eval() with torch.no_grad(): for images, labels in dataloader_test:
outputs = net(images) _, preds = torch.max(outputs, 1) metric_precision(preds, labels) metric_recall(preds, labels)
precision = metric_precision.compute() recall = metric_recall.compute()
  • นำเข้าและกำหนดเมตริก precision และ recall
  • วนซ้ำตัวอย่างทดสอบโดยไม่คำนวณ gradient
  • ในแต่ละ batch รับ output ของโมเดล เลือกคลาสที่เป็นไปได้มากที่สุด แล้วส่งพร้อม label ไปยังฟังก์ชันเมตริก
  • คำนวณเมตริก
print(f"Precision: {precision}")
print(f"Recall: {recall}")
Precision: 0.7284010648727417
Recall: 0.763038694858551
Deep Learning ระดับกลางด้วย PyTorch

วิเคราะห์ประสิทธิภาพรายคลาส

metric_recall = Recall(
  task="multiclass", num_classes=7, average=None
)
net.eval()
with torch.no_grad():
    for images, labels in dataloader_test:
        outputs = net(images)
        _, preds = torch.max(outputs, 1)
        metric_recall(preds, labels)
recall = metric_recall.compute()
print(recall)
tensor([0.6364, 1.0000, 0.9091, 0.7917, 
        0.5049, 0.9500, 0.5493],
       dtype=torch.float32)
  • คำนวณเมตริกด้วย average=None
  • จะได้ค่าคะแนนหนึ่งค่าต่อหนึ่งคลาส
  • แอตทริบิวต์ .class_to_idx ของ Dataset แมปชื่อคลาสไปยังดัชนี
dataset_test.class_to_idx
{'cirriform clouds': 0,
 'clear sky': 1,
 'cumulonimbus clouds': 2,
 'cumulus clouds': 3,
 'high cumuliform clouds': 4,
 'stratiform clouds': 5,
 'stratocumulus clouds': 6}
Deep Learning ระดับกลางด้วย PyTorch

วิเคราะห์ประสิทธิภาพรายคลาส

{
  k: recall[v].item() 
  for k, v 
  in dataset_test.class_to_idx.items()
}
{'cirriform clouds': 0.6363636255264282,
 'clear sky': 1.0,
 'cumulonimbus clouds': 0.9090909361839294,
 'cumulus clouds': 0.7916666865348816,
 'high cumuliform clouds': 0.5048543810844421,
 'stratiform clouds': 0.949999988079071,
 'stratocumulus clouds': 0.5492957830429077}
  • k = ชื่อคลาส เช่น cirriform clouds
  • v = ดัชนีคลาส เช่น 0
  • recall[v] = tensor(0.6364, dtype=torch.float32)
  • recall[v].item() = 0.6364
Deep Learning ระดับกลางด้วย PyTorch

มาฝึกกันเถอะ!

Deep Learning ระดับกลางด้วย PyTorch

Preparing Video For Download...