22 public Model(
string file)
25 Name = Path.GetFileNameWithoutExtension(file);
26 if (file.EndsWith(
".onnx"))
28 InferenceSession =
new InferenceSession(file);
29 foreach (var m
in InferenceSession.InputMetadata)
31 InputValueTypes.Add(m.Value);
33 foreach (var m
in InferenceSession.InputNames)
35 InputValueNames.Add(m);
37 foreach (var m
in InferenceSession.OutputMetadata)
39 OutputValueTypes.Add(m.Value);
42 else if (file.EndsWith(
".pt"))
46 Module = torch.jit.load(file);
48 var chs = Module.named_modules();
49 var input = chs.First();
50 InputModule = input.module;
51 var output = chs.Last();
52 OutputModule = output.module;
58 Console.WriteLine(e.Message.ToString());
60 string f = Path.GetDirectoryName(file) +
"/" + Path.GetFileNameWithoutExtension(file) +
".yaml";
61 if (!System.IO.File.Exists(f))
63 MessageBox.Show(
"No corresponding model Yaml metadata file for:" + Name);
66 using (var reader =
new StreamReader(f))
68 var dynamicDeserializer =
new DeserializerBuilder().Build();
69 Metadata = dynamicDeserializer.Deserialize<Dictionary<string, object>>(reader);
70 IDictionary conf = (IDictionary)Metadata[
"config"];
71 Object inpp = Metadata[
"inputs"];
72 List<Object> inpps = (List<Object>)inpp;
73 IDictionary inpinfo = (IDictionary)inpps[0];
74 IDictionary imagej = (IDictionary)conf[
"deepimagej"];
75 IDictionary test = (IDictionary)imagej[
"test_information"];
76 Object inputs = test[
"inputs"];
77 List<Object> inps = (List<Object>)inputs;
78 IDictionary info = (IDictionary)inps[0];
79 IDictionary inputinfo = (IDictionary)inpps[0];
80 IDictionary shapeinfo = (IDictionary)inputinfo[
"shape"];
81 List<Object> shapeinfomin = (List<Object>)shapeinfo[
"min"];
82 int len = shapeinfomin.Count;
83 string size = (string)info[
"size"];
84 string[] sts = size.Split(
"x");
85 if (len != sts.Length)
87 shape =
new long[len];
88 for (
int i = 0; i < len; i++)
90 if (len - i > sts.Length)
96 shape[i] =
long.Parse(sts[sts.Length - i]);
102 shape =
new long[sts.Length];
103 for (
int i = 0; i < sts.Length; i++)
106 shape[i] =
long.Parse(s);
109 List<Object> val = (List<Object>)inpinfo[
"data_range"];
110 MaxValue =
int.Parse(val[1].ToString());
114 public Dictionary<string, object> Metadata {
get;
set; } =
null;
115 public InferenceSession InferenceSession {
get;
set; }
116 public torch.jit.ScriptModule Module {
get;
set; }
117 public torch.nn.Module InputModule {
get;
set; }
118 public torch.nn.Module OutputModule {
get;
set; }
119 private List<NodeMetadata> InputValueTypes {
get;
set; } =
new List<NodeMetadata>();
120 private List<NodeMetadata> OutputValueTypes {
get;
set; } =
new List<NodeMetadata>();
121 private List<string> InputValueNames {
get;
set; } =
new List<string>();
122 public string File {
get;
set; }
123 public string Name {
get;
set; }
128 if (File.EndsWith(
".onnx"))
130 if (InputValueTypes.Count > 0)
132 int w = InputValueTypes[0].Dimensions[2];
135 if (OutputValueTypes.Count > 0)
137 for (
int i = 0; i < OutputValueTypes.Count; i++)
139 int max =
int.MinValue;
140 for (
int j = 0; j < OutputValueTypes[i].Dimensions.Length; j++)
142 if (OutputValueTypes[i].Dimensions[j] > max)
143 max = OutputValueTypes[i].Dimensions[j];
155 if (shape.Length == 4)
156 return (
int)Shape[0];
158 return (
int)Shape[4];
166 if (File.EndsWith(
".onnx"))
168 if (InputValueTypes.Count > 0)
170 int h = InputValueTypes[0].Dimensions[3];
173 if (OutputValueTypes.Count > 0)
175 for (
int i = 0; i < OutputValueTypes.Count; i++)
177 int max =
int.MinValue;
178 for (
int j = 0; j < OutputValueTypes[i].Dimensions.Length; j++)
180 if (OutputValueTypes[i].Dimensions[j] > max)
181 max = OutputValueTypes[i].Dimensions[j];
193 if (shape.Length == 4)
194 return (
int)Shape[1];
196 return (
int)Shape[3];
204 if (shape.Length == 4)
205 return (
int)Shape[2];
207 return (
int)shape[1];
215 if (Metadata ==
null)
216 return new long[] { 1, 3, 256, 256 };
223 public int MaxValue {
get;
set; }
224 public ModelOutputType ModelType {
get;
set; }
225 public enum ModelOutputType
230 public void Run(BioImage b)
232 if (File.EndsWith(
".onnx"))
234 else if (File.EndsWith(
".pt"))
237 public void RunONNX(BioImage b)
239 BioImage bb =
new BioImage(
"output.ome.tif");
240 if (b.Type != BioImage.ImageType.pyramidal)
242 for (
int i = 0; i < b.Buffers.Count; i++)
244 Bitmap bm = ResizeBilinear(b.Buffers[i], Width, Height);
247 int d = InputValueTypes[0].Dimensions[1];
248 float[] img =
new float[w * h * d];
249 for (
int y = 0; y < h; y++)
251 for (
int x = 0; x < w; x++)
253 for (
int c = 0; c < d; c++)
255 int index = (y * w * d) + x;
256 if (c > bm.RGBChannelsCount)
262 if (bm.BitsPerPixel > 8)
263 img[index] = bm.GetValue(x, y, c);
265 img[index] = bm.GetValue(x, y, c);
270 var tensor =
new DenseTensor<float>(img,
new[] { 1, InputValueTypes[0].Dimensions[1], w, h });
271 var inputs =
new List<NamedOnnxValue>
273 NamedOnnxValue.CreateFromTensor(InputValueNames[0], tensor)
275 var results = InferenceSession.Run(inputs);
277 foreach (var result
in results)
279 if (OutputValueTypes[r].Dimensions[3] == Width)
281 float[] outputmask = result.AsTensor<
float>().ToArray();
282 double mean = outputmask.Mean();
283 for (
int c = 0; c < OutputValueTypes[r].Dimensions[1]; c++)
285 Bitmap bmp =
new Bitmap(Width, Height, b.Buffers[0].PixelFormat);
286 for (
int y = 0; y < h; y++)
288 for (
int x = 0; x < w; x++)
290 int index = (y * w * d) + x;
291 bmp.SetPixel(x, y,
new ColorS((ushort)(outputmask[index] / mean)));
294 bb.Buffers.Add(ResizeBilinear(bmp, b.SizeX, b.SizeY));
300 bb.Channels.AddRange(b.Channels);
301 if (OutputValueTypes[0].Dimensions[1] > 1)
303 bb.Channels.AddRange(b.Channels);
304 for (
int i = 0; i < bb.Channels.Count; i++)
306 bb.Channels[i].Index = i;
309 bb.UpdateCoords(b.SizeZ, b.SizeC * OutputValueTypes[0].Dimensions[1], b.SizeT);
310 bb.Resolutions.Add(
new Resolution(bb.SizeX, bb.SizeY, bb.Buffers[0].PixelFormat, b.PhysicalSizeX, b.PhysicalSizeY, b.PhysicalSizeZ, b.StageSizeX, b.StageSizeY, b.StageSizeZ));
311 bb.Volume = b.Volume;
312 bb.bitsPerPixel = bb.Buffers[0].BitsPerPixel;
314 BioImage.AutoThreshold(bb,
true);
319 throw new NotImplementedException();
321 public void RunTorch(BioImage b)
323 BioImage bb =
new BioImage(
"output.ome.tif");
324 if (b.Type != BioImage.ImageType.pyramidal)
328 if (shape.Length == 4)
330 for (
int i = 0; i < b.Buffers.Count; i++)
332 Bitmap bm = ResizeBilinear(b.Buffers[i], Width, Height);
336 float[,,,] img =
new float[1, Depth, Width, Height];
337 for (
int y = 0; y < h; y++)
339 for (
int x = 0; x < w; x++)
341 for (
int c = 0; c < d; c++)
343 int index = (y * w * c) + x;
344 if (c >= bm.RGBChannelsCount)
350 if (bm.BitsPerPixel > 8)
351 img[0, c, x, y] = ((float)bm.GetValue(x, y, c) / (
float)ushort.MaxValue) * MaxValue;
353 img[0, c, x, y] = ((float)bm.GetValue(x, y, c) / (
float)
byte.MaxValue) * MaxValue;
358 var tensor = torch.tensor(img);
359 object results =
null;
362 results = Module.forward(tensor);
366 Console.WriteLine(e.ToString());
368 TorchSharp.Utils.TensorAccessor<
float> outputmask = ((torch.Tensor)results).data<float>();
369 double mean = outputmask.Mean();
370 resDepth = (int)((torch.Tensor)results).shape[1];
371 for (
int c = 0; c < resDepth; c++)
373 Bitmap bmp =
new Bitmap(Width, Height, b.Buffers[0].PixelFormat);
374 for (
int y = 0; y < h; y++)
376 for (
int x = 0; x < w; x++)
378 int index = (y * w * c) + x;
379 bmp.SetPixel(x, y,
new ColorS((ushort)(outputmask[index] / mean)));
382 bb.Buffers.Add(ResizeBilinear(bmp, b.SizeX, b.SizeY));
383 bb.Buffers[bb.Buffers.Count - 1].RotateFlip(AForge.RotateFlipType.Rotate90FlipX);
389 else if (shape.Length == 5)
391 torch.Tensor tensor = torch.empty(shape);
392 for (
int i = 0; i < b.Buffers.Count; i++)
394 Bitmap bm = ResizeBilinear(b.Buffers[i], Width, Height);
395 for (
int y = 0; y < Height; y++)
397 for (
int x = 0; x < Width; x++)
399 for (
int c = 0; c < Depth; c++)
401 if (c >= bm.RGBChannelsCount)
403 tensor[0, c, i, x, y] = 0;
407 if (bm.BitsPerPixel > 8)
408 tensor[0, c, i, x, y] = ((float)bm.GetValue(x, y, c) / (
float)ushort.MaxValue) * MaxValue;
410 tensor[0, c, i, x, y] = ((float)bm.GetValue(x, y, c) / (
float)
byte.MaxValue) * MaxValue;
416 torch.Tensor outputmask = (torch.Tensor)Module.forward(tensor);
417 resDepth = (int)(outputmask.shape[1]);
418 for (
int c = 0; c < resDepth; c++)
420 for (
int buf = 0; buf < (int)outputmask.shape[2]; buf++)
422 Bitmap bmp =
new Bitmap((
int)outputmask.shape[3], (
int)outputmask.shape[4], b.Buffers[0].PixelFormat);
423 for (
int y = 0; y < Height; y++)
425 for (
int x = 0; x < Width; x++)
427 float by = outputmask[0, c, buf, x, y].ToSingle();
428 bmp.SetPixel(x, y,
new ColorS((ushort)(by *
byte.MaxValue)));
431 bb.Buffers.Add(ResizeBilinear(bmp, b.SizeX, b.SizeY));
432 bb.Buffers[bb.Buffers.Count - 1].RotateFlip(AForge.RotateFlipType.Rotate90FlipX);
436 bb.Channels.AddRange(b.Channels);
439 bb.Channels.AddRange(b.Channels);
440 for (
int i = 0; i < bb.Channels.Count; i++)
442 bb.Channels[i].Index = i;
445 bb.UpdateCoords(b.SizeZ, b.SizeC * resDepth, b.SizeT);
446 bb.Resolutions.Add(
new Resolution(bb.SizeX, bb.SizeY, bb.Buffers[0].PixelFormat, b.PhysicalSizeX, b.PhysicalSizeY, b.PhysicalSizeZ, b.StageSizeX, b.StageSizeY, b.StageSizeZ));
447 bb.Volume = b.Volume;
448 bb.bitsPerPixel = bb.Buffers[0].BitsPerPixel;
449 BioImage.AutoThreshold(bb,
true);
454 throw new NotImplementedException();