Adline网络识别印刷体数字0到9-java实现

本篇只给出实现的代码,下一篇将讲一讲实现的原理,及其Adline网络中的LMS算法原理。

包含两个类:

package com.cgjr.com;

import java.security.DigestInputStream;
import java.util.Arrays;

import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.core.events.LearningEvent;
import org.neuroph.core.events.LearningEventListener;
import org.neuroph.util.TransferFunctionType;

public class AdalineDemo implements LearningEventListener {
    public final static int CHAR_WIDTH = 5;
    public final static int CHAR_HEIGHT = 7;
    public static String[][] DIGITS = {
            {
              " 000 ",
              "0   0",
              "0   0",
              "0   0",
              "0   0",
              "0   0",
              " 000 "
             },
            {
                  "  0  ",
                  " 00  ",
                  "0 0  ",
                  "  0  ",
                  "  0  ",
                  "  0  ",
                  "  0  " 

            }, {

                  " 000 ",
                  "0   0",
                  "    0",
                  "   0 ",
                  "  0  ",
                  " 0   ",
                  "00000"
            }, {

                  " 000 ",
                  "0   0",
                  "    0",
                  " 000 ",
                  "    0",
                  "0   0",
                  " 000 "     

            }, {

                  "   0 ",
                  "  00 ",
                  " 0 0 ",
                  "0  0 ",
                  "00000",
                  "   0 ",
                  "   0 "     

            }, {
                  "00000",
                  "0    ",
                  "0    ",
                  "0000 ",
                  "    0",
                  "0   0",
                  " 000 "     

            }, {
                  " 000 ",
                  "0   0",
                  "0    ",
                  "0000 ",
                  "0   0",
                  "0   0",
                  " 000 " 

            }, {
                  "00000",
                  "    0",
                  "    0",
                  "   0 ",
                  "  0  ",
                  " 0   ",
                  "0    " 

            }, {

                  " 000 ",
                  "0   0",
                  "0   0",
                  " 000 ",
                  "0   0",
                  "0   0",
                  " 000 " 

            }, {

                  " 000 ",
                  "0   0",
                  "0   0",
                  " 0000",
                  "    0",
                  "0   0",
                  " 000 " 

            }
    };

    public static void main(String[] args) {
        Adaline ada = new Adaline(CHAR_WIDTH * CHAR_HEIGHT,DIGITS.length,0.01d,TransferFunctionType.LINEAR);
        DataSet ds = new DataSet(CHAR_WIDTH * CHAR_HEIGHT, DIGITS.length);
        for (int i = 0; i < DIGITS.length; i++) {
            //一个数字符号就是一个训练的数据,第0个数字的的期望输出为0,第一个数字的期望输出为1等等。
            ds.addRow(createTrainDataRow(DIGITS[i],i));
        }
        //ada.getLearningRule().addListener(new AdalineDemo());
        ada.learn(ds);
        for (int i = 0; i < DIGITS.length; i++) {
            ada.setInput(image2data(DIGITS[i]));
            ada.calculate();
            printDIGITS(DIGITS[i]);
            System.out.println(maxIndex(ada.getOutput()));
            System.out.println(Arrays.toString(ada.getOutput()));
            System.out.println();
        }
    }

    private static int maxIndex(double[] output) {
        //这其实就是选出最接近一的那个
        double maxData=output[0];
        int maxIndex=0;
        for (int i = 0; i < output.length; i++) {
            if(maxData<output[i]){
                maxData=output[i];
                maxIndex=i;
            }
        }
        return maxIndex;
    }

    private static void printDIGITS(String[] image) {

        for (int i = 0; i < image.length; i++) {
            System.out.println(image[i]);
        }
        System.out.println("\n");
    }

    private static DataSetRow createTrainDataRow(String[] image, int idealValue) {
        //设置所有的为输出为负一,只有当那个等于
        double[] output=new double[DIGITS.length];
        for (int i = 0; i < output.length; i++) {
            output[i]=-1;
        }
        double[] input=image2data(image);
        output[idealValue]=1;
        DataSetRow dsr=new DataSetRow(input,output);
        return dsr;
    }

    //将图像转换为数字,空格的地方为-1,不空格的地方为1

    private static double[] image2data(String[] image) {
        double[] input=new double[CHAR_WIDTH*CHAR_HEIGHT];
        //行的长度,即为字符的长度,为整个字体的高度
        for (int row = 0; row < CHAR_HEIGHT; row++) {
            //有多少个列
            for (int col = 0; col < CHAR_WIDTH; col++) {
                int index=(row*CHAR_WIDTH)+col;
                char ch=image[row].charAt(col);
                input[index]=ch==‘0‘?1:-1;
            }
        }

        return input;
    }

    @Override
    public void handleLearningEvent(LearningEvent event) {
        // TODO Auto-generated method stub

    }

}

网络类:

package com.cgjr.com;

import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.nnet.comp.neuron.BiasNeuron;
import org.neuroph.nnet.learning.LMS;
import org.neuroph.util.ConnectionFactory;
import org.neuroph.util.LayerFactory;
import org.neuroph.util.NeuralNetworkFactory;
import org.neuroph.util.NeuralNetworkType;
import org.neuroph.util.NeuronProperties;
import org.neuroph.util.TransferFunctionType;

public class Adaline extends NeuralNetwork {

    /**
     * The class fingerprint that is set to indicate serialization compatibility
     * with a previous version of the class.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Creates new Adaline network with specified number of neurons in input
     * layer
     *
     * @param inputNeuronsCount
     *            number of neurons in input layer
     */
    public Adaline(int inputNeuronsCount, int outputNeuronsCount, double learnRate, TransferFunctionType transferFunction) {
        this.createNetwork(inputNeuronsCount, outputNeuronsCount, learnRate,transferFunction);
    }

    /**
     * Creates adaline network architecture with specified number of input
     * neurons
     *
     * @param inputNeuronsCount
     *            number of neurons in input layer
     */
    private void createNetwork(int inputNeuronsCount, int outputNeuronsCount, double learnRate,
            TransferFunctionType transferFunction) {
        // set network type code
        this.setNetworkType(NeuralNetworkType.ADALINE);

        // create input layer neuron settings for this network
        NeuronProperties inNeuronProperties = new NeuronProperties();
        inNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR);

        // createLayer input layer with specified number of neurons
        Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, inNeuronProperties);
        inputLayer.addNeuron(new BiasNeuron()); // add bias neuron (always 1,
                                                // and it will act as bias input
                                                // for output neuron)
        this.addLayer(inputLayer);

        // create output layer neuron settings for this network
        NeuronProperties outNeuronProperties = new NeuronProperties();
        if (transferFunction == TransferFunctionType.LINEAR) {
            outNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR);
        } else {
            outNeuronProperties.setProperty("transferFunction", TransferFunctionType.RAMP);
            outNeuronProperties.setProperty("transferFunction.slope", new Double(1));
            outNeuronProperties.setProperty("transferFunction.yHigh", new Double(1));
            outNeuronProperties.setProperty("transferFunction.xHigh", new Double(1));
            outNeuronProperties.setProperty("transferFunction.yLow", new Double(-1));
            outNeuronProperties.setProperty("transferFunction.xLow", new Double(-1));
        }
        // createLayer output layer (only one neuron)
        Layer outputLayer = LayerFactory.createLayer(outputNeuronsCount, outNeuronProperties);
        this.addLayer(outputLayer);

        // createLayer full conectivity between input and output layer
        ConnectionFactory.fullConnect(inputLayer, outputLayer);

        // set input and output cells for network
        NeuralNetworkFactory.setDefaultIO(this);

        // set LMS learning rule for this network
        LMS l = new LMS();
        l.setLearningRate(learnRate);
        this.setLearningRule(l);
    }

}

运行的结果截图:

时间: 2024-10-05 04:15:32

Adline网络识别印刷体数字0到9-java实现的相关文章

OpenCV——识别印刷体数字

数字识别和其他的所有计算机视觉相关的应用都会分为两个步骤:ROI抽取和识别. 1. ROI抽取即将感兴趣的区域从原始图像中分离初来,这个步骤包括二值化,噪点的消除等2. 识别即通过一些分类器将第一步中的结果进行分类,事实上属于机器学习的一个典型应用 数字识别步骤: 1.先处理图像: 转换为灰度值(灰度图较之原始图片,将三个维度的矩阵变成了一个维度) 转换为二值图(二值图即将灰度图转换成黑白图,每个点只有两种可能:非黑即白) Mat srcImage = imread("number.png&qu

利用卷积网络识别骰子点数

前言小叙 前一段时间通过bpnn反向传播神经网络实现了识别骰子点数的目标,而且效果不错,我们的识别率可以达到80%上下,其实已经可以应用于生产环境了.只不过读了卷积神经网络,第一次感受到原来还可以这样,感受到了新的世界观和人生观. 卷积这个词,第一次接触还是读图形处理的书的时候,中间会有卷积和滤波处理图片的内容,其实当时对于卷积也是懵懵懂懂,不明所以,无非就是一个个求积再求和,能有什么意义.不过这些天我算是有些明白了. 想通俗了解卷积的朋友可以访问这个链接.境外的朋友请看YouTube短视频,下

第五周学习--卷积网络识别含义多重性验证

先说这个验证的实验结论. 结论: 卷积网络识别的结果的各个位置具有多样性,即若一个神经网络识别出多个结果的时候,各个结果间即使数值相同,之间也不存在相互的影响. 推论1: 若要识别一个物体的n个不同特征,则必须至少提供共2*n个对应位置的不同特征的图片(即特征间组合不用考虑交叉但需全覆盖)进行识别才能使模型具有预测性. 推论2: 若要识别一个物体的n个相同特征,则最好将图片切分成n个1个特征的图片,再识别可以使模型具有最优识别效果 在前几周对卷积神经网络的学习过程之中,最终的结果虽然能够输出多个

输入一批整数,输出其中的最大值和最小值,输入数字0就结束循环。如下所示

import java.util.Scanner; /** * 输入一批整数,输出其中的最大值和最小值,输入数字0就结束循环.如*下所示 请输入一个整数(输入0结束):20 请输入一个整数(输入0结束):35 * 请输入一个整数(输入0结束):1 请输入一个整数(输入0结束):57 请输入一个整数(输入0结束):0 最大值是:57 最小值是:1 */ public class Max { public static void main(String[] args) { Scanner sc =

字符0,数字0,‘\0’,NULL

\0是一个转义字符,将字符0转义为数字0.

编程题:将数字0~5放入一个整型数组,并逆序输出数组

#include<stdio.h> void main() { int i,a[5]; for(i=0;i<5;i++)         /*给数组中元素赋值*/ a[i]=i; for(i=4;i>=0;i--)          /*逆序输出数组中元素值*/ printf("%3d",a[i]); printf("\n"); } 编程题:将数字0~5放入一个整型数组,并逆序输出数组,布布扣,bubuko.com

空暇时候思考2(&amp;#39;\0&amp;#39;等价于数字0还是字符0)

/********************************************************************** * * Copyright (c)2015,WK Studios * * Filename: A.h * * Compiler: GCC vc 6.0 * * Author:WK * * Time: 2015 6 7 * *******************************************************************

字符串分隔 -&gt;连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; ?长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

?连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组:?长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. 输入描述: 连续输入字符串(输入2次,每个字符串长度小于100) 输出描述: 输出到长度为8的新字符串数组 输入例子: abc 123456789 输出例子: abc00000 12345678 90000000 import java.util.*; public class Main{     public static void main(String[] ar

空闲时候思考2(&#39;\0&#39;等价于数字0还是字符0)

/********************************************************************** * * Copyright (c)2015,WK Studios * * Filename: A.h * * Compiler: GCC vc 6.0 * * Author:WK * * Time: 2015 6 7 * *******************************************************************