Skip to content

Commit

Permalink
Coding #71.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopauloschuler committed Oct 14, 2021
1 parent b68cb14 commit d2d073a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions neural/neuralfit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ TNeuralFitWithImageBase = class(TNeuralFitBase)
FHasFlipY: boolean;
FHasResizing: boolean;
FColorEncoding: integer;
FChannelShiftRate: TNeuralFloat;
public
constructor Create(); override;
destructor Destroy(); override;
procedure ClassifyImage(pNN: TNNet; pImgInput, pOutput: TNNetVolume);
procedure EnableDefaultImageTreatment(); virtual;

// ChannelShiftRate: 0 means no augmentation. 0.1 means 10% of maximum change per channel.
property ChannelShiftRate: TNeuralFloat read FChannelShiftRate write FChannelShiftRate;
property HasImgCrop: boolean read FHasImgCrop write FHasImgCrop;
property HasMakeGray: boolean read FHasMakeGray write FHasMakeGray;
property HasFlipX: boolean read FHasFlipX write FHasFlipX;
Expand Down Expand Up @@ -468,6 +471,7 @@ constructor TNeuralFitWithImageBase.Create();
FHasMakeGray := false;
FColorEncoding := 0;
FMultipleSamplesAtValidation := false;
FChannelShiftRate := 0;
end;

destructor TNeuralFitWithImageBase.Destroy();
Expand Down Expand Up @@ -892,6 +896,8 @@ procedure TNeuralDataLoadingFit.RunNNThread(index, threadnum: integer);
InitialWeightSum, FinalWeightSum: TNeuralFloat;
{$ENDIF}
CropSizeX, CropSizeY: integer;
DepthCnt: integer;
LocalChannelShiftRate: TNeuralFloat;
begin
vInput := TNNetVolume.Create();
pOutput := TNNetVolume.Create();
Expand Down Expand Up @@ -952,6 +958,16 @@ procedure TNeuralDataLoadingFit.RunNNThread(index, threadnum: integer);
vInput.CopyResizing(vInputCopy, vInput.SizeX, vInput.SizeY);
end;

if FChannelShiftRate > 0 then
begin
for DepthCnt := 0 to vInput.Depth - 1 do
begin
LocalChannelShiftRate := ((random(10000) - 5000) / 5000);
LocalChannelShiftRate := 1 + (LocalChannelShiftRate * LocalChannelShiftRate);
vInput.MulAtDepth(DepthCnt, LocalChannelShiftRate);
end;
end;

if (vInput.Depth = 3) then
begin
if FHasMakeGray and (Random(1000) > 750) then
Expand Down Expand Up @@ -1504,6 +1520,7 @@ constructor TNeuralImageFit.Create();
FColorEncoding := 0;
FMultipleSamplesAtValidation := true;
FTrainingSampleProcessedCnt := TNNetVolume.Create;
FChannelShiftRate := 0;
end;

destructor TNeuralImageFit.Destroy();
Expand Down Expand Up @@ -1905,6 +1922,8 @@ procedure TNeuralImageFit.RunNNThread(index, threadnum: integer);
OutputValue, CurrentLoss: TNeuralFloat;
LocalHit, LocalMiss: integer;
LocalTotalLoss, LocalErrorSum: TNeuralFloat;
DepthCnt: integer;
LocalChannelShiftRate: TNeuralFloat;
begin
ImgInput := TNNetVolume.Create();
ImgInputCp := TNNetVolume.Create();
Expand Down Expand Up @@ -1951,6 +1970,16 @@ procedure TNeuralImageFit.RunNNThread(index, threadnum: integer);
ImgInput.Copy(FImgVolumes[ImgIdx]);
end;

if FChannelShiftRate > 0 then
begin
for DepthCnt := 0 to ImgInput.Depth - 1 do
begin
LocalChannelShiftRate := ((random(10000) - 5000) / 5000);
LocalChannelShiftRate := 1 + (LocalChannelShiftRate * FChannelShiftRate);
ImgInput.MulAtDepth(DepthCnt, LocalChannelShiftRate);
end;
end;

if (ImgInput.Depth = 3) then
begin
if FHasMakeGray and (Random(1000) > 750) then
Expand Down

0 comments on commit d2d073a

Please sign in to comment.