Thuta Learning
Deep Learning with PyTorch
IntermediateAIintermediate

Convolutional Neural Networks

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

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

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

Fully-connected layer တစ်ခုက image ကို vector ရှည်ရှည်တစ်ခုအဖြစ် flatten လုပ်ပြီး pixel တစ်ခုချင်းစီကို neuron တစ်ခုချင်းစီနဲ့ weight သီးခြားစီနဲ့ ချိတ်ဆက်ပေးတယ်၊ ဒါက CNN တစ်ခုသုံးနိုင်တဲ့ အချက်နှစ်ချက်ကို စွန့်ပစ်လိုက်တာဖြစ်တယ် — အနီးအနားရှိ pixel တွေက space အရ ဆက်စပ်နေတယ် (edge တစ်ခုဆိုတာ local pattern ဖြစ်ပြီး global မဟုတ်ဘူး)၊ ပြီးတော့ edge ဒါမှမဟုတ် curve လို useful pattern တစ်ခုက image ထဲ ဘယ်နေရာမှာမဆို ပေါ်လာနိုင်ပြီး position ဘယ်လိုပဲရှိရှိ တူညီတဲ့ နည်းလမ်းနဲ့ detect လုပ်သင့်တယ်။ Convolutional layer တစ်ခုကတော့ filter (kernel) သေးသေးလေးတစ်ခု — ဥပမာ 3x3 pixel — ကို သင်ယူပြီး image တစ်ခုလုံးအပေါ် ရွှေ့ကာ position တိုင်းမှာ dot-product operation တူတူကိုပဲ တွက်ချက်ပေးတယ်။ Filter ရဲ့ weight တွေကို position အားလုံးမှာ share လုပ်ထားတာကြောင့် parameter set သေးသေးလေးတစ်ခုတည်းကပဲ pattern ကို ဘယ်နေရာမှာ ပေါ်ပေါ် detect လုပ်နိုင်ပြီး network ကို translation invariance ပေးသလို image တစ်ခုတည်းအတွက် fully-connected layer ထက် parameter အများကြီး နည်းသွားစေတယ်။ Conv layer များစွာကို ထပ်ချထားတာက layer ရှေ့ဆုံးတွေကို edge၊ color လို pattern ရိုးရိုးလေးတွေ detect လုပ်စေပြီး layer နောက်ပိုင်းတွေက ဒါတွေကို shape၊ texture၊ object လို complex pattern တွေအဖြစ် ပေါင်းစပ်ပေးတယ်။ Pooling layer (max pooling လိုမျိုး) တွေကတော့ region သေးသေးလေးတစ်ခုစီရဲ့ activation အားအကြီးဆုံးကိုပဲ ထိန်းထားလိုက်ခြင်းအားဖြင့် feature map ကို downsample လုပ်ပေးတယ်၊ spatial size နဲ့ computation ကို လျှော့ချပေမဲ့ detect လုပ်ထားတဲ့ feature အရေးကြီးဆုံးတွေကို ထိန်းထားပေးတယ်။

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

Tutorial Platform ရဲ့ duplicate/low-quality image detector က visual pattern တွေ — blur၊ watermark artifact၊ almost-identical screenshot တွေ — ကို frame ထဲ ဘယ်နေရာမှာ ရှိရှိ မှတ်မိရမယ်၊ screenshot တစ်ခုကို အနည်းငယ် ကွဲပြားစွာ crop လုပ်ထားတာက detector ကို လှည့်စားနိုင်သင့်မှာ မဟုတ်ဘူး။ Fully-connected network တစ်ခုက pixel position တစ်ခုချင်းစီအတွက် weight သီးခြားစီ လိုအပ်ပြီး blur pattern တူတူတစ်ခုကို pixel အနည်းငယ် ရွှေ့ထားရင် မှတ်မိတော့မှာ မဟုတ်ဘူး၊ CNN ရဲ့ shared filter တွေကတော့ position ဘယ်လိုပဲရှိရှိ ဒါကို detect လုပ်နိုင်တယ်။ Team က Conv2d+ReLU+MaxPool block အချို့ကို ထပ်ချထားတာကြောင့် layer ရှေ့ဆုံးတွေက edge၊ compression block လို artifact ရိုးရိုးလေးတွေကို ဖမ်းမိပြီး layer နောက်ပိုင်းတွေက ဒါတွေကို 'ဒါက lesson image ရှိပြီးသားတစ်ခုရဲ့ re-upload ပုံစံပဲ' လို high-level cue တွေအဖြစ် ပေါင်းစပ်ပေးတယ်။

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

python
import torch
import torch.nn as nn

conv = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3, padding=1)
pool = nn.MaxPool2d(kernel_size=2)

# Fake tiny RGB image: batch=1, channels=3, height=16, width=16
image = torch.randn(1, 3, 16, 16)

conv_out = conv(image)
print("After conv:", conv_out.shape)

pooled_out = pool(conv_out)
print("After pool:", pooled_out.shape)
You should see
`After conv: torch.Size([1, 8, 16, 16])` (padding ကြောင့် spatial size တူတူပဲ ဖြစ်ပြီး channel 8 ခုအထိ ချဲ့ပေးလိုက်တယ်) ပြီးရင် `After pool: torch.Size([1, 8, 8, 8])` (max pooling က height နဲ့ width ကို တစ်ဝက်ချ လျှော့ချလိုက်တယ်) ကို print ထုတ်ပေးတယ်။

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

Conv2d ထဲက `kernel_size` ကို 5 အဖြစ် `padding=2` နဲ့အတူ ပြောင်းပြီး output spatial size က 16x16 ပဲ ဆက်ရှိနေမယ်ဆိုတာ အတည်ပြုကြည့်ပါ။

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

`padding` ကို သတ်မှတ်ဖို့ မေ့ကျန်ပြီး conv layer တိုင်းပြီးတိုင်း spatial dimension လျော့သွားတာကို အံ့သြနေခြင်း — padding မရှိရင် kernel 3x3 တစ်ခုက layer တစ်ခုစီမှာ spatial dimension တစ်ခုစီကနေ pixel 2 ခု ဆုံးရှုံးစေတယ်၊ layer အများကြီးနောက်ပိုင်းမှာ feature map က ဘာမှ မကျန်တော့အောင် ကျုံ့သွားနိုင်တယ်။

`in_channels` ကို image ရဲ့ width/height နဲ့ ရှုပ်ထွေးမိခြင်း — `in_channels` က input ရဲ့ channel အရေအတွက်နဲ့ တူရမယ် (RGB အတွက် 3 လိုမျိုး) spatial dimension မဟုတ်ဘူး၊ တန်ဖိုးမှားထည့်ရင် shape-mismatch error တက်လာမယ်။

Wikipedia — Convolutional neural networkDeep Learning

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

  • `padding` ကို သတ်မှတ်ဖို့ မေ့ကျန်ပြီး conv layer တိုင်းပြီးတိုင်း spatial dimension လျော့သွားတာကို အံ့သြနေခြင်း — padding မရှိရင် kernel 3x3 တစ်ခုက layer တစ်ခုစီမှာ spatial dimension တစ်ခုစီကနေ pixel 2 ခု ဆုံးရှုံးစေတယ်၊ layer အများကြီးနောက်ပိုင်းမှာ feature map က ဘာမှ မကျန်တော့အောင် ကျုံ့သွားနိုင်တယ်။
  • `in_channels` ကို image ရဲ့ width/height နဲ့ ရှုပ်ထွေးမိခြင်း — `in_channels` က input ရဲ့ channel အရေအတွက်နဲ့ တူရမယ် (RGB အတွက် 3 လိုမျိုး) spatial dimension မဟုတ်ဘူး၊ တန်ဖိုးမှားထည့်ရင် shape-mismatch error တက်လာမယ်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

Conv2d ထဲက `kernel_size` ကို 5 အဖြစ် `padding=2` နဲ့အတူ ပြောင်းပြီး output spatial size က 16x16 ပဲ ဆက်ရှိနေမယ်ဆိုတာ အတည်ပြုကြည့်ပါ။

You'll know it worked when: `After conv: torch.Size([1, 8, 16, 16])` (padding ကြောင့် spatial size တူတူပဲ ဖြစ်ပြီး channel 8 ခုအထိ ချဲ့ပေးလိုက်တယ်) ပြီးရင် `After pool: torch.Size([1, 8, 8, 8])` (max pooling က height နဲ့ width ကို တစ်ဝက်ချ လျှော့ချလိုက်တယ်) ကို print ထုတ်ပေးတယ်။

Convolutional Neural Networks | Thuta Learning