0Pricing
MLOps Academy · レッスン

カスタムPredictorを書く

独自の前処理とモデルコードを組み込みます。

「カスタムPredictorを書く」はCoddyKit上の無料MLOps Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMLOps Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MLOps Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

When Built-ins Fall Short

The built-in runtimes cover sklearn, PyTorch, and friends. But when your model needs special logic, you write your own. A custom predictor gives you full control. 🛠️

It Is Just a Container

A custom predictor is your own container image that speaks the KServe protocol. You package your code, model, and dependencies into one image and hand it to KServe.

The KServe Python SDK

The kserve Python package gives you a base class to extend. You subclass kserve.Model and fill in how your model loads and predicts.

import kserve

class MyModel(kserve.Model):
    def __init__(self, name):
        super().__init__(name)
        self.ready = False

Implement load()

The load method reads your model from disk into memory once at startup. When it finishes, you set ready to True so KServe knows it can serve.

def load(self):
    self.model = joblib.load("/mnt/models/model.joblib")
    self.ready = True

Implement predict()

The predict method takes the request payload, runs your model, and returns the result as a dict. This is where your real inference logic lives.

def predict(self, payload, headers=None):
    rows = payload["instances"]
    preds = self.model.predict(rows)
    return {"predictions": preds.tolist()}

Custom Preprocessing Fits Here

Need to reshape inputs or apply business rules before inference? Put that logic right inside predict or in a preprocess step. The runtime never had to know about it.

Start the Model Server

You wire your class into a ModelServer and start it. KServe's server handles HTTP, health, and the protocol so you only write model code. ModelServer runs the loop.

if __name__ == "__main__":
    model = MyModel("custom-model")
    model.load()
    kserve.ModelServer().start([model])

Package It in a Dockerfile

You build a container that installs kserve, copies your code, and runs the server. This Dockerfile produces the image KServe will launch.

FROM python:3.11-slim
RUN pip install kserve joblib scikit-learn
COPY model.py /app/model.py
CMD ["python", "/app/model.py"]

Point the Spec at Your Image

In the InferenceService, the predictor uses a plain containers block instead of a model format. You name your image and KServe runs it.

spec:
  predictor:
    containers:
      - name: kserve-container
        image: my-registry/custom-model:latest

Mounting Your Model

You can still set a storageUri, and KServe mounts the downloaded model at a known path your code reads. The container and the storageUri work together.

Same Protocol, Your Logic

Because you implement the standard endpoints, clients call your custom predictor exactly like a built-in one. The protocol stays identical, only the internals are yours.

Quick Check

You subclass kserve.Model for a custom predictor. Which method loads the model into memory?

Recap

You built a custom predictor by subclassing kserve.Model, filling in load and predict, and shipping it as a container. Same protocol, your own logic. Awesome work! 🎉

よくある質問

「カスタムPredictorを書く」レッスンは無料ですか?

はい。「カスタムPredictorを書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MLOps Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MLOps Academyコースには全4レッスンが含まれています。

「カスタムPredictorを書く」で何を学びますか?

独自の前処理とモデルコードを組み込みます。 ブラウザで直接実行するハンズオンコードでMLOps Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MLOps Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMLOps Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「カスタムPredictorを書く」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMLOps Academyレッスンでコードを書いて実行できますか?

はい。すべてのMLOps Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. InferenceServiceリソース
  2. ゼロまでスケールして戻す
  3. カスタムPredictorを書く
  4. KServeとSeldon Core
← MLOps Academyに戻る