LaMa Image Inpainting 图像修复 Onnx Demo

目录

介绍

效果 

模型信息

项目

代码

下载


LaMa Image Inpainting 图像修复 Onnx Demo

介绍

gihub地址:https://github.com/advimman/lama

🦙 LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022

效果 

模型信息

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:image
tensor:Float[1, 3, 1000, 1504]
name:mask
tensor:Float[1, 1, 1000, 1504]
---------------------------------------------------------------

Outputs
-------------------------
name:inpainted
tensor:Float[1, 1000, 1504, 3]
---------------------------------------------------------------

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Onnx_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string image_path_mask = "";
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;
        Mat image_mask;

        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        Tensor<float> input_tensor_mask;
        List<NamedOnnxValue> input_container;
        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;

        StringBuilder sb = new StringBuilder();

        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;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            image = new Mat(image_path);
            pictureBox2.Image = null;
        }

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

            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image = new Mat(image_path);
            int w = image.Width;
            int h = image.Height;
            image_mask = new Mat(image_path_mask);

            Common.Preprocess(image, image_mask, input_tensor, input_tensor_mask);

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("image", input_tensor));

            //将 input_tensor_mask 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("mask", input_tensor_mask));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_container);
            dt2 = DateTime.Now;

            Mat result = Common.Postprocess(result_infer);

            Cv2.Resize(result, result, new OpenCvSharp.Size(w, h));

            sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");

            pictureBox2.Image = new Bitmap(result.ToMemoryStream());
            textBox1.Text = sb.ToString();

            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model/big_lama_regular_inpaint.onnx";

            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

            // 输入Tensor
            input_tensor = new DenseTensor<float>(new[] { 1, 3, 1000, 1504 });

            input_tensor_mask = new DenseTensor<float>(new[] { 1, 1, 1000, 1504 });

            // 创建输入容器
            input_container = new List<NamedOnnxValue>();

            image_path = "test_img/test.jpg";
            pictureBox1.Image = new Bitmap(image_path);

            image_path_mask = "test_img/mask.jpg";
            pictureBox3.Image = new Bitmap(image_path_mask);
        }
    }
}

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Onnx_Demo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string image_path_mask = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;string model_path;Mat image;Mat image_mask;SessionOptions options;InferenceSession onnx_session;Tensor<float> input_tensor;Tensor<float> input_tensor_mask;List<NamedOnnxValue> input_container;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;StringBuilder sb = new StringBuilder();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;pictureBox1.Image = new Bitmap(image_path);textBox1.Text = "";image = new Mat(image_path);pictureBox2.Image = null;}private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}button2.Enabled = false;pictureBox2.Image = null;textBox1.Text = "";image = new Mat(image_path);int w = image.Width;int h = image.Height;image_mask = new Mat(image_path_mask);Common.Preprocess(image, image_mask, input_tensor, input_tensor_mask);//将 input_tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("image", input_tensor));//将 input_tensor_mask 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("mask", input_tensor_mask));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);dt2 = DateTime.Now;Mat result = Common.Postprocess(result_infer);Cv2.Resize(result, result, new OpenCvSharp.Size(w, h));sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");pictureBox2.Image = new Bitmap(result.ToMemoryStream());textBox1.Text = sb.ToString();button2.Enabled = true;}private void Form1_Load(object sender, EventArgs e){model_path = "model/big_lama_regular_inpaint.onnx";// 创建输出会话,用于输出模型读取信息options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行// 创建推理模型类,读取本地模型文件onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径// 输入Tensorinput_tensor = new DenseTensor<float>(new[] { 1, 3, 1000, 1504 });input_tensor_mask = new DenseTensor<float>(new[] { 1, 1, 1000, 1504 });// 创建输入容器input_container = new List<NamedOnnxValue>();image_path = "test_img/test.jpg";pictureBox1.Image = new Bitmap(image_path);image_path_mask = "test_img/mask.jpg";pictureBox3.Image = new Bitmap(image_path_mask);}}
}

Common.cs

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Onnx_Demo
{internal class Common{public static void Preprocess(Mat image, Mat image_mask,  Tensor<float> input_tensor, Tensor<float> input_tensor_mask){Cv2.Resize(image, image, new OpenCvSharp.Size(1504, 1000));// 输入Tensorfor (int y = 0; y < image.Height; y++){for (int x = 0; x < image.Width; x++){input_tensor[0, 0, y, x] = image.At<Vec3b>(y, x)[0] / 255.0f;input_tensor[0, 1, y, x] = image.At<Vec3b>(y, x)[1] / 255.0f;input_tensor[0, 2, y, x] = image.At<Vec3b>(y, x)[2] / 255.0f;}}Cv2.Resize(image_mask, image_mask, new OpenCvSharp.Size(1504, 1000));//膨胀核函数Mat element1 = new Mat();OpenCvSharp.Size size1 = new OpenCvSharp.Size(11, 11);element1 = Cv2.GetStructuringElement(MorphShapes.Rect, size1);//膨胀一次,让轮廓突出Mat dilation = new Mat();Cv2.Dilate(image_mask, image_mask, element1);//输入Tensorfor (int y = 0; y < image_mask.Height; y++){for (int x = 0; x < image_mask.Width; x++){float v = image_mask.At<Vec3b>(y, x)[0];if (v > 127){input_tensor_mask[0, 0, y, x] = 1.0f;}else{input_tensor_mask[0, 0, y, x] = 0.0f;}}}}public static Mat Postprocess(IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer){// 将输出结果转为DisposableNamedOnnxValue数组DisposableNamedOnnxValue[] results_onnxvalue = result_infer.ToArray();// 读取第一个节点输出并转为Tensor数据Tensor<float> result_tensors = results_onnxvalue[0].AsTensor<float>();float[] result_array = result_tensors.ToArray();for (int i = 0; i < result_array.Length; i++){result_array[i] = Math.Max(0, Math.Min(255, result_array[i]));}Mat result = new Mat(1000, 1504, MatType.CV_32FC3, result_array);return result;}}
}

下载

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/501950.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大模型量化技术原理-ZeroQuant系列

近年来&#xff0c;随着Transformer、MOE架构的提出&#xff0c;使得深度学习模型轻松突破上万亿规模参数&#xff0c;从而导致模型变得越来越大&#xff0c;因此&#xff0c;我们需要一些大模型压缩技术来降低模型部署的成本&#xff0c;并提升模型的推理性能。 模型压缩主要分…

Manacher

Manacher #include<bits/stdc.h> using namespace std; ​ const int N 1e6 9; char s[N]; int p[N]; int mian() {cin >> s 1;int n strlen(s 1);for (int i 2 * n 1; i > 1; --i)s[i] (i & 1) ? # : s[i >> 1];s[0] ^, s[2 * n 2] $;in…

使用Python语言实现一个基于动态数组的序列队列

一、动态数组的实现 首先&#xff0c;我们需要创建一个DynamicArray类&#xff0c;该类将管理我们的动态数组。 动态数组能够动态地调整其大小&#xff0c;以容纳更多的元素。 目录 一、动态数组的实现 代码示例&#xff1a; 二、序列队列的实现 接下来&#xff0c;我…

Redis 在 Linux 系统下安装部署的两种方式详细说明

小伙伴们好&#xff0c;欢迎关注&#xff0c;一起学习&#xff0c;无限进步 Redis安装和配置 1、首先在官网下载好redis-6.0.9.tar.gzhttp://redis.io/ 或者使用 wget 命令下载&#xff1a;wget http://download.redis.io/releases/redis-6.0.9.tar.gz 2、下载使用上传到阿里…

力扣每日一题 使二叉树所有路径值相等的最小代价 满二叉树 贪心

Problem: 2673. 使二叉树所有路径值相等的最小代价 文章目录 思路复杂度Code 思路 &#x1f468;‍&#x1f3eb; 灵神题解 复杂度 ⏰ 时间复杂度: O ( n ) O(n) O(n) &#x1f30e; 空间复杂度: O ( 1 ) O(1) O(1) Code class Solution {public int minIncrements(int …

笑营宝高校选修课报名考勤系统源码开发方案

一、项目背景与目标 &#xff08;一&#xff09;项目背景 随着高等教育的普及和教学模式的不断创新&#xff0c;高校选修课程体系日趋复杂多变。学生对课程选择的自由度提高&#xff0c;使得传统的选课和考勤管理方式变得繁琐且效率低下。目前&#xff0c;许多高校仍然采用纸…

2024年【A特种设备相关管理(电梯)】考试总结及A特种设备相关管理(电梯)证考试

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年A特种设备相关管理&#xff08;电梯&#xff09;考试总结为正在备考A特种设备相关管理&#xff08;电梯&#xff09;操作证的学员准备的理论考试专题&#xff0c;每个月更新的A特种设备相关管理&#xff08;电梯…

【已解决】卸载软件时显示“无法使用此产品的安装源,请确认安装源存在,并且你可以访问它”报错截图如下

卸载软件时显示“无法使用此产品的安装源&#xff0c;请确认安装源存在&#xff0c;并且你可以访问它”报错截图如下 使用Uninstall Tool软件强制删除&#xff0c;绕过软件自带的uninstall程序。&#xff08;小白推荐&#xff0c;如下图&#xff09; Uninstall Tool - Unique…

羊大师分析,羊奶和牛奶那种更适合国人的口味?

羊大师分析&#xff0c;羊奶和牛奶那种更适合国人的口味&#xff1f; 羊奶奶和牛奶在口味上确实存在一些差异&#xff0c;哪种更适合中国人的口味可能因人而异。 一般来说&#xff0c;羊奶的味道相对较淡&#xff0c;带有一些特有的膻味&#xff0c;但这种膻味在经过现代除膻…

递归算法题练习(数的计算、带备忘录的递归、计算函数值)

目录 递归的介绍 递归如何实现 递归和循环的比较 例题: &#xff08;一、斐波那契数列&#xff0c;带备忘录的递归&#xff09; 如果直接使用递归&#xff0c;难以算出结果&#xff0c;需要优化 优化方法&#xff1a;带备忘录的递归 &#xff08;二、数的计算&#xff09…

蓝桥杯Python B组练习——斐波那契数列

一、题目 定义 斐波那契数列&#xff08;Fibonacci sequence&#xff09;&#xff0c;又称黄金分割数列&#xff0c;因数学家莱昂纳多斐波那契&#xff08;Leonardo Fibonacci&#xff09;以兔子繁殖为例子而引入&#xff0c;故又称为“兔子数列”&#xff0c;指的是这样一个数…

适配器模式 详解 设计模式

适配器模式 适配器模式是一种结构型设计模式&#xff0c;其主要作用是解决两个不兼容接口之间的兼容性问题。适配器模式通过引入一个适配器来将一个类的接口转换成客户端所期望的另一个接口&#xff0c;从而让原本由于接口不匹配而无法协同工作的类能够协同工作。 结构 适配…