-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathSuperResolutionTrain.lpr
More file actions
128 lines (113 loc) · 4.13 KB
/
Copy pathSuperResolutionTrain.lpr
File metadata and controls
128 lines (113 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
///This example trains a super resolution neural network with
//the TinyImageNet 200 dataset: https://tiny-imagenet.herokuapp.com/ .
program SuperResolutionTrain;
(*
Coded by Joao Paulo Schwarz Schuler.
https://github.com/joaopauloschuler/neural-api
*)
{$mode objfpc}{$H+}
uses {$IFDEF UNIX} cthreads, {$ENDIF}
Classes, SysUtils, CustApp, neuralnetwork, neuralvolume, Math, neuraldatasets,
neuralfit, usuperresolutionexample;
type
{ TTestCNNAlgo }
TTestCNNAlgo = class(TCustomApplication)
protected
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList;
ImgTrainingSmall, ImgValidationSmall, ImgTestSmall: TNNetVolumeList;
procedure DoRun; override;
public
procedure GetTrainingPair(Idx: integer; ThreadId: integer; pInput, pOutput: TNNetVolume);
procedure GetValidationPair(Idx: integer; ThreadId: integer; pInput, pOutput: TNNetVolume);
procedure GetTestPair(Idx: integer; ThreadId: integer; pInput, pOutput: TNNetVolume);
end;
procedure TTestCNNAlgo.DoRun;
var
NN: THistoricalNets;
NNMaxPool: TNNet;
NeuralFit: TNeuralDataLoadingFit;
begin
WriteLn('Creating Neural Network...');
NN := THistoricalNets.Create();
NN.AddSuperResolution({pSizeX=}16, {pSizeY=}16,
{BottleNeck=}csExampleBottleNeck, {pNeurons=}csExampleNeuronCount,
{pLayerCnt=}csExampleLayerCount, {IsSeparable=}csExampleIsSeparable);
LoadResizingWeights(NN, csExampleFileName);
NN.DebugStructure();
// Small Neural Network to resize 32x32 images into 16x16 images.
NNMaxPool := TNNet.Create();
NNMaxPool.AddLayer( TNNetInput.Create(32, 32, 3) );
NNMaxPool.AddLayer( TNNetMaxPool.Create(2) );
CreateCifar10Volumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes);
ImgTrainingSmall := TNNetVolumeList.Create();
ImgValidationSmall := TNNetVolumeList.Create();
ImgTestSmall := TNNetVolumeList.Create();
NNMaxPool.Compute(ImgTrainingVolumes, ImgTrainingSmall);
NNMaxPool.Compute(ImgValidationVolumes, ImgValidationSmall);
NNMaxPool.Compute(ImgTestVolumes, ImgTestSmall);
NeuralFit := TNeuralDataLoadingFit.Create;
NeuralFit.FileNameBase := csExampleBaseFileName;
NeuralFit.InitialLearningRate := 0.001/(32*32);
NeuralFit.LearningRateDecay := 0.01;
NeuralFit.StaircaseEpochs := 10;
NeuralFit.Inertia := 0.9;
NeuralFit.L2Decay := 0;
NeuralFit.Verbose := true;
NeuralFit.EnableBipolar99HitComparison();
NeuralFit.AvgWeightEpochCount := 1;
//NeuralFit.MaxThreadNum := 16;
NeuralFit.FitLoading(NN,
ImgTrainingVolumes.Count, ImgValidationVolumes.Count, ImgTestVolumes.Count,
{batchsize=}64, {epochs=}50,
@GetTrainingPair, @GetValidationPair, @GetTestPair);
NeuralFit.Free;
NN.Free;
NNMaxPool.Free;
ImgTestVolumes.Free;
ImgValidationVolumes.Free;
ImgTrainingVolumes.Free;
ImgTrainingSmall.Free;
ImgValidationSmall.Free;
ImgTestSmall.Free;
Terminate;
end;
procedure TTestCNNAlgo.GetTrainingPair(Idx: integer; ThreadId: integer;
pInput, pOutput: TNNetVolume);
var
LocalIdx: integer;
begin
LocalIdx := Random(ImgTrainingSmall.Count);
pInput.Copy(ImgTrainingSmall[LocalIdx]);
pOutput.Copy(ImgTrainingVolumes[LocalIdx]);
// insert data augmentation
if Random(1000)>500 then
begin
pInput.FlipX();
pOutput.FlipX();
end;
if Random(1000)>500 then
begin
pInput.FlipY();
pOutput.FlipY();
end;
end;
procedure TTestCNNAlgo.GetValidationPair(Idx: integer; ThreadId: integer;
pInput, pOutput: TNNetVolume);
begin
pInput.Copy(ImgValidationSmall[Idx]);
pOutput.Copy(ImgValidationVolumes[Idx]);
end;
procedure TTestCNNAlgo.GetTestPair(Idx: integer; ThreadId: integer; pInput,
pOutput: TNNetVolume);
begin
pInput.Copy(ImgTestSmall[Idx]);
pOutput.Copy(ImgTestVolumes[Idx]);
end;
var
Application: TTestCNNAlgo;
begin
Application := TTestCNNAlgo.Create(nil);
Application.Title:='CIFAR-10 Super Resolution Train';
Application.Run;
Application.Free;
end.