littlelittlecloud commited on
Commit
360f5f4
0 Parent(s):
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. .gitignore +217 -0
  3. Program.cs +63 -0
  4. clip.csproj +20 -0
.gitattributes ADDED
@@ -0,0 +1 @@
 
 
1
+ ddim_v_sampler.ckpt filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # .gitignore for C# and dotnet projects
2
+ *.csproj.user
3
+ *.suo
4
+ *.user
5
+ # Build results
6
+ [Dd]ebug/
7
+ [Rr]elease/
8
+ x64/
9
+ [Bb]in/
10
+ [Oo]bj/
11
+ # build folder is nowadays used for build scripts and should not be ignored
12
+ #build/
13
+
14
+ # NuGet Packages
15
+ *.nupkg
16
+ # The packages folder can be ignored because of Package Restore
17
+ **/packages/*
18
+ # except build/, which is used as an MSBuild target.
19
+ !**/packages/build/
20
+ # Uncomment if necessary however generally it will be regenerated when needed
21
+ #!**/packages/repositories.config
22
+
23
+ # MSTest test Results
24
+ [Tt]est[Rr]esult*/
25
+ [Bb]uild[Ll]og.*
26
+
27
+ *_i.c
28
+ *_p.c
29
+ *.ilk
30
+ *.meta
31
+ *.obj
32
+ *.pch
33
+ *.pdb
34
+ *.pgc
35
+ *.pgd
36
+ *.rsp
37
+ *.sbr
38
+ *.tlb
39
+ *.tli
40
+ *.tlh
41
+ *.tmp
42
+ *.tmp_proj
43
+ *.log
44
+ *.vspscc
45
+ *.vssscc
46
+ .builds
47
+ *.pidb
48
+ *.scc
49
+
50
+ # Visual C++ cache files
51
+ ipch/
52
+ *.aps
53
+ *.ncb
54
+ *.opensdf
55
+ *.sdf
56
+ *.cachefile
57
+
58
+ # Visual Studio profiler
59
+ *.psess
60
+ *.vsp
61
+ *.vspx
62
+
63
+ # Guidance Automation Toolkit
64
+ *.gpState
65
+
66
+ # ReSharper is a .NET coding add-in
67
+ _ReSharper*/
68
+ *.[Rr]e[Ss]harper
69
+
70
+ # TeamCity is a build add-in
71
+ _TeamCity*
72
+
73
+ # DotCover is a Code Coverage Tool
74
+ *.dotCover
75
+
76
+ # NCrunch
77
+ *.ncrunch*
78
+ .*crunch*.local.xml
79
+
80
+ # Installshield output folder
81
+ [Ee]xpress/
82
+
83
+ # DocProject is a documentation generator add-in
84
+ DocProject/buildhelp/
85
+ DocProject/Help/*.HxT
86
+ DocProject/Help/*.HxC
87
+ DocProject/Help/*.hhc
88
+ DocProject/Help/*.hhk
89
+ DocProject/Help/*.hhp
90
+ DocProject/Help/Html2
91
+ DocProject/Help/html
92
+
93
+ # Click-Once directory
94
+ publish/
95
+
96
+ # Publish Web Output
97
+ *.Publish.xml
98
+
99
+ # Windows Azure Build Output
100
+ csx
101
+ *.build.csdef
102
+
103
+ # Windows Store app package directory
104
+ AppPackages/
105
+
106
+ # Others
107
+ *.Cache
108
+ ClientBin/
109
+ [Ss]tyle[Cc]op.*
110
+ ~$*
111
+ *~
112
+ *.dbmdl
113
+ *.[Pp]ublish.xml
114
+ *.pfx
115
+ *.publishsettings
116
+ modulesbin/
117
+ tempbin/
118
+
119
+ # EPiServer Site file (VPP)
120
+ AppData/
121
+
122
+ # RIA/Silverlight projects
123
+ Generated_Code/
124
+
125
+ # Backup & report files from converting an old project file to a newer
126
+ # Visual Studio version. Backup files are not needed, because we have git ;-)
127
+ _UpgradeReport_Files/
128
+ Backup*/
129
+ UpgradeLog*.XML
130
+ UpgradeLog*.htm
131
+
132
+ # vim
133
+ *.txt~
134
+ *.swp
135
+ *.swo
136
+
137
+ # Temp files when opening LibreOffice on ubuntu
138
+ .~lock.*
139
+
140
+ # svn
141
+ .svn
142
+
143
+ # CVS - Source Control
144
+ **/CVS/
145
+
146
+ # Remainings from resolving conflicts in Source Control
147
+ *.orig
148
+
149
+ # SQL Server files
150
+ **/App_Data/*.mdf
151
+ **/App_Data/*.ldf
152
+ **/App_Data/*.sdf
153
+
154
+
155
+ #LightSwitch generated files
156
+ GeneratedArtifacts/
157
+ _Pvt_Extensions/
158
+ ModelManifest.xml
159
+
160
+ # =========================
161
+ # Windows detritus
162
+ # =========================
163
+
164
+ # Windows image file caches
165
+ Thumbs.db
166
+ ehthumbs.db
167
+
168
+ # Folder config file
169
+ Desktop.ini
170
+
171
+ # Recycle Bin used on file shares
172
+ $RECYCLE.BIN/
173
+
174
+ # OS generated files #
175
+ Icon?
176
+
177
+ # Mac desktop service store files
178
+ .DS_Store
179
+
180
+ # SASS Compiler cache
181
+ .sass-cache
182
+
183
+ # Visual Studio 2014 CTP
184
+ **/*.sln.ide
185
+
186
+ # Visual Studio temp something
187
+ .vs/
188
+
189
+ # dotnet stuff
190
+ project.lock.json
191
+
192
+ # VS 2015+
193
+ *.vc.vc.opendb
194
+ *.vc.db
195
+
196
+ # Rider
197
+ .idea/
198
+
199
+ # Visual Studio Code
200
+ .vscode/
201
+
202
+ # Output folder used by Webpack or other FE stuff
203
+ **/node_modules/*
204
+ **/wwwroot/*
205
+
206
+ # SpecFlow specific
207
+ *.feature.cs
208
+ *.feature.xlsx.*
209
+ *.Specs_*.html
210
+
211
+ # UWP Projects
212
+ AppPackages/
213
+
214
+ #####
215
+ # End of core ignore list, below put you custom 'per project' settings (patterns or path)
216
+ #####
217
+
Program.cs ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.IO;
4
+ using System.Linq;
5
+ using TorchSharp;
6
+ torchvision.io.DefaultImager = new torchvision.io.SkiaImager();
7
+ var device = TorchSharp.torch.device("cuda:0");
8
+ var ddpm_v_sampler = TorchSharp.torch.jit.load("ddim_v_sampler.ckpt");
9
+ ddpm_v_sampler.to(device);
10
+ ddpm_v_sampler.eval();
11
+
12
+ var start_token = 49406;
13
+ var end_token = 49407;
14
+ var dictionary = new Dictionary<string, long>(){
15
+ {"cat", 2368},
16
+ {"a", 320},
17
+ {"cute", 2242},
18
+ {"blue", 1746},
19
+ {"wild", 3220},
20
+ {"green", 1901},
21
+ };
22
+
23
+ var batch = 1;
24
+
25
+ var prompt = "a wild cute green cat";
26
+ var tokens = prompt.Split(' ').Select(x => dictionary[x]).ToList();
27
+ tokens = tokens.Prepend(start_token).ToList();
28
+ tokens = tokens.Append(end_token).ToList();
29
+ tokens = tokens.Concat(Enumerable.Repeat<long>(0, 77 - tokens.Count)).ToList();
30
+ var uncontional_tokens = new[]{start_token, end_token}.Concat(Enumerable.Repeat(0, 75)).ToList();
31
+ var tokenTensor = torch.tensor(tokens.ToArray(), dtype: torch.ScalarType.Int64, device: device);
32
+ tokenTensor = tokenTensor.reshape((long)batch, -1);
33
+ var unconditional_tokenTensor = torch.tensor(uncontional_tokens.ToArray(), dtype: torch.ScalarType.Int64, device: device);
34
+ unconditional_tokenTensor = unconditional_tokenTensor.reshape((long)batch, -1);
35
+ var img = torch.randn(batch, 4, 96, 96, dtype: torch.ScalarType.Float32, device: device);
36
+ var t = torch.ones(batch, dtype: torch.ScalarType.Int32, device: device);
37
+ var condition = ddpm_v_sampler.invoke("clip_encoder", tokenTensor);
38
+ var unconditional_condition = ddpm_v_sampler.invoke("clip_encoder", unconditional_tokenTensor);
39
+ Console.WriteLine(condition);
40
+ var timesteps = 1000;
41
+ var ddim_steps = 50;
42
+ int gap = timesteps / ddim_steps;
43
+ using(var context = torch.enable_grad(false))
44
+ {
45
+ for(var i = timesteps-1; i >=0; i -= gap)
46
+ {
47
+ var t_cur = torch.full(batch, i, dtype: torch.ScalarType.Int64, device: device);
48
+ var t_prev = torch.full(batch, i - gap >= 0? i - gap: 0, dtype: torch.ScalarType.Int64, device: device);
49
+ img = (torch.Tensor)ddpm_v_sampler.invoke("ddim_sampler", img, condition, unconditional_condition, t_cur, t_prev);
50
+ Console.WriteLine($"step {i}");
51
+ }
52
+
53
+ var decoded_images = (torch.Tensor)ddpm_v_sampler.invoke("decode_image", img);
54
+ decoded_images = torch.clamp((decoded_images + 1.0) / 2.0, 0.0, 1.0);
55
+
56
+ for(int i = 0; i!= batch; ++i)
57
+ {
58
+ // c * h * w
59
+ var image = decoded_images[i];
60
+ image = (image * 255.0).to(torch.ScalarType.Byte).cpu();
61
+ torchvision.io.write_image(image, $"{i}.png", torchvision.ImageFormat.Png);
62
+ }
63
+ }
clip.csproj ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Project Sdk="Microsoft.NET.Sdk">
2
+
3
+ <PropertyGroup>
4
+ <OutputType>Exe</OutputType>
5
+ <TargetFramework>net6.0</TargetFramework>
6
+ <Nullable>enable</Nullable>
7
+ <Platform>x64</Platform>
8
+ <TorchVersion>0.99.3</TorchVersion>
9
+ </PropertyGroup>
10
+
11
+ <ItemGroup>
12
+ <PackageReference Include="TorchVision" Version="$(TorchVersion)" />
13
+ <PackageReference Include="TorchSharp-cuda-linux" Version="$(TorchVersion)" />
14
+ <None Update="*.ckpt">
15
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
16
+ </None>
17
+ <PackageReference Include="Microsoft.ML" Version="2.0.1" />
18
+ </ItemGroup>
19
+
20
+ </Project>