目录
介绍
效果
模型信息
项目
代码
下载
LaMa Image Inpainting 图像修复 OnnxRuntime-GPU版 Demo
介绍
gihub地址:GitHub - advimman/lama: 🦙 LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022
🦙 LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022
效果
CPU推理效果
GPU推理效果
模型信息
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]
---------------------------------------------------------------
项目
安装包及版本如下:
环境:
NVIDIA GeForce RTX 4060 Laptop GPU
cuda12.1+cudnn 8.8.1
代码
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
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 = "";
string model_path;
Mat image;
Mat image_mask;
LaMa laMa;
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 = "";
Application.DoEvents();
image = new Mat(image_path);
image_mask = new Mat(image_path_mask);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Mat result = laMa.Run(image,image_mask);
double costTime = stopwatch.Elapsed.TotalMilliseconds;
if (chkUseGPU.Checked)
{
textBox1.Text = "GPU推理耗时:" + costTime + "ms";
}
else {
textBox1.Text = "CPU推理耗时:" + costTime + "ms";
}
if (pictureBox2.Image!=null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = new Bitmap(result.ToMemoryStream());
button2.Enabled = true;
image_mask.Dispose();
image.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
model_path = "model/big_lama_regular_inpaint.onnx";
laMa = new LaMa(model_path);
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);
}
private void chkUseGPU_CheckedChanged(object sender, EventArgs e)
{
if (chkUseGPU.Checked)
{
Program.useGPU = true;
laMa = new LaMa(model_path);
}
else
{
Program.useGPU = false;
laMa = new LaMa(model_path);
}
}
}
}
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
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 = "";
string model_path;
Mat image;
Mat image_mask;
LaMa laMa;
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 = "";
Application.DoEvents();
image = new Mat(image_path);
image_mask = new Mat(image_path_mask);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Mat result = laMa.Run(image,image_mask);
double costTime = stopwatch.Elapsed.TotalMilliseconds;
if (chkUseGPU.Checked)
{
textBox1.Text = "GPU推理耗时:" + costTime + "ms";
}
else {
textBox1.Text = "CPU推理耗时:" + costTime + "ms";
}
if (pictureBox2.Image!=null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = new Bitmap(result.ToMemoryStream());
button2.Enabled = true;
image_mask.Dispose();
image.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
model_path = "model/big_lama_regular_inpaint.onnx";
laMa = new LaMa(model_path);
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);
}
private void chkUseGPU_CheckedChanged(object sender, EventArgs e)
{
if (chkUseGPU.Checked)
{
Program.useGPU = true;
laMa = new LaMa(model_path);
}
else
{
Program.useGPU = false;
laMa = new LaMa(model_path);
}
}
}
}
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));
// 输入Tensor
for (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);
//输入Tensor
for (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;
}
}
}
下载
源码下载