-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSAM.cs
More file actions
176 lines (153 loc) · 6.45 KB
/
SAM.cs
File metadata and controls
176 lines (153 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
namespace SAMViewer
{
/// <summary>
/// Segment Anything
/// </summary>
class SAM
{
public static SAM theSingleton = null;
InferenceSession mEncoder;
InferenceSession mDecoder;
public float mask_threshold = 0.0f;
bool mReady = false;
protected SAM()
{
}
public static SAM Instance()
{
if (null == theSingleton)
{
theSingleton = new SAM();
}
return theSingleton;
}
/// <summary>
/// 加载Segment Anything模型
/// </summary>
public void LoadONNXModel()
{
if (this.mEncoder != null)
this.mEncoder.Dispose();
if (this.mDecoder != null)
this.mDecoder.Dispose();
var options = new SessionOptions();
options.EnableMemoryPattern = false;
options.EnableCpuMemArena = false;
string exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string encode_model_path = exePath + @"\encoder-quant.onnx";
if (!File.Exists(encode_model_path))
{
MessageBox.Show(encode_model_path + " not exist!");
return;
}
this.mEncoder = new InferenceSession(encode_model_path, options);
string decode_model_path = exePath + @"\decoder-quant.onnx";
if (!File.Exists(decode_model_path))
{
MessageBox.Show(decode_model_path + " not exist!");
return;
}
this.mDecoder = new InferenceSession(decode_model_path, options);
}
/// <summary>
/// Segment Anything对图像进行编码
/// </summary>
public float[] Encode(Mat image,int orgWid,int orgHei)
{
Transforms tranform = new Transforms(1024);
float[] img = tranform.ApplyImage(image, orgWid, orgHei);
var tensor = new DenseTensor<float>(img, new[] { 1, 3, 1024, 1024 });
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("x", tensor)
};
var results = this.mEncoder.Run(inputs);
var embedding = results.First().AsTensor<float>().ToArray();
this.mReady = true;
return embedding;
}
/// <summary>
/// Segment Anything提示信息解码
/// </summary>
public MaskData Decode(List<Promotion> promotions, float[] embedding, int orgWid, int orgHei)
{
if (this.mReady == false)
{
MessageBox.Show("Image Embedding is not done!");
return null;
}
var embedding_tensor = new DenseTensor<float>(embedding, new[] { 1, 256, 64, 64 });
var bpmos = promotions.FindAll(e => e.mType == PromotionType.Box);
var pproms = promotions.FindAll(e => e.mType == PromotionType.Point);
int boxCount = promotions.FindAll(e => e.mType == PromotionType.Box).Count();
int pointCount = promotions.FindAll(e => e.mType == PromotionType.Point).Count();
float[] promotion = new float[2 * (boxCount * 2 + pointCount)];
float[] label = new float[boxCount * 2 + pointCount];
for (int i = 0; i < boxCount; i++)
{
var input = bpmos[i].GetInput();
for (int j = 0; j < input.Count(); j++)
{
promotion[4 * i + j] = input[j];
}
var la = bpmos[i].GetLable();
for (int j = 0; j < la.Count(); j++)
{
label[2 * i + j] = la[j];
}
}
for (int i = 0; i < pointCount; i++)
{
var p = pproms[i].GetInput();
for (int j = 0; j < p.Count(); j++)
{
promotion[boxCount * 4 + 2 * i + j] = p[j];
}
var la = pproms[i].GetLable();
for (int j = 0; j < la.Count(); j++)
{
label[boxCount * 2 + i + j] = la[j];
}
}
var point_coords_tensor = new DenseTensor<float>(promotion, new[] { 1, boxCount * 2 + pointCount, 2 });
var point_label_tensor = new DenseTensor<float>(label, new[] { 1, boxCount * 2 + pointCount });
float[] mask = new float[256 * 256];
for (int i = 0; i < mask.Count(); i++)
{
mask[i] = 0;
}
var mask_tensor = new DenseTensor<float>(mask, new[] { 1, 1, 256, 256 });
float[] hasMaskValues = new float[1] { 0 };
var hasMaskValues_tensor = new DenseTensor<float>(hasMaskValues, new[] { 1 });
float[] orig_im_size_values = { (float)orgHei, (float)orgWid };
var orig_im_size_values_tensor = new DenseTensor<float>(orig_im_size_values, new[] { 2 });
var decode_inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("image_embeddings", embedding_tensor),
NamedOnnxValue.CreateFromTensor("point_coords", point_coords_tensor),
NamedOnnxValue.CreateFromTensor("point_labels", point_label_tensor),
NamedOnnxValue.CreateFromTensor("mask_input", mask_tensor),
NamedOnnxValue.CreateFromTensor("has_mask_input", hasMaskValues_tensor),
NamedOnnxValue.CreateFromTensor("orig_im_size", orig_im_size_values_tensor)
};
MaskData md = new MaskData();
var segmask = this.mDecoder.Run(decode_inputs).ToList();
md.mMask = segmask[0].AsTensor<float>().ToArray().ToList();
md.mShape = segmask[0].AsTensor<float>().Dimensions.ToArray();
md.mIoU = segmask[1].AsTensor<float>().ToList();
return md;
}
}
}