Skip to content

Commit 6fc01ab

Browse files
Adding grouped convolution example #52.
1 parent 3445196 commit 6fc01ab

File tree

2 files changed

+254
-0
lines changed

2 files changed

+254
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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="CIFAR-10 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\SimpleImageClassifierGroupedConv"/>
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\SimpleImageClassifierGroupedConv"/>
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="6">
93+
<Define0 Value="Release"/>
94+
<Define1 Value="Debug"/>
95+
<Define2 Value="CheckRange"/>
96+
<Define3 Value="AVX2"/>
97+
<Define4 Value="AVX"/>
98+
<Define5 Value="OpenCL"/>
99+
</OtherDefines>
100+
</Other>
101+
</CompilerOptions>
102+
</Item3>
103+
</BuildModes>
104+
<PublishOptions>
105+
<Version Value="2"/>
106+
</PublishOptions>
107+
<RunParams>
108+
<local>
109+
<CommandLineParams Value="-a 3"/>
110+
</local>
111+
<FormatVersion Value="2"/>
112+
<Modes Count="1">
113+
<Mode0 Name="default">
114+
<local>
115+
<CommandLineParams Value="-a 3"/>
116+
</local>
117+
</Mode0>
118+
</Modes>
119+
</RunParams>
120+
<RequiredPackages Count="1">
121+
<Item1>
122+
<PackageName Value="multithreadprocslaz"/>
123+
</Item1>
124+
</RequiredPackages>
125+
<Units Count="1">
126+
<Unit0>
127+
<Filename Value="SimpleImageClassifierGroupedConv.lpr"/>
128+
<IsPartOfProject Value="True"/>
129+
</Unit0>
130+
</Units>
131+
</ProjectOptions>
132+
<CompilerOptions>
133+
<Version Value="11"/>
134+
<PathDelim Value="\"/>
135+
<Target>
136+
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\SimpleImageClassifierGroupedConv"/>
137+
</Target>
138+
<SearchPaths>
139+
<IncludeFiles Value="..\..\neural;$(ProjOutDir)"/>
140+
<OtherUnitFiles Value="..\..\neural;$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS)"/>
141+
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/>
142+
</SearchPaths>
143+
<CodeGeneration>
144+
<Optimizations>
145+
<OptimizationLevel Value="3"/>
146+
</Optimizations>
147+
</CodeGeneration>
148+
<Linking>
149+
<Debugging>
150+
<GenerateDebugInfo Value="False"/>
151+
</Debugging>
152+
</Linking>
153+
<Other>
154+
<CustomOptions Value="-dAVX
155+
-dRelease"/>
156+
<OtherDefines Count="2">
157+
<Define0 Value="AVX"/>
158+
<Define1 Value="Release"/>
159+
</OtherDefines>
160+
</Other>
161+
</CompilerOptions>
162+
<Debugging>
163+
<Exceptions Count="3">
164+
<Item1>
165+
<Name Value="EAbort"/>
166+
</Item1>
167+
<Item2>
168+
<Name Value="ECodetoolError"/>
169+
</Item2>
170+
<Item3>
171+
<Name Value="EFOpenError"/>
172+
</Item3>
173+
</Exceptions>
174+
</Debugging>
175+
</CONFIG>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
program SimpleImageClassifierGroupedConv;
2+
(*
3+
Coded by Joao Paulo Schwarz Schuler.
4+
https://github.com/joaopauloschuler/neural-api
5+
*)
6+
{$mode objfpc}{$H+}
7+
8+
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
9+
cthreads, {$ENDIF} {$ENDIF}
10+
Classes, SysUtils, CustApp, neuralnetwork, neuralvolume,
11+
Math, neuraldatasets, neuralfit, neuralthread;
12+
13+
type
14+
TTestCNNAlgo = class(TCustomApplication)
15+
protected
16+
procedure DoRun; override;
17+
end;
18+
19+
procedure TTestCNNAlgo.DoRun;
20+
var
21+
NN: TNNet;
22+
NeuralFit: TNeuralImageFit;
23+
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList;
24+
begin
25+
if not CheckCIFARFile() then
26+
begin
27+
Terminate;
28+
exit;
29+
end;
30+
WriteLn('Creating Neural Network...');
31+
NN := TNNet.Create();
32+
NN.AddLayer([
33+
TNNetInput.Create(32, 32, 3),
34+
TNNetConvolutionLinear.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1, {SuppressBias=}1),
35+
TNNetMaxPool.Create(4)
36+
]);
37+
NN.AddGroupedConvolution(TNNetConvolutionReLU,
38+
{Groups=}8, {Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1);
39+
NN.AddGroupedConvolution(TNNetConvolutionReLU,
40+
{Groups=}4, {Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1);
41+
NN.AddGroupedConvolution(TNNetConvolutionReLU,
42+
{Groups=}8, {Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1);
43+
NN.AddGroupedConvolution(TNNetConvolutionReLU,
44+
{Groups=}4, {Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1);
45+
NN.AddLayer([
46+
TNNetMaxPool.Create(2),
47+
TNNetFullConnectLinear.Create(10),
48+
TNNetSoftMax.Create()
49+
]);
50+
NN.DebugStructure();
51+
52+
CreateCifar10Volumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes);
53+
54+
NeuralFit := TNeuralImageFit.Create;
55+
NeuralFit.FileNameBase := 'SimpleImageClassifier-'+IntToStr(GetProcessId());
56+
NeuralFit.InitialLearningRate := 0.001;
57+
NeuralFit.LearningRateDecay := 0.01;
58+
NeuralFit.StaircaseEpochs := 10;
59+
NeuralFit.Inertia := 0.9;
60+
NeuralFit.L2Decay := 0.00001;
61+
NeuralFit.MaxThreadNum := 1;
62+
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, {NumClasses=}10, {batchsize=}64, {epochs=}50);
63+
NeuralFit.Free;
64+
65+
NN.Free;
66+
ImgTestVolumes.Free;
67+
ImgValidationVolumes.Free;
68+
ImgTrainingVolumes.Free;
69+
Terminate;
70+
end;
71+
72+
var
73+
Application: TTestCNNAlgo;
74+
begin
75+
Application := TTestCNNAlgo.Create(nil);
76+
Application.Title:='CIFAR-10 Classification Example';
77+
Application.Run;
78+
Application.Free;
79+
end.

0 commit comments

Comments
 (0)