Skip to menu

Robotics with Object Pascal

Deep Learning

CAI's Hypotenuse example's model loading.

2024.03.02 15:23

me Views:73

(*) Once model was trained, there is no need to train again. 

Therefore, from the Hypotenuse example, I have strip off all model training related codes.

It is focused on pre-trained model usage. (*)

 

program Hypotenuse_my_test;
{  Written by Henry Kim for testing model made from CAI's Hypotenuse example.
   Copy project file from Hypotenuse examples, otherwise, it won't compile.  }
{$mode objfpc}{$H+}

uses {$IFDEF UNIX} {$IFDEF UseCThreads}
  cthreads, {$ENDIF} {$ENDIF}
  Classes, neuralnetwork, neuralvolume, neuralfit;

var
   NN: TNNet;
   ModelFileName: string;
   input_pair_list : TNNetVolumePairList;
   CorrectOutput : real;
   pOutPut: TNNetVolume;
   x, y, Hypotenuse: TNeuralFloat; // TNeuralFloat = Single;

begin
   // Here, we are using the model trained from earlier example (inferencing)
   ModelFileName := 'hypotenus.nn'; // model file trained from CAI's Hypotenuse example
   NN := TNNet.Create;
   NN.LoadFromFile(ModelFileName); // Load pre-trained

   input_pair_list := TNNetVolumePairList.Create();
   x := 3.0;  // x side of right tri-angle
   y := 4.0;  // y side of right tri-angle
   Hypotenuse := 1.0; // Optimal value calculated by sqrt(x*x+y*y)

   input_pair_list.Add(
      TNNetVolumePair.Create(
      TNNetVolume.Create([x, y]),  // these two values are the key input for query.
      TNNetVolume.Create([Hypotenuse]))); // for reference only, not really needed.

   pOutPut := TNNetVolume.Create({pSizeX=}1, {pSizeY=}1, {pDepth=}1, {FillValue=}1);

   NN.Compute(input_pair_list[0].I);  // Actual inferencing occurs here
   NN.GetOutput(pOutPut);             // Store output result to pOutPut

   WriteLn( 'Input (x,y):', input_pair_list[0].I.FData[0]:5:2,', ',
      input_pair_list[0].I.FData[1]:5:2,'   Inferenced:', pOutPut.Raw[0]:5:2,' ',
      '  Calculated:', input_pair_list[0].O.FData[0]:5:2 );
   pOutPut.Free;
   input_pair_list.Free;
   NN.Free;
end.
{ it produces result on terminal
  Input (x,y): 3.00,  4.00   Inferenced: 5.01   Calculated: 5.00
}