diff --git a/examples/DelphiTemplate/NeuralInDelphi.dpr b/examples/DelphiTemplate/NeuralInDelphi.dpr new file mode 100644 index 00000000..88aff7cf --- /dev/null +++ b/examples/DelphiTemplate/NeuralInDelphi.dpr @@ -0,0 +1,14 @@ +program NeuralInDelphi; + +uses + System.StartUpCopy, + FMX.Forms, + Unit1 in 'Unit1.pas' {Form1}; + +{$R *.res} + +begin + Application.Initialize; + Application.CreateForm(TForm1, Form1); + Application.Run; +end. diff --git a/examples/DelphiTemplate/NeuralInDelphi.dproj b/examples/DelphiTemplate/NeuralInDelphi.dproj new file mode 100644 index 00000000..1b6d12d4 --- /dev/null +++ b/examples/DelphiTemplate/NeuralInDelphi.dproj @@ -0,0 +1,1028 @@ + + + {E04D6E84-9A86-4E40-BD1F-653A8FF5A9E0} + 19.5 + FMX + NeuralInDelphi.dpr + True + Debug + Win32 + 3 + Application + + + true + + + true + Base + true + + + true + Base + true + + + true + Base + true + + + true + Cfg_1 + true + true + + + true + Cfg_1 + true + true + + + true + Base + true + + + true + Cfg_2 + true + true + + + true + Cfg_2 + true + true + + + .\$(Platform)\$(Config) + .\$(Platform)\$(Config) + false + false + false + false + false + System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace) + true + true + true + true + true + true + true + true + $(BDS)\bin\delphi_PROJECTICON.ico + $(BDS)\bin\delphi_PROJECTICNS.icns + NeuralInDelphi + + + DBXSqliteDriver;IndyIPCommon;RESTComponents;bindcompdbx;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;svnui;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;TeeDB;FireDAC;vcltouch;vcldb;bindcompfmx;svn;FireDACSqliteDriver;FireDACPgDriver;inetdb;FMXTee;soaprtl;DbxCommonDriver;FmxTeeUI;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;Tee;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;TeeUI;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) + Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) + Debug + true + CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= + 1033 + $(BDS)\bin\default_app.manifest + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png + + + DBXSqliteDriver;IndyIPCommon;RESTComponents;bindcompdbx;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;TeeDB;FireDAC;vcltouch;vcldb;bindcompfmx;FireDACSqliteDriver;FireDACPgDriver;inetdb;FMXTee;soaprtl;DbxCommonDriver;FmxTeeUI;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;Tee;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;TeeUI;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) + Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace) + Debug + true + CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= + 1033 + $(BDS)\bin\default_app.manifest + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png + + + DEBUG;$(DCC_Define) + true + false + true + true + true + + + false + PerMonitorV2 + true + 1033 + ..\..\neural\;$(DCC_UnitSearchPath) + C:\app\cai\lazarus\bin\x86_64-win64\bin\ + true + + + true + PerMonitorV2 + + + false + RELEASE;$(DCC_Define) + 0 + 0 + + + true + PerMonitorV2 + + + true + PerMonitorV2 + + + + MainSource + + +
Form1
+ fmx +
+ + Base + + + Cfg_1 + Base + + + Cfg_2 + Base + +
+ + Delphi.Personality.12 + Application + + + + Microsoft Office 2000 Sample Automation Server Wrapper Components + Microsoft Office XP Sample Automation Server Wrapper Components + + + NeuralInDelphi.dpr + + + + + + true + + + + + true + + + + + true + + + + + + NeuralInDelphi.exe + true + + + + + 1 + + + Contents\MacOS + 1 + + + 0 + + + + + classes + 64 + + + classes + 64 + + + + + res\xml + 1 + + + res\xml + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + library\lib\armeabi + 1 + + + library\lib\armeabi + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + library\lib\mips + 1 + + + library\lib\mips + 1 + + + + + library\lib\armeabi-v7a + 1 + + + library\lib\arm64-v8a + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + res\values-v21 + 1 + + + res\values-v21 + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable-xxhdpi + 1 + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-xxxhdpi + 1 + + + res\drawable-xxxhdpi + 1 + + + + + res\drawable-ldpi + 1 + + + res\drawable-ldpi + 1 + + + + + res\drawable-mdpi + 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + res\drawable-xhdpi + 1 + + + + + res\drawable-mdpi + 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + res\drawable-xhdpi + 1 + + + + + res\drawable-xxhdpi + 1 + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-xxxhdpi + 1 + + + res\drawable-xxxhdpi + 1 + + + + + res\drawable-small + 1 + + + res\drawable-small + 1 + + + + + res\drawable-normal + 1 + + + res\drawable-normal + 1 + + + + + res\drawable-large + 1 + + + res\drawable-large + 1 + + + + + res\drawable-xlarge + 1 + + + res\drawable-xlarge + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + 1 + + + Contents\MacOS + 1 + + + 0 + + + + + Contents\MacOS + 1 + .framework + + + Contents\MacOS + 1 + .framework + + + Contents\MacOS + 1 + .framework + + + 0 + + + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + 0 + .dll;.bpl + + + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + 0 + .bpl + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + Contents\Resources\StartUp\ + 0 + + + Contents\Resources\StartUp\ + 0 + + + Contents\Resources\StartUp\ + 0 + + + 0 + + + + + 1 + + + 1 + + + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + + + ..\ + 1 + + + ..\ + 1 + + + ..\ + 1 + + + + + Contents + 1 + + + Contents + 1 + + + Contents + 1 + + + + + Contents\Resources + 1 + + + Contents\Resources + 1 + + + Contents\Resources + 1 + + + + + library\lib\armeabi-v7a + 1 + + + library\lib\arm64-v8a + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + Contents\MacOS + 1 + + + Contents\MacOS + 1 + + + Contents\MacOS + 1 + + + 0 + + + + + library\lib\armeabi-v7a + 1 + + + + + 1 + + + 1 + + + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + + + ..\ + 1 + + + ..\ + 1 + + + ..\ + 1 + + + + + 1 + + + 1 + + + 1 + + + + + ..\$(PROJECTNAME).launchscreen + 64 + + + ..\$(PROJECTNAME).launchscreen + 64 + + + + + 1 + + + 1 + + + 1 + + + + + Assets + 1 + + + Assets + 1 + + + + + Assets + 1 + + + Assets + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset + 1 + + + + + + + + + + + + + + + + + True + True + + + 12 + + + + +
diff --git a/examples/DelphiTemplate/NeuralInDelphi.res b/examples/DelphiTemplate/NeuralInDelphi.res new file mode 100644 index 00000000..efd10026 Binary files /dev/null and b/examples/DelphiTemplate/NeuralInDelphi.res differ diff --git a/examples/DelphiTemplate/Unit1.fmx b/examples/DelphiTemplate/Unit1.fmx new file mode 100644 index 00000000..d6cc8aec --- /dev/null +++ b/examples/DelphiTemplate/Unit1.fmx @@ -0,0 +1,31 @@ +object Form1: TForm1 + Left = 0 + Top = 0 + Caption = 'Form1' + ClientHeight = 76 + ClientWidth = 160 + FormFactor.Width = 320 + FormFactor.Height = 480 + FormFactor.Devices = [Desktop] + DesignerMasterStyle = 0 + object Button1: TButton + Position.X = 8.000000000000000000 + Position.Y = 40.000000000000000000 + Size.Width = 137.000000000000000000 + Size.Height = 22.000000000000000000 + Size.PlatformDefault = False + TabOrder = 0 + Text = 'Image Classifier' + OnClick = Button1Click + end + object Button2: TButton + Position.X = 8.000000000000000000 + Position.Y = 10.000000000000000000 + Size.Width = 137.000000000000000000 + Size.Height = 22.000000000000000000 + Size.PlatformDefault = False + TabOrder = 1 + Text = 'Simple Learning' + OnClick = Button2Click + end +end diff --git a/examples/DelphiTemplate/Unit1.pas b/examples/DelphiTemplate/Unit1.pas new file mode 100644 index 00000000..daa61d5f --- /dev/null +++ b/examples/DelphiTemplate/Unit1.pas @@ -0,0 +1,183 @@ +unit Unit1; + +interface + +uses + System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, + FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, + FMX.Controls.Presentation, FMX.StdCtrls, + // Neural specifc files. + neuralnetwork, neuralvolume, neuraldatasets, neuralfit, neuralthread; + +// In Delphi, in project options: +// * At compiler, search path (-U), you'll add the "neural" folder: ..\..\neural\ +// * Still at the compiler, set the final output directory (-E) to: ..\..\bin\x86_64-win64\bin\ +// * In "generate console application", set it to true. + +// In your "uses" section, include: +// neuralnetwork, neuralvolume, neuraldatasets, neuralfit, neuralthread; + +type + TForm1 = class(TForm) + Button1: TButton; + Button2: TButton; + procedure Button1Click(Sender: TObject); + procedure Button2Click(Sender: TObject); + private + { Private declarations } + public + { Public declarations } + end; + +var + Form1: TForm1; + +implementation + + +type + // Define the input and output types for training data + TBackInput = array[0..3] of array[0..1] of TNeuralFloat; // Input data for OR operation + TBackOutput = array[0..3] of array[0..0] of TNeuralFloat; // Expected output for OR operation + +const + cs_false = 0.1; // Encoding for "false" value + cs_true = 0.8; // Encoding for "true" value + cs_threshold = (cs_false + cs_true) / 2; // Threshold for neuron activation + +const + cs_inputs : TBackInput = + ( + // Input data for OR operation + (cs_false, cs_false), + (cs_false, cs_true), + (cs_true, cs_false), + (cs_true, cs_true) + ); + +const + cs_outputs : TBackOutput = + ( + // Expected outputs for OR operation + (cs_false), + (cs_true), + (cs_true), + (cs_true) + ); + + procedure RunSimpleLearning(); + var + NN: TNNet; + EpochCnt: integer; + Cnt: integer; + pOutPut: TNNetVolume; + vInputs: TBackInput; + vOutput: TBackOutput; + begin + NN := TNNet.Create(); + + // Create the neural network layers + NN.AddLayer(TNNetInput.Create(2)); // Input layer with 2 neurons + NN.AddLayer(TNNetFullConnectLinear.Create(1)); // Single neuron layer connected to both inputs from the previous layer. + + NN.SetLearningRate(0.01, 0.9); // Set the learning rate and momentum + + vInputs := cs_inputs; // Assign the input data + vOutput := cs_outputs; // Assign the expected output data + pOutPut := TNNetVolume.Create(1, 1, 1, 1); // Create a volume to hold the output + + WriteLn('Value encoding FALSE is: ', cs_false:4:2); // Display the encoding for "false" + WriteLn('Value encoding TRUE is: ', cs_true:4:2); // Display the encoding for "true" + WriteLn('Threshold is: ', cs_threshold:4:2); // Display the threshold value + WriteLn; + + for EpochCnt := 1 to 1200 do + begin + for Cnt := Low(cs_inputs) to High(cs_inputs) do + begin + // Feed forward and backpropagation + NN.Compute(vInputs[Cnt]); // Perform feedforward computation + NN.GetOutput(pOutPut); // Get the output of the network + NN.Backpropagate(vOutput[Cnt]); // Perform backpropagation to adjust weights + + if EpochCnt mod 100 = 0 then + WriteLn( + EpochCnt:7, 'x', Cnt, + ' Output:', pOutPut.Raw[0]:5:2,' ', + ' - Training/Desired Output:', vOutput[cnt][0]:5:2,' ' + ); + end; + + if EpochCnt mod 100 = 0 then + begin + WriteLn(''); + end; + + end; + + NN.DebugWeights(); // Display the final weights of the network + + pOutPut.Free; // Free the memory allocated for output + NN.Free; // Free the memory allocated for the network + + end; + + procedure RunNeuralNetwork; + var + NN: TNNet; + NeuralFit: TNeuralImageFit; + ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList; + begin + if not CheckCIFARFile() then + begin + exit; + end; + WriteLn('Creating Neural Network...'); + NN := TNNet.Create(); + NN.AddLayer([ + TNNetInput.Create(32, 32, 3), + TNNetConvolutionLinear.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1, {SuppressBias=}1), + TNNetMaxPool.Create(4), + TNNetMovingStdNormalization.Create(), + TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1), + TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1), + TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1), + TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1, {SuppressBias=}1), + TNNetDropout.Create(0.5), + TNNetMaxPool.Create(2), + TNNetFullConnectLinear.Create(10), + TNNetSoftMax.Create() + ]); + NN.DebugStructure(); + CreateCifar10Volumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes); + + NeuralFit := TNeuralImageFit.Create; + NeuralFit.FileNameBase := 'SimpleImageClassifier-'+IntToStr(GetProcessId()); + NeuralFit.InitialLearningRate := 0.001; + NeuralFit.LearningRateDecay := 0.01; + NeuralFit.StaircaseEpochs := 10; + NeuralFit.Inertia := 0.9; + NeuralFit.L2Decay := 0; + NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, {NumClasses=}10, {batchsize=}64, {epochs=}50); + NeuralFit.Free; + + NN.Free; + ImgTestVolumes.Free; + ImgValidationVolumes.Free; + ImgTrainingVolumes.Free; + end; + + +{$R *.fmx} + +procedure TForm1.Button1Click(Sender: TObject); +begin + RunNeuralNetwork; +end; + +procedure TForm1.Button2Click(Sender: TObject); +begin + RunSimpleLearning; +end; + +end. diff --git a/examples/DelphiTemplate/readme.md b/examples/DelphiTemplate/readme.md new file mode 100644 index 00000000..70b25b4a --- /dev/null +++ b/examples/DelphiTemplate/readme.md @@ -0,0 +1,8 @@ +In Delphi, in project options: +* At compiler, search path (-U), you'll add the "neural" folder: ..\..\neural\ +* Still at the compiler, set the final output directory (-E) to: ..\..\bin\x86_64-win64\bin\ +* In "generate console application", set it to true. + +In your "uses" section, include: + neuralnetwork, neuralvolume, neuraldatasets, neuralfit, neuralthread; +