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

相关文章

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三