Skip to content

Commit

Permalink
Updating web server.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopauloschuler committed Dec 21, 2022
1 parent de2fb07 commit 8b260eb
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions examples/ResNet/server/ResNetServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ procedure Home(aRequest: TRequest; aResponse: TResponse);
Add('<form action="/nn" method="post" enctype="multipart/form-data">');
Add('<input type="file" name="input" />');
Add('<br/><br/>');
Add('<fieldset style="border: 0">'+
'<legend>Select the output type:</legend>'+
'<div>'+
' <input type="radio" id="text" name="otype" value="text" checked>'+
' <label for="text">text</label>'+
'</div>'+
'<div>'+
' <input type="radio" id="json" name="otype" value="json">'+
' <label for="huey">json</label>'+
'</div>'+
'</fieldset>');
Add('<br/>');
Add('<input type="submit" value="Classify" />');
Add('</form>');
Add('</body></html>');
Expand All @@ -56,13 +68,17 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
i: integer;
InputV, OutputV: TNNetVolume;
LocalNN: TNNet;
OutputType, OutputStr: string;
FoundClassId: integer;
begin
SrcImage := TPicture.Create;
PredImage := TBitmap.Create;
InputV := TNNetVolume.Create;
OutputV := TNNetVolume.Create;
LocalNN := NN.Clone();
LocalNN.EnableDropouts(false);

This comment has been minimized.

Copy link
@DrDub

DrDub Dec 25, 2022

Contributor

I tried using the Cifar100 network for a desktop automation software (Villavu/Simba#507) and I'm experiencing a lot of unstability. I now see this might be the "magic line" I'm missing.

PredImage.SetSize(32,32);
OutputType := ARequest.ContentFields.Values['otype'];

try
SrcImage.LoadFromStream(ARequest.Files[0].Stream);
Expand All @@ -71,8 +87,7 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
LoadBitmapIntoTinyImage(PredImage, TI);
LoadTinyImageIntoNNetVolume(TI, InputV);
// Bipolar representation.
InputV.Divi(64);
InputV.Sub(2);
InputV.RgbImgToNeuronalInput(csEncodeRGB);

This comment has been minimized.

Copy link
@DrDub

DrDub Dec 25, 2022

Contributor

Yes, I did another server for Cifar100 and needed this line. Sorry about that 😬

finally
SrcImage.Free;
PredImage.Free;
Expand All @@ -88,18 +103,37 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);

OutputV.Divi(2);

jObject := TJSONObject.Create;
try
for i := 0 to OutputV.SizeX - 1 do
if (OutputType = 'text') then
begin
FoundClassId := OutputV.GetClass();
OutputStr := '<html><body>'+
'The found class is: '+Labels.ValueFromIndex[FoundClassId]+'.<br/><br/>';
for i := 0 to OutputV.Size - 1 do
begin
jObject.Floats[Labels.ValueFromIndex[i]] := OutputV.Raw[i];
if i = FoundClassId then OutputStr += '<b>';
OutputStr += Labels.ValueFromIndex[i]+': '+FloatToStr(OutputV.Raw[i])+'.<br/>';
if i = FoundClassId then OutputStr += '</b>';
end;
OutputStr += '</body></html>';
aResponse.ContentType := 'text/html';
aResponse.Content := OutputStr;
end
else
begin
jObject := TJSONObject.Create;
try
for i := 0 to OutputV.Size - 1 do
begin
jObject.Floats[Labels.ValueFromIndex[i]] := OutputV.Raw[i];
end;
aResponse.Content := jObject.AsJSON;
aResponse.ContentType := 'application/json';
finally
jObject.Free;
end;
aResponse.Content := jObject.AsJSON;
aResponse.ContentType := 'application/json';
aResponse.SendContent;
finally
jObject.Free;
end;
aResponse.SendContent;

InputV.Free;
OutputV.Free;
LocalNN.Free;
Expand Down Expand Up @@ -131,6 +165,12 @@ procedure Endpoint(aRequest: TRequest; aResponse: TResponse);
Close(LabelFile);
end;

WriteLn(Labels.Count,' class labels loaded.');
if NN.GetLastLayer().Output.Size <> Labels.Count then
begin
WriteLn('WARNING: you have ',Labels.Count,' lables and ',
NN.GetLastLayer().Output.Size,' outputs on the last layer.');
end;
HTTPRouter.RegisterRoute('/nn', @Endpoint);
HTTPRouter.RegisterRoute('/', @Home);
Application.Port := StrToInt(paramStr(1));
Expand Down

0 comments on commit 8b260eb

Please sign in to comment.