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

下載

源碼下載

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/712361.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/712361.shtml
英文地址,請注明出處:http://en.pswp.cn/news/712361.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《PyTorch深度學習實踐》第十三講RNN進階

一、 雙向循環神經網絡&#xff08;Bidirectional Recurrent Neural Network&#xff0c;BiRNN&#xff09;是一種常見的循環神經網絡結構。與傳統的循環神經網絡只考慮歷史時刻的信息不同&#xff0c;雙向循環神經網絡不僅考慮歷史時刻的信息&#xff0c;還考慮未來時刻的信息…

wireshark過濾和tcpdump抓包指令

Wireshark 過濾器的表達式&#xff0c;用于過濾源 IP 地址為 10.184.148.247 并且目標 TCP 端口為 1883 的數據包。啟用抓包后過濾 ip.addr 10.184.148.247 && tcp.port 1883 主機位10.184.148.19和目標端口為 8080 的操作目標 抓包前過濾 host 10.184.148.19 &…

軟件說明書怎么寫?終于有人一次性說清楚了!

每次寫軟件說明書&#xff0c;你是不是總是毫無頭緒&#xff0c;不知道從何下手&#xff1f;到各網站找資料&#xff0c;不僅格式不規范&#xff0c;甚至可能遺漏關鍵內容&#xff01;挨一頓批不說&#xff0c;還浪費大把時間。別著急&#xff0c;編寫軟件說明書&#xff0c;關…

PostgreSQL開發與實戰(2)常用命令

作者&#xff1a;太陽 1、連庫相關 #連庫 $ psql -h <hostname or ip> -p <端口> [數據庫名稱] [用戶名稱] #連庫并執行命令 $ psql -h <hostname or ip> -p <端口> -d [數據庫名稱] -U <用戶名> -c "運行一個命令;"備注&#xff1…

從理論到落地,大模型評測體系綜合指南

1956年夏&#xff0c;“人工智能” 這一概念被提出。距今已有近70年的發展歷史。中國科學院將其劃分為六個階段&#xff1a;起步發展期&#xff08;1956年—1960s&#xff09;&#xff0c;反思發展期&#xff08;1960s-1970s&#xff09;,應用發展期&#xff08;1970s-1980s),低…

SpringBoot集成Activiti案例

前言 Activiti項目是一項新的基于Apache許可的開源BPM平臺&#xff0c;從基礎開始構建&#xff0c;旨在提供支持新的BPMN 2.0標準&#xff0c;包括支持對象管理組&#xff08;OMG&#xff09;&#xff0c;面對新技術的機遇&#xff0c;諸如互操作性和云架構&#xff0c;提供技…

3.1log | 62.不同路徑,63. 不同路徑 II,343. 整數拆分,96.不同的二叉搜索樹

62.不同路徑 class Solution { public:int uniquePaths(int m, int n) {vector<vector<int>> dp(m,vector<int>(n,0));for(int i0;i<n;i) dp[0][i]1;for(int i0;i<m;i) dp[i][0]1;for(int i1;i<m;i){for(int j1;j<n;j){dp[i][j]dp[i][j-1]dp[i-…

c++八股文:c++編譯與內存管理

文章目錄 1. c內存管理2. 堆與棧3.變量定義與生命周期4.內存對齊5.內存泄露6.智能指針7.new 和 malloc 有什么區別8.delete和free的區別9.什么野指針&#xff0c;怎么產生的&#xff0c;如何避免野指針10.野指針和指針懸浮的區別11.字符串操作函數參考 1. c內存管理 c在運行程…

LeetCode刷題--- 乘積為正數的最長子數組長度

個人主頁&#xff1a;元清加油_【C】,【C語言】,【數據結構與算法】-CSDN博客 個人專欄 力扣遞歸算法題 http://t.csdnimg.cn/yUl2I 【C】 ??????http://t.csdnimg.cn/6AbpV 數據結構與算法 ???http://t.csdnimg.cn/hKh2l 前言&#xff1a;這個專欄主要講述動…

ScheduledThreadPoolExecutor學習

簡介 ScheduledThreadPoolExecutor 是 Java 中的一個類&#xff0c;它屬于 java.util.concurrent 包。這個類是一個線程池&#xff0c;用于在給定的延遲后運行命令&#xff0c;或者定期地執行命令。它是 ThreadPoolExecutor 的一個子類&#xff0c;專門用于處理需要定時或周期…

解釋索引是什么以及它們是如何提高查詢性能的

索引在數據庫管理系統中是一個重要的數據結構&#xff0c;用于幫助快速檢索數據庫表中的數據。它可以被看作是一個指向表中數據的指針列表&#xff0c;這些指針按照某種特定的順序&#xff08;如字母順序或數字順序&#xff09;排列。索引的工作原理類似于書籍的目錄&#xff1…

Python爬蟲實戰第二例【二】

零.前言&#xff1a; 本文章借鑒&#xff1a;Python爬蟲實戰&#xff08;五&#xff09;&#xff1a;根據關鍵字爬取某度圖片批量下載到本地&#xff08;附上完整源碼&#xff09;_python爬蟲下載圖片-CSDN博客 大佬的文章里面有API的獲取&#xff0c;在這里我就不贅述了。 一…

kitex 入門和基于grpc的使用

&#x1f4d5;作者簡介&#xff1a; 過去日記&#xff0c;致力于Java、GoLang,Rust等多種編程語言&#xff0c;熱愛技術&#xff0c;喜歡游戲的博主。 &#x1f4d7;本文收錄于kitex系列&#xff0c;大家有興趣的可以看一看 &#x1f4d8;相關專欄Rust初階教程、go語言基礎系…

【Web】青少年CTF擂臺挑戰賽 2024 #Round 1 wp

好家伙&#xff0c;比賽結束了還有一道0解web題是吧( 隨緣寫點wp(簡單過頭&#xff0c;看個樂就好) 目錄 EasyMD5 PHP的后門 PHP的XXE Easy_SQLi 雛形系統 EasyMD5 進來是個文件上傳界面 說是只能上傳pdf&#xff0c;那就改Content-Type為application/pdf&#xff0c;改…

11.盛最多水的容器

題目&#xff1a;給定一個長度為 n 的整數數組 height 。有 n 條垂線&#xff0c;第 i 條線的兩個端點是 (i, 0) 和 (i, height[i]) 。 找出其中的兩條線&#xff0c;使得它們與 x 軸共同構成的容器可以容納最多的水。 返回容器可以儲存的最大水量。 解題思路&#xff1a;可以…

判斷閏年(1000-2000)

判斷規則&#xff1a;1.能被4整除&#xff0c;不能被100整除是閏年,2.能被400整除是閏年 #include <stdio.h>int is_leap_year(int n){if((n % 400 0)||((n % 4 0)&&(n % 100 ! 0)))return 1;elsereturn 0; } int main() {int i 0;int count 0;for(i 1000;…

基于PHP的在線英語學習平臺

有需要請加文章底部Q哦 可遠程調試 基于PHP的在線英語學習平臺 一 介紹 此在線英語學習平臺基于原生PHP開發&#xff0c;數據庫mysql。系統角色分為學生&#xff0c;教師和管理員。(附帶參考設計文檔) 技術棧&#xff1a;phpmysqlphpstudyvscode 二 功能 學生 1 注冊/登錄/…

C++/Python簡單練手題

前言 最近需要開始使用python&#xff0c;但是對python了解的并不多&#xff0c;于是先從很早之前剛學C時寫過的一些練手題開始&#xff0c;使用python來實現相同的功能&#xff0c;在溫習python基礎語法的同時&#xff0c;也一起來感受感受python的魅力 99乘法表 c&#xf…

kettle開發-Day43-加密環境下運行作業

前言&#xff1a; 金三銀四&#xff0c;開年第一篇我們來介紹下&#xff0c;怎么在加密情況下運行我們的kettle作業及任務。無疑現在所有企業都認識到加密的重要性&#xff0c;加密后的文件在對外傳輸的時候不能被訪問&#xff0c;訪問時出現一堆亂碼&#xff0c;同時正常的應用…

1分鐘學會Python字符串前后綴與編解碼

1.前綴和后綴 前綴和后綴指的是&#xff1a;字符串是否以指定字符開頭和結尾 2.startswith() 判斷字符串是否以指定字符開頭&#xff0c;若是返回True&#xff0c;若不是返回False str1 "HelloPython"print(str1.startswith("Hello")) # Trueprint…