Skip to content

Commit 8559435

Browse files
Adding a deplhi example for delphi users.
1 parent adb764e commit 8559435

File tree

6 files changed

+1264
-0
lines changed

6 files changed

+1264
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
program NeuralInDelphi;
2+
3+
uses
4+
System.StartUpCopy,
5+
FMX.Forms,
6+
Unit1 in 'Unit1.pas' {Form1};
7+
8+
{$R *.res}
9+
10+
begin
11+
Application.Initialize;
12+
Application.CreateForm(TForm1, Form1);
13+
Application.Run;
14+
end.

examples/DelphiTemplate/NeuralInDelphi.dproj

+1,028
Large diffs are not rendered by default.
110 KB
Binary file not shown.

examples/DelphiTemplate/Unit1.fmx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
object Form1: TForm1
2+
Left = 0
3+
Top = 0
4+
Caption = 'Form1'
5+
ClientHeight = 76
6+
ClientWidth = 160
7+
FormFactor.Width = 320
8+
FormFactor.Height = 480
9+
FormFactor.Devices = [Desktop]
10+
DesignerMasterStyle = 0
11+
object Button1: TButton
12+
Position.X = 8.000000000000000000
13+
Position.Y = 40.000000000000000000
14+
Size.Width = 137.000000000000000000
15+
Size.Height = 22.000000000000000000
16+
Size.PlatformDefault = False
17+
TabOrder = 0
18+
Text = 'Image Classifier'
19+
OnClick = Button1Click
20+
end
21+
object Button2: TButton
22+
Position.X = 8.000000000000000000
23+
Position.Y = 10.000000000000000000
24+
Size.Width = 137.000000000000000000
25+
Size.Height = 22.000000000000000000
26+
Size.PlatformDefault = False
27+
TabOrder = 1
28+
Text = 'Simple Learning'
29+
OnClick = Button2Click
30+
end
31+
end

examples/DelphiTemplate/Unit1.pas

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
unit Unit1;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
7+
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
8+
FMX.Controls.Presentation, FMX.StdCtrls,
9+
// Neural specifc files.
10+
neuralnetwork, neuralvolume, neuraldatasets, neuralfit, neuralthread;
11+
12+
// In Delphi, in project options:
13+
// * At compiler, search path (-U), you'll add the "neural" folder: ..\..\neural\
14+
// * Still at the compiler, set the final output directory (-E) to: ..\..\bin\x86_64-win64\bin\
15+
// * In "generate console application", set it to true.
16+
17+
// In your "uses" section, include:
18+
// neuralnetwork, neuralvolume, neuraldatasets, neuralfit, neuralthread;
19+
20+
type
21+
TForm1 = class(TForm)
22+
Button1: TButton;
23+
Button2: TButton;
24+
procedure Button1Click(Sender: TObject);
25+
procedure Button2Click(Sender: TObject);
26+
private
27+
{ Private declarations }
28+
public
29+
{ Public declarations }
30+
end;
31+
32+
var
33+
Form1: TForm1;
34+
35+
implementation
36+
37+
38+
type
39+
// Define the input and output types for training data
40+
TBackInput = array[0..3] of array[0..1] of TNeuralFloat; // Input data for OR operation
41+
TBackOutput = array[0..3] of array[0..0] of TNeuralFloat; // Expected output for OR operation
42+
43+
const
44+
cs_false = 0.1; // Encoding for "false" value
45+
cs_true = 0.8; // Encoding for "true" value
46+
cs_threshold = (cs_false + cs_true) / 2; // Threshold for neuron activation
47+
48+
const
49+
cs_inputs : TBackInput =
50+
(
51+
// Input data for OR operation
52+
(cs_false, cs_false),
53+
(cs_false, cs_true),
54+
(cs_true, cs_false),
55+
(cs_true, cs_true)
56+
);
57+
58+
const
59+
cs_outputs : TBackOutput =
60+
(
61+
// Expected outputs for OR operation
62+
(cs_false),
63+
(cs_true),
64+
(cs_true),
65+
(cs_true)
66+
);
67+
68+
procedure RunSimpleLearning();
69+
var
70+
NN: TNNet;
71+
EpochCnt: integer;
72+
Cnt: integer;
73+
pOutPut: TNNetVolume;
74+
vInputs: TBackInput;
75+
vOutput: TBackOutput;
76+
begin
77+
NN := TNNet.Create();
78+
79+
// Create the neural network layers
80+
NN.AddLayer(TNNetInput.Create(2)); // Input layer with 2 neurons
81+
NN.AddLayer(TNNetFullConnectLinear.Create(1)); // Single neuron layer connected to both inputs from the previous layer.
82+
83+
NN.SetLearningRate(0.01, 0.9); // Set the learning rate and momentum
84+
85+
vInputs := cs_inputs; // Assign the input data
86+
vOutput := cs_outputs; // Assign the expected output data
87+
pOutPut := TNNetVolume.Create(1, 1, 1, 1); // Create a volume to hold the output
88+
89+
WriteLn('Value encoding FALSE is: ', cs_false:4:2); // Display the encoding for "false"
90+
WriteLn('Value encoding TRUE is: ', cs_true:4:2); // Display the encoding for "true"
91+
WriteLn('Threshold is: ', cs_threshold:4:2); // Display the threshold value
92+
WriteLn;
93+
94+
for EpochCnt := 1 to 1200 do
95+
begin
96+
for Cnt := Low(cs_inputs) to High(cs_inputs) do
97+
begin
98+
// Feed forward and backpropagation
99+
NN.Compute(vInputs[Cnt]); // Perform feedforward computation
100+
NN.GetOutput(pOutPut); // Get the output of the network
101+
NN.Backpropagate(vOutput[Cnt]); // Perform backpropagation to adjust weights
102+
103+
if EpochCnt mod 100 = 0 then
104+
WriteLn(
105+
EpochCnt:7, 'x', Cnt,
106+
' Output:', pOutPut.Raw[0]:5:2,' ',
107+
' - Training/Desired Output:', vOutput[cnt][0]:5:2,' '
108+
);
109+
end;
110+
111+
if EpochCnt mod 100 = 0 then
112+
begin
113+
WriteLn('');
114+
end;
115+
116+
end;
117+
118+
NN.DebugWeights(); // Display the final weights of the network
119+
120+
pOutPut.Free; // Free the memory allocated for output
121+
NN.Free; // Free the memory allocated for the network
122+
123+
end;
124+
125+
procedure RunNeuralNetwork;
126+
var
127+
NN: TNNet;
128+
NeuralFit: TNeuralImageFit;
129+
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList;
130+
begin
131+
if not CheckCIFARFile() then
132+
begin
133+
exit;
134+
end;
135+
WriteLn('Creating Neural Network...');
136+
NN := TNNet.Create();
137+
NN.AddLayer([
138+
TNNetInput.Create(32, 32, 3),
139+
TNNetConvolutionLinear.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1, {SuppressBias=}1),
140+
TNNetMaxPool.Create(4),
141+
TNNetMovingStdNormalization.Create(),
142+
TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1),
143+
TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1),
144+
TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1),
145+
TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1),
146+
TNNetDropout.Create(0.5),
147+
TNNetMaxPool.Create(2),
148+
TNNetFullConnectLinear.Create(10),
149+
TNNetSoftMax.Create()
150+
]);
151+
NN.DebugStructure();
152+
CreateCifar10Volumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes);
153+
154+
NeuralFit := TNeuralImageFit.Create;
155+
NeuralFit.FileNameBase := 'SimpleImageClassifier-'+IntToStr(GetProcessId());
156+
NeuralFit.InitialLearningRate := 0.001;
157+
NeuralFit.LearningRateDecay := 0.01;
158+
NeuralFit.StaircaseEpochs := 10;
159+
NeuralFit.Inertia := 0.9;
160+
NeuralFit.L2Decay := 0;
161+
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, {NumClasses=}10, {batchsize=}64, {epochs=}50);
162+
NeuralFit.Free;
163+
164+
NN.Free;
165+
ImgTestVolumes.Free;
166+
ImgValidationVolumes.Free;
167+
ImgTrainingVolumes.Free;
168+
end;
169+
170+
171+
{$R *.fmx}
172+
173+
procedure TForm1.Button1Click(Sender: TObject);
174+
begin
175+
RunNeuralNetwork;
176+
end;
177+
178+
procedure TForm1.Button2Click(Sender: TObject);
179+
begin
180+
RunSimpleLearning;
181+
end;
182+
183+
end.

examples/DelphiTemplate/readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In Delphi, in project options:
2+
* At compiler, search path (-U), you'll add the "neural" folder: ..\..\neural\
3+
* Still at the compiler, set the final output directory (-E) to: ..\..\bin\x86_64-win64\bin\
4+
* In "generate console application", set it to true.
5+
6+
In your "uses" section, include:
7+
neuralnetwork, neuralvolume, neuraldatasets, neuralfit, neuralthread;
8+

0 commit comments

Comments
 (0)