Skip to content

Commit 9b8b50e

Browse files
committed
add Streetlight recognize simple learn by Numpy data functions
1 parent 4d3a37f commit 9b8b50e

4 files changed

+113
-49
lines changed

MLTests/DigitalRecognizerTests.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

MLTests/DigitalRecognizer.cs renamed to MLTests/NDigitalRecognizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace MLTests
22
{
3-
public class DigitalRecognizer
3+
public class NDigitalRecognizer
44
{
55
private ImageToVector _vectorReader;
66
private string _trainDataPath;
@@ -13,7 +13,7 @@ public class DigitalRecognizer
1313
private const int DefaultIterations = 10;
1414
private const int DefaultSampleSize = 100;
1515

16-
public DigitalRecognizer(ImageToVector vectorReader, int inputSize, int outputSize, string trainDataPath, int sampleSize = DefaultSampleSize, float alpha = DefaultAlpha, int iterations = DefaultIterations)
16+
public NDigitalRecognizer(ImageToVector vectorReader, int inputSize, int outputSize, string trainDataPath, int sampleSize = DefaultSampleSize, float alpha = DefaultAlpha, int iterations = DefaultIterations)
1717
{
1818
_sampleSize = sampleSize;
1919
_iterations = iterations;

MLTests/NDigitalRecognizerTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using NUnit.Framework;
2+
3+
namespace MLTests
4+
{
5+
public class NDigitalRecognizerTests
6+
{
7+
[Test]
8+
public void ShouldRecognizeDigit()
9+
{
10+
var projectPath = "D:/projects.active/LearnML";
11+
var trainDataPath = Path.GetFullPath(@$"{projectPath}/data");
12+
var digitZeroPath = Path.Combine(trainDataPath, "0", "155.jpg");
13+
var digitOnePath = Path.Combine(trainDataPath, "1", "1055.jpg");
14+
var digitTwoPath = Path.Combine(trainDataPath, "2", "1055.jpg");
15+
var digitThreePath = Path.Combine(trainDataPath, "3", "1055.jpg");
16+
var digitFourPath = Path.Combine(trainDataPath, "4", "1055.jpg");
17+
var digitFivePath = Path.Combine(trainDataPath, "5", "1055.jpg");
18+
var digitSixPath = Path.Combine(trainDataPath, "6", "1055.jpg");
19+
var digitSevenPath = Path.Combine(trainDataPath, "7", "1055.jpg");
20+
var digitEightPath = Path.Combine(trainDataPath, "8", "1055.jpg");
21+
var digitNinePath = Path.Combine(trainDataPath, "9", "1055.jpg");
22+
23+
var inputSize = 28 * 28;
24+
var outputSize = 10;
25+
26+
var alpha = 0.001f;
27+
var iterations = 20;
28+
var sampleSize = 100;
29+
30+
var vectorReader = new ImageToVector();
31+
var recognizer = new NDigitalRecognizer(vectorReader, inputSize, outputSize, trainDataPath, sampleSize, alpha, iterations);
32+
33+
for (int learnDigit = 0; learnDigit < 10; learnDigit++)
34+
{
35+
recognizer.Learn(learnDigit);
36+
}
37+
for (int learnDigit = 0; learnDigit < 10; learnDigit++)
38+
{
39+
recognizer.Learn(learnDigit);
40+
}
41+
for (int learnDigit = 0; learnDigit < 10; learnDigit++)
42+
{
43+
recognizer.Learn(learnDigit);
44+
}
45+
46+
{
47+
var (digit, value) = recognizer.Predict(digitNinePath);
48+
Assert.That(digit, Is.EqualTo(9));
49+
Assert.That(value, Is.AtLeast(0.5f));
50+
}
51+
{
52+
var (digit, value) = recognizer.Predict(digitFivePath);
53+
Assert.That(digit, Is.EqualTo(5));
54+
Assert.That(value, Is.AtLeast(0.5f));
55+
56+
}
57+
58+
//recognizer.Predict(digitOnePath)[zero].Should().BeInRange(0.0f, 0.6f);
59+
//recognizer.Predict(digitZeroPath)[zero].Should().BeInRange(0.8f, 1.2f);
60+
}
61+
}
62+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using FluentAssertions;
2+
using Numpy;
3+
using NUnit.Framework;
4+
5+
namespace MLTests
6+
{
7+
public class NStreetLightRecognizerTests
8+
{
9+
private NDarray NGradient(NDarray weights, NDarray input, NDarray predictionGoal, float alpha, int iterations)
10+
{
11+
var resultWeights = weights.copy();
12+
for(int i = 0; i < iterations; i++)
13+
{
14+
var prediction = input.dot(resultWeights);
15+
var delta = prediction - predictionGoal ;
16+
resultWeights = resultWeights - alpha * (input * delta);
17+
}
18+
return resultWeights;
19+
}
20+
21+
[Test]
22+
public void ShouldPredictWalkOrStayOnDifferentLight()
23+
{
24+
var stop = 0f;
25+
var walk = 1f;
26+
27+
var streetLights = np.array(new float[,] {
28+
{1f, 0f, 1f},
29+
{0f, 1f, 1f},
30+
{0f, 0f, 1f},
31+
{1f, 1f, 1f},
32+
{0f, 1f, 1f},
33+
{1f, 0f, 1f},
34+
});
35+
var stayOrWalk = np.array(new float[] { 0f, 1f, 0f, 1f, 1f, 0f });
36+
var weights = np.array(new float[] { 0.5f, 0.48f, -0.7f });
37+
var alpha = 0.1f;
38+
39+
var input = streetLights[0];
40+
var predictionGoal = stayOrWalk[0];
41+
42+
var iterations = 100;
43+
44+
var weightsResult = NGradient(weights, input, predictionGoal, alpha, iterations);
45+
var result = (float) input.dot(weightsResult);
46+
result.Should().BeInRange(-0.0001f, 0.0001f);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)