Nav*_*avi 8 c# ocr artificial-intelligence aforge neural-network
我试图用C#中的Aforge.Net通过感知器进行OCR.我用九个30*30二进制图片学习了我的网络.但在结果中,它将所有内容都识别为"C".这是代码:
private void button1_Click(object sender, EventArgs e)
{
AForge.Neuro.ActivationNetwork network = new AForge.Neuro.ActivationNetwork(new AForge.Neuro.BipolarSigmoidFunction(2), 900, 3);
network.Randomize();
AForge.Neuro.Learning.PerceptronLearning learning = new AForge.Neuro.Learning.PerceptronLearning(network);
learning.LearningRate =1 ;
double[][] input = new double[9][];
for (int i = 0; i < 9; i++)
{
input[i] = new double[900];
}
//Reading A images
for (int i = 1; i <= 3; i++)
{
Bitmap a = AForge.Imaging.Image.FromFile(path + "\\a" + i + ".bmp");
for (int j = 0; j < 30; j++)
for (int k = 0; k < 30; k++)
{
if (a.GetPixel(j, k).ToKnownColor() == KnownColor.White)
{
input[i-1][j * 10 + k] = -1;
}
else
input[i-1][j * 10 + k] = 1;
}
// showImage(a);
}
//Reading B images
for (int i = 1; i <= 3; i++)
{
Bitmap a = AForge.Imaging.Image.FromFile(path + "\\b" + i + ".bmp");
for (int j = 0; j < 30; j++)
for (int k = 0; k < 30; k++)
{
if (a.GetPixel(j , k).ToKnownColor() == KnownColor.White)
{
input[i + 2][j * 10 + k] = -1;
}
else
input[i + 2][j * 10 + k] = 1;
}
// showImage(a);
}
//Reading C images
for (int i = 1; i <= 3; i++)
{
Bitmap a = AForge.Imaging.Image.FromFile(path + "\\c" + i + ".bmp");
for (int j = 0; j < 30; j++)
for (int k = 0; k < 30; k++)
{
if (a.GetPixel(j , k ).ToKnownColor() == KnownColor.White)
{
input[i + 5][j * 10 + k] = -1;
}
else
input[i + 5][j * 10 + k] = 1;
}
// showImage(a);
}
bool needToStop = false;
int iteration = 0;
while (!needToStop)
{
double error = learning.RunEpoch(input, new double[9][] { new double[3] { 1, -1, -1 },new double[3] { 1, -1, -1 },new double[3] { 1, -1, -1 },//A
new double[3] { -1, 1, -1 },new double[3] { -1, 1, -1 },new double[3] { -1, 1, -1 },//B
new double[3] { -1, -1, 1 },new double[3] { -1, -1, 1 },new double[3] { -1, -1, 1 } }//C
/*new double[9][]{ input[0],input[0],input[0],input[1],input[1],input[1],input[2],input[2],input[2]}*/
);
//learning.LearningRate -= learning.LearningRate / 1000;
if (error == 0)
break;
else if (iteration < 1000)
iteration++;
else
needToStop = true;
System.Diagnostics.Debug.WriteLine("{0} {1}", error, iteration);
}
Bitmap b = AForge.Imaging.Image.FromFile(path + "\\b1.bmp");
//Reading A Sample to test Netwok
double[] sample = new double[900];
for (int j = 0; j < 30; j++)
for (int k = 0; k < 30; k++)
{
if (b.GetPixel(j , k ).ToKnownColor() == KnownColor.White)
{
sample[j * 30 + k] = -1;
}
else
sample[j * 30 + k] = 1;
}
foreach (double d in network.Compute(sample))
System.Diagnostics.Debug.WriteLine(d);//Output is Always C = {-1,-1,1}
}
Run Code Online (Sandbox Code Playgroud)
我真的很想知道它为什么回答错误.
将初始 30x30 图像加载到结构中的 double[900] 数组中时,input您使用以下计算:
for (int j = 0; j < 30; j++)
for (int k = 0; k < 30; k++)
{
if (a.GetPixel(j, k).ToKnownColor() == KnownColor.White)
input[i-1][j * 10 + k] = -1;
else
input[i-1][j * 10 + k] = 1;
}
Run Code Online (Sandbox Code Playgroud)
您的偏移量计算在这里是错误的。您需要更改j * 10 + k为j * 30 + k,否则您将得到无效结果。稍后,您在加载测试图像时使用正确的偏移计算,这就是它无法与损坏的样本正确匹配的原因。
您应该编写一个方法将位图加载到double[900]数组中并为每个图像调用它,而不是多次编写相同的代码。这有助于减少此类问题,即两段应该返回相同结果的代码给出不同的结果。
| 归档时间: |
|
| 查看次数: |
5838 次 |
| 最近记录: |