Project: Image Classification with CNN

इस ब्लॉग में हम step-by-step एक पूरा Image Classification Project बनाएंगे, जिसमें Convolutional Neural Network (CNN) का इस्तेमाल करके real-world dataset को train और evaluate किया जाएगा।

📸 Project: Image Classification with CNN

Computer Vision के field में सबसे popular और fundamental task है Image Classification। इस project में हम Convolutional Neural Networks (CNN) का इस्तेमाल करके एक model बनाएंगे जो images को अलग-अलग categories में classify कर सके। यह project beginner से लेकर advanced learners तक सभी के लिए बहुत जरूरी है क्योंकि इससे deep learning के core concepts practically clear होते हैं।

🔍 Step 1: Problem Understanding

Image Classification का मतलब है कि हमें input image दी जाएगी और हमें बताना होगा कि वह किस category की है। Example: Dog vs Cat, Digits Recognition (MNIST), CIFAR-10 classes आदि।

🗂 Step 2: Dataset Selection

हम यहाँ CIFAR-10 dataset का इस्तेमाल करेंगे, जिसमें 60,000 images (32x32 pixels) हैं और 10 categories: Airplane, Automobile, Bird, Cat, Deer, Dog, Frog, Horse, Ship, Truck।

from tensorflow.keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print("Training Data:", x_train.shape)
print("Test Data:", x_test.shape)
    

⚙️ Step 3: Data Preprocessing

Neural Network को train करने से पहले data preprocessing करनी जरूरी है:

  • Normalization (pixel values को 0-255 से 0-1 में scale करना)
  • One-hot Encoding (labels को categorical format में convert करना)
  • Data Augmentation (images को rotate, flip, zoom करके dataset बड़ा करना)
from tensorflow.keras.utils import to_categorical

x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
    

🧠 Step 4: CNN Model Architecture

CNN model में convolutional layers, pooling layers और fully connected layers होती हैं। नीचे एक sample CNN architecture है:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

model = Sequential([
    Conv2D(32, (3,3), activation="relu", input_shape=(32,32,3)),
    MaxPooling2D((2,2)),
    Conv2D(64, (3,3), activation="relu"),
    MaxPooling2D((2,2)),
    Flatten(),
    Dense(128, activation="relu"),
    Dropout(0.5),
    Dense(10, activation="softmax")
])

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.summary()
    

📈 Step 5: Model Training

Model को train करने के लिए हम Adam Optimizer और Categorical Crossentropy Loss use करेंगे।

history = model.fit(
    x_train, y_train,
    epochs=20,
    batch_size=64,
    validation_data=(x_test, y_test)
)
    

📊 Step 6: Evaluation

Model को test dataset पर evaluate किया जाएगा ताकि हमें accuracy और loss मिल सके।

test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print("Test Accuracy:", test_acc)
    

📉 Step 7: Visualization

Training और validation accuracy/loss graph plot करके हम देख सकते हैं कि model overfit तो नहीं कर रहा।

import matplotlib.pyplot as plt

plt.plot(history.history["accuracy"], label="train_accuracy")
plt.plot(history.history["val_accuracy"], label="val_accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
    

🚀 Step 8: Predictions

Trained model को इस्तेमाल करके हम नई unseen images पर predictions कर सकते हैं।

import numpy as np

predictions = model.predict(x_test[:5])
print(np.argmax(predictions, axis=1))
    

💡 Real-world Applications

  • Medical Image Diagnosis
  • Face Detection and Recognition
  • Traffic Sign Recognition in Self-driving Cars
  • Satellite Image Classification
  • Wildlife Monitoring with Drones

📊 Project Report & Insights

- CNN based models shallow networks से ज्यादा powerful होते हैं।
- Data Augmentation का use करके accuracy improve की जा सकती है।
- Transfer Learning (जैसे VGG, ResNet, MobileNet) future में इस project को और भी powerful बना सकते हैं।

❓ FAQs

Q1: CNN और MLP (Multilayer Perceptron) में क्या अंतर है?
Ans: CNN images से automatically features निकालता है जबकि MLP raw pixels पर काम करता है।

Q2: CIFAR-10 के अलावा कौन से dataset beginner projects के लिए best हैं?
Ans: MNIST, Fashion-MNIST, Tiny-ImageNet।

✅ Conclusion

यह project CNN based image classification को practically implement करने का एक शानदार example है। अगर आप deep learning और computer vision में career बनाना चाहते हैं तो इस तरह के projects आपके portfolio को बहुत strong बना देंगे।