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

397 lines
18 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",
" - [కంప్యూటర్ విజన్ పాఠాలు](../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:04+00:00",
"source_file": "examples/03-image-classifier.ipynb",
"language_code": "te"
}
},
"nbformat": 4,
"nbformat_minor": 4
}