পাঠ ৪২ · ৪৫-এর মধ্যে · মডিউল ৫
Home / AI Courses / Machine Learning / Pipeline & Deployment

Pipeline ও Model Deployment

Pipelines & deployment — production ML
৭ মিনিট পড়া মাঝারি · Intermediate sklearn / FastAPI

এই পাঠে যা শিখবেন

  • Pipeline-এর core idea — leak prevention
  • ColumnTransformer — heterogeneous data
  • Cross-validation pipeline-এর ভেতরে — proper CV
  • FastAPI দিয়ে REST endpoint
  • Model versioning, monitoring, drift

১ · Why pipeline?

Naive ML code:

scaler = StandardScaler().fit(X_train)
X_train_s = scaler.transform(X_train)
X_test_s = scaler.transform(X_test)
model = LogisticRegression().fit(X_train_s, y_train)
preds = model.predict(X_test_s)

কাজ করে — কিন্তু dangerous। CV-এ যদি accidentally fit_transform(X) use করেন (full data), test info leak। 100 line-এর এর project-এ এই bug সাধারণ।

Pipeline: preprocessing + model একসাথে। CV-এ each fold-এ pipeline পুরো refit। Leak প্রায় impossible।

Pipeline = leak-proof + reproducible

Pipeline একটি single object যা fit() ও predict() support করে। Train data দিয়ে fit, test data দিয়ে predict — পুরো preprocessing-চেইন automatic apply। CV-এ প্রতিটি fold-এ separate fit। Production-এ একই pipeline serialize ও deploy।

২ · Basic Pipeline

Python · sklearn
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score

X, y = make_classification(n_samples=500, n_features=20, random_state=0)

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("clf",   LogisticRegression(max_iter=1000)),
])

# CV proper — each fold scaler fit fresh
scores = cross_val_score(pipe, X, y, cv=5, scoring="roc_auc")
print(f"CV AUC: {scores.mean():.3f} ± {scores.std():.3f}")

pipe.fit(X, y)
print(f"Full data score: {pipe.score(X, y):.3f}")

    
Pipeline একটি object — CV-এ প্রতি fold-এর জন্য fresh fit। Step-গুলো named (scale, clf) — hyperparameter access সহজ (scale__with_mean, clf__C)।

৩ · ColumnTransformer — heterogeneous data

বাস্তব data — numeric, categorical, text একসাথে। Different transformation দরকার:

Python · sklearn
import pandas as pd
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier

# Synthetic Bangladesh loan dataset
np.random.seed(0)
n = 1000
df = pd.DataFrame({
    "age":         np.random.randint(20, 65, n),
    "income":      np.random.lognormal(10, 0.6, n),
    "education":   np.random.choice(["primary", "secondary", "graduate"], n),
    "occupation":  np.random.choice(["farmer", "service", "business", "other"], n),
    "credit_history": np.random.choice([0, 1, np.nan], n, p=[0.3, 0.6, 0.1]),
})
y = (df["income"] > df["income"].median()).astype(int).values

numeric_features = ["age", "income"]
categorical_features = ["education", "occupation"]

numeric_pipe = Pipeline([
    ("impute", SimpleImputer(strategy="median")),
    ("scale", StandardScaler()),
])
categorical_pipe = Pipeline([
    ("impute", SimpleImputer(strategy="most_frequent")),
    ("onehot", OneHotEncoder(handle_unknown="ignore")),
])

preprocessor = ColumnTransformer([
    ("num", numeric_pipe, numeric_features),
    ("cat", categorical_pipe, categorical_features),
])

full_pipe = Pipeline([
    ("preprocess", preprocessor),
    ("clf", RandomForestClassifier(n_estimators=100, random_state=0)),
])

full_pipe.fit(df, y)
print(f"Pipeline trained — score: {full_pipe.score(df, y):.3f}")
print(f"Number of features after preprocessing: {full_pipe[:-1].transform(df).shape[1]}")

    
ColumnTransformer numeric column-এ scale, categorical-এ one-hot — leak-free। নতুন data point arrive করলে — same pipeline-এ predict, manual preprocessing লাগবে না।

৪ · Pipeline + HPO + CV

L41-এর hyperparameter tuning Pipeline-এর সাথে natural। Hyperparameter name step_name__param format-এ:

Python · sklearn
from sklearn.model_selection import GridSearchCV

param_grid = {
    "clf__n_estimators": [50, 100, 200],
    "clf__max_depth":    [5, 10, None],
}

gs = GridSearchCV(full_pipe, param_grid, cv=3, n_jobs=-1, scoring="roc_auc")
gs.fit(df, y)
print(f"Best params: {gs.best_params_}")
print(f"Best CV score: {gs.best_score_:.4f}")

    

৫ · Model serialization

Python · joblib
import joblib

# Save trained pipeline
joblib.dump(full_pipe, "loan_model_v1.joblib")

# Later — production load
loaded = joblib.load("loan_model_v1.joblib")
new_applicant = pd.DataFrame([{
    "age": 35, "income": 50000,
    "education": "graduate", "occupation": "service",
    "credit_history": 1
}])
prob = loaded.predict_proba(new_applicant)[0, 1]
print(f"Default probability: {prob:.3f}")

    
joblib NumPy array efficient serialization-এ better than pickle। File size XGBoost ~১-১০০ MB। Save করলে — Python version, sklearn version compatible রাখুন।
Training → Deployment → Monitoring end-to-end ML lifecycle Training data → pipeline CV + HPO model.joblib save Serving FastAPI endpoint Docker container Load balancer Monitoring data drift detect performance metric alert + retrain trigger retrain (drift detected) Production stack 1. Pipeline (sklearn) 2. Joblib serialize 3. FastAPI/Flask REST 4. Docker + Kubernetes 5. MLflow registry 6. Prometheus metrics
ML lifecycle — training pipeline + serving + monitoring + retrain loop। Production system এই cycle-এই চলে।

৬ · FastAPI deployment

Python · FastAPI (api.py)
# pip install fastapi uvicorn joblib pandas
# api.py
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd

app = FastAPI(title="Loan Default API")
model = joblib.load("loan_model_v1.joblib")

class Applicant(BaseModel):
    age: int
    income: float
    education: str
    occupation: str
    credit_history: float | None = None

@app.get("/health")
def health():
    return {"status": "ok", "model": "v1"}

@app.post("/predict")
def predict(app_data: Applicant):
    df = pd.DataFrame([app_data.dict()])
    prob = float(model.predict_proba(df)[0, 1])
    decision = "deny" if prob > 0.5 else "approve"
    return {"default_probability": round(prob, 3),
            "decision": decision}

# Run: uvicorn api:app --host 0.0.0.0 --port 8000
# Test: curl -X POST http://localhost:8000/predict \
#   -H "Content-Type: application/json" \
#   -d '{"age":35,"income":50000,"education":"graduate","occupation":"service","credit_history":1}'

    
FastAPI + Pydantic — type-safe, auto-doc (Swagger UI at /docs)। Production-এ Docker-এ wrap, Kubernetes-এ deploy, gunicorn worker দিয়ে scale।

৭ · Deployment patterns

  • Real-time REST: single request, <১০০ms latency। FastAPI, Flask।
  • Batch: cron job, রাতে ১০ লাখ user score। Spark, Airflow।
  • Streaming: Kafka consumer, transaction fraud realtime।
  • Edge: mobile app, IoT — TFLite, ONNX, quantized model।
  • Serverless: AWS Lambda, Cloud Functions — autoscale, pay-per-use।

৮ · Monitoring ও drift

Production model টিকে থাকে না — world বদলায়:

  • Data drift: input distribution shift। COVID-এ shopping pattern বদলে গেছে।
  • Concept drift: input-output relationship বদলে। Fraud pattern evolve।
  • Label drift: target distribution shift।

Detection:

  • KS test, PSI (Population Stability Index) — distribution shift।
  • Performance monitor — accuracy/AUC track over time।
  • Statistical alarm — threshold breach।

Tooling:

  • MLflow: experiment tracking + model registry।
  • Evidently: drift detection।
  • Weights & Biases: experiment + monitoring।
  • Prometheus + Grafana: metrics dashboard।

৯ · Versioning

  • Model file — semantic versioning (v1.2.3)।
  • Training data hash।
  • Code git commit।
  • Hyperparameter snapshot।
  • Environment requirements.txt।
  • MLflow ও DVC — full lineage tracking।
Production ML-এ pipeline-এর ভেতরে imputation, encoding, scaling, model — সব। আলাদা step-এ করে save করলে inference-এ মিস হবে। "Pipeline as single object" — production rule #1।

ভাবনার প্রশ্ন

প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।

প্র ০১ Data leak — সবচেয়ে common ML bug। Subtle examples ও prevention strategies?

ML-এর silent killer। Result fool the analyst, fail in production।

Common leak types:

(১) Preprocessing leak:

  • StandardScaler full data fit।
  • Test mean ও std contributing।
  • CV optimistic।
  • Solution: Pipeline।

(২) Target encoding leak:

  • Categorical → mean target।
  • Full data target use।
  • Solution: K-fold target encoding।

(৩) Feature engineering leak:

  • "Customer's average past order" — uses future।
  • Solution: time-based feature, only past data।

(৪) Time leak:

  • Random CV on time series।
  • Future predict past।
  • Solution: TimeSeriesSplit।

(৫) Group leak:

  • Same patient train ও test।
  • GroupKFold use।

(৬) Label leak:

  • Feature directly contains label info।
  • "Loan_status_recent" predict default।
  • Solution: domain check।

(৭) Imputation leak:

  • Mean impute full data।
  • Test influences train mean।
  • Solution: Pipeline imputer।

Detection:

  • CV vs test gap — >5% suspect।
  • Feature importance — leakage feature dominate।
  • Time-based split, hold-out test।
  • Code review — preprocessing order।

Prevention checklist:

  • Pipeline always।
  • Time-aware split if temporal।
  • Group-aware if patient/user repeat।
  • Feature creation — only past data।
  • Hold-out test ZERO leakage।
  • Reviewer — separate person।

Bangladesh examples:

  • Loan default — applicant's most recent payment-প্রসঙ্গে — leak (label-related)।
  • Customer churn — "last activity date" feature — leak if > cutoff।
  • Medical diagnosis — same patient revisit — group leak।

মূল উপলব্ধি: Leak silent। Pipeline + time/group split + sanity check — defense।

প্র ০২ Pickle vs joblib vs ONNX vs TorchScript — model serialization-এর comparison?

Production deployment-এ format choice critical।

Pickle:

  • Python standard library।
  • Any Python object।
  • Pros: simple, ubiquitous।
  • Cons: Python-version-dependent, security (arbitrary code execute)।
  • Use: quick local prototype।

Joblib:

  • Pickle wrapper, NumPy array efficient।
  • Compression।
  • sklearn's recommendation।
  • Same Python-version concerns।

ONNX:

  • Open Neural Network Exchange।
  • Framework-agnostic (sklearn, PyTorch, TF, XGBoost)।
  • Inference runtime — fast (C++ optimized)।
  • Cross-language (C#, Java, Rust deploy)।
  • Hardware-specific (CPU, GPU, NPU)।
  • Cons: not all sklearn supported।

TorchScript:

  • PyTorch-specific।
  • Trace বা script PyTorch model।
  • C++ deploy via libtorch।
  • Mobile (LibTorch mobile)।

TensorFlow SavedModel:

  • TF ecosystem।
  • Serving framework (TF Serving)।
  • TFLite mobile।

PMML:

  • XML-based, classical ML।
  • Enterprise (SAS, IBM)।
  • Limited modern model।

Comparison:

  • sklearn pipeline: joblib (default), ONNX (production scaling)।
  • PyTorch: TorchScript বা ONNX।
  • TF/Keras: SavedModel বা ONNX।
  • XGBoost: native binary, ONNX, joblib।

Production decision:

(১) Latency critical:

  • ONNX runtime (C++ optimized)।
  • ~10-100× faster than Python predict।

(২) Cross-platform:

  • ONNX — Java, C#, JS।
  • Mobile — TFLite।

(৩) Python-only:

  • Joblib simple, sufficient।

(৪) Versioning:

  • MLflow standard format-agnostic।
  • Save metadata + serialized।

Common pitfalls:

  • sklearn version mismatch — joblib break।
  • Custom transformer — pickle path issue।
  • ONNX export — unsupported op।
  • Security — never load untrusted pickle।

Modern stack:

  • Train: PyTorch/sklearn।
  • Export: ONNX।
  • Deploy: ONNX Runtime / Triton।
  • Monitor: MLflow / Weights & Biases।

Bangladesh case:

  • Local startup — joblib + FastAPI sufficient।
  • Bank — ONNX for compliance, audit trail।
  • Mobile app — TFLite for offline।

মূল উপলব্ধি: Format depends on deployment target। Joblib quick + Python-only। ONNX production-grade portable। MLflow integrate everything।

প্র ০৩ Drift detection — কীভাবে set up করবেন? False alarm avoid?

Production ML-এর continuous health check।

Drift types:

  • Data drift: input distribution change।
  • Concept drift: input-output relationship change।
  • Label drift: target distribution change।

Detection methods:

(১) Statistical tests:

  • KS test — continuous distribution।
  • Chi-square — categorical।
  • PSI (Population Stability Index) — banking standard।
  • Wasserstein distance।

(২) Performance metrics:

  • AUC, F1 monitor।
  • Lag — true label arrive late।
  • Proxy metric (e.g., click-through rate)।

(৩) Prediction monitoring:

  • Output distribution shift।
  • "Model approving more loans?" — flag।
  • Confidence score distribution।

(৪) Feature attribution:

  • SHAP value distribution।
  • Important feature importance change।
  • Concept drift signal।

Implementation:

  1. Reference window — training data baseline।
  2. Detection window — recent production।
  3. Compute statistic per feature।
  4. Threshold (PSI > 0.2 = warning, > 0.25 = action)।
  5. Alert system।

False alarm avoid:

  • Multiple metric agreement।
  • Sufficient sample size (> 1000)।
  • Smooth window (rolling 7-day)।
  • Domain priors — seasonality expected।
  • Effect size metric (statistical significance + practical)।

Tools:

  • Evidently — Python-based, dashboards।
  • WhyLabs — managed service।
  • NannyML — performance monitoring।
  • AWS SageMaker Model Monitor।
  • Custom Prometheus metrics।

Action triggers:

  • Mild drift — investigate।
  • Moderate — retrain।
  • Severe — rollback to known good।
  • Continuous — automated retrain pipeline।

Bangladesh fintech case:

  • COVID-19 — credit behavior shift।
  • Eid season — spending spike।
  • Political event — payment delay।
  • Seasonal expected vs anomaly distinguish।

Retraining strategy:

  • Schedule (monthly/quarterly)।
  • Trigger-based (drift detect)।
  • Champion-challenger A/B।
  • Shadow mode evaluate।

Operational:

  • On-call rotation।
  • Runbook।
  • Escalation।
  • Postmortem।

Pitfalls:

  • Alarm fatigue — too sensitive।
  • Sampling bias — production data filtered।
  • Definition drift — feature-engineering change masks model drift।

মূল উপলব্ধি: Drift inevitable। Multi-signal detection + threshold + action pipeline। Bangladesh-এ — economic event-এ দ্রুত adapt-able system competitive advantage।

প্র ০৪ Bangladesh-এ একটি মাঝারি startup-এ ML deployment — সীমিত resources-এ pragmatic stack?

Real-world resource-constrained scenario।

Constraints:

  • 2-3 ML engineer।
  • Limited cloud budget।
  • Python expertise।
  • Need fast iteration।

(১) Training environment:

  • Local laptop (CPU/Mac M2)।
  • Google Colab Pro — GPU on demand।
  • Kaggle Notebooks — free TPU।
  • One GCP/AWS instance — heavy training।

(২) Code & version control:

  • GitHub free/Pro।
  • DVC for data version।
  • poetry/pip for environment।

(৩) Experiment tracking:

  • MLflow — open-source, self-host।
  • Weights & Biases — free tier।
  • Neptune — alternative।

(৪) Pipeline:

  • sklearn Pipeline (single object)।
  • Pydantic schema input validation।
  • FastAPI REST।
  • Joblib serialize।

(৫) Deployment:

  • Docker container।
  • Cloud Run (GCP) / App Runner (AWS) / Render — pay-per-use।
  • Cloudflare Workers AI — edge inference।
  • VPS (DigitalOcean) — cheap fallback।

(৬) Database:

  • PostgreSQL (Supabase free)।
  • SQLite — small scale।
  • Redis — cache predictions।

(৭) Monitoring:

  • Custom Prometheus metrics।
  • Grafana cloud free tier।
  • Sentry error tracking।
  • Custom Evidently dashboard।

(৮) CI/CD:

  • GitHub Actions free।
  • Test on push।
  • Auto-deploy on main।
  • Slack notification।

(৯) Authentication:

  • API key for clients।
  • Rate limiting।
  • HTTPS via Cloudflare।

(১০) Cost optimization:

  • Spot instances।
  • Caching predictions (Redis)।
  • Batch inference for non-realtime।
  • Model size: distillation, quantization।

Sample architecture:

  • Frontend (React) → Cloudflare → FastAPI (Cloud Run) → Postgres।
  • Async batch (Celery + Redis) → ML jobs।
  • Model registry (MLflow self-hosted)।
  • Monitoring (Grafana cloud)।

Avoid:

  • Kubernetes — overkill জন্য startup।
  • SageMaker — vendor lock + cost।
  • Custom infra — engineering time waste।

Scaling roadmap:

  • 0-100 user/day: single VM, joblib।
  • 1K user/day: Cloud Run, Postgres।
  • 100K user/day: Kubernetes, ONNX Runtime।
  • 1M+ user/day: managed ML (SageMaker/Vertex AI)।

Bangladesh-specific:

  • Hosting in Singapore region (lower latency)।
  • BDT pricing visibility।
  • bKash/SSL Commerz integration।
  • Compliance — data privacy law।

Team workflow:

  • Weekly sprint।
  • Monthly model review।
  • Quarterly architecture review।
  • On-call rotation (২ engineer)।

মূল উপলব্ধি: Startup ML — simplicity > sophistication। FastAPI + Cloud Run + Postgres + MLflow — 80%+ use case cover। Scale পরে, দরকার হলে।

অনুশীলন

  1. হিসাব করুন: Pipeline-এ scaler ও clf step। Hyperparameter tune করতে চান scaler__with_mean ও clf__C। Grid কেমন?
    param_grid = {
        "scaler__with_mean": [True, False],
        "clf__C": [0.1, 1, 10],
    }
    # 2 × 3 = 6 combinations
    GridSearchCV(pipe, param_grid, cv=5)
  2. Pipeline-এ চেষ্টা: CSV থেকে data, mixed numeric+categorical, full pipeline।

    উপরের ColumnTransformer example ব্যবহার করুন। সাথে CV score, hold-out test।

  3. ভাবুন: Bangladesh-এর একটি e-commerce platform recommendation deploy করছে। Pipeline-এ কী include? Drift কীভাবে?
    • Pipeline: user feature + product feature + interaction history → embedding → ranking model।
    • Components: imputation, scaling, embedding lookup, NN model, post-processing।
    • Versioning: daily train, weekly deploy।
    • Drift: CTR shift detect, seasonal pattern (Eid), new product cold start।
    • Monitor: top-k accuracy, diversity, novelty।
    • A/B test: new vs old recommender।
    • Latency: ONNX runtime, embedding cache।
    • Privacy: aggregated logs, user opt-out।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? Google Colab ব্যবহার করুন।
পূর্ববর্তী পাঠ
পাঠ ৪১ · Hyperparameter Tuning