Reading Clocks using Neural Nets. Can Neural Network detect time from... | by Shiva Verma | Towards Data Science

I was really interested in an idea, which was to read the time from Analog Clock Images using Neural Nets. To do this task, I needed a dataset containing clock images, but there was not any dataset available on the web. One other way was to download the clock images from the web and manually label them. Which is a quite time-consuming process.
Finally, I decided to write a python script that generates animated clock images and their respective labels. The following are the few samples of clocks images generated by the script.
These images are not as realistic as real-world clock Images but it would be really exciting to see if a Neural Net can be trained on these images as well.
The following is the link to the dataset, which is available on Kaggle.
www.kaggle.com
Before designing the neural net, let’s see how do we read time from a clock.
in order to read the time from a clock, we need two values, an hour and a minute. An hour can take value from 0 to 11, where 0 is nothing but 12 o’clock. Similarly, a minute can take value from 0–59. The length of the hour-hand is smaller than the minute-hand. This is how we differentiate the Minute and Hour hand. And most important, there is at least one marker(12 o’clock at the top) in the clock.
Now let’s design a neural net that can read the time similarly.
As you know. our objective is to feed a clock image to the neural network and get the time value from it. So, the network has to output 2 values, hour, and minute. Let’s cover each case one by one.
Hour values can be from 0 to 11. We can consider this as a Classification task where we have a total of 12 classes. And the network has to select one of the classes.
Minutes values can be from 0–59. This means there are 60 possible values. It would be wise to consider it as a Regression task. Because in this case, we want to predict the minute value as close as possible.
Now let’s look at the architecture diagram below which explains the complete network architecture.
In the beginning, the Network contains Convolutional layers, which will extract useful features from the image. On top of the Convolutional layers, there are 2 branches of Fully-Connected layers. One branch is for detecting Hour and one for detecting Minute.
Since predicting Hour value is a classification task. There would be 12 output nodes in the hour-branch. And we apply a Softmax activation on top of output nodes.
In the minute-branch, there would be just one output node with the Linear activation, since in regression we just need a single value. Linear activation is essentially no activation. I will not go into details of classification and regression here.
The whole network is ready which can read the time from clock images. Following is the code to create this network in Keras.
We have designed the network architecture and created it in Keras. It’s time to feed the data to the network and train it. Let’s quickly discuss the input and target of the network.
My clock dataset contains RGB images of (300*300) size. Before feeding the image to the network, I converted all images to greyscale and reduced the size to (100*100). I am doing this to load the images in less memory and to make training faster. On the other hand, we may lose some information due to low resolution.
The target is the hour and minute values. I divided the minute value by 60 to keep it in the range of (0 – 1). The neural net performs better on small-range output.
I have trained the network on 40K clock images for 10 epochs. During the training, I kept decreasing the learning rate and increasing batch size. The final results were as follows on 500 test images.
Hour, accuracy: ~99.00Minute, Mean Absolute Error: ~3.5
On average, the network was able to read the time close to 3.5 minutes. Which is quite impressive putting little effort.
Following is a snippet of my Jupyter Notebook which shows the Network reading time.
Following is the GitHub link for this complete project. I have also saved the trained model in the Repo. Thanks for reading.