LaMa Image Inpainting 图像修复 TensorRT Demo
目录
效果
项目
NVIDIA GeForce RTX 4060 Laptop GPU
cuda12.1+cudnn 8.8.1
代码
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using TensorRtSharp.Custom;
namespace LaMa_Image_Inpainting_图像修复_TensorRT_Demo
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string image_path = "";
string image_mask_path = "";
string model_path;
Mat image;
Mat image_mask;
StringBuilder sb = new StringBuilder();
private Nvinfer predictor;
private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
pictureBox2.Image = null;
textBox1.Text = "";
sb.Clear();
Application.DoEvents();
image = new Mat(image_path);
int w = image.Width;
int h = image.Height;
image_mask = new Mat(image_mask_path);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
float[] input_tensor_data, input_mask_data;
Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);
predictor.LoadInferenceData("image", input_tensor_data);
predictor.LoadInferenceData("mask", input_mask_data);
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
predictor.infer();
double inferTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
float[] outputData = predictor.GetInferenceResult("inpainted");
Mat result = Common.Postprocess(outputData, w, h);
double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Stop();
double totalTime = preprocessTime + inferTime + postprocessTime;
pictureBox2.Image = new Bitmap(result.ToMemoryStream());
sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
sb.AppendLine($"Infer: {inferTime:F2}ms");
sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
sb.AppendLine($"Total: {totalTime:F2}ms");
textBox1.Text = sb.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
image_path = "test_img/test.jpg";
pictureBox1.Image = new Bitmap(image_path);
image_mask_path = "test_img/mask.jpg";
pictureBox3.Image = new Bitmap(image_mask_path);
model_path = "model/big_lama_regular_inpaint.engine";
if (File.Exists(model_path))
{
}
else
{
Nvinfer.OnnxToEngine("model/big_lama_regular_inpaint.onnx", 20);
}
predictor = new Nvinfer(model_path);
}
float[] input_tensor_data, input_mask_data;
private void button1_Click(object sender, EventArgs e)
{
image = new Mat(image_path);
int w = image.Width;
int h = image.Height;
image_mask = new Mat(image_mask_path);
Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);
}
private void button3_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int index = 1;
for (int i = 0; i < 100; i++)
{
stopwatch.Restart();
predictor.LoadInferenceData("image", input_tensor_data);
predictor.LoadInferenceData("mask", input_mask_data);
predictor.infer();
float[] outputData = predictor.GetInferenceResult("inpainted");
Console.WriteLine("第" + index + "次,耗时:" + stopwatch.Elapsed.TotalMilliseconds);
index++;
}
}
}
}
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using TensorRtSharp.Custom;
namespace LaMa_Image_Inpainting_图像修复_TensorRT_Demo
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string image_path = "";
string image_mask_path = "";
string model_path;
Mat image;
Mat image_mask;
StringBuilder sb = new StringBuilder();
private Nvinfer predictor;
private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
pictureBox2.Image = null;
textBox1.Text = "";
sb.Clear();
Application.DoEvents();
image = new Mat(image_path);
int w = image.Width;
int h = image.Height;
image_mask = new Mat(image_mask_path);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
float[] input_tensor_data, input_mask_data;
Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);
predictor.LoadInferenceData("image", input_tensor_data);
predictor.LoadInferenceData("mask", input_mask_data);
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
predictor.infer();
double inferTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
float[] outputData = predictor.GetInferenceResult("inpainted");
Mat result = Common.Postprocess(outputData, w, h);
double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Stop();
double totalTime = preprocessTime + inferTime + postprocessTime;
pictureBox2.Image = new Bitmap(result.ToMemoryStream());
sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
sb.AppendLine($"Infer: {inferTime:F2}ms");
sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
sb.AppendLine($"Total: {totalTime:F2}ms");
textBox1.Text = sb.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
image_path = "test_img/test.jpg";
pictureBox1.Image = new Bitmap(image_path);
image_mask_path = "test_img/mask.jpg";
pictureBox3.Image = new Bitmap(image_mask_path);
model_path = "model/big_lama_regular_inpaint.engine";
if (File.Exists(model_path))
{
}
else
{
Nvinfer.OnnxToEngine("model/big_lama_regular_inpaint.onnx", 20);
}
predictor = new Nvinfer(model_path);
}
float[] input_tensor_data, input_mask_data;
private void button1_Click(object sender, EventArgs e)
{
image = new Mat(image_path);
int w = image.Width;
int h = image.Height;
image_mask = new Mat(image_mask_path);
Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);
}
private void button3_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int index = 1;
for (int i = 0; i < 100; i++)
{
stopwatch.Restart();
predictor.LoadInferenceData("image", input_tensor_data);
predictor.LoadInferenceData("mask", input_mask_data);
predictor.infer();
float[] outputData = predictor.GetInferenceResult("inpainted");
Console.WriteLine("第" + index + "次,耗时:" + stopwatch.Elapsed.TotalMilliseconds);
index++;
}
}
}
}