|
- function TArtNet.Run(CharImg : TGrayImg; var No : string) : Boolean;
- var
- S : Double;
- begin
- LoadInVects(CharImg);
- LoadWeights(CharImg);
- While Reset do
- begin
- ClearRVect;
- ClearPVect;
- RunCompLayer; //XVect => CVect
- if not RunRecoLayer then //Get BestNeuron
- begin
- Result := False; //分类超出最大识别单元数
- Exit;
- end;
- RVect2PVect(BestNeuron); [color=#008000]//Wt[BestNeuron,i] = >
-
- PVect[/color]
- RunCompLayer; //XVect * PVect => CVect
- S := Vigilence; //Sum(CVect) / Sum(XVect)
- if S < VigilThresh then
- begin
- Reset := True;
- RVect[BestNeuron] := 0;
- Disabled[BestNeuron] := True;
- end
- else begin
- Reset := False;
- Train;
- end;
- end;
- SaveWeights(CharImg);
- No := GetRecoChar;
- Result := True;
- end;
-
- procedure TArtNet.SaveWeights(CharImg : TGrayImg);
- var
- FileStream : TFileStream;
- WeightRecord : TWeightRecord;
- WeightRecordLength : Integer;
- i, k : Integer;
- TempM : Integer;
- begin
- WeightRecordLength := sizeof(TWeightRecord);
- //权库文件不存在
- if FileExists(FileName) then
- begin
- //打开权文件
- FileStream := TFileStream.Create(FileName, fmOpenReadWrite);
- //如果有新分配单元,则修改文件中的M
- if BestNeuron >= M then
- begin
- TempM := M + 1;
- FileStream.WriteBuffer(TempM, sizeof(TempM));
- //索引
- WeightRecord.RecordIndex := BestNeuron;
- //权值
- for i := 0 to N - 1 do
- begin
- WeightRecord.PWb[i] := Wb[i, BestNeuron];
- WeightRecord.PWt[i] := Wt[BestNeuron, i];
- end;
- //结果
- WeightRecord.CharResult := '?';
- //该次识别对应的字符图象
- WeightRecord.CharImgWidth := CharImg.Width;
- WeightRecord.CharImgHeight := CharImg.Height;
- for i := 0 to CharImg.Height - 1 do
- for k := 0 to CharImg.Width - 1 do
- WeightRecord.CharImg[i * CharImg.Width + k] :=
-
- CharImg.Img[i, k];
- //写入文件
- FileStream.Seek(BestNeuron * WeightRecordLength + sizeof(M),
-
- soFromBeginning);
- FileStream.WriteBuffer(WeightRecord, WeightRecordLength);
- end
- else begin
- //如果不是新分配的单元,则先填充WeightRecord结构
- FileStream.Seek(BestNeuron * WeightRecordLength +
-
- sizeof(M),0);
- FileStream.ReadBuffer(WeightRecord, WeightRecordLength);
- //修改WeightRecord结构的权值
- for i := 0 to N - 1 do
- begin
- WeightRecord.PWb[i] := Wb[i, BestNeuron]; //权值
- WeightRecord.PWt[i] := Wt[BestNeuron, i];
- end;
- //写入文件
- FileStream.Seek(BestNeuron * WeightRecordLength + sizeof(M),
-
- soFromBeginning);
- FileStream.WriteBuffer(WeightRecord, WeightRecordLength);
- end;
- FileStream.Free;
- end;
- end;
- procedure TArtNet.LoadWeights(CharImg : TGrayImg);
- var
- FileStream : TFileStream;
- WeightRecord : TWeightRecord;
- i, j, k : Integer;
- WeightRecordLength : LongInt;
- begin
- WeightRecordLength := sizeof(TWeightRecord);
- InitWeights;
- //权库文件不存在
- if not FileExists(FileName) then
- begin
- //创建权文件
- FileStream := TFileStream.Create(FileName, fmCreate);
- //先写入识别层单元数
- FileStream.WriteBuffer(M, sizeof(M));
- //填充WeightRecord结构
- for j := 0 to M - 1 do
- begin
- WeightRecord.RecordIndex := j; //索引
- for i := 0 to N - 1 do
- begin
- WeightRecord.PWb[i] := Wb[i, j]; //权值
- WeightRecord.PWt[i] := Wt[j, i];
- end;
- WeightRecord.CharResult := '?'; //结果
- WeightRecord.CharImgWidth := CharImg.Width;
- WeightRecord.CharImgHeight := CharImg.Height;
- for i := 0 to CharImg.Height - 1 do
- for k := 0 to CharImg.Width - 1 do
- WeightRecord.CharImg[i * CharImg.Width + k] :=
-
- CharImg.Img[i, k];
- FileStream.WriteBuffer(WeightRecord, WeightRecordLength);
- end;
- FileStream.Free;
- end
- else begin
- FileStream := TFileStream.Create(FileName, fmOpenRead);
- //跳过识别层单元数
- FileStream.Seek(sizeof(M), soFromBeginning);
- for j := 0 to M - 1 do
- begin
- FileStream.ReadBuffer(WeightRecord, WeightRecordLength);
- //从文件中读入权值
- for i := 0 to N - 1 do
- begin
- Wb[i, j] := WeightRecord.PWb[i];
- Wt[j, i] := WeightRecord.PWt[i];
- end;
- //读入对应识别字符的ASCII
- RecoCharASCII[j] := WeightRecord.CharResult;
- end;
- FileStream.Free;
- end;
- end;
复制代码 |
|