LoginSignup
2
0

More than 5 years have passed since last update.

watchdogとDropboxでChainerの学習をどこでも監視

Posted at

chaineruiとか便利なツールが充実してきたけど、もっと手軽に学習過程をDropboxから監視したかった(ソースコード)。

動作環境

  • Ubuntu 16.04.3 LTS
  • Python 3.5.2
  • chainer 3.2
  • watchdog 0.8.3

コード

watchdogextensions.PlotReport()等から出力されるPNGデータを監視して、任意のフォルダにコピーするだけ。

png_monitoring.py
import os
import time
import shutil
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

class ChangeHandler(FileSystemEventHandler):

    def __init__(self, copy):
        self.copy = copy

    def on_modified(self, event):
        filepath1 = event.src_path
        filename1 = os.path.basename(filepath1)

        name, ext = os.path.splitext(filename1)
        if('png' in ext):
            time.sleep(1)
            filepath2 = os.path.join(self.copy, filename1)
            shutil.copy2(filepath1, filepath2)

def main(monitor, copy):
    while 1:
        event_handler = ChangeHandler(copy)
        observer = Observer()
        observer.schedule(event_handler, monitor, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()

        observer.join()

使い方

trainer.run()によってPNGファイルが保存されるフォルダと、それが更新されたときにコピーしたいフォルダを設定する。どちらのフォルダも事前に作成しておくこと。
以下の例ではPNGファイルはresultフォルダ以下(サブフォルダでも追跡可能)に保存され、Dropboxに作成しておいたtmpフォルダにコピーする。

$ ./Tools/png_monitoring.py ./result/ ~/Dropbox/tmp/

Dropboxが開ける環境であればどこでも進捗を確認できて便利。監視をやめるときはCtrl-cを連打(もう少しスマートに終了したいが…)。
iPhoneの場合、以下のように学習が進捗すると更新が表示されるのでそれをタップすれば画面が最新に切り替わる。

capture.jpg

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0