Thuta Learning
Computer Vision
IntermediateAIintermediate

Data Augmentation ကို လက်တွေ့ သုံးခြင်း

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

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

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

Data augmentation ၏ အဓိက idea မှာ training image တစ်ခုစီကို slight-random transform (flip, rotate, color shift စသည်) ဖြင့် ပြင်ဆင်ပြီးမှသာ model ကို ပြသခြင်းဖြစ်သည်။ Model သည် epoch တစ်ခုစီတိုင်း image တစ်ခုတည်း၏ slightly different version ကို မြင်ရသောကြောင့် exact pixel value pattern များကို memorize လုပ်ဖို့ ခက်ခဲသွားပြီး၊ အစား class ကို သတ်မှတ်ပေးသည့် ပိုမိုတည်ငြိမ်သော အသွင်အပြင် (shape, texture arrangement) ကို သင်ယူဖို့ အားထုတ်လာသည်။ ဒါက overfitting လျှော့ချရာတွင် dropout ကဲ့သို့ regularization technique များနှင့် နည်းလမ်းချင်းတူသော်လည်း လုပ်ဆောင်ပုံ မတူညီပါ — dropout သည် network internal ကို ရှုပ်ထွေးစေသော်လည်း data augmentation သည် dataset ကို effectively ကြီးထွားစေခြင်း (dataset expansion) ဖြစ်သည်။

Image data အတွက် augmentation ၏ အထူးအာနိသင်ကားမှာ real-world variation တွေကို တုပနိုင်ခြင်းပင် — ဓာတ်ပုံရိုက်တဲ့ ထောင့်၊ အလင်းအမှောင်၊ ဘယ်/ညာ ဦးတည်ချက်တို့သည် object identity ကို မပြောင်းလဲသင့်ပါ။ Cat တစ်ကောင်ကို flip လုပ်ထားတာဖြစ်စေ၊ contrast ပြောင်းထားတာဖြစ်စေ ဒါဟာ cat အနေနဲ့ပဲ ကျန်ရှိနေရမည်။ Model ကို ဒီ variation တွေအားလုံးကို training တုန်းက training-time-only random transform များ (inference အချိန်တွင် မသုံးရ) ဖြင့် ပြသလိုက်ခြင်းအားဖြင့် test set ပေါ်ရှိ မမြင်ဖူးသေးသော variation များအပေါ် ပိုမို robust ဖြစ်လာစေသည်။

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

Tutorial Platform တွင် user များ upload လုပ်ပေးသော lesson diagram (flowchart, architecture sketch) များ အနည်းငယ်သာ ရရှိထားပြီး၊ ၎င်းတို့ 'quality အနိမ့်' ဟုတ်မဟုတ် ခွဲခြားပေးမည့် internal classifier တစ်ခု train လုပ်နေသည်ဆိုပါစို့။ Sample diagram အရေအတွက် နည်းလွန်းသောကြောင့် augmentation pipeline (flip, slight rotation, color jitter) ကို training image တစ်ခုစီအပေါ် သုံးလိုက်ခြင်းဖြင့် dataset ကို effectively ချဲ့ထွင်ပေးနိုင်ပြီး classifier သည် sample အနည်းငယ်ကိုသာ memorize လုပ်မိသွားခြင်းမှ ကာကွယ်နိုင်သည်။

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

python
import torch
import torchvision.transforms.v2 as T

torch.manual_seed(0)

transform = T.Compose([
    T.RandomHorizontalFlip(p=0.5),
    T.RandomRotation(degrees=15),
    T.ColorJitter(brightness=0.3, contrast=0.3),
])

# A single fake RGB image, 3x64x64, values in [0, 1]
image = torch.rand(3, 64, 64)

for i in range(3):
    augmented = transform(image)
    print(f"call {i}: shape={tuple(augmented.shape)}, mean={augmented.mean().item():.4f}")
You should see
3 ကြောင်း print ထုတ်ပြီး shape=(3, 64, 64) ဟု အမြဲတမ်း ပြသမည် (transform က spatial size ကို မပြောင်းလဲပါ)။ သို့သော် mean value မှာမူ call တစ်ခုစီတွင် random flip/rotation/color-jitter parameter များ မတူညီစွာ sample ထုတ်ခံရသောကြောင့် line သုံးကြောင်းစလုံး၏ mean value မတူညီပါ (input tensor ကိုယ်တိုင်ကတော့ မပြောင်းလဲဘဲ ကျန်ရှိနေသည်)။

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

Pipeline ထဲသို့ T.RandomResizedCrop(size=(64, 64), scale=(0.7, 1.0)) ကို ထပ်ထည့်ပြီး loop ကို 3 ကြိမ်အစား 5 ကြိမ်ပြင်ပြီး run ကြည့်ပါ၊ crop transform ထည့်ပြီးနောက် output mean value များ ပိုမို ကွဲပြားလားကို လေ့လာပါ။

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

Validation/test data ပေါ်တွင် random augmentation transform များကို ထားခဲ့မိလျှင် evaluation metric သည် run တစ်ခုနှင့်တစ်ခု mismatch ဖြစ်ကာ non-reproducible ဖြစ်သွားနိုင်သည်။

ColorJitter ကို mask/label image တစ်ခုတည်း (segmentation mask) အပေါ် image နှင့်အတူ သီးခြား apply လုပ်မိလျှင် mask ၏ pixel value (class index) များ မမှန်ကန်တော့ဘဲ ပျက်စီးသွားနိုင်သည်။

Wikipedia — Data augmentationComputer Vision

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

  • Validation/test data ပေါ်တွင် random augmentation transform များကို ထားခဲ့မိလျှင် evaluation metric သည် run တစ်ခုနှင့်တစ်ခု mismatch ဖြစ်ကာ non-reproducible ဖြစ်သွားနိုင်သည်။
  • ColorJitter ကို mask/label image တစ်ခုတည်း (segmentation mask) အပေါ် image နှင့်အတူ သီးခြား apply လုပ်မိလျှင် mask ၏ pixel value (class index) များ မမှန်ကန်တော့ဘဲ ပျက်စီးသွားနိုင်သည်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

Pipeline ထဲသို့ T.RandomResizedCrop(size=(64, 64), scale=(0.7, 1.0)) ကို ထပ်ထည့်ပြီး loop ကို 3 ကြိမ်အစား 5 ကြိမ်ပြင်ပြီး run ကြည့်ပါ၊ crop transform ထည့်ပြီးနောက် output mean value များ ပိုမို ကွဲပြားလားကို လေ့လာပါ။

You'll know it worked when: 3 ကြောင်း print ထုတ်ပြီး shape=(3, 64, 64) ဟု အမြဲတမ်း ပြသမည် (transform က spatial size ကို မပြောင်းလဲပါ)။ သို့သော် mean value မှာမူ call တစ်ခုစီတွင် random flip/rotation/color-jitter parameter များ မတူညီစွာ sample ထုတ်ခံရသောကြောင့် line သုံးကြောင်းစလုံး၏ mean value မတူညီပါ (input tensor ကိုယ်တိုင်ကတော့ မပြောင်းလဲဘဲ ကျန်ရှိနေသည်)။

Data Augmentation ကို လက်တွေ့ သုံးခြင်း | Thuta Learning