अब आपने deep learning models बनाना, train करना और save/load करना सीख लिया है — अगला ज़रूरी कदम है:Model Evaluation and Deployment
🔷 Part 1: 📊 Model Evaluation
Model को train करने के बाद यह जानना ज़रूरी है कि वो कितना अच्छा काम कर रहा है — इसके लिए हम evaluation metrics का उपयोग करते हैं।
✅ 1. Accuracy (शुद्धता)
- Classification के लिए सबसे आम metric
- Formula:
Accuracy = सही अनुमान / कुल उदाहरण
# Keras
model.evaluate(X_test, y_test)
# PyTorch
with torch.no_grad():
preds = model(X_test)
predicted = preds.round()
acc = (predicted == y_test).float().mean()
✅ 2. Confusion Matrix
Classification output को True Positives, False Positives, True Negatives, और False Negatives में बांटता है।
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_true, y_pred))
✅ 3. Precision, Recall, F1 Score
Metric | Meaning |
---|---|
Precision | Positive prediction में से कितने सही थे |
Recall | कितने actual positive सही पकड़े गए |
F1 Score | Precision और Recall का balance |
from sklearn.metrics import classification_report
print(classification_report(y_true, y_pred))
✅ 4. Loss Curve / Accuracy Curve (Visualization)
import matplotlib.pyplot as plt
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.legend()
plt.show()
🔷 Part 2: 🚀 Model Deployment
Model को दुनिया तक पहुँचाने के लिए हमें उसे deploy करना पड़ता है — यानी किसी app, API, या web server पर चलाना।
✅ 1. Deployment Methods:
Method | Use Case |
---|---|
Flask API | Simple Web/API deployment |
TensorFlow Serving | Production-ready serving |
TorchScript | PyTorch model serialization |
ONNX | Cross-platform compatibility |
TF Lite | Mobile apps (Android/iOS) |
TF.js | Browser-based models |
Gradio / Streamlit | Quick UI + demo |
✅ 2. PyTorch Model Export (TorchScript)
traced_model = torch.jit.trace(model, torch.rand(1, 2))
traced_model.save("model.pt")
Use in any PyTorch-enabled environment.
✅ 3. TensorFlow/Keras Model Export
pythonCopyEditmodel.save("model.h5") # Full model (Keras)
Convert to TF Lite for mobile:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
✅ 4. Simple Flask API for Inference
from flask import Flask, request, jsonify
import torch
app = Flask(__name__)
model = torch.load("model.pt")
model.eval()
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
input_tensor = torch.tensor(data['input'])
with torch.no_grad():
prediction = model(input_tensor).item()
return jsonify({'prediction': prediction})
app.run(debug=True)
✅ 5. Deployment Tools
Tool | Purpose |
---|---|
Docker | Containerize model + environment |
Kubernetes | Deploy at scale |
Heroku | Simple web hosting |
AWS/GCP/Azure | Cloud deployment |
Hugging Face Spaces | Deploy public demos |
📝 Practice Questions:
- Accuracy और F1-score में क्या अंतर है?
- PyTorch में confusion matrix कैसे calculate करते हैं?
- Model को Flask API में कैसे deploy किया जाता है?
torch.jit.trace()
औरmodel.save()
में क्या अंतर है?- TensorFlow Lite का उपयोग कब और क्यों करना चाहिए?
🧠 Summary Table
Task | PyTorch | TensorFlow/Keras |
---|---|---|
Accuracy Eval | (preds == y).float().mean() | model.evaluate() |
Save for Deployment | torch.jit.save() | model.save() |
Mobile | ONNX or TorchScript | TF Lite |
API | Flask/FastAPI | Flask/FastAPI |
Web UI | Gradio / Streamlit | Gradio / Streamlit |