使用Java語(yǔ)言開(kāi)發(fā)人工智能服務(wù)應(yīng)用

目前市面上主流的深度學(xué)習(xí)框架 TensorFlow、pytorch、MxNet都是以Python語(yǔ)言為主,Java工程師們想要利用自己的優(yōu)勢(shì)開(kāi)發(fā)一款深度學(xué)習(xí)應(yīng)用絕非易事;通過(guò)本篇文章我們將解決這個(gè)問(wèn)題,用極少的代碼實(shí)現(xiàn)一個(gè)圖片分類服務(wù)

場(chǎng)景

【物體分類】
通過(guò)Http請(qǐng)求,向后端服務(wù)傳入一張圖片地址,后端服務(wù)調(diào)用深度學(xué)習(xí)模型對(duì)圖片進(jìn)行處理,給出分類預(yù)測(cè)結(jié)果

安裝本地庫(kù)

以mxnet為例
首先下載本地庫(kù)文件,根據(jù)機(jī)器配置選擇下載,比我的GPU服務(wù)器
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構(gòu)建maven工程,并導(dǎo)入以下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模型,并對(duì)輸入圖片進(jìn)行分類預(yù)測(cè)

 
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

通過(guò)web spark 快速實(shí)現(xiàn)restful api


 public static void main(String[] args) {
        port(8899);
        get("/img_classes/predict", (request, response) -> {
            return ImageNetTest.predict(request.queryParams("img_url"));
        });
    }

測(cè)試效果

斗牛犬
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容