-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
78 lines (71 loc) · 2.35 KB
/
Utils.cs
File metadata and controls
78 lines (71 loc) · 2.35 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
using TensorFlow;
namespace PaintAI
{
public class Utils
{
public static TFTensor BitmapToTensorGrayScale(Bitmap bitmap)
{
// Resize to 28x28
bitmap = new Bitmap(bitmap, new Size(28, 28));
using (bitmap)
{
var matrix = new float[1, bitmap.Size.Height, bitmap.Size.Width, 1];
for (var iy = 0; iy < bitmap.Size.Height; iy++)
{
for (int ix = 0, index = iy * bitmap.Size.Width; ix < bitmap.Size.Width; ix++, index++)
{
Color pixel = bitmap.GetPixel(ix, iy);
matrix[0, iy, ix, 0] = pixel.B / 255.0f;
//matrix[0, iy, ix, 1] = pixel.Green() / 255.0f;
//matrix[0, iy, ix, 2] = pixel.Red() / 255.0f;
}
}
TFTensor tensor = matrix;
return tensor;
}
}
internal static int[] Quantized(float[,] results)
{
int[] q = new int[]
{
results[0,0]>0.5?1:0,
results[0,1]>0.5?1:0,
results[0,2]>0.5?1:0,
results[0,3]>0.5?1:0,
results[0,4]>0.5?1:0,
results[0,5]>0.5?1:0,
results[0,6]>0.5?1:0,
results[0,7]>0.5?1:0,
results[0,8]>0.5?1:0,
results[0,9]>0.5?1:0,
};
return q;
}
internal static int[] GetSortedResults(float[,] results)
{
var mapped = new Dictionary<int, float>();
for (int i = 0; i < 10; i++)
{
mapped.Add(i, results[0, i]);
}
var sorted = mapped.OrderByDescending(x => x.Value);
var output = new int[10];
var j = 0;
foreach (var pair in sorted)
{
output[j] = pair.Key;
j++;
}
return output;
}
internal static int GetIntegerFromQuantized(int[] quantized)
{
var index = Array.IndexOf(quantized, 1);
if (index == -1)
{
throw new ArgumentException("Quantized data contained no valid outputs");
}
return index;
}
}
}