397 lines
17 KiB
Plaintext
397 lines
17 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",
|
|
"याला **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": [
|
|
"## चरण 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": [
|
|
"## चरण ५: तुमच्या स्वतःच्या प्रतिमा वापरून पहा!\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-rays विश्लेषण)\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:42:40+00:00",
|
|
"source_file": "examples/03-image-classifier.ipynb",
|
|
"language_code": "mr"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |