AI-For-Beginners/translations/my/examples/03-image-classifier.ipynb

395 lines
19 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ရိုးရှင်းသော ပုံရိပ် ခွဲခြားစနစ်\n",
"\n",
"ဒီ notebook က သင့်ကို အကြိုပြင်ဆင်ထားသော နာရူရယ်ကွန်ယက်ကို အသုံးပြုပြီး ပုံရိပ်များကို ခွဲခြားပုံပြောပြပေးပါမယ်။\n",
"\n",
"**သင်လေ့လာနိုင်မယ့်အရာများ:**\n",
"- အကြိုပြင်ဆင်ထားသော မော်ဒယ်ကို ဘယ်လို load လုပ်ပြီး အသုံးပြုမလဲ\n",
"- ပုံရိပ်များကို ကြိုတင်ပြင်ဆင်ခြင်း\n",
"- ပုံရိပ်များအပေါ် အတိအကျခန့်မှန်းချက်များ ပြုလုပ်ခြင်း\n",
"- ယုံကြည်မှုအဆင့်များကို နားလည်ခြင်း\n",
"\n",
"**အသုံးပြုမှုကိစ္စ:** ပုံရိပ်များအတွင်းရှိ အရာဝတ္ထုများ (ဥပမာ \"ကြောင်\", \"ခွေး\", \"ကား\" စသည်) ကို ဖော်ထုတ်ခြင်း\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## အဆင့် ၁ - လိုအပ်သော စာကြည့်တိုက်များကို တင်သွင်းပါ\n",
"\n",
"လိုအပ်သော ကိရိယာများကို တင်သွင်းကြမယ်။ အားလုံးကို မနားလည်သေးရင် စိတ်မပူပါနဲ့!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Core libraries\n",
"import numpy as np\n",
"from PIL import Image\n",
"import requests\n",
"from io import BytesIO\n",
"\n",
"# TensorFlow for deep learning\n",
"try:\n",
" import tensorflow as tf\n",
" from tensorflow.keras.applications import MobileNetV2\n",
" from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions\n",
" print(\"✅ TensorFlow loaded successfully!\")\n",
" print(f\" Version: {tf.__version__}\")\n",
"except ImportError:\n",
" print(\"❌ Please install TensorFlow: pip install tensorflow\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## အဆင့် ၂ - ကြိုတင်လေ့ကျင့်ထားသော မော်ဒယ်ကို Load လုပ်ပါ\n",
"\n",
"ကျွန်တော်တို့ **MobileNetV2** ကို အသုံးပြုပါမယ်၊ ဒါဟာ သန်းပေါင်းများစွာသော ပုံများပေါ်မှာ ရှိပြီးသား Neural Network တစ်ခုဖြစ်ပါတယ်။\n",
"\n",
"ဒါကို **Transfer Learning** လို့ခေါ်ပါတယ် - တစ်ခြားသူလေ့ကျင့်ထားတဲ့ မော်ဒယ်ကို အသုံးပြုတာပါ!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"📦 Loading pre-trained MobileNetV2 model...\")\n",
"print(\" This may take a minute on first run (downloading weights)...\")\n",
"\n",
"# Load the model\n",
"# include_top=True means we use the classification layer\n",
"# weights='imagenet' means it was trained on ImageNet dataset\n",
"model = MobileNetV2(weights='imagenet', include_top=True)\n",
"\n",
"print(\"✅ Model loaded!\")\n",
"print(f\" The model can recognize 1000 different object categories\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## အဆင့် ၃ - အကူအညီပေးသော Function များ\n",
"\n",
"အကြောင်းအရာများကို မော်ဒယ်အတွက် load လုပ်ပြီး ပြင်ဆင်ရန် Function များကို ဖန်တီးကြမယ်။\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def load_image_from_url(url):\n",
" \"\"\"\n",
" Load an image from a URL.\n",
" \n",
" Args:\n",
" url: Web address of the image\n",
" \n",
" Returns:\n",
" PIL Image object\n",
" \"\"\"\n",
" response = requests.get(url)\n",
" img = Image.open(BytesIO(response.content))\n",
" return img\n",
"\n",
"\n",
"def prepare_image(img):\n",
" \"\"\"\n",
" Prepare an image for the model.\n",
" \n",
" Steps:\n",
" 1. Resize to 224x224 (model's expected size)\n",
" 2. Convert to array\n",
" 3. Add batch dimension\n",
" 4. Preprocess for MobileNetV2\n",
" \n",
" Args:\n",
" img: PIL Image\n",
" \n",
" Returns:\n",
" Preprocessed image array\n",
" \"\"\"\n",
" # Resize to 224x224 pixels\n",
" img = img.resize((224, 224))\n",
" \n",
" # Convert to numpy array\n",
" img_array = np.array(img)\n",
" \n",
" # Add batch dimension (model expects multiple images)\n",
" img_array = np.expand_dims(img_array, axis=0)\n",
" \n",
" # Preprocess for MobileNetV2\n",
" img_array = preprocess_input(img_array)\n",
" \n",
" return img_array\n",
"\n",
"\n",
"def classify_image(img):\n",
" \"\"\"\n",
" Classify an image and return top predictions.\n",
" \n",
" Args:\n",
" img: PIL Image\n",
" \n",
" Returns:\n",
" List of (class_name, confidence) tuples\n",
" \"\"\"\n",
" # Prepare the image\n",
" img_array = prepare_image(img)\n",
" \n",
" # Make prediction\n",
" predictions = model.predict(img_array, verbose=0)\n",
" \n",
" # Decode predictions to human-readable labels\n",
" # top=5 means we get the top 5 most likely classes\n",
" decoded = decode_predictions(predictions, top=5)[0]\n",
" \n",
" # Convert to simpler format\n",
" results = [(label, float(confidence)) for (_, label, confidence) in decoded]\n",
" \n",
" return results\n",
"\n",
"\n",
"print(\"✅ Helper functions ready!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## အဆင့် ၄ - နမူနာပုံများကို စမ်းသပ်ပါ\n",
"\n",
"အင်တာနက်မှ ပုံများကို သတ်မှတ်ကြည့်ရအောင်!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Sample images to classify\n",
"# These are from Unsplash (free stock photos)\n",
"test_images = [\n",
" {\n",
" \"url\": \"https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?w=400\",\n",
" \"description\": \"A cat\"\n",
" },\n",
" {\n",
" \"url\": \"https://images.unsplash.com/photo-1552053831-71594a27632d?w=400\",\n",
" \"description\": \"A dog\"\n",
" },\n",
" {\n",
" \"url\": \"https://images.unsplash.com/photo-1511919884226-fd3cad34687c?w=400\",\n",
" \"description\": \"A car\"\n",
" },\n",
"]\n",
"\n",
"print(f\"🧪 Testing on {len(test_images)} images...\")\n",
"print(\"=\" * 70)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ရုပ်ပုံတစ်ခုချင်းစီကို အမျိုးအစားခွဲပါ\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i, img_data in enumerate(test_images, 1):\n",
" print(f\"\\n📸 Image {i}: {img_data['description']}\")\n",
" print(\"-\" * 70)\n",
" \n",
" try:\n",
" # Load image\n",
" img = load_image_from_url(img_data['url'])\n",
" \n",
" # Display image\n",
" display(img.resize((200, 200))) # Show smaller version\n",
" \n",
" # Classify\n",
" results = classify_image(img)\n",
" \n",
" # Show predictions\n",
" print(\"\\n🎯 Top 5 Predictions:\")\n",
" for rank, (label, confidence) in enumerate(results, 1):\n",
" # Create a visual bar\n",
" bar_length = int(confidence * 50)\n",
" bar = \"█\" * bar_length\n",
" \n",
" print(f\" {rank}. {label:20s} {confidence*100:5.2f}% {bar}\")\n",
" \n",
" except Exception as e:\n",
" print(f\"❌ Error: {e}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 70)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## အဆင့် ၅: သင့်ပုံများကို စမ်းကြည့်ပါ!\n",
"\n",
"အောက်ပါ URL ကို သင့်ရဲ့ အမျိုးအစားသတ်မှတ်လိုသော ပုံ URL တစ်ခုဖြင့် အစားထိုးပါ။\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Try your own image!\n",
"# Replace this URL with any image URL\n",
"custom_image_url = \"https://images.unsplash.com/photo-1472491235688-bdc81a63246e?w=400\" # A flower\n",
"\n",
"print(\"🖼️ Classifying your custom image...\")\n",
"print(\"=\" * 70)\n",
"\n",
"try:\n",
" # Load and show image\n",
" img = load_image_from_url(custom_image_url)\n",
" display(img.resize((300, 300)))\n",
" \n",
" # Classify\n",
" results = classify_image(img)\n",
" \n",
" # Show results\n",
" print(\"\\n🎯 Top 5 Predictions:\")\n",
" print(\"-\" * 70)\n",
" for rank, (label, confidence) in enumerate(results, 1):\n",
" bar_length = int(confidence * 50)\n",
" bar = \"█\" * bar_length\n",
" print(f\" {rank}. {label:20s} {confidence*100:5.2f}% {bar}\")\n",
" \n",
" # Highlight top prediction\n",
" top_label, top_confidence = results[0]\n",
" print(\"\\n\" + \"=\" * 70)\n",
" print(f\"\\n🏆 Best guess: {top_label} ({top_confidence*100:.2f}% confident)\")\n",
" \n",
"except Exception as e:\n",
" print(f\"❌ Error: {e}\")\n",
" print(\" Make sure the URL points to a valid image!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 💡 ဘာတွေဖြစ်ခဲ့တာလဲ?\n",
"\n",
"1. **Pre-trained model ကို load လုပ်ခဲ့တယ်** - MobileNetV2 ကို သန်းပေါင်းများစွာသော ပုံများပေါ်မှာ training လုပ်ထားပါတယ်။\n",
"2. **ပုံတွေကို preprocess လုပ်ခဲ့တယ်** - Model အတွက် အရွယ်အစားကို ပြင်ဆင်ပြီး format လုပ်ပေးခဲ့တယ်။\n",
"3. **Model က ခန့်မှန်းချက်တွေ ပြုလုပ်ခဲ့တယ်** - 1000 object class အတွက် probability တွေကို output ထုတ်ပေးခဲ့တယ်။\n",
"4. **ရလဒ်တွေကို decode လုပ်ခဲ့တယ်** - နံပါတ်တွေကို လူနားလည်နိုင်တဲ့ label တွေကို ပြောင်းပေးခဲ့တယ်။\n",
"\n",
"### Confidence Score ကို နားလည်ခြင်း\n",
"\n",
"- **90-100%**: အရမ်းယုံကြည်မှုရှိ (မှန်မယ်လို့ အတော်လေးသေချာ)\n",
"- **70-90%**: ယုံကြည်မှုရှိ (မှန်နိုင်တယ်)\n",
"- **50-70%**: တစ်ချို့ယုံကြည်မှုရှိ (မှန်နိုင်တယ်)\n",
"- **50% အောက်**: ယုံကြည်မှုမရှိသလောက် (မသေချာ)\n",
"\n",
"### ဘာကြောင့် ခန့်မှန်းချက်တွေ မှားနိုင်သလဲ?\n",
"\n",
"- **Angle သို့မဟုတ် အလင်းအရောင် မမှန်ခြင်း** - Model ကို ပုံမှန်ဓာတ်ပုံတွေမှာ training လုပ်ထားတာပါ။\n",
"- **Object အများကြီးပါဝင်ခြင်း** - Model က အဓိက object တစ်ခုကိုသာ မျှော်လင့်ထားပါတယ်။\n",
"- **ရှားပါး object တွေ** - Model က category 1000 ခုသာ သိပါတယ်။\n",
"- **ပုံအရည်အသွေး မကောင်းခြင်း** - မိုဃ်းမဲတဲ့ပုံတွေ သို့မဟုတ် pixelated ပုံတွေကို ခန့်မှန်းဖို့ ပိုခက်ပါတယ်။\n",
"\n",
"---\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🚀 နောက်တစ်ဆင့်လုပ်ဆောင်ရန်\n",
"\n",
"1. **ပုံအမျိုးမျိုးကို စမ်းကြည့်ပါ:**\n",
" - [Unsplash](https://unsplash.com) မှ ပုံများရှာဖွေပါ\n",
" - Right-click → \"Copy image address\" ကိုနှိပ်ပြီး URL ကိုရယူပါ\n",
"\n",
"2. **စမ်းသပ်ကြည့်ပါ:**\n",
" - အ抽象ပန်းချီပုံများတွင် ဘာဖြစ်မလဲ?\n",
" - အမြင်အမျိုးမျိုးမှ အရာဝတ္ထုများကို မှတ်မိနိုင်ပါသလား?\n",
" - အရာဝတ္ထုများစွာကို တစ်ခါတည်း တွေ့ရှိနိုင်ပါသလား?\n",
"\n",
"3. **ပိုမိုလေ့လာပါ:**\n",
" - [Computer Vision သင်ခန်းစာများ](../lessons/4-ComputerVision/README.md) ကို စူးစမ်းပါ\n",
" - သင့်ကိုယ်ပိုင် ပုံခွဲခြားစနစ်ကို လေ့ကျင့်ပေးပါ\n",
" - CNNs (Convolutional Neural Networks) အလုပ်လုပ်ပုံကို နားလည်ပါ\n",
"\n",
"---\n",
"\n",
"## 🎉 ဂုဏ်ယူပါတယ်!\n",
"\n",
"သင်သည် နောက်ဆုံးပေါ် နယူးရယ်နက်ဝက်ကို အသုံးပြု၍ ပုံခွဲခြားစနစ်တစ်ခုကို တည်ဆောက်ပြီးပါပြီ!\n",
"\n",
"ဒီနည်းပညာတစ်ခုသည် အောက်ပါအရာများကို အားပေးပါသည်။\n",
"- Google Photos (သင့်ရဲ့ပုံများကို စီစဉ်ပေးခြင်း)\n",
"- ကိုယ်တိုင်မောင်းနှင်သော ကားများ (အရာဝတ္ထုများကို မှတ်မိခြင်း)\n",
"- ဆေးဘက်ဆိုင်ရာ ရောဂါရှာဖွေခြင်း (X-ray များကို ခွဲခြားစစ်ဆေးခြင်း)\n",
"- အရည်အသွေးထိန်းချုပ်မှု (အနာအဆာများကို ရှာဖွေခြင်း)\n",
"\n",
"ဆက်လက် စူးစမ်းလေ့လာပါ! 🚀\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n---\n\n**အကြောင်းကြားချက်**: \nဤစာရွက်စာတမ်းကို AI ဘာသာပြန်ဝန်ဆောင်မှု [Co-op Translator](https://github.com/Azure/co-op-translator) ကို အသုံးပြု၍ ဘာသာပြန်ထားပါသည်။ ကျွန်ုပ်တို့သည် တိကျမှုအတွက် ကြိုးစားနေသော်လည်း၊ အလိုအလျောက် ဘာသာပြန်မှုများတွင် အမှားများ သို့မဟုတ် မတိကျမှုများ ပါဝင်နိုင်သည်ကို သတိပြုပါ။ မူရင်းဘာသာစကားဖြင့် ရေးသားထားသော စာရွက်စာတမ်းကို အာဏာတရ အရင်းအမြစ်အဖြစ် သတ်မှတ်သင့်ပါသည်။ အရေးကြီးသော အချက်အလက်များအတွက် လူက ဘာသာပြန်မှုကို အသုံးပြုရန် အကြံပြုပါသည်။ ဤဘာသာပြန်မှုကို အသုံးပြုခြင်းမှ ဖြစ်ပေါ်လာသော အလွဲအလွတ်များ သို့မဟုတ် အနားယူမှုများအတွက် ကျွန်ုပ်တို့သည် တာဝန်မယူပါ။\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
},
"coopTranslator": {
"original_hash": "1d472141d9df46b751542b3c29f88677",
"translation_date": "2025-10-03T11:55:04+00:00",
"source_file": "examples/03-image-classifier.ipynb",
"language_code": "my"
}
},
"nbformat": 4,
"nbformat_minor": 4
}