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

397 lines
19 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ലളിതമായ ഇമേജ് ക്ലാസിഫയർ\n",
"\n",
"ഈ നോട്ട്‌ബുക്ക് പ്രീ-ട്രെയിൻ ചെയ്ത ന്യൂറൽ നെറ്റ്‌വർക്ക് ഉപയോഗിച്ച് ചിത്രങ്ങൾ എങ്ങനെ ക്ലാസിഫൈ ചെയ്യാമെന്ന് കാണിക്കുന്നു.\n",
"\n",
"**നിങ്ങൾ പഠിക്കാനിരിക്കുന്നതെന്ത്:**\n",
"- പ്രീ-ട്രെയിൻ ചെയ്ത മോഡൽ എങ്ങനെ ലോഡ് ചെയ്ത് ഉപയോഗിക്കാമെന്ന്\n",
"- ചിത്ര പ്രോസസ്സിംഗ്\n",
"- ചിത്രങ്ങളിൽ പ്രവചനങ്ങൾ നടത്തുന്നത്\n",
"- വിശ്വാസ സ്കോറുകൾ മനസിലാക്കൽ\n",
"\n",
"**ഉപയോഗം:** ചിത്രങ്ങളിൽ ഉള്ള വസ്തുക്കൾ തിരിച്ചറിയുക (ഉദാഹരണത്തിന് \"പൂച്ച\", \"നായ\", \"കാർ\" തുടങ്ങിയവ) \n",
"\n",
"---\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ഘട്ടം 1: ആവശ്യമായ ലൈബ്രറികൾ ഇറക്കുമതി ചെയ്യുക\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": [
"## ഘട്ടം 2: മുൻകൂട്ടി പരിശീലിച്ച മോഡൽ ലോഡ് ചെയ്യുക\n",
"\n",
"നാം **MobileNetV2** ഉപയോഗിക്കും, ഇത് മില്യണുകൾക്കണക്കിന് ചിത്രങ്ങളിൽ മുമ്പ് പരിശീലിപ്പിച്ച ഒരു ന്യൂറൽ നെറ്റ്‌വർക്ക് ആണ്.\n",
"\n",
"ഇത് **ട്രാൻസ്ഫർ ലേണിംഗ്** എന്ന് വിളിക്കുന്നു - മറ്റൊരാൾ പരിശീലിപ്പിച്ച മോഡൽ ഉപയോഗിക്കുന്നത്!\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": [
"## ഘട്ടം 3: സഹായക ഫംഗ്ഷനുകൾ\n",
"\n",
"നമ്മുടെ മോഡലിനായി ചിത്രങ്ങൾ ലോഡ് ചെയ്ത് തയ്യാറാക്കാൻ ഫംഗ്ഷനുകൾ സൃഷ്ടിക്കാം.\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": [
"## ഘട്ടം 4: സാമ്പിൾ ചിത്രങ്ങളിൽ പരീക്ഷിക്കുക\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": [
"## ഘട്ടം 5: നിങ്ങളുടെ സ്വന്തം ചിത്രങ്ങൾ പരീക്ഷിക്കുക!\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. **നാം ഒരു മുൻകൂട്ടി പരിശീലിപ്പിച്ച മോഡൽ ലോഡ് ചെയ്തു** - MobileNetV2 മില്യണുകൾക്കണക്കിന് ചിത്രങ്ങളിൽ പരിശീലിപ്പിച്ചിരിക്കുന്നു \n",
"2. **നാം ചിത്രങ്ങൾ മുൻപ്രോസസ്സ് ചെയ്തു** - മോഡലിനായി അവയുടെ വലിപ്പം മാറ്റി ഫോർമാറ്റ് ചെയ്തു \n",
"3. **മോഡൽ പ്രവചനങ്ങൾ നടത്തി** - 1000 വസ്തു വർഗ്ഗങ്ങൾക്ക് സാധ്യതകൾ പുറത്തുവിട്ടു \n",
"4. **നാം ഫലങ്ങൾ ഡികോഡ് ചെയ്തു** - സംഖ്യകൾ മനുഷ്യർക്ക് വായിക്കാൻ കഴിയുന്ന ലേബലുകളാക്കി മാറ്റി \n",
"\n",
"### ആത്മവിശ്വാസ സ്കോറുകൾ മനസ്സിലാക്കൽ\n",
"\n",
"- **90-100%**: വളരെ ആത്മവിശ്വാസമുള്ളത് (ഏകദേശം തീർച്ചയായും ശരി) \n",
"- **70-90%**: ആത്മവിശ്വാസമുള്ളത് (സാധാരണയായി ശരി) \n",
"- **50-70%**: കുറച്ച് ആത്മവിശ്വാസമുള്ളത് (ശരി ആകാമെന്ന് തോന്നുന്നു) \n",
"- **50% താഴെ**: അധികം ആത്മവിശ്വാസമില്ലാത്തത് (അസാധുത) \n",
"\n",
"### പ്രവചനങ്ങൾ തെറ്റായേക്കാനുള്ള കാരണങ്ങൾ?\n",
"\n",
"- **അസാധാരണ കോണോ പ്രകാശനോ** - മോഡൽ സാധാരണ ഫോട്ടോകളിൽ പരിശീലിപ്പിച്ചിരിക്കുന്നു \n",
"- **പല വസ്തുക്കൾ** - മോഡൽ ഒരു പ്രധാന വസ്തുവിനെ പ്രതീക്ഷിക്കുന്നു \n",
"- **അസാധാരണ വസ്തുക്കൾ** - മോഡൽ 1000 വർഗ്ഗങ്ങൾ മാത്രമേ അറിയൂ \n",
"- **താഴ്ന്ന ഗുണമേന്മയുള്ള ചിത്രം** - മങ്ങിയതോ പിക്‌സലേറ്റായതോ ആയ ചിത്രങ്ങൾ കഠിനമാണ് \n",
"\n",
"---\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🚀 അടുത്ത ഘട്ടങ്ങൾ\n",
"\n",
"1. **വിവിധ ചിത്രങ്ങൾ പരീക്ഷിക്കുക:**\n",
" - [Unsplash](https://unsplash.com) ൽ ചിത്രങ്ങൾ കണ്ടെത്തുക\n",
" - റൈറ്റ്-ക്ലിക്ക് → \"Copy image address\" തിരഞ്ഞെടുക്കുക URL ലഭിക്കാൻ\n",
"\n",
"2. **പരീക്ഷണം നടത്തുക:**\n",
" - ആബ്സ്ട്രാക്റ്റ് ആർട്ടുമായി എന്ത് സംഭവിക്കും?\n",
" - വ്യത്യസ്ത കോണുകളിൽ നിന്നുള്ള വസ്തുക്കൾ തിരിച്ചറിയാൻ കഴിയും吗?\n",
" - ഒരേ സമയം പല വസ്തുക്കളെ എങ്ങനെ കൈകാര്യം ചെയ്യുന്നു?\n",
"\n",
"3. **കൂടുതൽ പഠിക്കുക:**\n",
" - [Computer Vision lessons](../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-റേ വിശകലനം)\n",
"- ഗുണനിലവാര നിയന്ത്രണം (ദോഷങ്ങൾ കണ്ടെത്തൽ)\n",
"\n",
"തുടർന്ന് അന്വേഷിക്കുകയും പഠിക്കുകയും ചെയ്യുക! 🚀\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n\n<!-- CO-OP TRANSLATOR DISCLAIMER START -->\n**അസൂയാ**: \nഈ രേഖ AI വിവർത്തന സേവനം [Co-op Translator](https://github.com/Azure/co-op-translator) ഉപയോഗിച്ച് വിവർത്തനം ചെയ്തതാണ്. നാം കൃത്യതയ്ക്ക് ശ്രമിച്ചെങ്കിലും, സ്വയം പ്രവർത്തിക്കുന്ന വിവർത്തനങ്ങളിൽ പിശകുകൾ അല്ലെങ്കിൽ തെറ്റുകൾ ഉണ്ടാകാമെന്ന് ദയവായി ശ്രദ്ധിക്കുക. അതിന്റെ മാതൃഭാഷയിലുള്ള യഥാർത്ഥ രേഖയാണ് പ്രാമാണികമായ ഉറവിടം എന്ന് പരിഗണിക്കേണ്ടതാണ്. നിർണായകമായ വിവരങ്ങൾക്ക്, പ്രൊഫഷണൽ മനുഷ്യ വിവർത്തനം ശുപാർശ ചെയ്യപ്പെടുന്നു. ഈ വിവർത്തനം ഉപയോഗിക്കുന്നതിൽ നിന്നുണ്ടാകുന്ന ഏതെങ്കിലും തെറ്റിദ്ധാരണകൾക്കോ വ്യാഖ്യാനക്കേടുകൾക്കോ ഞങ്ങൾ ഉത്തരവാദികളല്ല.\n<!-- CO-OP TRANSLATOR DISCLAIMER END -->\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-11-25T23:46:39+00:00",
"source_file": "examples/03-image-classifier.ipynb",
"language_code": "ml"
}
},
"nbformat": 4,
"nbformat_minor": 4
}