行业知识
C# 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 input_tensor;
Tensor input_tensor_mask;
List input_container;
IDisposableReadOnlyCollection 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(new[] { 1, 3, 1000, 1504 });
input_tensor_mask = new DenseTensor(new[] { 1, 1, 1000, 1504 });
// 创建输入容器
input_container = new List();
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 input_tensor; Tensor input_tensor_mask; List input_container; IDisposableReadOnlyCollection 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(new[] { 1, 3, 1000, 1504 }); input_tensor_mask = new DenseTensor(new[] { 1, 1, 1000, 1504 }); // 创建输入容器 input_container = new List(); 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 input_tensor, Tensor input_tensor_mask) { Cv2.Resize(image, image, new OpenCvSharp.Size(1504, 1000)); // 输入Tensor for (int y = 0; y 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 result_infer) { // 将输出结果转为DisposableNamedOnnxValue数组 DisposableNamedOnnxValue[] results_onnxvalue = result_infer.ToArray(); // 读取第一个节点输出并转为Tensor数据 Tensor result_tensors = results_onnxvalue[0].AsTensor(); float[] result_array = result_tensors.ToArray(); for (int i = 0; i 下载
源码下载
上一篇:zone域名怎么样?