当前位置: 首页 > news >正文

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;}}
}

下载

源码下载

相关文章:

  • 贪心算法(算法竞赛、蓝桥杯)--修理牛棚
  • jmeter接口测试
  • Docker 第十九章 : 阿里云个人镜像仓使用
  • FPGA之带有进位逻辑的加法运算
  • docker单机启动mysql、redis容器命令
  • 2023中国PostgreSQL数据库生态大会:洞察前沿趋势,探索无限可能(附核心PPT资料下载)
  • 【SpringBoot3】统一参数校验
  • MySQL数据库基础知识总结(适合小白入门使用)一
  • Swagger3 使用详解
  • ChatGPT plus 的平替:9个可以联网的免费AI搜索引擎
  • MySQL:快照读和当前读
  • liunx操作系统 进程的基本概念
  • 未来已来:智慧餐饮点餐系统引领餐饮业的数字化转型
  • go mod中如何解决 xxx/yyy/lib@v1.1.0: unrecognized import path
  • vue购物车实战
  • es6
  • Magento 1.x 中文订单打印乱码
  • Python打包系统简单入门
  • Vue 动态创建 component
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 分布式事物理论与实践
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 关于springcloud Gateway中的限流
  • 好的网址,关于.net 4.0 ,vs 2010
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 坑!为什么View.startAnimation不起作用?
  • 力扣(LeetCode)357
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 一份游戏开发学习路线
  • #LLM入门|Prompt#3.3_存储_Memory
  • (03)光刻——半导体电路的绘制
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (五)IO流之ByteArrayInput/OutputStream
  • (一)Java算法:二分查找
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)socket Aio demo
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .NET开源项目介绍及资源推荐:数据持久层 (微软MVP写作)
  • .NET连接MongoDB数据库实例教程
  • ::before和::after 常见的用法
  • @property @synthesize @dynamic 及相关属性作用探究
  • [ SNOI 2013 ] Quare
  • [04] Android逐帧动画(一)
  • [acwing周赛复盘] 第 94 场周赛20230311
  • [AI]文心一言爆火的同时,ChatGPT带来了这么多的开源项目你了解吗
  • [AIGC] Java 和 Kotlin 的区别
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • [JavaScript]_[初级]_[关于forin或for...in循环语句的用法]
  • [javaSE] 看知乎学习工厂模式
  • [Linux] LVS+Keepalived高可用集群部署
  • [noip2015 d1t2] 信息传递