Skip to content

Commit 424c8e3

Browse files
Adding Simple Tiny ImageNet 200 Example
1 parent 6f8a197 commit 424c8e3

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectOptions>
4+
<Version Value="11"/>
5+
<PathDelim Value="\"/>
6+
<General>
7+
<Flags>
8+
<MainUnitHasCreateFormStatements Value="False"/>
9+
</Flags>
10+
<SessionStorage Value="InProjectDir"/>
11+
<MainUnit Value="0"/>
12+
<Title Value="Tiny ImageNet 200 Classification Example"/>
13+
<UseAppBundle Value="False"/>
14+
<ResourceType Value="res"/>
15+
</General>
16+
<i18n>
17+
<EnableI18N LFM="False"/>
18+
</i18n>
19+
<BuildModes Count="3">
20+
<Item1 Name="Default" Default="True"/>
21+
<Item2 Name="Debug">
22+
<CompilerOptions>
23+
<Version Value="11"/>
24+
<PathDelim Value="\"/>
25+
<Target>
26+
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\SimpleTinyImageNet"/>
27+
</Target>
28+
<SearchPaths>
29+
<IncludeFiles Value="..\..\neural;$(ProjOutDir)"/>
30+
<OtherUnitFiles Value="..\..\neural;$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS)"/>
31+
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/>
32+
</SearchPaths>
33+
<Parsing>
34+
<SyntaxOptions>
35+
<IncludeAssertionCode Value="True"/>
36+
</SyntaxOptions>
37+
</Parsing>
38+
<CodeGeneration>
39+
<Checks>
40+
<IOChecks Value="True"/>
41+
<RangeChecks Value="True"/>
42+
<OverflowChecks Value="True"/>
43+
<StackChecks Value="True"/>
44+
</Checks>
45+
<VerifyObjMethodCallValidity Value="True"/>
46+
</CodeGeneration>
47+
<Linking>
48+
<Debugging>
49+
<DebugInfoType Value="dsDwarf2Set"/>
50+
<UseValgrind Value="True"/>
51+
<UseExternalDbgSyms Value="True"/>
52+
</Debugging>
53+
</Linking>
54+
<Other>
55+
<CustomOptions Value="-dDebug
56+
-dAVX"/>
57+
<OtherDefines Count="3">
58+
<Define0 Value="Debug"/>
59+
<Define1 Value="AVX"/>
60+
<Define2 Value="MakeQuick"/>
61+
</OtherDefines>
62+
</Other>
63+
</CompilerOptions>
64+
</Item2>
65+
<Item3 Name="Release">
66+
<CompilerOptions>
67+
<Version Value="11"/>
68+
<PathDelim Value="\"/>
69+
<Target>
70+
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\SimpleTinyImageNet"/>
71+
</Target>
72+
<SearchPaths>
73+
<IncludeFiles Value="..\..\neural;$(ProjOutDir)"/>
74+
<OtherUnitFiles Value="..\..\neural;$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS)"/>
75+
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/>
76+
</SearchPaths>
77+
<CodeGeneration>
78+
<SmartLinkUnit Value="True"/>
79+
<Optimizations>
80+
<OptimizationLevel Value="3"/>
81+
</Optimizations>
82+
</CodeGeneration>
83+
<Linking>
84+
<Debugging>
85+
<GenerateDebugInfo Value="False"/>
86+
</Debugging>
87+
<LinkSmart Value="True"/>
88+
</Linking>
89+
<Other>
90+
<CustomOptions Value="-dRelease
91+
-dAVX"/>
92+
<OtherDefines Count="5">
93+
<Define0 Value="Release"/>
94+
<Define1 Value="Debug"/>
95+
<Define2 Value="CheckRange"/>
96+
<Define3 Value="AVX2"/>
97+
<Define4 Value="AVX"/>
98+
</OtherDefines>
99+
</Other>
100+
</CompilerOptions>
101+
</Item3>
102+
</BuildModes>
103+
<PublishOptions>
104+
<Version Value="2"/>
105+
</PublishOptions>
106+
<RunParams>
107+
<local>
108+
<CommandLineParams Value="-a 3"/>
109+
</local>
110+
<FormatVersion Value="2"/>
111+
<Modes Count="1">
112+
<Mode0 Name="default">
113+
<local>
114+
<CommandLineParams Value="-a 3"/>
115+
</local>
116+
</Mode0>
117+
</Modes>
118+
</RunParams>
119+
<RequiredPackages Count="2">
120+
<Item1>
121+
<PackageName Value="FCL"/>
122+
</Item1>
123+
<Item2>
124+
<PackageName Value="multithreadprocslaz"/>
125+
</Item2>
126+
</RequiredPackages>
127+
<Units Count="1">
128+
<Unit0>
129+
<Filename Value="SimpleTinyImageNet.pas"/>
130+
<IsPartOfProject Value="True"/>
131+
</Unit0>
132+
</Units>
133+
</ProjectOptions>
134+
<CompilerOptions>
135+
<Version Value="11"/>
136+
<PathDelim Value="\"/>
137+
<Target>
138+
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\SimpleTinyImageNet"/>
139+
</Target>
140+
<SearchPaths>
141+
<IncludeFiles Value="..\..\neural;$(ProjOutDir)"/>
142+
<OtherUnitFiles Value="..\..\neural;$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS)"/>
143+
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/>
144+
</SearchPaths>
145+
<CodeGeneration>
146+
<Optimizations>
147+
<OptimizationLevel Value="3"/>
148+
</Optimizations>
149+
</CodeGeneration>
150+
<Linking>
151+
<Debugging>
152+
<GenerateDebugInfo Value="False"/>
153+
</Debugging>
154+
</Linking>
155+
<Other>
156+
<CustomOptions Value="-dAVX
157+
-dRelease"/>
158+
<OtherDefines Count="2">
159+
<Define0 Value="AVX"/>
160+
<Define1 Value="Release"/>
161+
</OtherDefines>
162+
</Other>
163+
</CompilerOptions>
164+
<Debugging>
165+
<Exceptions Count="3">
166+
<Item1>
167+
<Name Value="EAbort"/>
168+
</Item1>
169+
<Item2>
170+
<Name Value="ECodetoolError"/>
171+
</Item2>
172+
<Item3>
173+
<Name Value="EFOpenError"/>
174+
</Item3>
175+
</Exceptions>
176+
</Debugging>
177+
</CONFIG>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
///This file has an implementation to classify
2+
//the TinyImageNet 200 dataset: https://tiny-imagenet.herokuapp.com/ .
3+
program SimpleTinyImageNet;
4+
(*
5+
Coded by Joao Paulo Schwarz Schuler.
6+
https://github.com/joaopauloschuler/neural-api
7+
*)
8+
{$mode objfpc}{$H+}
9+
10+
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
11+
cthreads, {$ENDIF} {$ENDIF}
12+
Classes, SysUtils, CustApp, neuralnetwork, neuralvolume, Math, neuraldatasets,
13+
neuralfit;
14+
15+
type
16+
TTestCNNAlgo = class(TCustomApplication)
17+
protected
18+
procedure DoRun; override;
19+
end;
20+
21+
procedure TTestCNNAlgo.DoRun;
22+
var
23+
NN: TNNet;
24+
NeuralFit: TNeuralImageFit;
25+
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList;
26+
ProportionToLoad: Single;
27+
begin
28+
WriteLn('Creating Neural Network...');
29+
NN := THistoricalNets.Create();
30+
NN.AddLayer([
31+
TNNetInput.Create(64, 64, 3),
32+
TNNetConvolutionLinear.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}2),
33+
TNNetMaxPool.Create(2),
34+
TNNetMovingStdNormalization.Create(),
35+
TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1),
36+
TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1),
37+
TNNetMaxPool.Create(2),
38+
TNNetConvolutionReLU.Create({Features=}128, {FeatureSize=}3, {Padding=}1, {Stride=}1),
39+
TNNetConvolutionReLU.Create({Features=}256, {FeatureSize=}3, {Padding=}1, {Stride=}1),
40+
TNNetConvolutionReLU.Create({Features=}512, {FeatureSize=}3, {Padding=}1, {Stride=}2),
41+
TNNetDropout.Create(0.5),
42+
TNNetMaxPool.Create(2),
43+
TNNetFullConnectLinear.Create(200),
44+
TNNetSoftMax.Create()
45+
]);
46+
NN.DebugStructure();
47+
// change ProportionToLoad to a smaller number if you don't have available 6GB of RAM.
48+
ProportionToLoad := 1;
49+
WriteLn('Loading ', Round(ProportionToLoad*100), '% of the Tiny ImageNet 200 dataset into memory.');
50+
CreateVolumesFromImagesFromFolder
51+
(
52+
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes,
53+
{FolderName=}'tiny-imagenet-200/train', {pImageSubFolder=}'images',
54+
{color_encoding=}0{RGB},
55+
{TrainingProp=}0.9*ProportionToLoad,
56+
{ValidationProp=}0.05*ProportionToLoad,
57+
{TestProp=}0.05*ProportionToLoad
58+
);
59+
60+
WriteLn
61+
(
62+
'Training Images:', ImgTrainingVolumes.Count,
63+
' Validation Images:', ImgValidationVolumes.Count,
64+
' Test Images:', ImgTestVolumes.Count
65+
);
66+
67+
NeuralFit := TNeuralImageFit.Create;
68+
NeuralFit.FileNameBase := 'SimpleTinyImageNet200';
69+
NeuralFit.InitialLearningRate := 0.001;
70+
NeuralFit.LearningRateDecay := 0.01;
71+
NeuralFit.StaircaseEpochs := 10;
72+
NeuralFit.Inertia := 0.9;
73+
NeuralFit.CyclicalLearningRateLen := 100;
74+
NeuralFit.L2Decay := 0.00001;
75+
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, {NumClasses=}200, {batchsize=}64, {epochs=}400);
76+
NeuralFit.Free;
77+
78+
NN.Free;
79+
ImgTestVolumes.Free;
80+
ImgValidationVolumes.Free;
81+
ImgTrainingVolumes.Free;
82+
Terminate;
83+
end;
84+
85+
var
86+
Application: TTestCNNAlgo;
87+
begin
88+
Application := TTestCNNAlgo.Create(nil);
89+
Application.Title:='Tiny ImageNet 200 Classification Example';
90+
Application.Run;
91+
Application.Free;
92+
end.

0 commit comments

Comments
 (0)