使用Java语言开发人工智能服务应用
2020-06-08 本文已影响0人
郭彦超
目前市面上主流的深度学习框架 TensorFlow、pytorch、MxNet都是以Python语言为主,Java工程师们想要利用自己的优势开发一款深度学习应用绝非易事;通过本篇文章我们将解决这个问题,用极少的代码实现一个图片分类服务
场景
【物体分类】
通过Http请求,向后端服务传入一张图片地址,后端服务调用深度学习模型对图片进行处理,给出分类预测结果
安装本地库
以mxnet为例
首先下载本地库文件,根据机器配置选择下载,比我的GPU服务器
https://publish.djl.ai/mxnet-1.7.0-backport/win/cu102mkl/mxnet_61.dll.gz
win/mkl/libmxnet.dll.gz
win/common/libgcc_s_seh-1.dll.gz
win/common/libgfortran-3.dll.gz
win/common/libopenblas.dll.gz
win/common/libquadmath-0.dll.gz
将文件解压到 C:\Users\bigdata.djl.ai\mxnet
搭建工程
使用idea或者eclipse构建maven工程,并导入以下maven依赖
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>ai.djl</groupId>
<artifactId>api</artifactId>
<version>${djl.version}</version>
</dependency>
<dependency>
<groupId>ai.djl</groupId>
<artifactId>basicdataset</artifactId>
<version>${djl.version}</version>
</dependency>
<dependency>
<groupId>ai.djl</groupId>
<artifactId>model-zoo</artifactId>
<version>${djl.version}</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>ai.djl.mxnet</groupId>
<artifactId>mxnet-model-zoo</artifactId>
<version>${djl.version}</version>
</dependency>
<dependency>
<groupId>ai.djl.mxnet</groupId>
<artifactId>mxnet-engine</artifactId>
<version>${djl.version}</version>
</dependency>
<dependency>
<groupId>ai.djl.mxnet</groupId>
<artifactId>mxnet-native-auto</artifactId>
<version>1.7.0-a</version>
<scope>runtime</scope>
</dependency>
加载模型
使用djl ModelZoo加载ImageNet模型,并对输入图片进行分类预测
public class ImageNetTest {
private static Predictor<BufferedImage, Classifications> predictor = null;
static {
load();
}
private static void load(){
Criteria<BufferedImage, Classifications> criteria =
Criteria.builder()
.optApplication(Application.CV.IMAGE_CLASSIFICATION)
.setTypes(BufferedImage.class, Classifications.class)
.optFilter("multiplier", "0.75")
.optFilter("flavor", "v1")
.optFilter("dataset", "imagenet")
.optArtifactId("mobilenet")
.optProgress(new ProgressBar())
.build();
try {
ZooModel<BufferedImage, Classifications> model = ModelZoo.loadModel(criteria);
predictor = model.newPredictor();
} catch (IOException e) {
e.printStackTrace();
} catch (ModelNotFoundException e) {
e.printStackTrace();
} catch (MalformedModelException e) {
e.printStackTrace();
}
}
public static String predict(String imagePath) throws Exception {
BufferedImage image;
if (imagePath.startsWith("http")) {
image = BufferedImageUtils.fromUrl(new URL(imagePath));
} else {
image = BufferedImageUtils.fromFile(Paths.get(imagePath));
}
return new Gson().toJson(predictor.predict(image).topK(3));
}
public static void main(String[] args)throws Exception {
System.out.println(predict("src/test/resources/dog-cat.jpg"));
}
}
模型下载地址
下载模型 将文件解压到
{your_os_user_root}\.djl.ai\cache\repo\model\cv\image_classification\ai\djl\mxnet\mobilenet\v1\0.75
扫码下载
SimpleHttp
通过web spark 快速实现restful api
public static void main(String[] args) {
port(8899);
get("/img_classes/predict", (request, response) -> {
return ImageNetTest.predict(request.queryParams("img_url"));
});
}