Thuta Learning
Computer Vision
BasicAIintermediate

Image Transformation နှင့် Augmentation အခြေခံ

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

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

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

torchvision ရှိ transform တစ်ခုဆိုသည်မှာ image tensor တစ်ခုကို လက်ခံပြီး ပြင်ဆင်ထားသော tensor တစ်ခု ပြန်ပေးသည့် function (သို့) callable object သေးငယ်တစ်ခုမျှသာ ဖြစ်ပြီး၊ transforms.v2 API သည် ၎င်းတို့ကို ရေးသားရန် လက်ရှိ active ဖြစ်နေသော နည်းလမ်း ဖြစ်သည် — image tensor ရိုးရိုးတစ်ခု၊ batch တစ်ခု၊ (သို့) လိုအပ်ပါက bounding box, segmentation mask ကဲ့သို့ paired data များကို transform လုပ်နေသည်ဖြစ်စေ တညီတညွတ်တည်း အလုပ်လုပ်သည်။ transform အချို့သည် shape-preserving ဖြစ်သည် — RandomHorizontalFlip သည် pixel များကို ဘယ်မှညာ ပြန်စီပေးသော်လည်း tensor သည် ဝင်ရောက်လာချိန် H, W အတိုင်းပင် (C, H, W) ဖြင့် ပြန်ထွက်လာသည်၊ Normalize နှင့် color-jitter operation များကတော့ spatial dimension ကို လုံးဝ မထိခိုက်စေဘဲ pixel value ကိုသာ rescale လုပ်ပေးသည်။ transform အခြားများကတော့ shape-changing ဖြစ်သည် — Resize သည် H, W အသစ်ကို ရှင်းလင်းစွာ ထုတ်ပေးပြီး၊ RandomCrop/CenterCrop ကတော့ tensor ကို ဧရိယာသေးငယ်တစ်ခုအဖြစ် ဖြတ်တောက်ပေးသည်။ Compose သည် transform များစွာကို callable တစ်ခုတည်းအဖြစ် အစဉ်လိုက် chain လုပ်ပေးသည် — DataLoader တစ်ခုက sample တစ်ခုစီအတွက် run လုပ်သော pipeline အတိအကျပင် ဖြစ်ပြီး၊ ဘယ်အဆင့်က shape ပြောင်းပြီး ဘယ်အဆင့်က မပြောင်းဘူးဆိုတာ နားလည်ထားခြင်းသည် နောက်ပိုင်း batch-shape mismatch များကို debug လုပ်ရန် အရေးကြီးသည်။

Flip, crop, color-jitter ကဲ့သို့ augmentation transform များ ရှိနေရသည့်အကြောင်းရင်းမှာ ဓာတ်ပုံအသစ်တစ်ပုံမျှ မရိုက်ဘဲ training set ၏ diversity ကို artificial ဖန်တီးပေးနိုင်ခြင်းကြောင့် ဖြစ်သည် — training image တစ်ခုကို ဘယ်မှညာ ကျပန်း flip လုပ်ခြင်းသည် ၎င်းထဲမှာ ဘာ object ပါသလဲဆိုတာကို မပြောင်းလဲစေပါ — ထို့ကြောင့် ဗားရှင်းနှစ်ခုစလုံးဖြင့် train လုပ်ခြင်းသည် label အတွက် orientation က အရေးမကြီးကြောင်း network ကို သင်ကြားပေးသည် — ၎င်းသည် network ကိုယ်တိုင် fixed dataset တစ်ခုမှ ရှာဖွေတွေ့ရှိလိမ့်မည်ဟု မျှော်လင့်နေမည့်အစား တမင်ထည့်သွင်းပေးလိုက်သော invariance အမျိုးအစားတစ်ခုပင် ဖြစ်သည်။ အရေးကြီးသည်မှာ ဤ random transform များကို training အချိန်တွင်သာ stochastic ဖြင့် apply လုပ်ရသည် — evaluation (သို့) inference အချိန်တွင်တော့ deterministic ဖြစ်ပြီး ပြန်လည်ထုတ်လုပ်နိုင်သော transform (fixed resize, random flip မပါ) ကိုသာ လိုချင်သည် — input တစ်ခုတည်းသည် အမြဲတမ်း prediction တစ်ခုတည်းကို ထုတ်ပေးစေရန်နှင့် သင်၏ metric များသည် random augmentation ကြောင့်သာ run တစ်ခုနှင့်တစ်ခု မတိမ်းမယိမ်း ဖြစ်စေရန် ဖြစ်သည်။

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

Tutorial Platform သည် instructor တစ်ဦး upload လုပ်လိုက်သော course image တစ်ခုတည်းမှ thumbnail size သုံးမျိုး — grid-card thumbnail, hero banner, mobile-view crop — ကို page-render အချိန်တွင် Resize-based pipeline တစ်ခုဖြင့် ထုတ်ပေးပြီး၊ 'course-image quality' classifier အတွင်းပိုင်း တစ်ခုကိုလည်း သီးခြား ထိန်းသိမ်းထားသည် — ၎င်း training set ထဲသို့ RandomHorizontalFlip နှင့် random crop သေးငယ်များ ထည့်သွင်းထားခြင်းဖြင့် model သည် ဘယ်ဘက်ကို မျက်နှာမူထားသော (သို့) အလယ်ဗဟိုတည့်တည့် banner များသာ thumbnail ကောင်းအဖြစ် အလွဲယူဆမိခြင်းကို ကာကွယ်ပေးထားသည်။

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

python
import torch
from torchvision.transforms import v2

torch.manual_seed(0)

# Synthetic batch: a single fake RGB image, 3 channels, 64x64.
image = torch.rand(3, 64, 64)

flip = v2.RandomHorizontalFlip(p=1.0)  # p=1.0 makes this deterministic for the demo
flipped = flip(image)
print("original shape:", image.shape)
print("flipped shape:", flipped.shape)
print("flip changed pixel order:", not torch.equal(image, flipped))

resize = v2.Resize((32, 32))
resized = resize(image)
print("resized shape:", resized.shape)

# Compose chains several transforms into one callable pipeline --
# this is exactly the kind of pipeline a DataLoader applies per sample.
pipeline = v2.Compose([
    v2.Resize((32, 32)),
    v2.RandomHorizontalFlip(p=0.5),
])
augmented = pipeline(image)
print("pipeline output shape:", augmented.shape)
You should see
ဤသို့ print ထုတ်သည်: original shape: torch.Size([3, 64, 64])၊ flipped shape: torch.Size([3, 64, 64]) (မပြောင်းလဲပါ — flip က pixel များကို ပြန်စီပေးရုံသာ ဖြစ်သည်)၊ flip changed pixel order: True (p=1.0 က flip ကို အတင်း ဖြစ်စေပြီး ကျပန်း continuous image တစ်ခုသည် ဘယ်-ညာ symmetric ဖြစ်နိုင်ခြေ လုံးဝနီးပါး မရှိသောကြောင့် အာမခံနိုင်သည်)၊ resized shape: torch.Size([3, 32, 32])၊ နှင့် pipeline output shape: torch.Size([3, 32, 32]) (pipeline ထဲက resize သည် အမြဲတမ်း apply ဖြစ်သောကြောင့် 50% probability ရှိသော flip ဖြစ်ခဲ့သည်ဖြစ်စေ မဖြစ်ခဲ့သည်ဖြစ်စေ output shape အတူတူပင် ဖြစ်နေမည်)။

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

pipeline ထဲသို့ v2.RandomCrop((24, 24)) ထည့်ပါ၊ pipeline ကို loop ထဲတွင် သုံးကြိမ် run ပြီး run တစ်ခုစီတွင် augmented.shape နှင့် torch.equal(augmented, image) (resize ကို ထည့်တွက်၍) ကို print ထုတ်ပြီး၊ output ၏ ဘယ်အပိုင်းက run တိုင်း တူညီနေသလဲ (shape) နှင့် ဘယ်အပိုင်းက ကွဲပြားနေသလဲ (random flip ကြောင့် pixel content အတိအကျ) ကို မှတ်သားကြည့်ပါ။

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

training အတွင်းသာ run သင့်သော RandomHorizontalFlip (သို့) random augmentation များကို evaluation/inference အချိန်တွင်ပါ apply လုပ်ခြင်း — ၎င်းသည် input တစ်ခုတည်းကို run တစ်ခုနှင့်တစ်ခု prediction ကွဲပြားစေပြီး eval metric များကို noisy ဖြစ်စေကာ ပြန်လည်ထုတ်လုပ်၍ မရနိုင်စေတော့ပါ။

Compose chain ထဲက transform တိုင်းသည် shape ကို ထိန်းသိမ်းသည်ဟု ယူဆခြင်း — chain ထဲရှိ Resize (သို့) CenterCrop မည်သည့်နေရာမဆို H/W ကို ပြောင်းလဲပစ်နိုင်သည် — ထို့ကြောင့် နောက်ပိုင်း (ဥပမာ fixed-size buffer တစ်ခုအတွက်) tensor shape ကို hardcode လုပ်ထားလျှင် pipeline ရှေ့ပိုင်းတွင် တစ်စုံတစ်ယောက်က shape-changing transform တစ်ခု ထပ်ထည့်လိုက်သည်နှင့် ချက်ချင်း ပျက်စီးသွားနိုင်သည်။

PyTorch Vision Docs — Transforming images, videos, boxes and moreComputer Vision

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

  • training အတွင်းသာ run သင့်သော RandomHorizontalFlip (သို့) random augmentation များကို evaluation/inference အချိန်တွင်ပါ apply လုပ်ခြင်း — ၎င်းသည် input တစ်ခုတည်းကို run တစ်ခုနှင့်တစ်ခု prediction ကွဲပြားစေပြီး eval metric များကို noisy ဖြစ်စေကာ ပြန်လည်ထုတ်လုပ်၍ မရနိုင်စေတော့ပါ။
  • Compose chain ထဲက transform တိုင်းသည် shape ကို ထိန်းသိမ်းသည်ဟု ယူဆခြင်း — chain ထဲရှိ Resize (သို့) CenterCrop မည်သည့်နေရာမဆို H/W ကို ပြောင်းလဲပစ်နိုင်သည် — ထို့ကြောင့် နောက်ပိုင်း (ဥပမာ fixed-size buffer တစ်ခုအတွက်) tensor shape ကို hardcode လုပ်ထားလျှင် pipeline ရှေ့ပိုင်းတွင် တစ်စုံတစ်ယောက်က shape-changing transform တစ်ခု ထပ်ထည့်လိုက်သည်နှင့် ချက်ချင်း ပျက်စီးသွားနိုင်သည်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

pipeline ထဲသို့ v2.RandomCrop((24, 24)) ထည့်ပါ၊ pipeline ကို loop ထဲတွင် သုံးကြိမ် run ပြီး run တစ်ခုစီတွင် augmented.shape နှင့် torch.equal(augmented, image) (resize ကို ထည့်တွက်၍) ကို print ထုတ်ပြီး၊ output ၏ ဘယ်အပိုင်းက run တိုင်း တူညီနေသလဲ (shape) နှင့် ဘယ်အပိုင်းက ကွဲပြားနေသလဲ (random flip ကြောင့် pixel content အတိအကျ) ကို မှတ်သားကြည့်ပါ။

You'll know it worked when: ဤသို့ print ထုတ်သည်: original shape: torch.Size([3, 64, 64])၊ flipped shape: torch.Size([3, 64, 64]) (မပြောင်းလဲပါ — flip က pixel များကို ပြန်စီပေးရုံသာ ဖြစ်သည်)၊ flip changed pixel order: True (p=1.0 က flip ကို အတင်း ဖြစ်စေပြီး ကျပန်း continuous image တစ်ခုသည် ဘယ်-ညာ symmetric ဖြစ်နိုင်ခြေ လုံးဝနီးပါး မရှိသောကြောင့် အာမခံနိုင်သည်)၊ resized shape: torch.Size([3, 32, 32])၊ နှင့် pipeline output shape: torch.Size([3, 32, 32]) (pipeline ထဲက resize သည် အမြဲတမ်း apply ဖြစ်သောကြောင့် 50% probability ရှိသော flip ဖြစ်ခဲ့သည်ဖြစ်စေ မဖြစ်ခဲ့သည်ဖြစ်စေ output shape အတူတူပင် ဖြစ်နေမည်)။

Image Transformation နှင့် Augmentation အခြေခံ | Thuta Learning