LaMa Image Inpainting 图像修复 OpenVINO Demo
目录
介绍
🦙 LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022
效果
模型信息
OVVersion { BuildNumber = 2023.1.0-12185-9e6b00e51cd-releases/2023/1, Description = OpenVINO Runtime }
本机可用设备
CPU
GNA
GPU
Inputs
name:image
tensor:F32[1, 3, 1000, 1504]
name:mask
tensor:F32[1, 1, 1000, 1504]
Outputs
name:inpainted
tensor:F32[1, 1000, 1504, 3]
项目
代码
using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LaMa_Image_Inpainting_图像修复_OpenVINO_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string image_path = "";
string image_mask_path = "";
string model_path;
Mat image;
Mat image_mask;
StringBuilder sb = new StringBuilder();
Model rawModel;
PrePostProcessor pp;
Model m;
CompiledModel cm;
InferRequest ir;
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);
Tensor input_tensor = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 1000, 1504));
Tensor input_tensor_mask = Tensor.FromArray(input_mask_data, new Shape(1, 1, 1000, 1504));
ir.Inputs[0] = input_tensor;
ir.Inputs[1] = input_tensor_mask;
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
ir.Run();
double inferTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
Mat result = Common.Postprocess(ir.Outputs[0], 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.onnx";
rawModel = OVCore.Shared.ReadModel(model_path);
pp = rawModel.CreatePrePostProcessor();
m = pp.BuildModel();
cm = OVCore.Shared.CompileModel(m, "CPU");
ir = cm.CreateInferRequest();
}
}
}
using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LaMa_Image_Inpainting_图像修复_OpenVINO_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string image_path = "";
string image_mask_path = "";
string model_path;
Mat image;
Mat image_mask;
StringBuilder sb = new StringBuilder();
Model rawModel;
PrePostProcessor pp;
Model m;
CompiledModel cm;
InferRequest ir;
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);
Tensor input_tensor = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 1000, 1504));
Tensor input_tensor_mask = Tensor.FromArray(input_mask_data, new Shape(1, 1, 1000, 1504));
ir.Inputs[0] = input_tensor;
ir.Inputs[1] = input_tensor_mask;
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
ir.Run();
double inferTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();
Mat result = Common.Postprocess(ir.Outputs[0], 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.onnx";
rawModel = OVCore.Shared.ReadModel(model_path);
pp = rawModel.CreatePrePostProcessor();
m = pp.BuildModel();
cm = OVCore.Shared.CompileModel(m, "CPU");
ir = cm.CreateInferRequest();
}
}
}