文章へ移動
ShemolKubeEdge Sedna で連合推論の例を動かす
流程记录 / 云原生

KubeEdge Sedna で連合推論の例を動かす

前の記事のとおり、KubeEdge、EdgeMesh、Sedna はすでにデプロイ済み。次は公式ドキュメントに従って連合推論の例を動かす。

公式ドキュメント:https://sedna.readthedocs.io/en/latest/examples/joint_inference/helmet_detection_inference/README.html#

前の EdgeMesh が正しくデプロイされていれば、この例はドキュメントどおりで問題は出ない。

データとモデルの準備

  • 小モデルをエッジへダウンロード
shell
mkdir -p /data/little-model
cd /data/little-model
wget <https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection-inference/little-model.tar.gz>
tar -zxvf little-model.tar.gz
  • 大モデルをクラウドへダウンロード
shell
mkdir -p /data/big-model
cd /data/big-model
wget <https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection-inference/big-model.tar.gz>
tar -zxvf big-model.tar.gz
  • イメージを用意

小モデル推論 worker:kubeedge/sedna-example-joint-inference-helmet-detection-little:v0.3.0

大モデル推論 worker:kubeedge/sedna-example-joint-inference-helmet-detection-big:v0.3.0

shell
git clone <https://github.com/kubeedge/sedna.git>
./examples/build_image.sh joint_inference # 后面加joint_inference就只生成联合推理的镜像,不加的话就把包括联邦学习那些都生成了

遅い場合、私がやったのはイメージ構築ファイル(joint-inference-helmet-detection-big.Dockerfile と joint-inference-helmet-detection-little.Dockerfile)に次を足すことだ

plain text
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN apt-get clean
RUN pip config set global.index-url <http://mirrors.aliyun.com/pypi/simple>
RUN pip config set install.trusted-host mirrors.aliyun.com
RUN pip install --upgrade pip

apt と pip のミラーを追加する。

連合推論サービスを作る

(kubectl の操作はすべてクラウド側)

  • クラウドに大モデルのリソースオブジェクトを作る
shell
kubectl create -f - <<EOF
apiVersion: sedna.io/v1alpha1
kind:  Model
metadata:
  name: helmet-detection-inference-big-model
  namespace: default
spec:
  url: "/data/big-model/yolov3_darknet.pb"
  format: "pb"
EOF
  • エッジに小モデルのリソースオブジェクトを作る
shell
kubectl create -f - <<EOF
apiVersion: sedna.io/v1alpha1
kind: Model
metadata:
  name: helmet-detection-inference-little-model
  namespace: default
spec:
  url: "/data/little-model/yolov3_resnet18.pb"
  format: "pb"
EOF

エッジ側にフォルダを作り、推論画像の結果はすべてそのフォルダに出す:

shell
mkdir -p /joint_inference/output

クラウドで環境変数 CLOUD_NODE と EDGE_NODE を定義

shell
CLOUD_NODE="cloud-node-name"
EDGE_NODE="edge-node-name"

クラウドで連合推論サービスを作る。イメージは国内ミラーに差し替えた。ファイル内容は以下:

yaml
kind: JointInferenceService
metadata:
  name: helmet-detection-inference-example
  namespace: default
spec:
  edgeWorker:
    model:
      name: "helmet-detection-inference-little-model"
    hardExampleMining:
      name: "IBT"
      parameters:
        - key: "threshold_img"
          value: "0.9"
        - key: "threshold_box"
          value: "0.9"
    template:
      spec:
        nodeName: $EDGE_NODE
        dnsPolicy: ClusterFirstWithHostNet
        containers:
        - image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/kubeedge/sedna-example-joint-inference-helmet-detection-little:v0.3.0
          imagePullPolicy: IfNotPresent
          name:  little-model
          env:  # user defined environments
          - name: input_shape
            value: "416,736"
          - name: "video_url"
            value: "rtsp://localhost/video"
          - name: "all_examples_inference_output"
            value: "/data/output"
          - name: "hard_example_cloud_inference_output"
            value: "/data/hard_example_cloud_inference_output"
          - name: "hard_example_edge_inference_output"
            value: "/data/hard_example_edge_inference_output"
          resources:  # user defined resources
            requests:
              memory: 64M
              cpu: 100m
            limits:
              memory: 2Gi
          volumeMounts:
            - name: outputdir
              mountPath: /data/
        volumes:   # user defined volumes
          - name: outputdir
            hostPath:
              # user must create the directory in host
              path: /joint_inference/output
              type: Directory
  cloudWorker:
    model:
      name: "helmet-detection-inference-big-model"
    template:
      spec:
        nodeName: $CLOUD_NODE
        dnsPolicy: ClusterFirstWithHostNet
        containers:
          - image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/kubeedge/sedna-example-joint-inference-helmet-detection-big:v0.3.0
            name:  big-model
            imagePullPolicy: IfNotPresent
            env:  # user defined environments
              - name: "input_shape"
                value: "544,544"
            resources:  # user defined resources
              requests:
                memory: 2Gi
EOF

エッジで動画ストリームを模擬して推論する

  • 1.オープンソースの動画ストリームサーバ EasyDarwin を入れる。
  • 2.EasyDarwin サーバを起動する。
  • 3.動画をダウンロードする。
  • 4.推論サービスがつながる URL(例:rtsp://localhost/video)へストリームを送る。

(EasyDarwin-linux-8.1.0-1901141151.tar.gz のドキュメント上のアドレスはもう見つからないはずだが、あるサイトで見つけてダウンロードした)

shell
cd EasyDarwin-linux-8.1.0-1901141151
./start.sh

mkdir -p /data/video
cd /data/video
wget <https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection-inference/video.tar.gz>
tar -zxvf video.tar.gz

ffmpeg -re -i /data/video/video.mp4 -vcodec libx264 -f rtsp rtsp://localhost/video

正常なら pod はすべて running で、JointInferenceService の設定で定義した出力パス(例:/joint_inference/output)で推論結果を見られる。