Keeping the eye on Keras models with CodeMonitor
If you work with deep learning, you probably have faced a situation where your model takes too long to learn, and you have to keep watching its progress, however staying in front of the desk watching it learn is not exactly the most exciting thing ever, for this reason, this article’s purpose is to introduce CodeMonitor, a simple but very useful python library. It helps you to make better use of your time and still keep an eye on your model wherever it is being trained by sending reports on each epoch directly to your phone in the form of messages.
The first thing you need to know about CodeMonitor is that it is an open-source initiative from the person who writes this article thus any suggestions or problem retorts are more than welcome. The code is being hosted on Github and for any report, please use the issues section.
Currently, this library uses only Telegram to send the messages and have two ways to do it, you can make use of a Keras callback that will send a report of the logs generated on the training to your Telegram chat at the end of each epoch, these logs may be the loss or the accuracy, for example, it may vary according to your project. The other way you could use it is just sending any string you like, as a message thus you can send messages of any nature, in any context you come up with.
In this article we are assuming that you already have a Keras model, if it is not your case you can learn more about Keras with this tutorial that addresses the classification of handwriting texts on images and come back later or just read it without fully implementing the code.
To make use of this library you can download the source code directly from GitHub or get it with pip, at this tutorial we are going to use the second option for sake of simplicity, to do so just copy and paste the following line of code on your terminal (we are assuming that you already have pip installed, to learn more about pip visit their installation page).
pip install CodeMonitor
Once you have it installed you need to set up the Telegram side, which is pretty simple, if you already have the app installed on your phone just open it and touch the search icon at the right upper corner and search for CodeMonitor and in the image below, select it.
At this point you just need to send any message to it, in response, you will receive a code, this is the chat id, we are using it to send messages to this chat, keep this code in secret, because if anyone who has it can send you messages.
Now everything is set up on Telegram side, so we can come back to the computer and send our first message from python! On python side, we have two classes that are responsible for the functionalities we have discussed earlier, lets import those.
from CodeMonitor.telegram import Messenger, FitMonitor
Please note that on the sample code, we will use “123456789” as chat id, however, you’ll need to replace it on your code
This class allows you to send any string you want to the Telegram chat, only requiring the chat id as mentioned on the Telegram side section.
messenger = Messenger("123456789")
When the class is instantiated it sends the message “All ready!”, to send your messages to use the function send_message.
messenger.send_message("your awesome message!")
As mentioned above this class allows you to send reports based on the logs generated on the training as in the first class you will have to provide the chat id to identify your chat. To simply send all logs generated on the training just add the FitMonitor class to the callbacks list as shown below.
model.fit(X_train, Y_train, epochs = 10, validation_data = (X_test, Y_test), callbacks=[FitMonitor("123456789")])
To specify the logs you want to send you can set a list of the ones you want to include in the message by using the log_keys parameter, as shown in the code snippet which specifies that only the loss should be included in the message.
model.fit(X_train, Y_train, epochs = 10, validation_data = (X_test, Y_test), callbacks=[FitMonitor("123456789", log_keys=["loss"])])
In this article, you have been introduced to a simple but useful tool that may help you to keep monitoring your model from anywhere with your phone, as mentioned above this is an open-source initiative and is still being developed so if you have any idea or suggestion you are more than welcome to drop it at comments.