LLaVA: Large Language and Vision Assistant 图片解析 图生文

2024-03-22 19:12

本文主要是介绍LLaVA: Large Language and Vision Assistant 图片解析 图生文,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LLaVA: Large Language and Vision Assistant 图片解析  图生文

目录

介绍 

效果

​编辑项目 

测试代码

Form1.cs

Helper.cs

下载


介绍 

    LLaVA,一种新的大型多模态模型,称为“大型语言和视觉助手”,旨在开发一种通用视觉助手,可以遵循语言和图像指令来完成各种现实世界的任务。 这个想法是将 GPT-4 等大型语言模型 (LLM) 的强大功能与 CLIP 等视觉编码器相结合,创建一个经过端到端训练的神经助手,可以理解多模态指令并根据多模态指令采取行动。

项目地址:https://github.com/IntptrMax/LLavaSharp
模型下载地址:https://hf-mirror.com/jartine/llava-v1.5-7B-GGUF/tree/main

效果

项目 

测试代码

Form1.cs

using LLavaSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinformTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*
           项目地址:https://github.com/IntptrMax/LLavaSharp
           模型下载地址:https://hf-mirror.com/jartine/llava-v1.5-7B-GGUF/tree/main  
         */

        IntPtr llamaDllPtr = Lib.LoadLibrary(@".\dll\cuda12\llama.dll");
        IntPtr llavaSharedDllPtr = Lib.LoadLibrary(@".\dll\cuda12\llava_shared.dll");

        string model = @"C:\MyStudy\llava\llava-v1.5-7b-Q4_K.gguf";
        string mmproj = @"C:\MyStudy\llava\llava-v1.5-7b-mmproj-Q4_0.gguf";

        Helper helper;

        private void Form1_Load(object sender, EventArgs e)
        {
            helper = new Helper(model, mmproj);
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        Bitmap bitmap;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            bitmap = new Bitmap(image_path);
            pictureBox1.Image = bitmap;
            txtInfo.Text = "";
        }

        StringBuilder sb = new StringBuilder();
        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            if (String.IsNullOrEmpty(txtPrompt.Text))
            {
                return;
            }

            txtInfo.Text = "";
            button2.Enabled = false;
            sb.Clear();
            System.Windows.Forms.Application.DoEvents();
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            string result = helper.ProcessImage(bitmap, txtPrompt.Text);
            double totalTime = stopwatch.Elapsed.TotalSeconds;
            stopwatch.Stop();
            sb.AppendLine($"totalTime: {totalTime:F2}s");
            sb.AppendLine("- - - - - - - - - - - - - - - - ");
            sb.AppendLine(result);
            txtInfo.Text = sb.ToString();
            button2.Enabled = true;
        }
    }
}

using LLavaSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WinformTest
{public partial class Form1 : Form{public Form1(){InitializeComponent();}/*项目地址:https://github.com/IntptrMax/LLavaSharp模型下载地址:https://hf-mirror.com/jartine/llava-v1.5-7B-GGUF/tree/main  */IntPtr llamaDllPtr = Lib.LoadLibrary(@".\dll\cuda12\llama.dll");IntPtr llavaSharedDllPtr = Lib.LoadLibrary(@".\dll\cuda12\llava_shared.dll");string model = @"C:\MyStudy\llava\llava-v1.5-7b-Q4_K.gguf";string mmproj = @"C:\MyStudy\llava\llava-v1.5-7b-mmproj-Q4_0.gguf";Helper helper;private void Form1_Load(object sender, EventArgs e){helper = new Helper(model, mmproj);}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";Bitmap bitmap;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;bitmap = new Bitmap(image_path);pictureBox1.Image = bitmap;txtInfo.Text = "";}StringBuilder sb = new StringBuilder();private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}if (String.IsNullOrEmpty(txtPrompt.Text)){return;}txtInfo.Text = "";button2.Enabled = false;sb.Clear();System.Windows.Forms.Application.DoEvents();Stopwatch stopwatch = new Stopwatch();stopwatch.Start();string result = helper.ProcessImage(bitmap, txtPrompt.Text);double totalTime = stopwatch.Elapsed.TotalSeconds;stopwatch.Stop();sb.AppendLine($"totalTime: {totalTime:F2}s");sb.AppendLine("- - - - - - - - - - - - - - - - ");sb.AppendLine(result);txtInfo.Text = sb.ToString();button2.Enabled = true;}}
}

Helper.cs

using System;
using System.Drawing;

namespace LLavaSharp
{
    public class Helper : IDisposable
    {
        private llava_context ctx_llava;
        gpt_params @params = new gpt_params();

        public Helper(string model_path, string mmproj_path, int ngl = 32)
        {
            @params.model = model_path;
            @params.mmproj = mmproj_path;
            @params.n_gpu_layers = ngl;
            @params.n_gpu_layers_draft = ngl;
            ctx_llava = Lib.llava_init(@params);
        }

        public string ProcessImage(Bitmap bitmap, string prompt, float temp = 0)
        {
            llava_image_embed image_embed = Lib.load_image(ctx_llava, bitmap, @params.n_threads);
            string result = Lib.process_prompt(ctx_llava, image_embed, @params, prompt, temp);
            Lib.llava_image_embed_free(image_embed);
            Lib.llama_free_kv_cache(ctx_llava.ctx_llama);
            GC.Collect();
            return result;
        }

        public void Dispose()
        {
            Lib.llava_free(ctx_llava);
        }

    }
}

using System;
using System.Drawing;namespace LLavaSharp
{public class Helper : IDisposable{private llava_context ctx_llava;gpt_params @params = new gpt_params();public Helper(string model_path, string mmproj_path, int ngl = 32){@params.model = model_path;@params.mmproj = mmproj_path;@params.n_gpu_layers = ngl;@params.n_gpu_layers_draft = ngl;ctx_llava = Lib.llava_init(@params);}public string ProcessImage(Bitmap bitmap, string prompt, float temp = 0){llava_image_embed image_embed = Lib.load_image(ctx_llava, bitmap, @params.n_threads);string result = Lib.process_prompt(ctx_llava, image_embed, @params, prompt, temp);Lib.llava_image_embed_free(image_embed);Lib.llama_free_kv_cache(ctx_llava.ctx_llama);GC.Collect();return result;}public void Dispose(){Lib.llava_free(ctx_llava);}}
}

下载

源码下载

模型下载地址:https://hf-mirror.com/jartine/llava-v1.5-7B-GGUF/tree/main

这篇关于LLaVA: Large Language and Vision Assistant 图片解析 图生文的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/836020

相关文章

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

MySQL 筛选条件放 ON后 vs 放 WHERE 后的区别解析

《MySQL筛选条件放ON后vs放WHERE后的区别解析》文章解释了在MySQL中,将筛选条件放在ON和WHERE中的区别,文章通过几个场景说明了ON和WHERE的区别,并总结了ON用于关... 今天我们来讲讲数据库筛选条件放 ON 后和放 WHERE 后的区别。ON 决定如何 "连接" 表,WHERE

Mybatis的mapper文件中#和$的区别示例解析

《Mybatis的mapper文件中#和$的区别示例解析》MyBatis的mapper文件中,#{}和${}是两种参数占位符,核心差异在于参数解析方式、SQL注入风险、适用场景,以下从底层原理、使用场... 目录MyBATis 中 mapper 文件里 #{} 与 ${} 的核心区别一、核心区别对比表二、底

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

SQL 注入攻击(SQL Injection)原理、利用方式与防御策略深度解析

《SQL注入攻击(SQLInjection)原理、利用方式与防御策略深度解析》本文将从SQL注入的基本原理、攻击方式、常见利用手法,到企业级防御方案进行全面讲解,以帮助开发者和安全人员更系统地理解... 目录一、前言二、SQL 注入攻击的基本概念三、SQL 注入常见类型分析1. 基于错误回显的注入(Erro

Python多任务爬虫实现爬取图片和GDP数据

《Python多任务爬虫实现爬取图片和GDP数据》本文主要介绍了基于FastAPI开发Web站点的方法,包括搭建Web服务器、处理图片资源、实现多任务爬虫和数据可视化,同时,还简要介绍了Python爬... 目录一. 基于FastAPI之Web站点开发1. 基于FastAPI搭建Web服务器2. Web服务

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关

Springboot主配置文件解析

《Springboot主配置文件解析》SpringBoot主配置文件application.yml支持多种核心值类型,包括字符串、数字、布尔值等,文章详细介绍了Profile环境配置和加载位置,本文... 目录Profile环境配置配置文件加载位置Springboot主配置文件 application.ym