当前位置:首页|资讯

MVP 聚技站|C# OnnxRuntime Diffusion-Low-Light 低光照图像增强

作者:微软中国MSDN发布时间:2024-09-11

M

作者:天天代码码天天- 微软最有价值专家(MVP)

排版:Alan Wang

本文转载自: 天天代码码天天

天天代码码天天

微软最有价值专家(MVP)

架构师,主要研究计算机视觉、机器学习、人工智能,主要语言 C#、C++、Python,喜欢使用 C# 实现各种模型推理!

说明

官网地址:

https://github.com/JianghaiSCU/Diffusion-Low-Light

代码实现参考:

https://github.com/hpc203/Diffusion-Low-Light-onnxrun

效果

模型信息

Model Properties----------------------------------------------------------------------------------------

Inputs-------------------------name:inputtensor:Float[1, 3, 192, 320]---------------------------------------------------------------

Outputs-------------------------name:outputtensor:Float[1, 3, 192, 320]---------------------------------------------------------------

项目

代码

usingMicrosoft.ML.OnnxRuntime;usingMicrosoft.ML.OnnxRuntime.Tensors;usingOpenCvSharp;usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Linq;usingSystem.Numerics;usingSystem.Windows.Forms;

namespaceOnnx_Demo{publicpartialclassForm1: Form{publicForm1(){InitializeComponent;}

stringfileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";stringimage_path = "";stringstartupPath;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;stringmodel_path;Mat image;Mat result_image;SessionOptions options;InferenceSession onnx_session;Tensor<float> input_tensor;List<NamedOnnxValue> input_container;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;DisposableNamedOnnxValue[] results_onnxvalue;Tensor<float> result_tensors;intinpHeight, inpWidth;

privatevoidbutton1_Click(objectsender, EventArgs e){OpenFileDialog ofd = newOpenFileDialog;ofd.Filter = fileFilter;if(ofd.ShowDialog != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;pictureBox1.Image = newBitmap(image_path);textBox1.Text = "";image = newMat(image_path);pictureBox2.Image = null;}

privatevoidbutton2_Click(objectsender, EventArgs e){if(image_path == ""){return;}

button2.Enabled = false;pictureBox2.Image = null;textBox1.Text = "";Application.DoEvents;

//读图片image = newMat(image_path);intcols = image.Cols;introws = image.Rows;

Mat dstimg = newMat;Cv2.CvtColor(image, dstimg, ColorConversionCodes.BGR2RGB);Cv2.Resize(dstimg, dstimg, newOpenCvSharp.Size(inpWidth, inpHeight));dstimg.ConvertTo(dstimg, MatType.CV_32FC3, 1/ 255.0f);

//输入Tensorinput_tensor = newDenseTensor<float>(Common.ExtractMat(dstimg), new[] { 1, 3, inpHeight, inpWidth });

//将 input_tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));

dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);dt2 = DateTime.Now;

// 将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray;

// 读取第一个节点输出并转为Tensor数据result_tensors = results_onnxvalue[0].AsTensor<float>;

int[] out_shape = result_tensors.Dimensions.ToArray;intout_h = out_shape[2];intout_w = out_shape[3];float[] pred = result_tensors.ToArray;

float[] temp_r = newfloat[out_h * out_w];float[] temp_g = newfloat[out_h * out_w];float[] temp_b = newfloat[out_h * out_w];

Array.Copy(pred, temp_b, out_h * out_w);Array.Copy(pred, out_h * out_w, temp_g, 0, out_h * out_w);Array.Copy(pred, out_h * out_w * 2, temp_r, 0, out_h * out_w);

intchannel_step = out_h * out_w;Mat bmat = newMat(out_h, out_w, MatType.CV_32FC1, temp_b);Mat gmat = newMat(out_h, out_w, MatType.CV_32FC1, temp_g);Mat rmat = newMat(out_h, out_w, MatType.CV_32FC1, temp_r);

bmat *= 255.0f;gmat *= 255.0f;rmat *= 255.0f;

Mat[] channel_mats = newMat[] { rmat, gmat, bmat };dstimg = newMat;Cv2.Merge(channel_mats.ToArray, dstimg);dstimg.ConvertTo(dstimg, MatType.CV_8UC3);

Cv2.Resize(dstimg, dstimg, newOpenCvSharp.Size(cols, rows));

pictureBox2.Image = newBitmap(dstimg.ToMemoryStream);textBox1.Text = "推理耗时:"+ (dt2 - dt1).TotalMilliseconds + "ms";

button2.Enabled = true;

}

privatevoidForm1_Load(objectsender, EventArgs e){startupPath = System.Windows.Forms.Application.StartupPath;model_path = "model/diffusion_low_light_1x3x192x320.onnx";

// 创建输出会话,用于输出模型读取信息options = newSessionOptions;options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

// 创建推理模型类,读取本地模型文件onnx_session = newInferenceSession(model_path, options);

// 创建输入容器input_container = newList<NamedOnnxValue>;

image_path = "test_img/1.png";pictureBox1.Image = newBitmap(image_path);image = newMat(image_path);

inpHeight = 192;inpWidth = 320;

}

privatevoidpictureBox1_DoubleClick(objectsender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}

privatevoidpictureBox2_DoubleClick(objectsender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}

SaveFileDialog sdf = newSaveFileDialog;privatevoidbutton3_Click(objectsender, EventArgs e){if(pictureBox2.Image == null){return;}Bitmap output = newBitmap(pictureBox2.Image);sdf.Title = "保存";sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";if(sdf.ShowDialog == DialogResult.OK){switch(sdf.FilterIndex){case1:{output.Save(sdf.FileName, ImageFormat.Jpeg);break;}case2:{output.Save(sdf.FileName, ImageFormat.Png);break;}case3:{output.Save(sdf.FileName, ImageFormat.Bmp);break;}case4:{output.Save(sdf.FileName, ImageFormat.Emf);break;}case5:{output.Save(sdf.FileName, ImageFormat.Exif);break;}case6:{output.Save(sdf.FileName, ImageFormat.Gif);break;}case7:{output.Save(sdf.FileName, ImageFormat.Icon);break;}

case8:{output.Save(sdf.FileName, ImageFormat.Tiff);break;}case9:{output.Save(sdf.FileName, ImageFormat.Wmf);break;}}MessageBox.Show("保存成功,位置:"+ sdf.FileName);}}}}

微软最有价值专家(MVP)

微软最有价值专家是微软公司授予第三方技术专业人士的一个全球奖项。31年来,世界各地的技术社区领导者,因其在线上和线下的技术社区中分享专业知识和经验而获得此奖项。

MVP 是经过严格挑选的专家团队,他们代表着技术最精湛且最具智慧的人,是对社区投入极大的热情并乐于助人的专家。MVP 致力于通过演讲、论坛问答、创建网站、撰写博客、分享视频、开源项目、组织会议等方式来帮助他人,并最大程度地帮助微软技术社区用户使用 Microsoft 技术。

更多详情请登录官方网站:

https://mvp.microsoft.com/zh-cn

微信公众号|微软开发者MSDN

新浪微博|微软中国MSDN

·END·


Copyright © 2025 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1