Recent

Author Topic: [AI] [ObjectDetection] Run/Train any YOLO model in pure pascal, no dependancies  (Read 2238 times)

hshatti

  • New Member
  • *
  • Posts: 11
  • Don't be evil isn't enough, Be virtuous!
Greetings good people,
I have just finish an initial library to run YOLO (or any other model),
Lightnet : Object Detection Neural Network libary coded entirely in pure pascal , no external libraries or dependancies, it can run any version of YOLO object detection model in Delphi or Lazarus,
 I have optimized inference to be extremly fast (even faster than the C implementation)
Please put a star on the repository it will give us a motivation to find time to improve it, I will try work on examples to run famous  AI models nativly in pure pascal such as:
- Fast Object Segmentation and tracking
- Fast Image Background removal
- Voice Recognition
- GAN ( Image Upscaling and supper resolution)
- Any voted for AI model you guys would like to see running in pure pascal
- Enjoy reading the strong typing, clear and clean syntax pascal provides to understand YOLO Neural Network under the hood.


Further notes
- Post few tutorials shortly.
- this is a CPU implementation im still working on GPU implementation (OpenCL or cuDNN)
- for training i will submit further examples but note that now it is using CPU and even with SIMD and ThreadPooling it will still be slow on big datasets compared to GPU
- development is still ongoing but eventually i will try make it capable of running LLMs locally using pure pascal  (Inspired by GGML library, + except some C shader kernel codes for GPU of course)

#AI #objectdetection #neuralnetworks #ReptileFreeCoding #MakePascalGreatAgain
- Contributions/ Criticism/ Issues to improve are most welcome
Link
https://github.com/hshatti/LightNet

Cheers
« Last Edit: May 22, 2024, 07:51:30 pm by hshatti »
Don't be evil isn't enough, Be virtuous!
------------
Research-O-Holic .. Highly Skilled Delphi/Lazarus/FPC developer Oh C/C++ too - Solutions with Micro-Controllers (PIC+AVR +STM .. etc )- I.A - anything related to bind thinking and humanity with technology .. all the interesting ideas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 317
  • From C# to Lazarus
Hi,

I tried on Windows 64 Bits, without any option, but it crash with illegal instruction...
Why is there in--dUSE_TELEMETRY? Microsoft habits???

B->
Regards,
Dzandaa

hshatti

  • New Member
  • *
  • Posts: 11
  • Don't be evil isn't enough, Be virtuous!
Quote
I tried on Windows 64 Bits, without any option, but it crash with illegal instruction...

I Apologize for this  :-[, I just fixed it, please re-clone the repo, or try git pull, or just comment line #638 under initialization section in "uMain.pas". or just set ocl.ActivePlatformId to 0 instead of 1
Code: Pascal  [Select][+][-]
  1. ocl.ActivePlatformId:=0;

The crash is due to a silly test I was doing with my PC GPU using OpenCL which is my next checkpoint for this lib,
check and let me know  ::) , however! in your case  my assumption is that the "illegal instruction" is due to your CPU
 not being equipped with AVX2 SIMD instructionset, in this case I suggest to remove -CfAVX2 from the
project custom options and try again, try to keep -dUSE_MULTITHREADING otherwise the detection will be awfully slow!!,
keep an eye on the console messages if -dUSE_TELEMETRY is enabled, it will give you an idea about your CPU performance



Quote
Why is there in--dUSE_TELEMETRY? Microsoft habits???
got me there  :D, keeping track of performance measurements, any other naming suggestion  ?

Cheers
H
« Last Edit: May 22, 2024, 11:24:46 pm by hshatti »
Don't be evil isn't enough, Be virtuous!
------------
Research-O-Holic .. Highly Skilled Delphi/Lazarus/FPC developer Oh C/C++ too - Solutions with Micro-Controllers (PIC+AVR +STM .. etc )- I.A - anything related to bind thinking and humanity with technology .. all the interesting ideas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 317
  • From C# to Lazarus
Hi again,

Instead of d-USE_TELEMETRY why -dUSE_PERFORMANCE?
I tested it on Linux  and it works fine.

Without OpenCV -> O.K.
With OpenCV, problem in linking.

I have OpenCV and the libraries are in /usr/local/lib

But I have a more recent OpenCV (4.5.3) written in CPP so it's normal

Just added:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF UNIX}
  3.   {$IFDEF UseCThreads}
  4.      cthreads,
  5.   {$ENDIF}
  6.   {$ENDIF}
  7.  

Try to test on a Windows 7 Pro 64

Changed in line 234 of steroids.pas or I get an error:

Code: Pascal  [Select][+][-]
  1.   Setlength(Pool, GetSystemThreadCount);
  2. //  Setlength(Pool,ceil(GetSystemThreadCount*0.9));    // or else error in GetSystemThreadCount Call
  3.  

but have errors in ntensor

Line 773

Code: Pascal  [Select][+][-]
  1.  ten.fill(1,1);

ILLEGAL Instruction

On Windows 10 64 Bits it's working but I have a more recent
OpenCV (343)Library.

So I modify opencv.pas

Code: Pascal  [Select][+][-]
  1. {$if defined(MSWINDOWS)}
  2.   {$Linklib opencv_core2413.dll}
  3.   {$LinkLib opencv_highgui2413.dll}
  4.   {$LinkLib opencv_imgproc2413.dll}
  5.   //{$LinkLib opencv_video2413.dll}  
  6.  
 

to

Code: Pascal  [Select][+][-]
  1. {$if defined(MSWINDOWS)}
  2.   {$Linklib opencv_core2413}
  3.   {$LinkLib opencv_highgui2413}
  4.   {$LinkLib opencv_imgproc2413}
  5.   //{$LinkLib opencv_video2413}  
  6.  

And put the 3 *2413.a  library (downloaded from the OpenCV web site https://sourceforge.net/projects/opencvlibrary/files/) in the main directory

On MacOS Sierra, It work without OpenCV.

Anyway, you did a great job!!!

Continue

B->
« Last Edit: May 23, 2024, 04:18:54 pm by Dzandaa »
Regards,
Dzandaa

Dzandaa

  • Sr. Member
  • ****
  • Posts: 317
  • From C# to Lazarus
Hi again,


Would be fine that in Compiler->Options->Path

bin\Release\$(TargetCPU)-$(TargetOS)\MapViewer\TestYolo
bin\Debug\$(TargetCPU)-$(TargetOS)\MapViewer\TestYolo

For Different OS

And perhaps a directory structure where we can choose the root Directory

Root-->
-------->Data
-------->Weight
-------->Cfg
-------->Test or (Test_Pictures)

Original Darknet from Joseph Redmon is a mess with the files , I know :)

But that's detail.

Best improvement will be OpenCL or CUDA.


B->
« Last Edit: May 23, 2024, 04:20:06 pm by Dzandaa »
Regards,
Dzandaa

hshatti

  • New Member
  • *
  • Posts: 11
  • Don't be evil isn't enough, Be virtuous!
Hello ,
Glad to know it's working well so far, I'm interested to see the telemetry figures you got on each platform you tried, can you post them here?, I would appreciate if you git pull the recent changes before running it (with release profile), then copy and past the telemetry with the printed CPU info from the console along with telemetry table, it will help me to get a grasp on how is it behaving on different CPUs, and for each platform,

Quote
Just added:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF UNIX}
  3.   {$IFDEF UseCThreads}
  4.      cthreads,
  5.   {$ENDIF}
  6.   {$ENDIF}
  7.  

Try to test on a Windows 7 Pro 64
well I don't recommend placeing cthread in any unit head specially if you need to reuse it in Delphi, Lazarus usually places it in the .lpr project entry and it should be enough to link cthread libraries used for POSIX systems

Quote

Code: Pascal  [Select][+][-]
  1.  ten.fill(1,1);
  2.  
ILLEGAL Instruction
My assumption it's due to the old CPU generation? please try to disable the switch -CfAVX2 or use the (Debug Profile) and let me know.

 I have made some quick changes to get some context about the processors you may try to git pull the changes.


- regarding OPENCV i'm trying to make this library available for both Delphi and Lazarus, I have translated OpenCV headers for quick webcam usage and haven't test all the functionalities yet, I'm in favour of (and eventually planning to) a platform wrapper for each OS with tiny video capturing & media playing/recording library natively with in pascal, nevertheless I highly recommend sticking to OpenCV version 2.4.13 files .dll ( or .so in linux case) don't use static linking .a files, and because the folks at Intel unfortunately have decided to ditch the ABI cdecl C calling which was compatible with both FPC and Delphi and replace it with C++ cppdecl starting from version 3.x.x and this is AFAIK not really compatible with Delphi and still experimental in FPC (perhaps the wizards of this forum can advise?) , finally just place those DLL's next to the executable the webcam should work (use it with -dUSE_OPENCV switch).


Thanks again for the good feedback

Cheers
H
« Last Edit: May 25, 2024, 03:23:52 am by hshatti »
Don't be evil isn't enough, Be virtuous!
------------
Research-O-Holic .. Highly Skilled Delphi/Lazarus/FPC developer Oh C/C++ too - Solutions with Micro-Controllers (PIC+AVR +STM .. etc )- I.A - anything related to bind thinking and humanity with technology .. all the interesting ideas

Dzandaa

  • Sr. Member
  • ****
  • Posts: 317
  • From C# to Lazarus
Hi,
I tried on my Linux 64b

compared to the old version, it crashes a compilation at steroids.pas

{$if defined(CPUX64)}

Error: unknown identifier "RBX", etc...

I don't know if this help:https://stackoverflow.com/questions/70045274/function-to-retrieve-cpuid-does-not-compile-in-lazarus

B->

« Last Edit: May 25, 2024, 01:21:35 pm by Dzandaa »
Regards,
Dzandaa

hshatti

  • New Member
  • *
  • Posts: 11
  • Don't be evil isn't enough, Be virtuous!
Hi again,
forgot to change the CPU directive, it's a simple fix though :

in steroids.pas head, just change

Code: Pascal  [Select][+][-]
  1.   {$ifdef MSWINDOWS}
  2.     {$FPUType AVX2}
  3.     {$asmmode intel}
  4.   {$endif}
  5.  

to

Code: Pascal  [Select][+][-]
  1.   {$ifdef CPUX64}
  2.     {$FPUType AVX2}
  3.     {$asmmode intel}
  4.   {$endif}
  5.  

start with debug running profile and no -dUSE_OPENCV then you can try adding -CfAVX2 see if there is a performance change , then if you manage to install OpenCV 2.4.13 for linux place the .so files next to the executables and try

Cheers :)
H
Don't be evil isn't enough, Be virtuous!
------------
Research-O-Holic .. Highly Skilled Delphi/Lazarus/FPC developer Oh C/C++ too - Solutions with Micro-Controllers (PIC+AVR +STM .. etc )- I.A - anything related to bind thinking and humanity with technology .. all the interesting ideas

gidesa

  • Jr. Member
  • **
  • Posts: 84

- regarding OPENCV i'm trying to make this library available for both Delphi and Lazarus, I have translated OpenCV headers for quick webcam usage and haven't test all the functionalities yet, I'm in favour of (and eventually planning to) a platform wrapper for each OS with tiny video capturing & media playing/recording library natively with in pascal, nevertheless I highly recommend sticking to OpenCV version 2.4.13 files .dll ( or .so in linux case) don't use static linking .a files, and because the folks at Intel unfortunately have decided to ditch the ABI cdecl C calling which was compatible with both FPC and Delphi and replace it with C++ cppdecl starting from version 3.x.x and this is AFAIK not really compatible with Delphi and still experimental in FPC (perhaps the wizards of this forum can advise?) , finally just place those DLL's next to the executable the webcam should work (use it with -dUSE_OPENCV switch).

Hello, I have written a wrapper library that translates  Opencv 4.6 C++ functions/methods to functions with C API. So recent versions of Opencv now are compatible with FPC and Delphi.
See: https://github.com/gidesa/ocvWrapper46
 

Dzandaa

  • Sr. Member
  • ****
  • Posts: 317
  • From C# to Lazarus
Hi,

Works now on Linux with AVX2.

It's too complicated to install an older version of OpenCV,
because some of my programs depend on a newer version and my CMake is also newer.

B->
Regards,
Dzandaa

 

TinyPortal © 2005-2018