国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用 java-onnx 部署 Meta-ai Segment anything 分割一切

這篇具有很好參考價(jià)值的文章主要介紹了使用 java-onnx 部署 Meta-ai Segment anything 分割一切。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

java onnx,深度學(xué)習(xí)/機(jī)器學(xué)習(xí)/強(qiáng)化學(xué)習(xí)/算法,人工智能,opencv,meta-ai,sam,分割一切java onnx,深度學(xué)習(xí)/機(jī)器學(xué)習(xí)/強(qiáng)化學(xué)習(xí)/算法,人工智能,opencv,meta-ai,sam,分割一切

java onnx,深度學(xué)習(xí)/機(jī)器學(xué)習(xí)/強(qiáng)化學(xué)習(xí)/算法,人工智能,opencv,meta-ai,sam,分割一切

?

近日,Meta AI在官網(wǎng)發(fā)布了基礎(chǔ)模型 Segment Anything Model(SAM)并開源,其本質(zhì)是用GPT的方式(基于Transform 模型架構(gòu))讓計(jì)算機(jī)具備理解了圖像里面的一個(gè)個(gè)“對(duì)象”的通用能力。SAM模型建立了一個(gè)可以接受文本提示、基于海量數(shù)據(jù)(603138)訓(xùn)練而獲得泛化能力的圖像分割大模型。圖像分割是計(jì)算機(jī)視覺中的一項(xiàng)重要任務(wù),有助于識(shí)別和確認(rèn)圖像中的不同物體,把它們從背景中分離出來,這在自動(dòng)駕駛(檢測其他汽車、行人和障礙物)、醫(yī)學(xué)成像(提取特定結(jié)構(gòu)或潛在病灶)等應(yīng)用中特別重要。

下面是 java 使用 onnx 進(jìn)行推理的分割代碼,提示摳圖點(diǎn)進(jìn)行分割,目前還沒有文本交互式提示的部署按理。代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-752540.html

package tool.deeplearning;

import ai.onnxruntime.*;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.FloatBuffer;
import java.util.*;


/**
*   @desc : meta-ai sam , 使用摳圖點(diǎn)進(jìn)行分割
*   @auth : tyf
*   @date : 2023-04-25  09:34:40
*/
public class metaai_sam_test {


    // 模型1
    public static OrtEnvironment env1;
    public static OrtSession session1;

    // 模型2
    public static OrtEnvironment env2;
    public static OrtSession session2;


    // 模型1
    public static void init1(String weight) throws Exception{
        // opencv 庫
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        env1 = OrtEnvironment.getEnvironment();
        session1 = env1.createSession(weight, new OrtSession.SessionOptions());

        // 打印模型信息,獲取輸入輸出的shape以及類型:
        System.out.println("---------模型1輸入-----------");
        session1.getInputInfo().entrySet().stream().forEach(n->{
            String inputName = n.getKey();
            NodeInfo inputInfo = n.getValue();
            long[] shape = ((TensorInfo)inputInfo.getInfo()).getShape();
            String javaType = ((TensorInfo)inputInfo.getInfo()).type.toString();
            System.out.println(inputName+" -> "+ Arrays.toString(shape)+" -> "+javaType);
        });
        System.out.println("---------模型1輸出-----------");
        session1.getOutputInfo().entrySet().stream().forEach(n->{
            String outputName = n.getKey();
            NodeInfo outputInfo = n.getValue();
            long[] shape = ((TensorInfo)outputInfo.getInfo()).getShape();
            String javaType = ((TensorInfo)outputInfo.getInfo()).type.toString();
            System.out.println(outputName+" -> "+Arrays.toString(shape)+" -> "+javaType);
        });
//        session1.getMetadata().getCustomMetadata().entrySet().forEach(n->{
//            System.out.println("元數(shù)據(jù):"+n.getKey()+","+n.getValue());
//        });

    }

    // 模型2
    public static void init2(String weight) throws Exception{
        // opencv 庫
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        env2 = OrtEnvironment.getEnvironment();
        session2 = env2.createSession(weight, new OrtSession.SessionOptions());

        // 打印模型信息,獲取輸入輸出的shape以及類型:
        System.out.println("---------模型2輸入-----------");
        session2.getInputInfo().entrySet().stream().forEach(n->{
            String inputName = n.getKey();
            NodeInfo inputInfo = n.getValue();
            long[] shape = ((TensorInfo)inputInfo.getInfo()).getShape();
            String javaType = ((TensorInfo)inputInfo.getInfo()).type.toString();
            System.out.println(inputName+" -> "+ Arrays.toString(shape)+" -> "+javaType);
        });
        System.out.println("---------模型2輸出-----------");
        session2.getOutputInfo().entrySet().stream().forEach(n->{
            String outputName = n.getKey();
            NodeInfo outputInfo = n.getValue();
            long[] shape = ((TensorInfo)outputInfo.getInfo()).getShape();
            String javaType = ((TensorInfo)outputInfo.getInfo()).type.toString();
            System.out.println(outputName+" -> "+Arrays.toString(shape)+" -> "+javaType);
        });
//        session2.getMetadata().getCustomMetadata().entrySet().forEach(n->{
//            System.out.println("元數(shù)據(jù):"+n.getKey()+","+n.getValue());
//        });

    }


    public static class ImageObj{

        // 原始圖片
        Mat src;
        Mat dst_3_1024_1024;
        BufferedImage image_3_1024_1024;
        float[][][] image_embeddings;
        ArrayList<float[]> points;
        float[][] info;
        public ImageObj(String image) {
            this.src = this.readImg(image);
            this.dst_3_1024_1024 = this.resizeWithoutPadding(src,1024,1024);
            this.image_3_1024_1024 = mat2BufferedImage(this.dst_3_1024_1024);
        }
        public void setPoints(ArrayList<float[]> points) {

            this.points = points;
        }

        public Mat readImg(String path){
            Mat img = Imgcodecs.imread(path);
            return img;
        }
        public Mat resizeWithoutPadding(Mat src,int inputWidth,int inputHeight){
            Mat resizedImage = new Mat();
            Size size = new Size(inputWidth, inputHeight);
            Imgproc.resize(src, resizedImage, size, 0, 0, Imgproc.INTER_AREA);
            return resizedImage;
        }
        public float[] chw2chw(float[][][] chw,int c,int h,int w){
            float[] res = new float[ c * h * w ];

            int index = 0;
            for(int i=0;i<c;i++){
                for(int j=0;j<h;j++){
                    for(int k=0;k<w;k++){
                        float d = chw[i][j][k];
                        res[index] = d;
                        index++;
                    }
                }
            }

            return res;
        }

        // 推理1
        public void infenence1() throws Exception{
            Imgproc.cvtColor(dst_3_1024_1024, dst_3_1024_1024, Imgproc.COLOR_BGR2RGB);
            dst_3_1024_1024.convertTo(dst_3_1024_1024, CvType.CV_32FC1);
            float[] whc = new float[ 3 * 1024 * 1024];
            dst_3_1024_1024.get(0, 0, whc);
            float[] chw = new float[whc.length];
            int j = 0;
            for (int ch = 0; ch < 3; ++ch) {
                for (int i = ch; i < whc.length; i += 3) {
                    chw[j] = whc[i];
                    j++;
                }
            }
            float mean = 0.0f;
            float std = 0.0f;
            for (int i = 0; i < chw.length; i++) {
                mean += chw[i];
            }
            mean /= chw.length;
            for (int i = 0; i < chw.length; i++) {
                std += Math.pow(chw[i] - mean, 2);
            }
            std = (float) Math.sqrt(std / chw.length);
            for (int i = 0; i < chw.length; i++) {
                chw[i] = (chw[i] - mean) / std;
            }
            OnnxTensor tensor = OnnxTensor.createTensor(env1, FloatBuffer.wrap(chw), new long[]{1,3,1024,1024});
            OrtSession.Result res = session1.run(Collections.singletonMap("x", tensor));
            float[][][] image_embeddings  = ((float[][][][])(res.get(0)).getValue())[0];
            this.image_embeddings = image_embeddings;

        }

        // 推理2
        public void infenence2() throws Exception{


            float[] chw = this.chw2chw(this.image_embeddings,256,64,64);
            OnnxTensor _image_embeddings = OnnxTensor.createTensor(env2, FloatBuffer.wrap(chw), new long[]{1,256, 64, 64});

            float[] pc = new float[points.size()*2];
            float[] pc_label = new float[points.size()];

            for(int i=0;i<points.size();i++){
                float[] xyl = points.get(i);
                pc[i*2] = xyl[0] * 1024f / Float.valueOf(src.width());
                pc[i*2+1] = xyl[1] * 1024f / Float.valueOf(src.height());
                pc_label[i] = xyl[2];
            }

            OnnxTensor _point_coords = OnnxTensor.createTensor(env2, FloatBuffer.wrap(pc), new long[]{1,points.size(),2});
            OnnxTensor _point_labels = OnnxTensor.createTensor(env2, FloatBuffer.wrap(pc_label), new long[]{1,points.size()});

            OnnxTensor _orig_im_size = OnnxTensor.createTensor(env2, FloatBuffer.wrap(new float[]{1024,1024}), new long[]{2});


            OnnxTensor _has_mask_input = OnnxTensor.createTensor(env2, FloatBuffer.wrap(new float[]{0}), new long[]{1});

            float[] ar_256_156 = new float[256*256];
            for(int i=0;i<256*156;i++){
                ar_256_156[i] = 0;
            }
            OnnxTensor _mask_input = OnnxTensor.createTensor(env2, FloatBuffer.wrap(ar_256_156), new long[]{1,1,256,256});

            // 封裝參數(shù)
            Map<String,OnnxTensor> in = new HashMap<>();
            in.put("image_embeddings",_image_embeddings);
            in.put("point_coords", _point_coords);
            in.put("point_labels",_point_labels);
            in.put("has_mask_input",_has_mask_input);
            in.put("orig_im_size",_orig_im_size);
            in.put("mask_input",_mask_input);


            // 推理
            OrtSession.Result res = session2.run(in);

            float[][][] masks  = ((float[][][][])(res.get(0)).getValue())[0];
            float[][] iou_predictions  = ((float[][])(res.get(1)).getValue());
            float[][][][] low_res_masks  = ((float[][][][])(res.get(2)).getValue());


            int count = masks.length;

            for(int i=0;i < count;i++){
                float[][] info = masks[i];
                this.info = info;
                break;

            }

        }

        public BufferedImage mat2BufferedImage(Mat mat){
            BufferedImage bufferedImage = null;
            try {
                MatOfByte matOfByte = new MatOfByte();
                Imgcodecs.imencode(".jpg", mat, matOfByte);
                byte[] byteArray = matOfByte.toArray();
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
                bufferedImage = ImageIO.read(byteArrayInputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bufferedImage;
        }

        public BufferedImage resize(BufferedImage img, int newWidth, int newHeight) {
            Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
            BufferedImage scaledBufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = scaledBufferedImage.createGraphics();
            g2d.drawImage(scaledImage, 0, 0, null);
            g2d.dispose();
            return scaledBufferedImage;
        }

        public void show(){


            int sub_w = info.length;
            int sub_h = info[0].length;

            for(int j=0;j<sub_w;j++){
                for(int k=0;k<sub_h;k++){
                    float da = info[j][k];
                    da = da + 1;
                    if(da>0.5){
                        // 修改顏色為綠色
                        image_3_1024_1024.setRGB(k,j, Color.GREEN.getRGB());
                    }
                }
            }

            BufferedImage showImg = resize(image_3_1024_1024,src.width(),src.height());

            // 彈窗顯示
            JFrame frame = new JFrame();
            frame.setTitle("Meta-ai: SAM");
            JPanel content = new JPanel();
            content.add(new JLabel(new ImageIcon(showImg)));
            frame.add(content);
            frame.pack();
            frame.setVisible(true);


        }

    }


    public static void main(String[] args) throws Exception{


        init1(new File("").getCanonicalPath()+
                "\\src\\main\\resources\\deeplearning\\metaai_sam\\encoder-vit_b.quant.onnx");

        init2(new File("").getCanonicalPath()+
                "\\src\\main\\resources\\deeplearning\\metaai_sam\\decoder-vit_b.quant.onnx");


        // 圖片
        ImageObj imageObj = new ImageObj(new File("").getCanonicalPath()+
                "\\src\\main\\resources\\deeplearning\\metaai_sam\\truck.jpg");


        // 提示,這里使用摳圖點(diǎn)進(jìn)行提示,可以設(shè)置多個(gè)提示點(diǎn)
        ArrayList<float[]> points = new ArrayList<>();
        points.add(new float[]{514,357,1});// 車窗戶
        points.add(new float[]{555,377,1});// 車窗戶
        points.add(new float[]{556,387,1});// 車窗戶
        imageObj.setPoints(points);

        // 推理
        imageObj.infenence1();
        imageObj.infenence2();

        // 顯示
        imageObj.show();


    }


}

到了這里,關(guān)于使用 java-onnx 部署 Meta-ai Segment anything 分割一切的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Meta AI最新出品,全能的分割模型SAM:掀桌子的Segment Anything,CV屆的ChatGPT已經(jīng)到來!

    Meta AI最新出品,全能的分割模型SAM:掀桌子的Segment Anything,CV屆的ChatGPT已經(jīng)到來!

    本來不打算再發(fā)關(guān)于分割的相關(guān)內(nèi)容的,但是13小時(shí)前,2023年4月5號(hào),Meta AI在Arxiv網(wǎng)站發(fā)布了文章《Segment Anything》,并將SAM模型代碼和數(shù)據(jù)開源。作為通用的分割網(wǎng)絡(luò),SAM或許將成為,甚至是已經(jīng)成為了CV屆的ChatGPT。簡簡單單的兩個(gè)詞Segment Anything,簡單粗暴卻不失優(yōu)雅。 說

    2023年04月15日
    瀏覽(23)
  • Meta:segment anything

    Meta:segment anything

    介紹地址:https://ai.facebook.com/research/publications/segment-anything/ 演示地址:https://segment-anything.com/demo# 論文:https://scontent-akl1-1.xx.fbcdn.net/v/t39.2365-6/10000000_900554171201033_1602411987825904100_n.pdf?_nc_cat=100ccb=1-7_nc_sid=3c67a6_nc_ohc=Ald4OYhL6hgAX-FZV7S_nc_ht=scontent-akl1-1.xxoh=00_AfDDJRfDV85B3em0zMZvyCIp882H7Ha

    2024年02月05日
    瀏覽(22)
  • AI模型部署落地綜述(ONNX/NCNN/TensorRT等)

    AI模型部署落地綜述(ONNX/NCNN/TensorRT等)

    導(dǎo)讀 費(fèi)盡心血訓(xùn)練好的深度學(xué)習(xí)模型如何給別人展示?只在服務(wù)器上運(yùn)行demo怎么吸引別人的目光?怎么才能讓自己的成果落地?這篇文章帶你進(jìn)入模型部署的大門。 模型部署的步驟: 訓(xùn)練一個(gè)深度學(xué)習(xí)模型; 使用不同的推理框架對(duì)模型進(jìn)行推理轉(zhuǎn)換; 在應(yīng)用平臺(tái)運(yùn)行轉(zhuǎn)換

    2024年01月22日
    瀏覽(21)
  • Meta的分割一切模型SAM( Segment Anything )測試

    Meta的分割一切模型SAM( Segment Anything )測試

    Meta不久前開源發(fā)布了一款圖像處理模型,即分割一切模型:Segment Anything Model,簡稱 SAM,號(hào)稱要從任意一張圖片中分割萬物,源碼地址為: 打開后看到目錄結(jié)構(gòu)大概這樣: 一般一個(gè)開源項(xiàng)目中都會(huì)有項(xiàng)目介紹和示例代碼。本示例中的文件 README.md 即為項(xiàng)目概況介紹,主要說明

    2023年04月27日
    瀏覽(24)
  • CV不存在了?體驗(yàn)用Segment Anything Meta分割清明上河圖

    CV不存在了?體驗(yàn)用Segment Anything Meta分割清明上河圖

    在圖像處理與計(jì)算機(jī)視覺領(lǐng)域, 圖像分割(image segmentation) 是在像素級(jí)別將一個(gè)完整圖像劃分為若干具有特定語義 區(qū)域(region) 或 對(duì)象(object) 的過程。每個(gè)分割區(qū)域是一系列擁有相似特征——例如顏色、強(qiáng)度、紋理等的像素集合,因此圖像分割也可視為 以圖像屬性為特征空間,

    2023年04月20日
    瀏覽(26)
  • 【AIGC】6、Segment Anything | Meta 推出超強(qiáng)悍可分割一切的模型 SAM

    【AIGC】6、Segment Anything | Meta 推出超強(qiáng)悍可分割一切的模型 SAM

    論文:Segment Anything 官網(wǎng):https://segment-anything.com/ 代碼:https://github.com/facebookresearch/segment-anything 出處:Meta、FAIR 時(shí)間:2023.04.05 貢獻(xiàn)點(diǎn): 首次提出基于提示的分割任務(wù),并開源了可以分割一切的模型 SAM 開源了一個(gè)包含 1100 萬張圖像(約包含 10 億 masks)的數(shù)據(jù)集 SA-1B,是目前

    2023年04月23日
    瀏覽(24)
  • 【多模態(tài)】12、Segment Anything | Meta 推出超強(qiáng)悍可分割一切的模型 SAM

    【多模態(tài)】12、Segment Anything | Meta 推出超強(qiáng)悍可分割一切的模型 SAM

    論文:Segment Anything 官網(wǎng):https://segment-anything.com/ 代碼:https://github.com/facebookresearch/segment-anything 出處:Meta、FAIR 時(shí)間:2023.04.05 貢獻(xiàn)點(diǎn): 首次提出基于提示的分割任務(wù),并開源了可以分割一切的模型 SAM 開源了一個(gè)包含 1100 萬張圖像(約包含 10 億 masks)的數(shù)據(jù)集 SA-1B,是目前

    2024年02月17日
    瀏覽(19)
  • 【多模態(tài)】14、Segment Anything | Meta 推出超強(qiáng)悍可分割一切的模型 SAM

    【多模態(tài)】14、Segment Anything | Meta 推出超強(qiáng)悍可分割一切的模型 SAM

    論文:Segment Anything 官網(wǎng):https://segment-anything.com/ 代碼:https://github.com/facebookresearch/segment-anything 出處:Meta、FAIR 時(shí)間:2023.04.05 貢獻(xiàn)點(diǎn): 首次提出基于提示的分割任務(wù),并開源了可以分割一切的模型 SAM 開源了一個(gè)包含 1100 萬張圖像(約包含 10 億 masks)的數(shù)據(jù)集 SA-1B,是目前

    2024年02月16日
    瀏覽(23)
  • segment-anything本地部署使用

    segment-anything本地部署使用

    前言 Segment Anything Model(SAM)是一種先進(jìn)的圖像分割模型,它基于Facebook AI在2020年發(fā)布的Foundation Model3,能夠根據(jù)簡單的輸入提示(如點(diǎn)或框)準(zhǔn)確地分割圖像中的任何對(duì)象,并且無需額外訓(xùn)練就能適應(yīng)不熟悉的對(duì)象和圖像4。它利用了傳統(tǒng)的計(jì)算機(jī)視覺技術(shù)和深度學(xué)習(xí)算法,

    2024年02月07日
    瀏覽(18)
  • ONNX格式模型 學(xué)習(xí)筆記 (onnxRuntime部署)---用java調(diào)用yolov8模型來舉例

    ONNX格式模型 學(xué)習(xí)筆記 (onnxRuntime部署)---用java調(diào)用yolov8模型來舉例

    ONNX(Open Neural Network Exchange)是一個(gè)開源項(xiàng)目,旨在建立一個(gè)開放的標(biāo)準(zhǔn),使深度學(xué)習(xí)模型 可以在不同的軟件平臺(tái)和工具之間輕松移動(dòng)和重用 。 ONNX模型可以用于各種應(yīng)用場景,例如機(jī)器翻譯、圖像識(shí)別、語音識(shí)別、自然語言處理等。 由于ONNX模型的互操作性,開發(fā)人員 可以

    2024年01月22日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包