Package: knockknock
Knock KnockA 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.InstallationInstall withpipor equivalent.pipinstallknockknockThis code has only been tested with Python >= 3.6.UsageThe 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 currentlytwelveways to setup notifications:PlatformExternal Contributorsemail-Slack-Telegram-Microsoft Teams@noklamText Message@abhishekkrthakurDiscord@watkinsmDesktop@atakanyenel@eyalmazuzMatrix@jcklieAmazon Chime@prabhakar267DingTalk@wuutiingRocketChat@radaoWeChat Work@jcykEmailThe service relies onYagmaila GMAIL/SMTP client. You'll need a gmail email address to use it (you can setup onehere, 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 byTurning on less secure apps.Pythonfromknockknockimportemail_sender@email_sender(recipient_emails=["<your_email@address.com>","<your_second_email@address.com>"],sender_email="<grandma's_email@gmail.com>")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueCommand-lineknockknockemail\--recipient-emails<your_email@address.com>,<your_second_email@address.com>\--sender-email<grandma's_email@gmail.com>\sleep10Ifsender_emailis not specified, then the first email inrecipient_emailswill 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 thekeyringPython library.SlackSimilarly, you can also use Slack to get notifications. You'll have to get your Slack roomwebhook URLand optionally youruser id(if you want to tag yourself or someone else).Pythonfromknockknockimportslack_senderwebhook_url="<webhook_url_to_your_slack_room>"@slack_sender(webhook_url=webhook_url,channel="<your_favorite_slack_channel>")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueYou can also specify an optional argument to tag specific people:user_mentions=[<your_slack_id>, <grandma's_slack_id>].Command-lineknockknockslack\--webhook-url<webhook_url_to_your_slack_room>\--channel<your_favorite_slack_channel>\sleep10You can also specify an optional argument to tag specific people:--user-mentions <your_slack_id>,<grandma's_slack_id>.TelegramYou 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 Telegramhereand save your API accessTOKEN.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 thechat_idrequired (identification of your messaging room) by visitinghttps://api.telegram.org/bot<YourBOTToken>/getUpdatesand get theintunder the keymessage['chat']['id'].Pythonfromknockknockimporttelegram_senderCHAT_ID:int=<your_messaging_room_id>@telegram_sender(token="<your_api_token>",chat_id=CHAT_ID)deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueCommand-lineknockknocktelegram\--token<your_api_token>\--chat-id<your_messaging_room_id>\sleep10Microsoft TeamsThanks to@noklam, you can also use Microsoft Teams to get notifications. You'll have to get your Team Channelwebhook URL.Pythonfromknockknockimportteams_sender@teams_sender(token="<webhook_url_to_your_teams_channel>")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10)return{'loss':0.9}# Optional return valueCommand-lineknockknockteams\--webhook-url<webhook_url_to_your_teams_channel>\sleep10You can also specify an optional argument to tag specific people:user_mentions=[<your_teams_id>, <grandma's_teams_id>].Text Message (SMS)Thanks to@abhishekkrthakur, you can use Twilio to send text message notifications. You'll have to setup aTwilioaccounthere, 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) youraccount SIDand (c) yourauthentification token. Some detailhere.Pythonfromknockknockimportsms_senderACCOUNT_SID:str="<your_account_sid>"AUTH_TOKEN:str="<your_auth_token>"@sms_sender(account_sid=ACCOUNT_SID,auth_token=AUTH_TOKEN,recipient_number="<recipient's_number>",sender_number="<sender's_number>")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10)return{'loss':0.9}# Optional return valueCommand-lineknockknocksms\--account-sid<your_account_sid>\--auth-token<your_account_auth_token>\--recipient-number<recipient_number>\--sender-number<sender_number>sleep10DiscordThanks to@watkinsm, you can also use Discord to get notifications. You'll just have to get your Discord channel'swebhook URL.Pythonfromknockknockimportdiscord_senderwebhook_url="<webhook_url_to_your_discord_channel>"@discord_sender(webhook_url=webhook_url)deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueCommand-lineknockknockdiscord\--webhook-url<webhook_url_to_your_discord_channel>\sleep10Desktop NotificationYou 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.Pythonfromknockknockimportdesktop_sender@desktop_sender(title="Knockknock Desktop Notifier")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{"loss":0.9}Command Lineknockknockdesktop\--title'Knockknock Desktop Notifier'\sleep2MatrixThanks to@jcklie, you can send notifications viaMatrix. The homeserver is the
server on which your user that will send messages is registered. Do not forget the schema for the URL (httporhttps).
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:<click to reveal>. 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 underRoom Addressesand add an alias.Pythonfromknockknockimportmatrix_senderHOMESERVER="<url_to_your_home_server>"# e.g. https://matrix.orgTOKEN="<your_auth_token>"# e.g. WiTyGizlr8ntvBXdFfZLctyYROOM="<room_alias"# e.g. #knockknock:matrix.org@matrix_sender(homeserver=HOMESERVER,token=TOKEN,room=ROOM)deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueCommand-lineknockknockmatrix\--homeserver<homeserver>\--token<token>\--room<room>\sleep10Amazon ChimeThanks to@prabhakar267, you can also use Amazon Chime to get notifications. You'll have to get your Chime roomwebhook URL.Pythonfromknockknockimportchime_sender@chime_sender(webhook_url="<webhook_url_to_your_chime_room>")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10)return{'loss':0.9}# Optional return valueCommand-lineknockknockchime\--webhook-url<webhook_url_to_your_chime_room>\sleep10You can also specify an optional argument to tag specific people:user_mentions=[<your_alias>, <grandma's_alias>].DingTalkDingTalk 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.Pythonfromknockknockimportdingtalk_senderwebhook_url="<webhook_url_to_your_dingtalk_chatroom_robot>"@dingtalk_sender(webhook_url=webhook_url,secret="<your_robot_secret_if_set>",keywords=["<list_of_keywords_if_set>"])deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueCommand-lineknockknockdingtalk\--webhook-url<webhook_url_to_your_dingtalk_chatroom_robot>\--secret<your_robot_secret_if_set>\sleep10You can also specify an optional argument to at specific people:user_mentions=["<list_of_phonenumbers_who_you_want_to_tag>"].RocketChatYou can useRocketChatto get notifications. You'll need the following before you can post notifications:a RocketChat server e.g. rocketchat.yourcompany.coma 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 channelPythonfromknockknockimportrocketchat_sender@rocketchat_sender(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>")deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueYou can also specify two optional arguments:to tag specific users:user_mentions=[<your_user_name>, <grandma's_user_name>]to use an alias for the notification:alias="My Alias"Command-lineknockknockrocketchat\--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>\sleep10WeChat WorkWeChat 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.Pythonfromknockknockimportwechat_senderwebhook_url="<webhook_url_to_your_wechat_work_chatroom_robot>"@wechat_sender(webhook_url=webhook_url)deftrain_your_nicest_model(your_nicest_parameters):importtimetime.sleep(10000)return{'loss':0.9}# Optional return valueCommand-lineknockknockwechat\--webhook-url<webhook_url_to_your_wechat_work_chatroom_robot>\sleep10You can also specify an optional argument to tag specific people:user-mentions=["<list_of_userids_you_want_to_tag>"]and/oruser-mentions-mobile=["<list_of_phonenumbers_you_want_to_tag>"].Note on distributed trainingWhen 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 oftorch.distributed.launchsets up a RANK environment variable for each process (seehere). 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.