-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de2fb07
commit 8b260eb
Showing
1 changed file
with
51 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>'); | ||
|
@@ -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.
Sorry, something went wrong. |
||
PredImage.SetSize(32,32); | ||
OutputType := ARequest.ContentFields.Values['otype']; | ||
|
||
try | ||
SrcImage.LoadFromStream(ARequest.Files[0].Stream); | ||
|
@@ -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.
Sorry, something went wrong.
DrDub
Contributor
|
||
finally | ||
SrcImage.Free; | ||
PredImage.Free; | ||
|
@@ -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; | ||
|
@@ -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)); | ||
|
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.