Implementing FANN (Fast Artificial Neural Network) to C++ Builder/Delphi Projects

Posted by on in Programming

Would you like to make your applications learning and predicting applications (May be just thinking applications) ? 

In this blog post we gonna use open source Fast Artificial Neural Network Library made by Steffen Nissen. FANN Library is very simple to use and it has good documentation and written in C programming language which makes it faster. It is open source so you can easily implement and modify your code. It has English, French & Polish help.

 

1. Quick Introduction To Artificial Intelligence

The human brain is one of the great parts of our body; we want to know more and more and we want to dive deeper and deeper after Alan Turing - who is the founder of computer science, mathematician, philosopher - built the first Enigma Computer. Now, large computation capabilities, multi-core CPUs and large memory capacities with 64bit systems are boosting this technology as well as with the large and high speed internet and better programming techniques. Nowadays many companies developing Artificial Intelligence (AI) applications, it is very clear that there is an apparent increment in issue of patents in the last 5 year. Still this is also a good research area in academic area. Computers thinking like a human are still absent in our world but day by day we are getting much more closer. If you are new to this area you can get more information about AI 

https://en.wikipedia.org/wiki/Artificial_intelligence

https://futureoflife.org/background/benefits-risks-of-artificial-intelligence/

 

2.  Quick Explanation to ANN

Artificial Neural Network is part of Artificial Intelligence which models artificial neurons connected each other in artificial layers. Codes based on this ANN theory works well in many applications. In a general view, ANN applications simulate connected neurons in a specific problem and this allows you to train from inputs and estimate results by trained ANN. You can compare this predictions by real results or results obtained from any other techniques.

Some of ANN applications can be File/Data Compression by ANNs, Neocognitron, Feedforward NN for Rapid Vision, Speechreading (Lipreading), Detection and Tracking of Moving Targets (ICBMs), GPS Data Mining Applications, Reconstruction of 3D Buildings and Models, Age Modeling Systems, Games, Predictions in Medical Applications, Simulation of Robotic Systems, Digital Assistants, Engineering Approaches in Analyzes of Systems ... https://cw.felk.cvut.cz/w/_media/courses/a4m33bia/ann_examples_2011.pdf

https://en.wikipedia.org/wiki/Artificial_neural_network

http://neuralnetworksanddeeplearning.com/chap1.html 

https://www.analyticsvidhya.com/blog/2014/10/ann-work-simplified/

 

3. An Application from my Study

Let me give an example from my PhD thesis. I studied in a ceramic factory which has huge co-generation system with big dryers, pumps, ex-changers, pipes and heating systems. There were many points in this system and hourly Energy Efficiency and Exergy Efficiency of this system needs to be calculated by theories of thermodynamic laws. System was running and there were some difficulties in measurement. It's done with input data of the system with calculations by many formulas, tables, and variables like inlet air velocity, temperature, pressure, power generation, heat generations etc. It was really hard to calculate enthalpy and entropy of each node when this huge system is operating. In my thesis I also use ANN which learns thermodynamics of the system from some given parameters and predicts results with the calculations made by thermodynamic results. Here are some papers about this research if u are interested.

http://www.inderscienceonline.com/doi/abs/10.1504/IJEX.2010.031239

http://onlinelibrary.wiley.com/doi/10.1002/er.1561/abstract

http://ieeexplore.ieee.org/document/5381338/?reload=true

 

4. Quick Introduction To FANN Library

Fast Artificial Neural Network (FANN) is an old ( about 14 years) open source ANN library made by  Steffen Nissen (http://leenissen.dk). It supports more than 20+ programming languages (http://leenissen.dk/fann/wp/language-bindings/) including Delphi and C++ Builder. You can reach full information and documentation (http://libfann.github.io/fann/docs/files/fann-h.html) including downloads of sources from their web page ((http://leenissen.dk).

I will not explain much more about what AI ,  ANN and FANN is. There are good documentations about those on their web page and on the internet. 

You can easily find many ANN libraries on the internet. This library is simple and written in C that makes it generic and simple to implement. It is also fast due to C programming language. Compiled library can be used in many programming languages as mentioned above.

 

5. Implementing FANN (Fast Artificial Neural Network) to C++ Builder Applications

First I want to say that you can use FANN library in console applications,  also in both VCL and FMX applications. Here I will explain how you can implement into MultiDevice applications. Therby you can use this library in your applications which runs on Win, Android, iOS, MacOS and Linux operating systems.

Step 1: Download FANN Library package from Nissen's official web page (http://leenissen.dk). This package includes binaries, cmake files, datasets, examples, source codes in src folder and some examples.

Step 2: Create a new MultiDevice C++ Builder Application. Add a Panel (Panel1) which has 32 pixels height and two Buttons (TEST, TRAIN) into it.  Align Panel  to the bottom of Form1. Add a Memo (Memo1) into form an make it client. This will be our visual output place :)

Step 3Create FANN_Test folder and  save all project files with "FANN_" prefix into this folder as listed below; 

FANN_Project1.cbproj

FANN_Project1.cbproj.local

FANN_Project1.cpp 

FANN_Project1PCH1.h 

FANN_Unit1.cpp 

FANN_Unit1.fmx 

FANN_Unit1.h

 

Step 4Now we need source files to compile with our application. Unzip package , copy "src" folder from this folder into this FANN_Test folder. Make your RADS/C++ Builder/Delphi IDE size smaller and drag src folder from FANN_Test to Project Manager on the "FANN_Project1.exe". When IDE asks "Would you like to add the selected files to project ..." confirm with Yes. 

Step 5We need include folder of FANN for headers. From the IDE menu go to Project -> Options ->C++ Compiler->Directories and Conditionals->Include File Search path. Add here "src/include" folder which is in our FANN_Test folder

Step 6Now you can add a header into our FANN_Unit1.cpp. Below #include "FANN_Unit1.h" line add this #include "floatfann.h".

If you do all steps now we implemented FANN library into our project with its original sources without any tweaks or changes on original files. That means FANN source files and headers are very friendly because of its native C codes.

Step 7Now lets test if all is fine. Build all projects from Projects -> Build All Projects menu. If Success that means all is fine. if there are errors please check steps above. 

Save All

If you want to compile for other OSes don't Forget to add include search path "src/include" to its project options as we do above. Please check examples, other commands and options of FANN Library to understand its mechanism. 

That's All :) 

Testing Code Phase:

In the folder of FANN Package, there are native examples. Please check simple_train .c and simple_test.c  codes. We gonna cut and paste those codes with some tweaks to implement our MultiDevice application.

Step 1: Go to Form view and double click to TRAIN button and add below  

 

	/* --- TRAINING FANN (from the simple_train.c) --- */

	const unsigned int num_input = 2;
	const unsigned int num_output = 1;
	const unsigned int num_layers = 3;
	const unsigned int num_neurons_hidden = 3;
	const float desired_error = (const float) 0.001;
	const unsigned int max_epochs = 500000;
	const unsigned int epochs_between_reports = 1000;

	struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output);

	fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
	fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);

	fann_train_on_file(ann, "xor.data", max_epochs, epochs_between_reports, desired_error);

	fann_save(ann, "xor_float.net");

	fann_destroy(ann);

	Memo1->Lines->LoadFromFile("xor_float.net");

	Memo1->Lines->Add("Training done on xor_float.net file as above");

 

Step 2: Double click to TEST button and add below  

	/* ---- TESTING FANN (from the simple_test.c) ----*/
	char s[255];

	fann_type *calc_out;
	fann_type input[2];

	struct fann *ann = fann_create_from_file("xor_float.net");

	input[0] = -1;
	input[1] = 1;
	calc_out = fann_run(ann, input);

	Memo1->Lines->Add("");
	Memo1->Lines->Add("Testing ... ");
	sprintf(s, "xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]);
	Memo1->Lines->Add(s);
	Memo1->Lines->Add("Testing Done ! ");

	fann_destroy(ann);

That's all now you can run your application successfully. Please check other examples, I am sure you can do very innovative examples and you can implement it easily on your applications. 

 

6. Implementing FANN (Fast Artificial Neural Network) to Delphi Applications

Delphi coders are very lucky :) Just check language bindings section (http://leenissen.dk/fann/wp/language-bindings/)  there is a ready library package with examples.

 

Good Luck All !

 

 



About ( )
Gold User, No rank,
Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his M.Sc. and Ph.D. degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics (Exergy), Fluid Mechanics and Artificial Intelligence. He also likes the graphical design and high-end innovations. He has been programming since collage, has a wide range programming skills including Basic, Pascal, C++, Ruby, HTML5, PHP, Javascript and other programming languages in different platforms Windows, Linux, Android, MacOS, iOS and some old OSs. He has more than 30 great software applications about engineering, automation and games mostly codded in C++ Builder.

Comments

  • mackyo0384
    mackyo0384 Tuesday, 5 December 2017

    Won't install. Says: "can't load package the specified module could not be found" even though the BPL file is there. I've read that it's some dependency on something. I installed FANN moved dll's to system32.. tried to load it into 10.1 and 10.2 same thing.

  • Yilmaz Yoru
    Yilmaz Yoru Tuesday, 5 December 2017

    You need source codes and you should include its headers in your code, if you mail me i can send you a simple example

  • martijnlaan
    martijnlaan Wednesday, 19 July 2017

    Thanks for the article! It looks like this library is abandoned though? The latest stable release is from 2012, the latest GitHub commit from 2015. Do you think this library can still be used for modern NN work? Are you aware of any active fork?

  • Yilmaz Yoru
    Yilmaz Yoru Wednesday, 19 July 2017

    @martijnlaan
    Yes, this still can be used with modern approaches because you can also modify source codes for the modern approaches. If you are good at programming and if u know well modern ANN theories this library is awesome. You have no limit. You can develop your own theory too. That gives you much power. Google's TensorFlow is the latest ANN which uses CNN, it has API and some Unix C examples (https://www.tensorflow.org/). It is still underdevelopment and not compatible with C++ Builder ( i have tried to test it too) . Mostly supports Unix C and Phyton programming language.

  • martijnlaan
    martijnlaan Thursday, 20 July 2017

    Thanks for your reply!

  • Yilmaz Yoru
    Yilmaz Yoru Tuesday, 4 July 2017

    @Kiryll S41699
    From a FANN discussion author says that it is not directly supporting CNN. Here is a discussion about this. http://leenissen.dk/fann/forum/viewtopic.php?f=1&t=32

    As I know FANN is a very core ANN Library, Convolutional Neural Networks (CNN) is mostly about Image recognition and multiple ANN based processes like robots, automation systems etc can be analyzed. If you consider that ANN is an apple hanging on a main rope, CNN is like bucked of apples connected to each other and hanging on a main rope. So you can tie them according to num of apples, size of apples, ropes and the bucket (the case). In application; for the face recognition; each apple can be a part of face (eye, nose, lips ...) and the bucket is face or each parts of automation system and the whole system . I think CNN codes must be designed according to the case problem. So each part of system can be trained well. I am sure you can adapt this by coding and doing changes in the main library.

    There are some CNN codes on the web may be you can implement them alone or with FANN Library.
    http://conv-net.sourceforge.net/doc/

    I hope those helps. Nowadays I focus on many projects and I have very narrow knowledge about CNN.

  • Kiryll Shynharow [S41699]
    Kiryll Shynharow [S41699] Tuesday, 4 July 2017

    Hello Yilmaz, thank you for interesting post.

    The neural network is primarily a universal approximator by its very nature and your application of this to the calculation of a complex thermodynamic system when it is difficult to perform this analytically looks fully justifiably.

    Do you know if there is built-in support for convolutional neural networks in FANN?

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: