POL
ENG

Simple neural network

AI, meaning Artificial Inteligence, appears more and more often in warious parts of our life. I decided to dive into this topic and see how it works in the details. Durring one of birthdays parties, I had that wonderful opportunity to talk with Rob O., who recomend me interesting channel on YT about this topic Andrew Ng - @ArtificialIntelligenceAllinOne). Author speaks in very accessible way about basics of this technology. That encouraged me look further and that is how I found another valuable chanel where it was visualized using graphic (@3blue1brown - https://www.youtube.com/@3blue1brown). That was good to catch the basics.

Other day, on my way back home, I again had to search for free parking place nearby my block of flats. Then I asked myself, why to not use AI to do the job for me? From idea to working project is a long way. I had to get know how to make NN on computer.

Tools

Implementation of basic neuron is trivial. Combine them in network is also quite simple. A far more work is with introducing back propagation and make code scalable. But these doors are already opened. In the internet, there are a few frameworks ready to be used, e.g.: OpenNN, PyTorch or TensorFlow. I took the last one.

Implementation

Most people start their jurney with NN buildng network to recognize hand write numbers. On almost all webpages you can find it. It is popular, but not the simplest. I needed an example simple enough to being debug if any problems occurs. This is how I came up with this NN.

Network

Task to solve: find a stright line on the black and white picture. Thickness and orientation doesn't matter. Picture consists of incredeble amount (3x3) of black and white pixels (no gray shades).

Structure

Each picture is transformed to a vector containing all 9 pixels/bites. That determines input layer as 9 elements wide. Signals from it are passed to two internal/hidden layers. Output layer size is 2. In the end it could be only on element wide. This signal is two state combination which means if input picture represents line or not. This task is simple, so it could be solved with combinational program or simple look up table, but this is not that case :).

model = keras.Sequential(name="FirstNN")

model.add(layers.Dense(

input_shape=(9,),

units=l1_units,

activation="relu",

use_bias = True,

name="Hidden1"))

model.add(layers.Dense(

units=l2_units,

activation="relu",

use_bias = True,

name="Hidden2"))

model.add(layers.Dense(

units=2,

activation="softmax",

use_bias = True,

name="Output"))

Learning

As I've already written, I decided to use TensorFlow (Python edition). This software contains built in Keras packet, which allows convinient and high level play with NN. Network model consists of all layers mentioned above. In such shape, it can do nothing. It needs to learn. For this purpose, we need a set of defined inputs and outputs e.g.: picture with defined description of what is on it. Such a bunch of training data is passed through network, pointing each time how far its result was from expected/defined. This delta is back propagated (with respect of coefficients) through whole NN updating internal variables (weights and biases).

In case of my NN, input is 9-bit, so there is only 512 different pictures. Of course, we could use all of them, but then this NN will have nothing in common with intelligence. We also may get overfeeding problem. Common practic is to exclude some part of samples for training, some for validation and the rest for normal working. And I did in the same way:

# Prepare random indexes for training samples

train_indexes = get_unique_vector(0, len(input_data), training_size)

# Prepare random indexes for validation samples

val_indexes = get_unique_vector(0, len(input_data), val_size)

Keras allows initiate training process in very simply way using only one command. Many parameters has default value, so basic call is extremally simple, but it is worth to play with some extra ones as they give more controll over learning process.

model.fit(

x_train,

y_train,

epochs = epochs,

callbacks = callbacks,

validation_data=(x_val, y_val),

use_multiprocessing=True,

workers=4,

verbose=0,

)

After learning our NN it is good to same this mode, so we will not have to repeat this process each time. We need to remember, that is trivial example and learning takes only seconds, while more complicated NN can be learnt in hours or days. To save model, Keras provides saving function:

model.save()

And this is how it was made. As I already mentioned, full collection is only 512 pictures. It can be seen below.

Pelna kolekcja

It not always gives perfect result. After a few iteration I got accuracy level 92,16%. It can do better. Remaining scores results in wrongly interpreted pictues. These are examples of pictures recognized as line:

Zle rozpoznanie

Even though, I'm satisfied from achieved results. Fortunately it is not used in autonomus drone or car ;).

Wszelkie prawa zastrzeżone. Projekt i wykonanie strony SrcPro.pl