PaddleServing用java编写yolov3客户端代码

该博客介绍了作者如何在学习人工智能三个月后,使用Paddle深度学习框架完成从数据集到模型部署的全流程,并重点补充了PaddleServing中Java客户端Yolov3的代码实现,包括图像预处理步骤和模型预测过程。
摘要由CSDN通过智能技术生成

大家好!学习人工智能断断续续的3个月,一直使用paddle深度学习框架,从数据集、训练、导出模型、部署serving整体流程都学了一遍,今天就是为大家补充PaddleServing中java example中缺少的yolov3的代码,已经测试过了没问。

public class Yolov3Client
{
    public String yolov3(String model_config_path,String filename)throws Exception
    {
        System.out.println("===================yolov3===================");
        int height = 608;
        int width = 608;
        int channels = 3;
        NativeImageLoader loader = new NativeImageLoader(height, width, channels);
        INDArray BGRimage = null;
        try {
            BGRimage = loader.asMatrix(new File(filename));
        } catch (java.io.IOException e) {
            System.out.println("load image fail.");
            throw new Exception("load image fail.");
        }
        //先做规划处理(img/255-mean)/std
        //        mean = [0.485, 0.456, 0.406]
//        std = [0.229, 0.224, 0.225]
        float[] mean = new float[]{0.485F, 0.456F, 0.406F};
        float[] std  = new float[]{0.229F, 0.224F, 0.225F};
        BGRimage = BGRimage.reshape(height,width,channels);
        INDArray mean_array = Nd4j.create(mean).reshape(1,1,-1);
        INDArray std_array = Nd4j.create(std).reshape(1,1,-1);
//        img  = (img/255.0-mean)/std
        INDArray img1 = BGRimage.div(255.0);
        INDArray img2 = img1.sub(mean_array);
        INDArray img = img2.div(std_array);
        INDArray imga = img.reshape(channels,height,width);
        HashMap<String, Object> feed_data = new HashMap<>();
        feed_data.put("image",imga);
        feed_data.put("im_shape",Nd4j.create(new float[]{608,608}));
        feed_data.put("scale_factor",Nd4j.create(new float[]{608.0F/474.0F,608.0F/800.0F}));
        List<String> fetch = Arrays.asList("multiclass_nms3_0.tmp_0");
        Client client = new Client();
        client.setIP("192.168.10.166");
        client.setPort("9494");
        client.set_use_grpc_client(false);
        client.set_http_proto(false);
        client.loadClientConfig(model_config_path);
        String result = client.predict(feed_data, fetch, false, 0);
        System.out.println(result);
        return result;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值