RK3588 AI 应用开发 (FCN-语义分割)【玩转华为云】
【摘要】 本章介绍了基于RK3588平台使用FCN模型进行语义分割的AI应用开发全流程,包括模型训练与转换、Gradio界面开发、推理代码编写、批量预测实现及Flask服务部署。通过该流程,开发者可实现高效的图像分割任务,并在本地或云端进行预测和展示。
RK3588 AI 应用开发 (FCN-语义分割)
一、模型训练与转换
FCN(全卷积网络,Fully Convolutional Networks)是用于语义分割任务的一种深度学习模型架构,引入了跳跃结构(Skip Architecture),通过融合浅层和深层的特征图,保留更多的细节信息,提升分割精度。此外,FCN还利用多尺度上下文聚合,捕捉不同层级的特征,增强了对不同大小目标的识别能力。FCN的成功推动了语义分割领域的发展,成为后续许多先进模型的基础。
模型的训练与转换教程已经开放在AI Gallery中,其中包含训练数据、训练代码、模型转换脚本。
在ModelArts的Notebook环境中训练后,再转换成对应平台的模型格式:onnx格式可以用在Windows设备上,RK系列设备上需要转换为rknn格式。
二、应用开发
1. 开发 Gradio 界面
import cv2
import json
import base64
import requests
import numpy as np
import gradio as gr
def test_image(image_path):
try:
image_bgr = cv2.imread(image_path)
image_string = cv2.imencode('.jpg', image_bgr)[1].tobytes()
image_base64 = base64.b64encode(image_string).decode('utf-8')
params = {"image_base64": image_base64}
response = requests.post(f'http://{ip}:{port}{url}', data=json.dumps(params),
headers={"Content-Type": "application/json"})
if response.status_code == 200:
image_base64 = response.json().get("image_base64")
image_binary = base64.b64decode(image_base64)
image_array = np.frombuffer(image_binary, dtype=np.uint8)
image_rgb = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
else:
image_rgb = None
except Exception as e:
return None
else:
return image_rgb
if __name__ == "__main__":
port = 8000
ip = "127.0.0.1"
url = "/v1/FCN"
demo = gr.Interface(fn=test_image, inputs=gr.Image(type="filepath"), outputs=["image"], title="FCN 果蔬病虫害分割")
demo.launch(share=False, server_port=3000)
/home/orangepi/miniconda3/envs/python-3.10.10/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
* Running on local URL: http://127.0.0.1:3000
* To create a public link, set `share=True` in `launch()`.
2. 编写推理代码
class FCN:
def __init__(self, model_path):
self.num_classes = 117
self.rknn_lite = RKNNLite()
self.rknn_lite.load_rknn(model_path)
self.rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0_1_2)
self.color_list = np.random.randint(0, 255, size=(self.num_classes, 3), dtype=np.uint8).tolist()
def preprocess(self, image):
image = image[:, :, ::-1]
image = cv2.resize(image, (224, 224))
return np.expand_dims(image, axis=0)
def rknn_infer(self, data):
outputs = self.rknn_lite.inference(inputs=[data])
return outputs[0]
def post_process(self, pred):
feat = pred.squeeze()
return np.argmax(feat, axis=-1).astype(np.uint8)
def predict(self, image):
# 图像预处理
data = self.preprocess(image)
# 模型推理
pred = self.rknn_infer(data)
# 模型后处理
feat = self.post_process(pred)
# 生成图像分割结果
canv = np.zeros_like(image)
mask = cv2.resize(feat, image.shape[:2][::-1], interpolation=cv2.INTER_NEAREST)
for i in range(1, self.num_classes):
canv[mask==i] = self.color_list[i]
return cv2.addWeighted(image[..., ::-1], 0.5, canv, 0.5, 0)
def release(self):
self.rknn_lite.release()
3. 图片批量预测
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from rknnlite.api import RKNNLite
model = FCN('model/FCN.rknn')
for image in os.listdir("image"):
image = cv2.imread(os.path.join("image", image))
image = model.predict(image)
plt.imshow(image)
plt.axis('off')
plt.show()
model.release()
4. 创建 Flask 服务
import cv2
import base64
import numpy as np
from rknnlite.api import RKNNLite
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/v1/FCN', methods=['POST'])
def inference():
data = request.get_json()
image_base64 = data.get("image_base64")
image_binary = base64.b64decode(image_base64)
image_array = np.frombuffer(image_binary, dtype=np.uint8)
image_bgr = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
image_rgb = model.predict(image_bgr)
image_string = cv2.imencode('.jpg', image_rgb)[1].tobytes()
image_base64 = base64.b64encode(image_string).decode('utf-8')
return jsonify({
"image_base64": image_base64
}), 200
if __name__ == '__main__':
model = FCN('model/FCN.rknn')
app.run(host='0.0.0.0', port=8000)
model.release()
W rknn-toolkit-lite2 version: 2.3.2
I RKNN: [00:06:51.738] RKNN Runtime Information: librknnrt version: 1.4.0 (a10f100eb@2022-09-09T09:07:14)
I RKNN: [00:06:51.738] RKNN Driver Information: version: 0.9.6
I RKNN: [00:06:51.739] RKNN Model Information: version: 1, toolkit version: 1.4.0-22dcfef4(compiler version: 1.4.0 (3b4520e4f@2022-09-05T12:50:09)), target: RKNPU v2, target platform: rk3588, framework name: TFLite, framework layout: NHWC
* Serving Flask app '__main__'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://192.168.3.50:8000
Press CTRL+C to quit
127.0.0.1 - - [02/May/2025 00:07:17] "POST /v1/FCN HTTP/1.1" 200 -
127.0.0.1 - - [02/May/2025 00:07:24] "POST /v1/FCN HTTP/1.1" 200 -
127.0.0.1 - - [02/May/2025 00:07:31] "POST /v1/FCN HTTP/1.1" 200 -
127.0.0.1 - - [02/May/2025 00:07:39] "POST /v1/FCN HTTP/1.1" 200 -
5. 上传图片预测
三、小结
本章介绍了基于RK3588平台使用FCN模型进行语义分割的AI应用开发全流程,包括模型训练与转换、Gradio界面开发、推理代码编写、批量预测实现及Flask服务部署。通过该流程,开发者可实现高效的图像分割任务,并在本地或云端进行预测和展示。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)