Keras tracking in real time (python)
Aug 2021 by Francisco Juretig
In this example we will show how to use the keras-metrics package to track in real time, precision/recall and f1-score in keras. We will work with an example predicting whether the curtains are closed or open using convolutional neural networks in python. When training deep learning models in keras, we may struggle to see how the training is going in real time. Sure, we can use tensorboard, but it requires some setup, requires opening a web-browser, and is a quite sophisticated tool which may be just "too much" for some situations. prython automatically parses keras models outputs and produces charts in real time which get embedded in your project. So what do we need to load to a convolutional neural network intended for image classification? We just need a tensor of images x width x height x channels.
Here we will use prython as our IDE since it allows us to separate our code in a nice way. It is a new R and Python IDE that allows you to put your code in panels that you can connect to each other. Inside each panel you can use any R or python code you would normally use
These are the images we want to classify. As you can see the curtain can be open or closed, and the objective is to build an algorithm that can tell which one is it.
How do we run it? Each panel has three running modes. You can see the blue carets next to the python button (this button is used to switch between R and python). The first running mode runs only one panel, the second one runs up to that panel (meaning everything that is connected to IN) and the third one runs everything that is connected to OUT (meaning everything that uses the objects defined in here).
Next to each panel, a list of variables and its associated dimensions get loaded. Here we are loading the images that come from two folders and putting them together into a big list. We then transform the list into an array X. For Y we simply build an array containing 0 or 1 (corresponding to a closed/non-closed situation). Each image here (img) has 375x500 dimensions with obviously 3 channels. Note that the final X array has 156 of those.
We are using a pretty standard convolutional network. We added some regularization to the
Dense(20) layer here. The poolings and 2D convolutions used here are pretty standard.
Maybe the only interesting point is why we chose 2,2 for the strides. We think that even though it sacrifices some model quality, it greatly reduces the RAM memory used for the model.
Here you can see the training (this happens in real time and gets updated everytime an epoch finishes).
Here we have the loss and the accuracy. We added a callback that gets called every time an epoch finishes to update our progressbar.
Note that we are passing test and train samples, and both get painted in a different color. We could argue that maybe we neeed more epochs for this training as the test loss/accuracy has not yet stabilized.
With the model trained, we can now predict. This is done by calling
.predict() and this will produce two values which sum up to 1 (you can think of them as being the probabilities of being a 0 or 1 in my case)
In the first image (here on top) you can see how that looks like (not posted here again to avoid the repetition)
Prefer a video?
Here we use the keras-python to predict if the curtains are open or closed
You will also find the code, project and files that you can download from a github repo.https://github.com/fjuretig/amazing_data_science_projects.git