معاينة مختبر آمنة
تصنيف وجوه الحيوانات الأليفة
هذي معاينة منقّحة للقراءة فقط؛ ما فيه أي شيء يشتغل داخل الصفحة.
تعليمات المختبر
تصنيف وجوه الحيوانات الأليفة
طبّقوا مفاهيم الدرس في هالمختبر الموجّه.
المهمة
تخيّلوا إنكم بتطوّرون تطبيقًا لحضانة حيوانات أليفة يسجّل بيانات كل حيوان. من الميزات الممتازة في تطبيق زي كذا إنه يكتشف السلالة تلقائيًا من الصورة. ونقدر ننجز هالمهمة بنجاح باستخدام الشبكات العصبية.
المطلوب إنكم تدرّبون الشبكة العصبية الالتفافية عشان تصنّف سلالات مختلفة من القطط والكلاب باستخدام مجموعة البيانات Pet Faces.
مجموعة البيانات
بنستخدم مجموعة البيانات Oxford-IIIT Pet Dataset، وفيها صور لـ 37 سلالة مختلفة من الكلاب والقطط.
وصف الشكل: مجموعة البيانات اللي بنتعامل معها
نزّلوا مجموعة البيانات بهالمقطع البرمجي:
!
قراءة فقط
معاينة الدفتر
Pet Faces
# Generated pet images for deterministic, rights-safe execution.
from pathlib import Path
from PIL import Image
import numpy as np
image_root = Path("images")
image_root.mkdir(parents=True, exist_ok=True)
for breed_index, breed in enumerate(("Abyssinian", "Maine_Coon", "beagle", "pug")):
for image_index in range(1, 4):
yy, xx = np.mgrid[:64, :64]
pixels = np.zeros((64, 64, 3), dtype=np.uint8)
pixels[..., 0] = (xx * (2 + breed_index) + 23 * image_index) % 256
pixels[..., 1] = (yy * (3 + image_index) + 31 * breed_index) % 256
pixels[..., 2] = ((xx + yy) * 2 + 47 * breed_index) % 256
Image.fromarray(pixels).save(image_root / f"{breed}_{image_index}.jpg")
print("Prepared 12 generated pet images")import matplotlib.pyplot as plt
import os
from PIL import Image
import numpy as np
def display_images(images, titles=None, fontsize=12):
images = list(images)[:24]
if not images:
return
columns = min(len(images), 6)
rows = (len(images) + columns - 1) // columns
fig, axes = plt.subplots(rows, columns, squeeze=False, figsize=(columns * 2.5, rows * 2.5))
flat_axes = axes.reshape(-1)
for index, image in enumerate(images):
flat_axes[index].imshow(image)
flat_axes[index].axis("off")
if titles is not None and index < len(titles):
flat_axes[index].set_title(titles[index], fontsize=fontsize)
for axis in flat_axes[len(images):]:
axis.axis("off")
plt.tight_layout()
plt.show()# Note: The Oxford-IIIT Pet Dataset extracts to a folder named 'images'
# Images are named by breed (e.g., 'Abyssinian_1.jpg')
# We need to organize them into breed-specific subdirectories
import os
from collections import defaultdict
# Organize images by breed
if not os.path.exists('petfaces'):
os.makedirs('petfaces')
for img_file in os.listdir('images'):
if img_file.endswith(('.jpg', '.png')):
# Extract breed name from filename (everything before the last underscore and number)
breed = '_'.join(img_file.split('_')[:-1])
breed_dir = os.path.join('petfaces', breed)
if not os.path.exists(breed_dir):
os.makedirs(breed_dir)
os.rename(os.path.join('images', img_file), os.path.join(breed_dir, img_file))
for cls in os.listdir('petfaces'):
print(cls)
display_images([Image.open(os.path.join('petfaces',cls,x))
for x in os.listdir(os.path.join('petfaces',cls))[:10]])num_classes = len(os.listdir('petfaces'))
num_classes# CODE TO LOAD DATASET# CODE TO DO TRAIN/TEST SPLIT# Print tensor sizes# Display the data# CODE TO DEFINE NEURAL NETWORK# TRAIN THE NETWORK# PLOT THE ACCURACY on train and validation dataset# CALCULATE TOP-3 ACCURACY# Define dataset that contains only two labels: 0 = cat, 1 = dog
# Hint: use class name prefix to figure out which one is which# Define neural network architecture and trainحذفنا المخرجات وعدّادات التشغيل والودجات والمحتوى النشط وقت الاستيراد. شغّل الدفاتر بس في بيئة خارجية تثق فيها.
سجّل تطبيقك
التسجيل اختياري، يفيدك تتذكر وش طبّقت، ولا يمنع إكمال الدورة.