🚪✊Knock Knock: Get notified when your training ends with only two additional lines of code

Overview

Knock Knock

made-with-python Downloads Downloads GitHub stars

A small library to get a notification when your training is complete or when it crashes during the process with two additional lines of code.

When training deep learning models, it is common to use early stopping. Apart from a rough estimate, it is difficult to predict when the training will finish. Thus, it can be interesting to set up automatic notifications for your training. It is also interesting to be notified when your training crashes in the middle of the process for unexpected reasons.

Installation

Install with pip or equivalent.

pip install knockknock

This code has only been tested with Python >= 3.6.

Usage

The library is designed to be used in a seamless way, with minimal code modification: you only need to add a decorator on top your main function call. The return value (if there is one) is also reported in the notification.

There are currently twelve ways to setup notifications:

Platform External Contributors
email -
Slack -
Telegram -
Microsoft Teams @noklam
Text Message @abhishekkrthakur
Discord @watkinsm
Desktop @atakanyenel @eyalmazuz
Matrix @jcklie
Amazon Chime @prabhakar267
DingTalk @wuutiing
RocketChat @radao
WeChat Work @jcyk

Email

The service relies on Yagmail a GMAIL/SMTP client. You'll need a gmail email address to use it (you can setup one here, it's free). I recommend creating a new one (rather than your usual one) since you'll have to modify the account's security settings to allow the Python library to access it by Turning on less secure apps.

Python

", " "], sender_email=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import email_sender

@email_sender(recipient_emails=["
       
       
        
        "
       
       , "
       
       
        
        "
       
       ], sender_email="
       
       
        
        "
       
       )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock email \
    --recipient-emails <[email protected]>,<[email protected]> \
    --sender-email <grandma'[email protected]> \
    sleep 10

If sender_email is not specified, then the first email in recipient_emails will be used as the sender's email.

Note that launching this will asks you for the sender's email password. It will be safely stored in the system keyring service through the keyring Python library.

Slack

Similarly, you can also use Slack to get notifications. You'll have to get your Slack room webhook URL and optionally your user id (if you want to tag yourself or someone else).

Python

" @slack_sender(webhook_url=webhook_url, channel=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import slack_sender

webhook_url = "
     
     
      
      "
     
     
@slack_sender(webhook_url=webhook_url, channel="
      
      
       
       "
      
      )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

You can also specify an optional argument to tag specific people: user_mentions=[ , ] .

Command-line

knockknock slack \
    --webhook-url <webhook_url_to_your_slack_room> \
    --channel <your_favorite_slack_channel> \
    sleep 10

You can also specify an optional argument to tag specific people: --user-mentions , .

Telegram

You can also use Telegram Messenger to get notifications. You'll first have to create your own notification bot by following the three steps provided by Telegram here and save your API access TOKEN.

Telegram bots are shy and can't send the first message so you'll have to do the first step. By sending the first message, you'll be able to get the chat_id required (identification of your messaging room) by visiting https://api.telegram.org/bot /getUpdates and get the int under the key message['chat']['id'].

Python

", chat_id=CHAT_ID) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import telegram_sender

CHAT_ID: int = <your_messaging_room_id>
@telegram_sender(token="
    
    
     
     "
    
    , chat_id=CHAT_ID)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock telegram \
    --token <your_api_token> \
    --chat-id <your_messaging_room_id> \
    sleep 10

Microsoft Teams

Thanks to @noklam, you can also use Microsoft Teams to get notifications. You'll have to get your Team Channel webhook URL.

Python

") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10) return {'loss': 0.9} # Optional return value ">
from knockknock import teams_sender

@teams_sender(token="
     
     
      
      "
     
     )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock teams \
    --webhook-url <webhook_url_to_your_teams_channel> \
    sleep 10

You can also specify an optional argument to tag specific people: user_mentions=[ , ] .

Text Message (SMS)

Thanks to @abhishekkrthakur, you can use Twilio to send text message notifications. You'll have to setup a Twilio account here, which is paid service with competitive prices: for instance in the US, getting a new number and sending one text message through this service respectively cost $1.00 and $0.0075. You'll need to get (a) a phone number, (b) your account SID and (c) your authentification token. Some detail here.

Python

" AUTH_TOKEN: str = " " @sms_sender(account_sid=ACCOUNT_SID, auth_token=AUTH_TOKEN, recipient_number=" ", sender_number=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10) return {'loss': 0.9} # Optional return value ">
from knockknock import sms_sender

ACCOUNT_SID: str = "
       
       
        
        "
       
       
AUTH_TOKEN: str = "
       
       
        
        "
       
       
@sms_sender(account_sid=ACCOUNT_SID, auth_token=AUTH_TOKEN, recipient_number="
        
        
         
         "
        
        , sender_number="
        
        
         
         "
        
        )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock sms \
    --account-sid <your_account_sid> \
    --auth-token <your_account_auth_token> \
    --recipient-number <recipient_number> \
    --sender-number <sender_number>
    sleep 10

Discord

Thanks to @watkinsm, you can also use Discord to get notifications. You'll just have to get your Discord channel's webhook URL.

Python

" @discord_sender(webhook_url=webhook_url) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import discord_sender

webhook_url = "
    
    
     
     "
    
    
@discord_sender(webhook_url=webhook_url)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock discord \
    --webhook-url <webhook_url_to_your_discord_channel> \
    sleep 10

Desktop Notification

You can also get notified from a desktop notification. It is currently only available for MacOS and Linux and Windows 10. For Linux it uses the nofity-send command which uses libnotify, In order to use libnotify, you have to install a notification server. Cinnamon, Deepin, Enlightenment, GNOME, GNOME Flashback and KDE Plasma use their own implementations to display notifications. In other desktop environments, the notification server needs to be launched using your WM's/DE's "autostart" option.

Python

from knockknock import desktop_sender

@desktop_sender(title="Knockknock Desktop Notifier")
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {"loss": 0.9}

Command Line

knockknock desktop \
    --title 'Knockknock Desktop Notifier' \
    sleep 2

Matrix

Thanks to @jcklie, you can send notifications via Matrix. The homeserver is the server on which your user that will send messages is registered. Do not forget the schema for the URL (http or https). You'll have to get the access token for a bot or your own user. The easiest way to obtain it is to look into Riot looking in the riot settings, Help & About, down the bottom is: Access Token: . You also need to specify a room alias to which messages are sent. To obtain the alias in Riot, create a room you want to use, then open the room settings under Room Addresses and add an alias.

Python

" # e.g. https://matrix.org TOKEN = " " # e.g. WiTyGizlr8ntvBXdFfZLctyY ROOM = "
from knockknock import matrix_sender

HOMESERVER = "
      
      
       
       "
      
       # e.g. https://matrix.org
TOKEN = "
      
      
       
       "
      
                    # e.g. WiTyGizlr8ntvBXdFfZLctyY
ROOM = "
      
                           # e.g. #knockknock:matrix.org

@matrix_sender(homeserver=HOMESERVER, token=TOKEN, room=ROOM)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock matrix \
    --homeserver <homeserver> \
    --token <token> \
    --room <room> \
    sleep 10

Amazon Chime

Thanks to @prabhakar267, you can also use Amazon Chime to get notifications. You'll have to get your Chime room webhook URL.

Python

") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10) return {'loss': 0.9} # Optional return value ">
from knockknock import chime_sender

@chime_sender(webhook_url="
     
     
      
      "
     
     )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock chime \
    --webhook-url <webhook_url_to_your_chime_room> \
    sleep 10

You can also specify an optional argument to tag specific people: user_mentions=[ , ] .

DingTalk

DingTalk is now supported thanks to @wuutiing. Given DingTalk chatroom robot's webhook url and secret/keywords(at least one of them are set when creating a chatroom robot), your notifications will be sent to reach any one in that chatroom.

Python

" @dingtalk_sender(webhook_url=webhook_url, secret=" ", keywords=[" "]) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import dingtalk_sender

webhook_url = "
      
      
       
       "
      
      
@dingtalk_sender(webhook_url=webhook_url, secret="
       
       
        
        "
       
       , keywords=["
       
       
        
        "
       
       ])
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock dingtalk \
    --webhook-url <webhook_url_to_your_dingtalk_chatroom_robot> \
    --secret <your_robot_secret_if_set> \
    sleep 10

You can also specify an optional argument to at specific people: user_mentions=[" "] .

RocketChat

You can use RocketChat to get notifications. You'll need the following before you can post notifications:

  • a RocketChat server e.g. rocketchat.yourcompany.com
  • a RocketChat user id (you'll be able to view your user id when you create a personal access token in the next step)
  • a RocketChat personal access token (create one as per this guide)
  • a RocketChat channel

Python

", rocketchat_user_id=" ", rocketchat_auth_token=" ", channel=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import rocketchat_sender

@rocketchat_sender(
    rocketchat_server_url="
        
        
         
         "
        
        ,
    rocketchat_user_id="
        
        
         
         "
        
        ,
    rocketchat_auth_token="
        
        
         
         "
        
        ,
    channel="
        
        
         
         "
        
        )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

You can also specify two optional arguments:

  • to tag specific users: user_mentions=[ , ]
  • to use an alias for the notification: alias="My Alias"

Command-line

knockknock rocketchat \
    --rocketchat-server-url <url_to_your_rocketchat_server> \
    --rocketchat-user-id <your_rocketchat_user_id> \
    --rocketchat-auth-token <your_rocketchat_auth_token> \
    --channel <channel_name> \
    sleep 10

WeChat Work

WeChat Work is now supported thanks to @jcyk. Given WeChat Work chatroom robot's webhook url, your notifications will be sent to reach anyone in that chatroom.

Python

" @wechat_sender(webhook_url=webhook_url) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import wechat_sender

webhook_url = "
    
    
     
     "
    
    
@wechat_sender(webhook_url=webhook_url)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock wechat \
    --webhook-url <webhook_url_to_your_wechat_work_chatroom_robot> \
    sleep 10

You can also specify an optional argument to tag specific people: user-mentions=[" "] and/or user-mentions-mobile=[" "] .

Note on distributed training

When using distributed training, a GPU is bound to its process using the local rank variable. Since knockknock works at the process level, if you are using 8 GPUs, you would get 8 notifications at the beginning and 8 notifications at the end... To circumvent that, except for errors, only the master process is allowed to send notifications so that you receive only one notification at the beginning and one notification at the end.

Note: In PyTorch, the launch of torch.distributed.launch sets up a RANK environment variable for each process (see here). This is used to detect the master process, and for now, the only simple way I came up with. Unfortunately, this is not intended to be general for all platforms but I would happily discuss smarter/better ways to handle distributed training in an issue/PR.

Owner
Hugging Face
The AI community building the future.
Hugging Face
A Python step-by-step primer for Machine Learning and Optimization

early-ML Presentation General Machine Learning tutorials A Python step-by-step primer for Machine Learning and Optimization This github repository gat

Dimitri Bettebghor 8 Dec 01, 2022
A Multipurpose Library for Synthetic Time Series Generation in Python

TimeSynth Multipurpose Library for Synthetic Time Series Please cite as: J. R. Maat, A. Malali, and P. Protopapas, “TimeSynth: A Multipurpose Library

278 Dec 26, 2022
A Python toolkit for rule-based/unsupervised anomaly detection in time series

Anomaly Detection Toolkit (ADTK) Anomaly Detection Toolkit (ADTK) is a Python package for unsupervised / rule-based time series anomaly detection. As

Arundo Analytics 888 Dec 30, 2022
Simple data balancing baselines for worst-group-accuracy benchmarks.

BalancingGroups Code to replicate the experimental results from Simple data balancing baselines achieve competitive worst-group-accuracy. Replicating

Facebook Research 29 Dec 02, 2022
Mortality risk prediction for COVID-19 patients using XGBoost models

Mortality risk prediction for COVID-19 patients using XGBoost models Using demographic and lab test data received from the HM Hospitales in Spain, I b

1 Jan 19, 2022
Applied Machine Learning for Graduate Program in Computer Science (PPGCC)

Applied Machine Learning for Graduate Program in Computer Science (PPGCC) - Federal University of Santa Catarina

Jônatas Negri Grandini 1 Dec 22, 2021
Distributed training framework for TensorFlow, Keras, PyTorch, and Apache MXNet.

Horovod Horovod is a distributed deep learning training framework for TensorFlow, Keras, PyTorch, and Apache MXNet. The goal of Horovod is to make dis

Horovod 12.9k Jan 07, 2023
A Python implementation of GRAIL, a generic framework to learn compact time series representations.

GRAIL A Python implementation of GRAIL, a generic framework to learn compact time series representations. Requirements Python 3.6+ numpy scipy tslearn

3 Nov 24, 2021
Made in collaboration with Chris George for Art + ML Spring 2019.

Deepdream Eyes Made in collaboration with Chris George for Art + ML Spring 2019.

Francisco Cabrera 1 Jan 12, 2022
A simple guide to MLOps through ZenML and its various integrations.

ZenBytes Join our Slack Community and become part of the ZenML family Give the main ZenML repo a GitHub star to show your love ZenBytes is a series of

ZenML 127 Dec 27, 2022
This is the code repository for LRM Stochastic watershed model.

LRM-Squannacook Input data for generating stochastic streamflows are observed and simulated timeseries of streamflow. their format needs to be CSV wit

1 Feb 14, 2022
The project's goal is to show a real world application of image segmentation using k means algorithm

The project's goal is to show a real world application of image segmentation using k means algorithm

2 Jan 22, 2022
Compare MLOps Platforms. Breakdowns of SageMaker, VertexAI, AzureML, Dataiku, Databricks, h2o, kubeflow, mlflow...

Compare MLOps Platforms. Breakdowns of SageMaker, VertexAI, AzureML, Dataiku, Databricks, h2o, kubeflow, mlflow...

Thoughtworks 318 Jan 02, 2023
The MLOps is the process of continuous integration and continuous delivery of Machine Learning artifacts as a software product, keeping it inside a loop of Design, Model Development and Operations.

MLOps The MLOps is the process of continuous integration and continuous delivery of Machine Learning artifacts as a software product, keeping it insid

Maykon Schots 25 Nov 27, 2022
A toolkit for making real world machine learning and data analysis applications in C++

dlib C++ library Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real worl

Davis E. King 11.6k Jan 02, 2023
The unified machine learning framework, enabling framework-agnostic functions, layers and libraries.

The unified machine learning framework, enabling framework-agnostic functions, layers and libraries. Contents Overview In a Nutshell Where Next? Overv

Ivy 8.2k Dec 31, 2022
Machine Learning Study 혼자 해보기

Machine Learning Study 혼자 해보기 기여자 (Contributors) ✨ Teddy Lee 🏠 HongJaeKwon 🏠 Seungwoo Han 🏠 Tae Heon Kim 🏠 Steve Kwon 🏠 SW Song 🏠 K1A2 🏠 Wooil

Teddy Lee 1.7k Jan 01, 2023
A Python Module That Uses ANN To Predict A Stocks Price And Also Provides Accurate Technical Analysis With Many High Potential Implementations!

Stox A Module to predict the "close price" for the next day and give "technical analysis". It uses a Neural Network and the LSTM algorithm to predict

Stox 31 Dec 16, 2022
MICOM is a Python package for metabolic modeling of microbial communities

Welcome MICOM is a Python package for metabolic modeling of microbial communities currently developed in the Gibbons Lab at the Institute for Systems

57 Dec 21, 2022
Machine Learning Algorithms ( Desion Tree, XG Boost, Random Forest )

implementation of machine learning Algorithms such as decision tree and random forest and xgboost on darasets then compare results for each and implement ant colony and genetic algorithms on tsp map,

Mohamadreza Rezaei 1 Jan 19, 2022