25 Name = Path.GetFileNameWithoutExtension(file);
26 if (file.EndsWith(
".onnx"))
31 InputValueTypes.Add(m.Value);
35 InputValueNames.Add(m);
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();
51 var output = chs.Last();
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"];
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());
240 if (b.Type !=
BioImage.ImageType.pyramidal)
242 for (
int i = 0; i < b.Buffers.Count; i++)
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)
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++)
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)));
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;
317 throw new NotImplementedException();
322 if (b.Type !=
BioImage.ImageType.pyramidal)
326 if (shape.Length == 4)
328 for (
int i = 0; i < b.Buffers.Count; i++)
335 for (
int y = 0; y < h; y++)
337 for (
int x = 0; x < w; x++)
339 for (
int c = 0; c < d; c++)
341 int index = (y * w * c) + x;
342 if (c >= bm.RGBChannelsCount)
348 if (bm.BitsPerPixel > 8)
349 img[0, c, x, y] = ((float)bm.GetValue(x, y, c) / (
float)ushort.MaxValue) *
MaxValue;
351 img[0, c, x, y] = ((float)bm.GetValue(x, y, c) / (
float)
byte.
MaxValue) *
MaxValue;
356 var tensor = torch.tensor(img);
357 object results =
null;
360 results =
Module.forward(tensor);
364 Console.WriteLine(e.ToString());
366 TorchSharp.Utils.TensorAccessor<
float> outputmask = ((torch.Tensor)results).data<float>();
367 double mean = outputmask.Mean();
368 resDepth = (int)((torch.Tensor)results).shape[1];
369 for (
int c = 0; c < resDepth; c++)
372 for (
int y = 0; y < h; y++)
374 for (
int x = 0; x < w; x++)
376 int index = (y * w * c) + x;
377 bmp.SetPixel(x, y,
new ColorS((ushort)(outputmask[index] / mean)));
381 bb.Buffers[bb.Buffers.Count - 1].RotateFlip(AForge.RotateFlipType.Rotate90FlipX);
387 else if (shape.Length == 5)
389 torch.Tensor tensor = torch.empty(shape);
390 for (
int i = 0; i < b.Buffers.Count; i++)
393 for (
int y = 0; y <
Height; y++)
395 for (
int x = 0; x <
Width; x++)
397 for (
int c = 0; c <
Depth; c++)
399 if (c >= bm.RGBChannelsCount)
401 tensor[0, c, i, x, y] = 0;
405 if (bm.BitsPerPixel > 8)
406 tensor[0, c, i, x, y] = ((float)bm.GetValue(x, y, c) / (
float)ushort.MaxValue) *
MaxValue;
408 tensor[0, c, i, x, y] = ((float)bm.GetValue(x, y, c) / (
float)
byte.
MaxValue) *
MaxValue;
414 torch.Tensor outputmask = (torch.Tensor)
Module.forward(tensor);
415 resDepth = (int)(outputmask.shape[1]);
416 for (
int c = 0; c < resDepth; c++)
418 for (
int buf = 0; buf < (int)outputmask.shape[2]; buf++)
420 Bitmap bmp =
new Bitmap((
int)outputmask.shape[3], (
int)outputmask.shape[4], b.Buffers[0].PixelFormat);
421 for (
int y = 0; y <
Height; y++)
423 for (
int x = 0; x <
Width; x++)
425 float by = outputmask[0, c, buf, x, y].ToSingle();
426 bmp.SetPixel(x, y,
new ColorS((ushort)(by *
byte.
MaxValue)));
430 bb.Buffers[bb.Buffers.Count - 1].RotateFlip(AForge.RotateFlipType.Rotate90FlipX);
434 bb.Channels.AddRange(b.Channels);
437 bb.Channels.AddRange(b.Channels);
438 for (
int i = 0; i < bb.Channels.Count; i++)
440 bb.Channels[i].Index = i;
443 bb.UpdateCoords(b.SizeZ, b.SizeC * resDepth, b.SizeT);
444 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));
445 bb.Volume = b.Volume;
451 throw new NotImplementedException();