#บทนำ

MLOps (Machine Learning Operations) คือแนวทางที่ผสานแนวคิดจาก Machine Learning, Data Engineering, DevOps และ Software Engineering เพื่อทำให้การพัฒนาโมเดล Machine Learning สามารถนำขึ้นใช้งานจริง ดูแล ปรับปรุง และตรวจสอบได้อย่างเป็นระบบ

เป้าหมายของ MLOps ไม่ใช่เพียงการสร้างโมเดลที่มีค่า Accuracy สูง แต่ต้องทำให้โมเดลสามารถทำงานในระบบ Production ได้อย่าง เชื่อถือได้ ทำซ้ำได้ ตรวจสอบได้ และปรับปรุงได้ต่อเนื่อง


#ทำไมต้องมี MLOps

ในงาน Machine Learning แบบทดลอง เราอาจทำงานใน Notebook แล้วได้โมเดลที่ดี แต่เมื่อจะนำไปใช้งานจริงจะพบปัญหาหลายด้าน เช่น

  • Dataset เปลี่ยนไปจากตอนฝึกโมเดล
  • Feature ที่ใช้ตอน Train และตอน Predict ไม่ตรงกัน
  • ไม่ทราบว่าโมเดล Version ใดกำลังถูกใช้งาน
  • ไม่สามารถย้อนกลับไปยังโมเดล Version ก่อนหน้าได้
  • โมเดลมี Accuracy ลดลงเมื่อเวลาผ่านไป
  • Pipeline Train ไม่สามารถทำซ้ำได้
  • การ Deploy โมเดลต้องทำด้วยมือ
  • ไม่มีระบบ Monitoring สำหรับตรวจสอบ Prediction

MLOps จึงเข้ามาช่วยจัดการวงจรชีวิตของ Machine Learning ตั้งแต่ต้นจนจบ


#MLOps Lifecycle

วงจรการทำงานของ MLOps สามารถแบ่งเป็นขั้นตอนหลักดังนี้

Data Collection
      ↓
Data Validation
      ↓
Feature Engineering
      ↓
Model Training
      ↓
Model Evaluation
      ↓
Model Registry
      ↓
Deployment
      ↓
Monitoring
      ↓
Retraining

#1. Data Collection

เริ่มต้นจากการรวบรวมข้อมูลจากแหล่งต่าง ๆ เช่น

  • Database
  • Data Warehouse
  • Data Lake
  • REST API
  • IoT Device
  • Log File
  • Streaming Data

ตัวอย่างเครื่องมือ

  • Apache Kafka
  • Apache Airflow
  • dbt
  • AWS Glue
  • Google Cloud Dataflow
  • Azure Data Factory

#2. Data Validation

ก่อนนำข้อมูลไป Train Model ควรตรวจสอบคุณภาพข้อมูล เช่น

  • Missing Value
  • Duplicate Data
  • Invalid Data Type
  • Outlier
  • Schema Change
  • Distribution Shift

เครื่องมือที่นิยมใช้ เช่น

  • Great Expectations
  • Pandera
  • TensorFlow Data Validation

ตัวอย่างด้วย Pandera

import pandera as pa
from pandera import Column, DataFrameSchema

schema = DataFrameSchema({
    "age": Column(int, pa.Check.ge(0)),
    "income": Column(float, pa.Check.ge(0)),
})

validated_df = schema.validate(df)

#3. Feature Engineering

Feature Engineering คือการแปลงข้อมูลให้เหมาะสมกับโมเดล เช่น

Raw Data
   ↓
Cleaning
   ↓
Normalization
   ↓
Encoding
   ↓
Feature Selection

ตัวอย่าง Feature

age
income
transaction_count
average_purchase
customer_tenure

ในระบบขนาดใหญ่สามารถใช้ Feature Store เพื่อให้ Training และ Prediction ใช้ Feature เดียวกัน

ตัวอย่างเครื่องมือ

  • Feast
  • Tecton
  • Hopsworks

#4. Model Training

ขั้นตอนนี้คือการ Train Machine Learning Model

ตัวอย่างด้วย Scikit-learn

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(
    n_estimators=100,
    random_state=42
)

model.fit(X_train, y_train)

แต่ใน MLOps ควรบันทึกข้อมูลการทดลองด้วย

Model Version
Parameters
Metrics
Dataset Version
Source Code Version

#5. Experiment Tracking

Experiment Tracking ช่วยบันทึกผลการทดลองแต่ละครั้ง

ตัวอย่าง

Run Model Accuracy Learning Rate
001 Random Forest 0.91 -
002 XGBoost 0.94 0.10
003 XGBoost 0.95 0.05

เครื่องมือยอดนิยม

  • MLflow
  • Weights & Biases
  • Neptune
  • Comet

#MLflow

MLflow เป็นเครื่องมือ Open Source สำหรับจัดการ Machine Learning Lifecycle

องค์ประกอบหลัก ได้แก่

MLflow Tracking
MLflow Projects
MLflow Models
MLflow Model Registry

ตัวอย่างติดตั้ง

pip install mlflow

เริ่ม MLflow Server

mlflow server   --host 0.0.0.0   --port 5000

เปิด Browser

http://localhost:5000

ตัวอย่างบันทึก Experiment

import mlflow

with mlflow.start_run():

    mlflow.log_param(
        "n_estimators",
        100
    )

    mlflow.log_metric(
        "accuracy",
        0.95
    )

    mlflow.sklearn.log_model(
        model,
        "model"
    )

#6. Model Registry

Model Registry ใช้จัดการ Version ของ Model

ตัวอย่าง

Model
│
├── Version 1
│
├── Version 2
│
└── Version 3

Model อาจมีสถานะ เช่น

Development
Staging
Production
Archived

ข้อดีคือสามารถ

  • Version Model
  • Rollback
  • Track History
  • Promote Model
  • Audit Model

#7. Model Deployment

โมเดลสามารถ Deploy ได้หลายรูปแบบ

#Batch Prediction

เหมาะสำหรับ

Prediction รายวัน
Recommendation
Risk Scoring
Report Generation

ตัวอย่าง

Database
   ↓
Batch Job
   ↓
ML Model
   ↓
Prediction
   ↓
Database

#Real-time Prediction

เหมาะสำหรับงานที่ต้องตอบกลับทันที

Client
  ↓
REST API
  ↓
ML Model
  ↓
Prediction

Framework ที่นิยมใช้

  • FastAPI
  • Flask
  • BentoML
  • KServe
  • Seldon

#ตัวอย่าง FastAPI สำหรับ Model Serving

ติดตั้ง

pip install fastapi uvicorn joblib

สร้างไฟล์

app.py

ตัวอย่าง

from fastapi import FastAPI
import joblib

app = FastAPI()

model = joblib.load(
    "model.pkl"
)

@app.get("/")
def root():

    return {
        "message": "ML API"
    }


@app.post("/predict")
def predict(
    age: int,
    income: float
):

    result = model.predict([
        [
            age,
            income
        ]
    ])

    return {
        "prediction":
        int(result[0])
    }

Run

uvicorn app:app   --host 0.0.0.0   --port 8000

เปิด Swagger UI

http://localhost:8000/docs

#Dockerize ML Model

สร้าง

Dockerfile

ตัวอย่าง

FROM python:3.13-slim

WORKDIR /app

COPY . .

RUN pip install     fastapi     uvicorn     scikit-learn     joblib

CMD [
    "uvicorn",
    "app:app",
    "--host",
    "0.0.0.0",
    "--port",
    "8000"
]

Build

docker build   -t ml-api .

Run

docker run   -p 8000:8000   ml-api

#8. CI/CD สำหรับ Machine Learning

CI/CD ช่วยทำ Automation ให้ Pipeline

ตัวอย่าง Workflow

Push Code
   ↓
Run Test
   ↓
Data Validation
   ↓
Train Model
   ↓
Evaluate Model
   ↓
Build Docker Image
   ↓
Deploy

#ตัวอย่าง GitHub Actions

name: ML Pipeline

on:
  push:
    branches:
      - main

jobs:

  test:

    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - run: |
          pip install -r requirements.txt

      - run: |
          pytest

สามารถเพิ่มขั้นตอน Train Model

- name: Train Model
  run: |
    python train.py

#9. Model Monitoring

เมื่อ Deploy Model แล้วต้อง Monitoring

ควรตรวจสอบ

Prediction Accuracy
Latency
Error Rate
Data Drift
Model Drift
Resource Usage

#Data Drift

Data Drift คือ Distribution ของ Input เปลี่ยนไป

ตัวอย่าง

Training Data

Age = 20-40

Production Data

Age = 40-70

Model อาจ Predict ผิดมากขึ้น


#Concept Drift

Concept Drift คือความสัมพันธ์ระหว่าง Input และ Output เปลี่ยนไป

ตัวอย่าง

พฤติกรรมลูกค้าเปลี่ยน

Economic Condition เปลี่ยน

Fraud Pattern เปลี่ยน

#เครื่องมือ Monitoring

ตัวอย่างเครื่องมือ

  • Evidently
  • Prometheus
  • Grafana
  • WhyLabs
  • Arize
  • Fiddler

#MLOps Architecture

ตัวอย่าง Architecture

Data Source
    │
    ▼
Data Pipeline
    │
    ▼
Feature Store
    │
    ▼
Training Pipeline
    │
    ▼
Experiment Tracking
    │
    ▼
Model Registry
    │
    ▼
CI/CD
    │
    ▼
Model Serving
    │
    ▼
Monitoring

#ตัวอย่าง Technology Stack

#Data

PostgreSQL
Kafka
S3
BigQuery

#Pipeline

Airflow
Dagster
Prefect

#Experiment Tracking

MLflow
Weights & Biases

#Feature Store

Feast

#Container

Docker

#Orchestration

Kubernetes

#CI/CD

GitHub Actions
GitLab CI
Jenkins

#Monitoring

Prometheus
Grafana
Evidently

#ตัวอย่าง MLOps Pipeline

GitHub

  ↓

GitHub Actions

  ↓

Run Tests

  ↓

Train Model

  ↓

MLflow

  ↓

Model Registry

  ↓

Docker Image

  ↓

Container Registry

  ↓

Kubernetes

  ↓

FastAPI

  ↓

Prometheus

  ↓

Grafana

#MLOps Maturity Levels

องค์กรสามารถพัฒนา MLOps ตามระดับได้

#Level 0 — Manual ML

Notebook

Train Model

Manual Deploy

เหมาะสำหรับ

Prototype
Research
Experiment

#Level 1 — Automated Training

มี Pipeline

Data

↓

Train

↓

Evaluate

↓

Deploy

#Level 2 — CI/CD Pipeline

เพิ่ม

Version Control
Testing
CI/CD
Model Registry
Monitoring

#Level 3 — Full MLOps Platform

มีระบบ

Feature Store

Experiment Tracking

Model Registry

Automated Retraining

Model Monitoring

Data Drift Detection

#Automated Retraining

ตัวอย่าง Workflow

Monitoring

↓

Detect Drift

↓

Trigger Training

↓

Evaluate New Model

↓

Compare Model

↓

Deploy

↓

Monitor

แนวทางนี้ช่วยให้โมเดลสามารถปรับตัวตามข้อมูลใหม่ได้


#Machine Learning Testing

MLOps ควรมี Testing หลายระดับ

#Data Test

ตรวจสอบ

Schema
Missing Data
Distribution
Data Type

#Model Test

ตรวจสอบ

Accuracy
Precision
Recall
F1 Score

#API Test

ตรวจสอบ

HTTP Status

Response Schema

Latency

#Integration Test

ตรวจสอบ

Model

API

Database

Pipeline

#MLOps กับ DevOps ต่างกันอย่างไร

DevOps MLOps
Application ML Model
Source Code Code + Data
Build Software Train Model
Software Version Model Version
Application Monitoring Model Monitoring
CI/CD CI/CD/CT
Code Testing Code + Data + Model Testing

ใน MLOps มีแนวคิดเพิ่มคือ

CT
Continuous Training

#CI / CD / CT

#Continuous Integration

Code
↓
Test
↓
Build

#Continuous Delivery

Build
↓
Deploy

#Continuous Training

New Data
↓
Retrain
↓
Evaluate
↓
Deploy New Model

#MLOps กับ Kubernetes

Kubernetes เหมาะสำหรับ

Model Serving

Scaling

Rolling Update

Load Balancing

Resource Management

ตัวอย่าง Architecture

Client

↓

Ingress

↓

Kubernetes Service

↓

FastAPI Pod

↓

ML Model

เมื่อ Load เพิ่ม

Pod × 1

↓

Pod × 5

สามารถใช้ Horizontal Pod Autoscaler


#ตัวอย่าง Project Structure

mlops-project

├── data
│
├── notebooks
│
├── src
│   ├── train.py
│   └── predict.py
│
├── tests
│
├── models
│
├── app
│   └── main.py
│
├── Dockerfile
│
├── requirements.txt
│
├── .github
│   └── workflows
│       └── ml.yml
│
└── README.md

#แนวทางเริ่มต้น MLOps

สำหรับผู้เริ่มต้นสามารถเริ่มด้วย Stack

Python
+
Scikit-learn
+
MLflow
+
FastAPI
+
Docker
+
GitHub Actions

จากนั้นค่อยเพิ่ม

Kubernetes

Feature Store

Airflow

Monitoring

#Roadmap การเรียนรู้ MLOps

1 Python

2 Machine Learning

3 Git

4 Docker

5 FastAPI

6 MLflow

7 CI/CD

8 Kubernetes

9 Monitoring

10 Cloud MLOps

#MLOps บน Cloud

Cloud Provider มีบริการ MLOps Platform

#AWS

Amazon SageMaker

รองรับ

Training
Model Registry
Model Endpoint
Pipeline
Monitoring

#Google Cloud

Vertex AI

รองรับ

Training
Model Registry
Pipeline
Endpoint
Feature Store

#Microsoft Azure

Azure Machine Learning

รองรับ

Experiment
Pipeline
Model Registry
Deployment
Monitoring

#Best Practices

แนวทางที่ควรใช้ใน MLOps

  1. Version ทั้ง Code, Data และ Model
  2. สร้าง Pipeline ที่ Reproducible
  3. แยก Training และ Serving Environment
  4. ใช้ Model Registry
  5. ทำ Automated Testing
  6. ใช้ CI/CD
  7. Monitor Data Drift และ Model Drift
  8. เก็บ Metrics ทุก Experiment
  9. รองรับ Rollback
  10. ใช้ Infrastructure as Code

#สรุป

MLOps คือแนวทางสำคัญสำหรับการนำ Machine Learning ไปใช้งานจริงใน Production อย่างมีระบบ

หัวใจของ MLOps คือ

Automation

Reproducibility

Versioning

Testing

Deployment

Monitoring

Continuous Training

Architecture ที่พบได้บ่อยคือ

Data
 ↓
Training
 ↓
Experiment Tracking
 ↓
Model Registry
 ↓
CI/CD
 ↓
Deployment
 ↓
Monitoring
 ↓
Retraining

สำหรับผู้เริ่มต้นสามารถเริ่มจาก

Python

Scikit-learn

MLflow

FastAPI

Docker

GitHub Actions

แล้วจึงขยายระบบด้วย

Kubernetes

Airflow

Feature Store

Prometheus

Grafana

Cloud MLOps Platform

MLOps ทำให้ Machine Learning เปลี่ยนจากงานทดลองใน Notebook ไปสู่ ระบบ Production ที่สามารถดูแล ตรวจสอบ และพัฒนาได้อย่างต่อเนื่อง