Last active
July 7, 2019 15:52
-
-
Save zszazi/28cdb3e7486ef5f5bfd719b1337283be to your computer and use it in GitHub Desktop.
Fastai mobile push notifications using callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotificationCallback(Callback): | |
""" | |
PyTorch callback for model training | |
""" | |
def on_train_begin(self, metrics_names: StrList, **kwargs: Any) -> None: | |
send_notification("Training has Begun ") | |
def on_epoch_end(self, epoch: int, smooth_loss: Tensor, last_metrics: MetricsList, **kwargs: Any) -> None: | |
super().on_epoch_end(**kwargs) | |
val_loss, accuracy = last_metrics[0], last_metrics[1] | |
message = "epoch: " +str(epoch+1) + ": " +"train loss: " +f"{smooth_loss.item():.4f}" + " , " +" val loss: "+ f"{val_loss:.4f}" + " , " + " val accuracy: " +f"{accuracy:.4f}" | |
send_notification(message) | |
def on_train_end(self, metrics, **kwargs: Any) -> None: | |
send_notification("Training finished ") | |
def send_notification(msg): | |
""" | |
Send message to mobile using Pushover notifications. | |
Calls Pushover API to do that. | |
Pushover API docs: https://pushover.net/api | |
""" | |
import requests | |
from datetime import datetime | |
url = "https://api.pushover.net/1/messages.json" | |
data = { | |
"user" : "SECRET", | |
"token" : "SECRET", | |
"sound" : "cashregister" #head on to pushover docs to get list of many sounds on notif | |
} | |
data["message"] = msg | |
data['message'] = data['message'] + "\n" + str(datetime.now()) | |
r = requests.post(url = url, data = data) | |
notif_cb = NotificationCallback() | |
learn = cnn_learner(data, models.resnet50, metrics=[accuracy,error_rate] , model_dir="/tmp/model/",callbacks = [notif_cb]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment