SpringBoot通过ONNXRUNTIME调用深度学习模型
SpringBoot通过ONNXRUNTIME调用深度学习模型
·
半年时间未见了,技术又有了进步,特来分享,废话不多说,先来代码链接(最近学了git,可以上传到github了)https://github.com/2825354549/Spring_MySql 如果有用,记得点star
整个项目基于java的技术栈,主要使用SpringBoot、onnxruntime、MySQL等。

项目树结构如图所示。
需要一定的java技术栈 只有一个功能 就是说前端调用接口,后端从数据库取数据,经过onnx模型处理后,把数据返回给前端。
之所以用onnx格式,是希望提高深度学习的速度,之前用Flask框架取调用,速度较慢。
现在用SpringBoot框架后,速度提升了5~6倍
还有就是docker部署的时候,PyTorch环境太过于庞大,不太方便。
当然我的代码文件里面 已经写好了dockfile文件,可以适当修改就能使用,完全开源
controller层就一个接口 很方便集成 目前就是scrum开发
package com.ly.springboot.controller;
import com.ly.mysql.domain.Drilling;
import com.ly.springboot.service.PredictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin
@RestController
@RequestMapping("/predict")
public class PostPredictController {
@Autowired
private PredictService predictService;
@GetMapping("/data")
// public List<Drilling> getDrillingData() {
// return predictService.getList();
// }
public float[][] getPredictedData() {
try {
// 调用predict3方法并返回预测数据
return predictService.predict3();
} catch (Exception e) {
// 异常处理逻辑,根据需要进行修改
e.printStackTrace();
return null; // 或者返回一个错误信息
}
}
}
主程序 当然 需要修改onnx模型的路径 在application.yaml文件里面修改即可(文件已有)然后一键启动即可 当然我也打包了jar包 你需要重新打包才能运行
package com.ly;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.ly.mysql.mapper")
public class SpringMySqlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMySqlApplication.class, args);
System.out.println("helloworld");
}
}
好了 需要交流的可以后台留言即可
更多推荐
所有评论(0)