Thuta Learning
Computer Vision
IntermediateAIintermediate

Object Detection အခြေခံ

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

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

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

Object detection သည် classification ထက် လုပ်ငန်းတာဝန် ကွာခြားသည် — image တစ်ခုလုံးအတွက် label တစ်ခုတည်း ထုတ်ပေးမည့်အစား, object တစ်ခုစီ၏ 'ဘယ်မှာရှိသလဲ' (location) နှင့် 'ဘာဖြစ်သလဲ' (class) နှစ်မျိုးလုံးကို တစ်ပြိုင်နက်တည်း ခန့်မှန်းရသည်။ Location ကို bounding box (x1, y1, x2, y2) — top-left corner နှင့် bottom-right corner coordinate — အဖြစ် ကိုယ်စားပြုလေ့ရှိသည်။ Detection model ၏ output box သည် ground-truth box နှင့် ဘယ်လောက် ကောင်းစွာကိုက်ညီသလဲဆိုတာကို quantify လုပ်ဖို့ လိုအပ်လာသောအခါ IoU (Intersection over Union) ဟူသော metric ကို သုံးသည် — box နှစ်ခု ထပ်နေတဲ့ area ကို box နှစ်ခုပေါင်း occupy လုပ်ထားတဲ့ total area (union) နှင့် စားလိုက်ခြင်းသာ ဖြစ်သည်။

IoU value ၏ range သည် 0 (လုံးဝ မထပ်ဘူး) မှ 1 (box နှစ်ခု အတိအကျ တစ်ထပ်တည်း) အထိ ဖြစ်သည်။ Box နှစ်ခုသည် အလွန်ကြီးမားပြီး ခပ်ဝေးဝေး offset ရှိနေခဲ့ရင်တောင် intersection area သာ ကြီးမားနေခဲ့ရင် plain intersection-area metric တစ်ခုတည်းက misleading ဖြစ်နိုင်သော်လည်း, union နှင့် normalize လုပ်ခြင်းက box size ကွာခြားမှုကို account ထားပေးသောကြောင့် box size ဘယ်လောက်ပဲကြီးကြီး/သေးသေး fair comparison ဖြစ်စေသည်။ ဒါကြောင့် IoU သည် object detection evaluation (mAP) အားလုံး၏ building block ဖြစ်ပြီး, non-maximum suppression (ထပ်နေသော candidate box များကို ဖယ်ရှားရာတွင်) မှာလည်း core operation အဖြစ် အသုံးပြုသည်။

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

Tutorial Platform တွင် lesson screenshot image များထဲမှ code block area ကို auto-detect လုပ်ပြီး thumbnail preview ထုတ်ရာတွင် ထို area ကို ဗဟိုပြု၍ auto-crop လုပ်ပေးမည့် feature တစ်ခု တည်ဆောက်နေသည်ဆိုပါစို့။ Model ၏ predicted code-block box သည် hand-labeled ground-truth box နှင့် ဘယ်လောက် ကောင်းစွာကိုက်ညီသလဲကို quantify လုပ်ရန် ဒီ lesson ၏ IoU function ကို တိုက်ရိုက် သုံးနိုင်ပြီး, IoU မြင့်မှသာ auto-crop ကို production တွင် trust လုပ်နိုင်မည်။

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

python
import torch

def compute_iou(box1, box2):
    x1 = torch.max(box1[0], box2[0])
    y1 = torch.max(box1[1], box2[1])
    x2 = torch.min(box1[2], box2[2])
    y2 = torch.min(box1[3], box2[3])

    inter_width = (x2 - x1).clamp(min=0)
    inter_height = (y2 - y1).clamp(min=0)
    intersection = inter_width * inter_height

    area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
    union = area1 + area2 - intersection

    return intersection / union

# Two 10x10 boxes that overlap in a 5x10 strip
box_a = torch.tensor([0.0, 0.0, 10.0, 10.0])
box_b = torch.tensor([5.0, 0.0, 15.0, 10.0])

iou = compute_iou(box_a, box_b)
print(f"IoU: {iou.item():.4f}")
You should see
'IoU: 0.3333' ဟု print ထုတ်မည်။ Box နှစ်ခုစလုံး area = 100 (10×10) စီရှိပြီး၊ overlap region သည် x: 5-10, y: 0-10 ဖြစ်သောကြောင့် intersection = 5×10 = 50, union = 100+100-50 = 150 ဖြစ်ကာ IoU = 50/150 = 0.3333 ဖြစ်သည်။

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

compute_iou function ကို batch-aware ပုံစံအဖြစ် ပြင်ဆင်ကြည့်ပါ — box_a shape (N, 4) နှင့် box_b shape (N, 4) ကို လက်ခံပြီး element-wise IoU shape (N,) ကို broadcasting ဖြင့် တစ်ခါတည်း ထုတ်ပေးနိုင်အောင် torch.max/torch.min ကို dim parameter ထည့်ပြင်ဆင်ပါ။

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

inter_width/inter_height ကို clamp(min=0) မလုပ်ဘဲ ချန်ထားလျှင် box နှစ်ခု လုံးဝ မထပ်ဘူးဆိုရင် negative width/height ကြောင့် intersection area negative ဖြစ်ကာ IoU ကို negative value ဖြစ်စေနိုင်သည်။

Box coordinate ကို (x1, y1, width, height) format ဖြင့် ရေးထားသော dataset (ဥပမာ COCO-style annotation အချို့) ကို (x1, y1, x2, y2) ဟု မှားယူဆလိုက်လျှင် IoU calculation တစ်ခုလုံး လွဲမှားသွားနိုင်သည်။

Wikipedia — Object detectionComputer Vision

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

  • inter_width/inter_height ကို clamp(min=0) မလုပ်ဘဲ ချန်ထားလျှင် box နှစ်ခု လုံးဝ မထပ်ဘူးဆိုရင် negative width/height ကြောင့် intersection area negative ဖြစ်ကာ IoU ကို negative value ဖြစ်စေနိုင်သည်။
  • Box coordinate ကို (x1, y1, width, height) format ဖြင့် ရေးထားသော dataset (ဥပမာ COCO-style annotation အချို့) ကို (x1, y1, x2, y2) ဟု မှားယူဆလိုက်လျှင် IoU calculation တစ်ခုလုံး လွဲမှားသွားနိုင်သည်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

compute_iou function ကို batch-aware ပုံစံအဖြစ် ပြင်ဆင်ကြည့်ပါ — box_a shape (N, 4) နှင့် box_b shape (N, 4) ကို လက်ခံပြီး element-wise IoU shape (N,) ကို broadcasting ဖြင့် တစ်ခါတည်း ထုတ်ပေးနိုင်အောင် torch.max/torch.min ကို dim parameter ထည့်ပြင်ဆင်ပါ။

You'll know it worked when: 'IoU: 0.3333' ဟု print ထုတ်မည်။ Box နှစ်ခုစလုံး area = 100 (10×10) စီရှိပြီး၊ overlap region သည် x: 5-10, y: 0-10 ဖြစ်သောကြောင့် intersection = 5×10 = 50, union = 100+100-50 = 150 ဖြစ်ကာ IoU = 50/150 = 0.3333 ဖြစ်သည်။

Object Detection အခြေခံ | Thuta Learning