按照上篇文章已經部署好了 KubeEdge、EdgeMesh 和 Sedna,接下來按照官方文件執行聯合推理實例。
其實只要之前的 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.向推理服務可連線的網址(如
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 )中檢視推理結果。