Face recognition based on BP neural network

Time:2021-5-30

1、 Threshold logic unit

Threshold logic unit (Tlu), which can input a group of weighted coefficient quantity, sum them, if the sum reaches or exceeds a certain threshold, output a quantity. For example, the inputs are A1, A2,..., an and the weights W1, W2,  ...,  wn。 Then there is the sum of AI x wi  , The excitation layer t is generated
    t = f((a1 x w1)+(a2 x w2)+...+(aI x wI)+...+ (an x wn)+b)

Face recognition based on BP neural network
The threshold logic unit that I understand is a tool for solving the problem of multi-dimensional linear weighted summation. So what should I do when encountering complex problems?
2、 Artificial neural network

Artificial neural networks (ANNs) is also referred to as neural networks (NNs) or connection models. It is an algorithmic mathematical model that imitates the behavior characteristics of animal neural networks and carries out distributed parallel information processing. This kind of network depends on the complexity of the system, by adjusting the relationship between a large number of internal nodes, so as to achieve the purpose of processing information.

Face recognition based on BP neural network
By combining multiple threshold logic units, a network is formed to simulate nonlinear problems. It has been proved that the three-layer artificial neural network can simulate various functions.
3、 BP neural network

BP (back propagation) neural network is a kind of multilayer feedforward network trained by error back propagation algorithm, which is one of the most widely used neural network models at present. BP network can learn and store a large number of input-output pattern mapping relationships without revealing the mathematical equations describing the mapping relationships in advance. Its learning rule is to use the gradient descent method to continuously adjust the weights and thresholds of the network through back propagation, so as to minimize the sum of squared errors of the network.
 Here, I use the knowledge of partial derivative to deduce the calculation formula. I didn't list the details. When I understood it, I read artificial intelligence (Mechanical Industry Press). If you are interested, you can have a look at it for yourself.
 The process of BP neural network adjusting weights is more like a process of function image approximation. First guess a function, then read in a data point, and adjust the function according to the gap between the data point and the function image, so that the function image is more consistent with the data. Continuous iteration makes the whole data sample more consistent with our estimated function.

4、 Steps:
1. Set the initial value of weight coefficient
2. Input a set of samples and its desired output
3. Calculate the actual output value
4. Calculate the error value
5. Back propagation error value and adjust weight
6. Keep training (repeat steps 2-5)
7. Use the neural network to predict

5、 Java code:
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Random;

public class FaceRecognition {

private int picSize = 19 * 19;//  Image size
private int hiddenSize = 12;//  Number of hidden layer nodes
private byte[] imageinfor = new byte[374];//  Store image information
private double[] input = new double[picSize + 1];//  Normalized image information
private double[][] inputWeight = new double[hiddenSize][picSize + 1];//  Input layer parameters
private double[] alpha1 = new double[hiddenSize];//  Gradient of hidden layer adjustment
private double[] hiddenWeight = new double[hiddenSize + 1];//  Hidden layer parameters
private double[] hiddenOutput = new double[hiddenSize + 1];//  Hidden layer output
private double alpha2;//  Gradient of output layer adjustment
private double output;//  Output layer
private double ci = 0.3;//  Learning rate
private double opt;//  Expected output
Random random = new Random();
private double [] pro;

public FaceRecognition() {
}

//Initialization
public void init() {
    for (int i = 0; i < hiddenSize; i++) {
        for (int j = 0; j < picSize + 1; j++)
            inputWeight[i][j] = random.nextDouble() * 2 - 1;
        // inputWeight[i][j] =0;
    }
    for (int i = 0; i < hiddenSize + 1; i++) {
        hiddenWeight[i] = random.nextDouble() * 2 - 1;
        // hiddenWeight[i]=0;
    }
}

// sigmoid
private double Sigmoid(double x) {
    return 1.0d / (1.0d + Math.exp(-x));
}

//Image file read in
public void PGMReader(String filename) {
    File file = new File(filename);
    try {
        RandomAccessFile in = new RandomAccessFile(file, "r");
        in.read(imageinfor);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    for (int i = 0; i < picSize; i++) {
        int temp = (int) imageinfor[i + 13];
        input[i] = (double) (temp + 128) / 255;
    }
    input[picSize] = 1.0;
}

public void PGMReader(File file) {
    try {
        RandomAccessFile in = new RandomAccessFile(file, "r");
        in.read(imageinfor);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    for (int i = 0; i < picSize; i++) {
        int temp = (int) imageinfor[i + 13];
        input[i] = (double) (temp + 128) / 255;
    }
    input[picSize] = 1.0;
}

public void setOpt(double opt) {
    this.opt = opt;
}

private void forward() {
    for (int i = 0; i < hiddenSize; i++) {
        double temp = 0;
        for (int j = 0; j < picSize + 1; j++) {
            temp += input[j] * inputWeight[i][j];
        }
        hiddenOutput[i] = Sigmoid(temp);
    }
    hiddenOutput[hiddenSize] = 1.0;

    double temp = 0;
    for (int i = 0; i < hiddenSize + 1; i++) {
        temp += hiddenOutput[i] * hiddenWeight[i];
    }
    output = Sigmoid(temp);
}

public void BP() {
    //Calculate the gradient of each layer
    alpha2 = (opt - output) * output * (1 - output);

    for (int i = 0; i < hiddenSize; i++) {
        alpha1[i] = hiddenOutput[i] * (1 - hiddenOutput[i]) * alpha2 * hiddenWeight[i];
    }

    //Back propagation
    for (int i = 0; i < hiddenSize; i++) {    
        hiddenWeight[i] += ((hiddenOutput[i] * alpha2 * ci) );
        for (int j = 0; j < picSize + 1; j++) {
            inputWeight[i][j] +=((input[j] * alpha1[i] * ci));
        }
    }
    hiddenWeight[hiddenSize]+=(hiddenOutput[hiddenSize] * alpha2 * ci);
}

public void train() {
    String non_ Facepath = "D: // AI // face. Train // train // non face";
    File non_facFile = new File(non_facePath);
    File[] non_faceList = non_facFile.listFiles();
    String facepath = "D: // AI // face. Train // train / face";
    File faceFile = new File(facePath);
    File[] faceList = faceFile.listFiles();
    init();
    pro =new double [151];
    
    
    for(int i =0;i<151;i++){
        int right = 0;
        int facenumber =0;
        int nonfacenumber =0;
        
        for (int j = 0; j < 4000; j++) {
            int temp = random.nextInt();
            if(temp%2 ==0)
            Case study training
                this.setOpt(1.0);
                this.PGMReader(faceList[facenumber]);
                this.forward();
                this.BP();
                facenumber++;
            }
            Else {// counterexample training

                this.setOpt(0.0);
                this.PGMReader(non_faceList[nonfacenumber]);
                this.forward();
                this.BP();
                nonfacenumber++;
            }
        }

        for (int j = 2000; j <2400; j++) {
            {// positive example test
                this.PGMReader(faceList[j]);
                this.forward();
                if (output > 0.5)
                    right++;
            }
            {// counterexample test
                this.PGMReader(non_faceList[j]);
                this.forward();
                if (output < 0.5)
                    right++;
            }
        }
        pro[i] = (double) right / 800;
        if(i%10==0)
        {
            System. Out. Println ("the accuracy rate of the" + I + "iteration is" + Pro [i]);
        }
        
        if(pro[i]>=0.95){
            System. Out. Println ("the accuracy rate of the" + I + "iteration is" + Pro [i]);
            break;
        }
            
    }
}

}
The data format I use here is PGM, 19 * 19 in size. There are 4800 training sets, half positive examples and half negative examples.
Specific implementation, I feel the code is written more clearly, I will not repeat.