Skip to content

Commit 34b44dc

Browse files
committed
add vector normalizatin and fix double to float as the base type for csharp operations
1 parent fbe5c25 commit 34b44dc

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

MLTests/ImageToVector.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,32 @@ namespace MLTests
55
{
66
public static class ImageToVector
77
{
8-
public static double[] GetVectorOnPath(string path)
8+
private static float[] GetNormalizedVector(float[] vector)
9+
{
10+
var max = vector.Max();
11+
var min = vector.Min();
12+
for(var i = 0; i < vector.Length; i++) vector[i] /= max;
13+
return vector;
14+
}
15+
16+
public static float[] GetVectorOnPath(string path)
917
{
1018
var image = ImageUtil.LoadImg(path, "grayscale", target_size: (28, 28));
1119
NDarray x = ImageUtil.ImageToArray(image);
12-
//x = x.reshape(1);
13-
//x = x.reshape(78, 78, 3);
14-
//var shape = x.shape;
20+
var shape = x.shape; // (28, 28, 3)
21+
//var norm = new Keras.Layers.BatchNormalization();
22+
1523
//x = x.reshape(1, x.shape[0], x.shape[1], x.shape[2]);
16-
double[] data = x.GetData<double>();
24+
float[] data = x.GetData<float>();
1725
/*
1826
var array1 = ImageUtil.ImageToArray(image);
1927
2028
var array2 = np.expand_dims(array1, axis: 0);
2129
var array3 = (NDarray)array2;
22-
double[] data = array3.GetData<double>();
30+
float[] data = array3.GetData<float>();
2331
*/
24-
return data;
25-
//return new double[] { };
32+
return GetNormalizedVector(data);
33+
//return new float[] { };
2634
}
2735
}
2836
}

MLTests/ImageToVectorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public void LoadImage()
1111
var projectPath = "D:/projects.active/LearnML";
1212
var path = Path.GetFullPath(@$"{projectPath}/data/0/1.jpg");
1313
var vector = ImageToVector.GetVectorOnPath(path);
14-
vector.Sum().Should().NotBe(0.0);
14+
vector.Sum().Should().NotBe(0.0f);
1515
}
1616
}
1717
}

0 commit comments

Comments
 (0)