Neural network code description of universal image recognition

xiaoxiao2021-03-05  31

When writing a face test program, the network format is defined by reading a file, the format of the file is as follows:

Input image long input image wide hidden layer neuron number output neuron number of different network structures [Number of hidden neurons connected to hidden neurons] [hidden neuron connection input Neuron position table]

Below is an example:

24 28 52 1316 321 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 41 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 41 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 41 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 41 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 41 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 41 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 45 5 5 5 5 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 85 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 885 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 85 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 855 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 85 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 85 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 89 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 11 12 12 12 12 12 12 12 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 11 11 12 12 12 12 12 129 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 1212 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 129 9 9 9 9 9 10 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12 1213 13 13 13 13 13 13 13 13 13 14 14 14 14 16 16 16 16 16 16 13 13 13 13 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 16 16 16 13 13 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 16 16 13 13 13 14 14 14 14 14 14 14 14 14 16 16 16 16 16 16 16 16 16 13 13 13 13 13 14 14 14 14 14 14 14 15 13 13 13 13 16 16 16 16 13 13 13 13 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 16 16 16 16 16 16 16 16 16 16 16 16 16 16 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 1 2 2

2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 21 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 23 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 23 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

3 3 3 3 3 3 34 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 45 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 56 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 below is the program code:

Type

TsingleextendedArray = arrayial = arrayextendedArray = array of array of;

Tsamples = Packed Record INS: TsingleExtendedArray; Outs: TsingleExtendedArray;

Type

TGraphicBpnn = class private procedure BackPropagate (t: TSingleExtendedArray; n, m: extended); function UpDate (inputs: TSingleExtendedArray): extended; public samplecounts, TestCounts: longint; procedure AddToTrain (Ins, Outs: TSingleExtendedArray); procedure AddToTest (Ins, outs: TSingleExtendedArray); procedure SaveToFile (FileName: string); procedure LoadFromFile (FileName: string); procedure Train (n, m: extended); function Init (FileName: string): boolean; function Predict (Ins: TSingleExtendedArray): extended ; function Test: extended; destructor Destroy; override; private nI, nH, nO: longint; aI, aH, aO, Output_Deltas, Hidden_Deltas: TSingleExtendedArray; wI, wO, cI, cO: TDoubleExtendedArray; Connections: array of array of boolean; Samples: Array of Tsamples; TestSet: Array of Tsamples; End; Implementation

Function TgraphicbPnn.init (FileName: String): Boolean; Var i, J, K, Fi, FJ: Longint; Niw, NiH, Ropmax, RopNum, RopTypes: longint; RopMap: Array OfInt; Begin AssignFile (Input, FileName) Reset (INPUT); Readln (Input, NiW, NiH, NH, NO); Ni: = Niw * NiH; setlength (Ai, Ni); setlength (AH, NH); setlength (AO, NO); for i: = 0 to Ni - 1 DO Ai [I]: = 1; for i: = 0 to NH - 1 DO AH [I]: = 1; for i: = 0 to no - 1 do ao [i]: = 1 ;

SETLENGTH (Wi, Ni, NH); setlength (WO, NH, NO); setlength (CI, Ni, NH); SetLength (Co, NH, NO); SetLength (Connections, Ni, NH);

For i: = 0 to ni - 1 do for j: = 0 to nh - 1 do connections [i, j]: = false;

Readln (RopTypes); FJ: = 0; fork: = 1 to RopTypes Do Begin Readln (RopMax, RopNum); SETLENGTH (ROPMAP, NI); FI: = 0; for i: = 1 To NiH Do Begin for J: = 1 to Niw Do Begin Read (RopMap [Fi]); INC (FI); END; READLN; End; Fi: = 0; for i: = 1 To RopNum Do Begin Inc (Fi); if Fi> RopMax THEN FI : = 1; for j: = 0 to ni - 1 do if ropmap [j] = fi THEN CONNECTIONS [J, FJ]: = True; Inc (fj); end; end; setlength (Output_deltas, no); setlength Hidden_Deltas, NH);

Randomize; for i: = 0 to ni - 1 do for j: = 0 to NH - 1 do begin Ci [i, j]: = 0; Wi [i, j]: = random (40000) / 10000 - 2; END;

For i: = 0 to NH - 1 do for j: = 0 to no - 1 do begin co [i, j]: = 0; WO [i, j]: = random (40000) / 10000 - 2;

SETLENGTH (Samples, $ 100); SETLENGTH (TestSet, $ 100); SampleCounts: = 0; testcounts: = 0; Closefile (INPUT);

Procedure TgraphicbPnn.BackPropagate (TSINGLEXTENDEDARRAY; N, M: EXTENDED); VAR i, J, K: Longint; Sum, Change: Extended; Begin for i: = 0 to no - 1 do output_deltas [i]: = AO [ I] * (1 - AO [I]) * (T [I] - AO [I]);

For J: = 0 to NH - 1 Do Begin SUM: = 0; for K: = 0 to no - 1 do sum: = SUM OUTPUT_DELTAS [K] * WO [J, K]; Hidden_Deltas [J]: = AH [J] * (1 - AH [J]) * SUM; END;

For J: = 0 to NH - 1 do fork: = 0 to no - 1 do beg change: = output_deltas [k] * ah [j]; wo [j, k]: = WO [J, K] N * Change M * Co [J, K]; Co [J, K]: = Change; End;

For i: = 0 to ni - 1 do for j: = 0 to NH - 1 DO if connections [i, j] the beginning: = hidden_deltas [j] * ai [i]; wi [i, j]: = Wi [I, J] N * Change M * Ci [I, J]; CI [I, J]: = Change; End;

Function TgraphicbPnn.Update (Inputs: TsingleExtendedArray): Extended; Var i, J, K: longint; sum: extended; begin for i: = 0 to ni - 1 do ai [i]: = INPUTS [i]; for j: = 0 TO NH - 1 Do Begin SUM: = 0; for i: = 0 to Ni - 1 DO if Connections [i, j] Then Sum: = SUM AI [I] * Wi [i, j]; ah [ j]: = 1 / (1 exp (-ssum)); end; fork: = 0 to no - 1 do begin sum: = 0; for j: = 0 to NH - 1 do sum: = sum ah [J] * WO [J, K]; AO [K]: = 1 / (1 Exp (-SUM)); end; update: = AO [0];

Procedure tgraphicbpnn.train (n, m: extended); var i: longint; begin for i: = 0 to samplecounts - 1 do begin update (Samples [i] .ins); BackPropagate (Samples [i] .outs, n, M); END;

procedure TGraphicBpnn.AddToTrain (Ins, Outs: TSingleExtendedArray); var i: longint; begin if samplecounts> High (Samples) then setlength (Samples, samplecounts $ 100); setlength (Samples [samplecounts] .Ins, nI); setlength (Samples [SampleCounts] .outs, no); for i: = 0 to ni - 1 do samples [samplecounts] .ins [i]: = INS [i]; for i: = 0 to no - 1 do samples [samplecounts]. OUTS [I]: = OUTS [I]; INC (SampleCounts);

procedure TGraphicBpnn.AddToTest (Ins, Outs: TSingleExtendedArray); var i: longint; begin if TestCounts> High (TestSet) then setlength (TestSet, TestCounts $ 100); setlength (TestSet [TestCounts] .Ins, nI); setlength (TestSet [TestCounts] .outs, no); for i: = 0 to ni - 1 do testset [testcounts] .ins [i]: = INS [i]; for i: = 0 to no - 1 do testset [testcounts]. outs [i]: = outs [i]; Inc (TestCounts); end; procedure TGraphicBpnn.SaveToFile (FileName: string); var i, j, k: longint; SaveStream: TMemoryStream; begin SaveStream: = TMemoryStream.Create; SaveStream .Sek (0, 0); for i: = 0 to ni - 1 do for j: = 0 to NH - 1 do begin savestream.write (wi [i, j], sizeof (Wi [i, j])) Savestream.write (CI [i, j]); end; for j: = 0 to NH - 1 do for K: = 0 to no - 1 do beg 2011tream.write WO [J, K], SIZEOF (WO [J, K])); Savestream.write (Co [J, K], SizeOf (Co [J, K])); End; SaveStream.savetofile (FileName); SaveStream End;

procedure TGraphicBpnn.LoadFromFile (FileName: string); var i, j, k: longint; ReadStream: TMemoryStream; begin ReadStream: = TMemoryStream.Create; ReadStream.LoadFromFile (FileName); ReadStream.Seek (0, 0); for i: = 0 to ni - 1 do for j: = 0 to NH - 1 Do Begin ReadStream.read (Wi [i, j], sizeof (Wi [i, j])); ReadStream.read (Ci [i, j] , SIZEOF (CI [I, J])); End; for J: = 0 to NH - 1 Do for K: = 0 to no - 1 Do Begin ReadStream.read (Wo [J, K], Sizeof (WO [ J, k])); ReadStream.read (Co [J, K], SizeOf (Co [J, K])); End; ReadStream.Free;

Function TgraphicbPnn.Predict (INS: TSINGEEXTENDARRAY): Extended; Begin Try Predict: = Update (ins); Except Predict: = 0; end;

Function TgraphicbPnn.Test: Extended; Var preret: Extended; I, Count; Ret: longint; begin counts: = 0; for i: = 0 to testcounts - 1 do beg preret: = predict (TestSet [i] .ins); IF preret> 0.5 Then Ret: = 1 else ret: = 0; if Ret = TestSet [i] .outs [0] THEN INC (Count); end; result: = counts / testcounts; end; destructor tgraphicbpnn.deStroy; begin SETLENGTH (Ai, 0); setlength (AH, 0); setlength (AO, 0); setLength (Output_deltas, 0); setlength (hidden_deltas, 0); setlength (wi, 0, 0); setlength (Wo, 0, 0); setlength (CI, 0, 0); setlength (CO, 0, 0); setlength (connections, 0, 0); setLength (Samples, 0); inherited;

转载请注明原文地址:https://www.9cbs.com/read-33416.html

New Post(0)