Thuta Learning
Computer Vision
IntermediateAIintermediate

Image Classifier တစ်ခု တည်ဆောက်ခြင်း

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Image Classifier တစ်ခု တည်ဆောက်ခြင်း concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Image classifier တစ်ခုသည် feature extractor တစ်ခုနှင့် decision head တစ်ခုကို တွဲစပ်ထားခြင်းသာ ဖြစ်သည်။ Conv2d layer တစ်ခုစီသည် local pattern များ (edge, texture, shape fragment) ကို detect လုပ်ပြီး ReLU က non-linearity ထည့်သွင်း၍ MaxPool က spatial dimension ကို လျှော့ချကာ အရေးကြီးဆုံး activation များကိုသာ ဆက်ခဲ့စေသည်။ Block များကို stack လုပ်လိုက်တိုင်း network သည် low-level edge မှ high-level object part အထိ တဖြည်းဖြည်း abstract level မြင့်လာသော feature ကို သင်ယူနိုင်သည်။ နောက်ဆုံးတွင် Flatten layer က spatial feature map ကို vector တစ်ခုအဖြစ်ပြောင်းပေးပြီး Linear layer (classifier head) က class score များ ထုတ်ပေးသည်။ ဒါက fully-connected network တစ်ခုတည်းထက် ပိုကောင်းသည့်အကြောင်းရင်းမှာ convolution သည် parameter share (weight sharing) ပြုလုပ်နိုင်သောကြောင့် image တစ်ခုလုံးတွင် ရှိတဲ့ pattern အားလုံးအတွက် weight အသစ်ထပ်မလိုဘဲ efficient ဖြစ်စေခြင်းပင်။

Training loop ကိုယ်တိုင်ကတော့ Deep Learning with PyTorch course မှာ တွေ့ခဲ့ပြီးသား forward → loss → backward → optimizer.step() ပုံစံအတိုင်းပင် ဖြစ်သော်လည်း ဒီနေရာမှာ input သည် image tensor ဖြစ်ပြီး cross-entropy loss သည် class score logits ကို softmax probability အဖြစ် implicit conversion လုပ်ကာ true label နှင့် နှိုင်းယှဉ်သည်။ Fixed batch တစ်ခုတည်းအပေါ် repeatedly train လုပ်ခြင်းသည် (overfit a single batch) network architecture နှင့် training loop မှန်ကန်မှု ရှိမရှိကို quick sanity check လုပ်ရန် အသုံးဝင်သော နည်းလမ်းဖြစ်သည် — loss ကျမလာဘူးဆိုရင် bug တစ်ခုခု ရှိနေနိုင်သည်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Tutorial Platform တွင် instructor များ upload လုပ်သော course thumbnail image များကို topic category (Programming, Design, Math စသည်) အလိုက် အလိုအလျောက် tag တပ်ပေးရန် စမ်းသပ်နေသည်ဆိုပါစို့။ ဒီလို prototype classifier တစ်ခုကို ဒီ lesson ထဲက CNN architecture အတိုင်း စလုပ်ပြီး synthetic data နှင့် training loop မှန်ကန်မှုကို အရင်စစ်ဆေးလို့ရသည်၊ ထို့နောက်မှသာ Platform ပေါ်ရှိ real thumbnail dataset ဖြင့် ဆက်လက် train လုပ်နိုင်မည်။

အတူတူ စမ်းရေးကြည့်မယ်

python
import torch
import torch.nn as nn
import torch.optim as optim

torch.manual_seed(0)

class SimpleCNN(nn.Module):
    def __init__(self, num_classes=10):
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 16, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2),  # 32x32 -> 16x16
            nn.Conv2d(16, 32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2),  # 16x16 -> 8x8
        )
        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(32 * 8 * 8, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        return self.classifier(x)

model = SimpleCNN(num_classes=10)
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
criterion = nn.CrossEntropyLoss()

# Fake batch: 8 RGB images, 32x32, with random integer labels 0-9
images = torch.randn(8, 3, 32, 32)
labels = torch.randint(0, 10, (8,))

for step in range(5):
    optimizer.zero_grad()
    outputs = model(images)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    print(f"step {step}: loss = {loss.item():.4f}")
You should see
'step 0: loss = X.XXXX' ပုံစံဖြင့် line ၅ ကြောင်း ရိုက်ထုတ်မည်။ Model သည် fixed batch တစ်ခုတည်းအပေါ် ထပ်ခါထပ်ခါ train လုပ်နေသောကြောင့် loss value သည် step 0 မှာ (random 10-class classifier တစ်ခု၏ ကနဦး expected loss ဖြစ်သော ln(10)≈2.30 အနီးတွင် စတင်တတ်ပြီး) step ငါးခုကြားမှာ overall trend အနေနဲ့ ကျဆင်းသွားလိမ့်မည် (step တစ်ခုနှင့်တစ်ခုကြား မလုံးဝ monotonic မဖြစ်နိုင်သော်လည်း)။

၅ မိနစ် စမ်းကြည့်

Training loop ကို step ၅ ခုအစား step ၅၀ ခု run ပါ၊ ပြီးလျှင် final step ၏ output logits ကို argmax(dim=1) ဖြင့် predicted class label များနှင့် true labels ကို နှိုင်းယှဉ်ကာ accuracy တွက်ချက်သည့် line များ ထပ်ဖြည့်ပါ။

သတိလေးတစ်ချက်

Flatten output size ကို Conv/MaxPool layer အရေအတွက်နှင့် input image size အလိုက် ကိုယ်တိုင်တွက်ချက်ရန် မေ့ပြီး Linear layer ၏ in_features ကို မှားထည့်လျှင် shape mismatch RuntimeError တက်လိမ့်မည်။

optimizer.zero_grad() ကို loop အတွင်း step တစ်ခုစီအတွက် ခေါ်ရန်မေ့ခဲ့လျှင် gradient များ accumulate ဖြစ်ကာ loss သည် ကျဆင်းမည့်အစား မမျှော်လင့်ထားသော direction သို့ ရွေ့သွားနိုင်သည်။

PyTorch Tutorials — Training a Classifier (CIFAR10)Computer Vision

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Flatten output size ကို Conv/MaxPool layer အရေအတွက်နှင့် input image size အလိုက် ကိုယ်တိုင်တွက်ချက်ရန် မေ့ပြီး Linear layer ၏ in_features ကို မှားထည့်လျှင် shape mismatch RuntimeError တက်လိမ့်မည်။
  • optimizer.zero_grad() ကို loop အတွင်း step တစ်ခုစီအတွက် ခေါ်ရန်မေ့ခဲ့လျှင် gradient များ accumulate ဖြစ်ကာ loss သည် ကျဆင်းမည့်အစား မမျှော်လင့်ထားသော direction သို့ ရွေ့သွားနိုင်သည်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Training loop ကို step ၅ ခုအစား step ၅၀ ခု run ပါ၊ ပြီးလျှင် final step ၏ output logits ကို argmax(dim=1) ဖြင့် predicted class label များနှင့် true labels ကို နှိုင်းယှဉ်ကာ accuracy တွက်ချက်သည့် line များ ထပ်ဖြည့်ပါ။

You'll know it worked when: 'step 0: loss = X.XXXX' ပုံစံဖြင့် line ၅ ကြောင်း ရိုက်ထုတ်မည်။ Model သည် fixed batch တစ်ခုတည်းအပေါ် ထပ်ခါထပ်ခါ train လုပ်နေသောကြောင့် loss value သည် step 0 မှာ (random 10-class classifier တစ်ခု၏ ကနဦး expected loss ဖြစ်သော ln(10)≈2.30 အနီးတွင် စတင်တတ်ပြီး) step ငါးခုကြားမှာ overall trend အနေနဲ့ ကျဆင်းသွားလိမ့်မည် (step တစ်ခုနှင့်တစ်ခုကြား မလုံးဝ monotonic မဖြစ်နိုင်သော်လည်း)။

Image Classifier တစ်ခု တည်ဆောက်ခြင်း | Thuta Learning