Skip to content

Commit

Permalink
Updating web server example.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopauloschuler committed Dec 21, 2022
1 parent 1afaa01 commit de2fb07
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions examples/ResNet/server/ResNetServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
neuralnetwork, neuralvolume, neuraldatasets,
fphttpapp, httpdefs, httproute, fpjson;

var
NN: TNNet;
Labels: TStringList;
LabelFile: TextFile;
line: string;

procedure LoadBitmapIntoTinyImage(var Bitmap: TBitmap; out TI: TTinyImage);
var
I, J: integer;
Expand All @@ -32,6 +26,27 @@ procedure LoadBitmapIntoTinyImage(var Bitmap: TBitmap; out TI: TTinyImage);
end;
end;

procedure Home(aRequest: TRequest; aResponse: TResponse);
begin
aResponse.ContentType := 'text/html';
with aResponse.Contents do
begin
Add('<html><body>');
Add('<form action="/nn" method="post" enctype="multipart/form-data">');
Add('<input type="file" name="input" />');
Add('<br/><br/>');
Add('<input type="submit" value="Classify" />');
Add('</form>');
Add('</body></html>');
end;
aResponse.SendContent;
end;


var
NN: TNNet;
Labels: TStringList;

procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
var
jObject : TJSONObject;
Expand All @@ -55,6 +70,9 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
{PredImage.SaveToFile('/tmp/tmp.bmp');}
LoadBitmapIntoTinyImage(PredImage, TI);
LoadTinyImageIntoNNetVolume(TI, InputV);
// Bipolar representation.
InputV.Divi(64);
InputV.Sub(2);
finally
SrcImage.Free;
PredImage.Free;
Expand All @@ -63,6 +81,13 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
LocalNN.Compute(InputV);
LocalNN.GetOutput(OutputV);

// Increases accuracy with a flipped version.
InputV.FlipX();
LocalNN.Compute(InputV);
LocalNN.AddOutput(OutputV);

OutputV.Divi(2);

jObject := TJSONObject.Create;
try
for i := 0 to OutputV.SizeX - 1 do
Expand All @@ -80,22 +105,10 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
LocalNN.Free;
end;

procedure Home(aRequest: TRequest; aResponse: TResponse);
begin
aResponse.ContentType := 'text/html';
with aResponse.Contents do
begin
Add('<html><body>');
Add('<form action="/nn" method="post" enctype="multipart/form-data">');
Add('<input type="file" name="input" />');
Add('<br/>');
Add('<input type="submit" value="Classify" />');
Add('</form>');
Add('</body></html>');
end;
aResponse.SendContent;
end;

var
LabelFile: TextFile;
line: string;

begin
if paramCount <> 3 then
Expand Down Expand Up @@ -125,5 +138,8 @@ procedure Home(aRequest: TRequest; aResponse: TResponse);
Application.Initialize;
WriteLn('Listening in port ', paramStr(1));
Application.Run;
Labels.Free;
NN.Free;
WriteLn('Bye bye.');
end.

0 comments on commit de2fb07

Please sign in to comment.