-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCLIP.cs
105 lines (93 loc) · 3.11 KB
/
CLIP.cs
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
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
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;
namespace SAMViewer
{
/// <summary>
/// Image And Text Encoder
/// </summary>
class CLIP
{
InferenceSession mTxtEncoder;
InferenceSession mImgEncoder;
public static CLIP theSingleton = null;
int contextLength = 77;
protected CLIP()
{
Thread thread = new Thread(() =>
{
this.LoadONNXModel();
});
thread.Start();
}
public static CLIP Instance()
{
if (null == theSingleton)
{
theSingleton = new CLIP();
}
return theSingleton;
}
/// <summary>
/// 加载CLIP模型
/// </summary>
void LoadONNXModel()
{
if (this.mTxtEncoder != null)
this.mTxtEncoder.Dispose();
if (this.mImgEncoder != null)
this.mImgEncoder.Dispose();
string exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string txtencoder = exePath + @"\textual.onnx";
if (!File.Exists(txtencoder))
{
MessageBox.Show(txtencoder + " not exist!");
return;
}
this.mTxtEncoder = new InferenceSession(txtencoder);
string imgencoder = exePath + @"\visual.onnx";
if (!File.Exists(imgencoder))
{
MessageBox.Show(imgencoder + " not exist!");
return;
}
this.mImgEncoder = new InferenceSession(imgencoder);
}
/// <summary>
/// CLIP对文本进行编码
/// </summary>
public List<float> TxtEncoder(string txt)
{
List<Int64> token = SimpleTokenizer.Instance().tolikenlize(txt);
var txt_tensor = new DenseTensor<Int64>(token.ToArray(), new[] { 1,contextLength });
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("input", txt_tensor)
};
var results = this.mTxtEncoder.Run(inputs);
var result = results.First().AsTensor<float>().ToArray();
return result.ToList();
}
/// <summary>
/// CLIP对图像进行编码
/// </summary>
public List<float> ImgEncoder(float[] img)
{
var tensor = new DenseTensor<float>(img, new[] { 1, 3, 224, 224 });
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("input", tensor)
};
var results = this.mImgEncoder.Run(inputs);
var result = results.First().AsTensor<float>().ToArray();
return result.ToList();
}
}
}