概要
- 何らかの理由で計算機から離れざるを得ないときに、学習の経過をグラフィカルに通知してくれるツールをこしらえた
- 動作例:
![IMG_2361.JPG](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F182818%2F763c8245-e91f-f786-a182-757902a69b0b.jpeg?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=ec7fc56fd40995a92f35e9df65580515)
環境
- マシン:Ubuntu 18.04 LTS
- Python: Python 3.6.x
通知用のモジュール
コード
import subprocess as sp
def send_image(path_to_img, message = "notification"):
line_notify_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
line_notify_api = 'https://notify-api.line.me/api/notify'
sp.getoutput(
"curl -X POST {} -H 'Authorization: Bearer {}' -F 'message={}' -F 'imageFile=@{}'".format(line_notify_api, line_notify_token, m, path_to_img))
使用法
send_image(path_to_img, message)
LINE Notify APIを利用して、引数で指定されたパスの画像と任意の文字列を送信する
-
path_to_img
(string):画像へのパス。無意味なパスを指定したり送信に失敗すると、次の引数であるmessage
のみが通知される。 -
message
(string):画像とともに送信するメッセージ
Note
line_notify_token
はLINE Notify APIのアクセストークンで、自分自身のものを取得する必要があります。アクセストークンの取得方法はこちらなどを参照してください。
使用例(擬似的なコード)
import matplotlib.pyplot as plt
def make_prog_image(loss_progress, acc_progress, path_to_img = "./notify_img.png"):
"""
lossとaccuracyの推移を二段組のグラフに描画し、指定されたパスに保存する
"""
fig, (axU, axD) = plt.subplots(2, 1)
axU.plot(loss_progress)
axU.grid(True)
axU.set_title('Loss Progress')
axD.plot(acc_progress)
axD.grid(True)
axD.set_title('Acc. Progress')
fig.savefig(path_to_img)
for epoch in range(epochs)
~ 何らかの処理 ~
loss_progress = []
acc_progress = []
for iter in range(iters):
~ 何らかの処理 ~
loss = classification_loss(train_data, target)
acc = get_accuracy()
loss_progress.append(loss)
acc_progress.append(acc)
path_to_img = "path/to/img"
make_prog_image(loss_progress, acc_progress, path_to_img)
send_img(path_to_img, message = "loss progress")
結果
![IMG_2362.JPG](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F182818%2Fa4777ccb-341a-6511-eeb1-aa583951cb1c.jpeg?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=3b21ef228000e27e247593a7dc084d51)
Seems fine