Python Script For Notifying Experimental Results Via WeChat Work.
Posted onEdited onInTipsViews: Waline: Word count in article: 7kReading time ≈6 mins.
This is an automatically translated post by LLM. The original post is in Chinese.
If you find any translation errors, please leave a comment to help me
improve the translation. Thanks!
Deep learning experiments usually take a long time to run. Without
proactive notification, two situations may arise:
Check the experiment results after a period of time, unable to focus
on other things.
Abandon the experiment, not knowing whether it is running or
completed.
To solve the above problems, it is necessary to add a monitoring
process to the experimental program, notify and send the experimental
results after the experiment ends. This article's script mainly includes
three parts: process monitoring, tensorboard data extraction, and
enterprise WeChat robot notification.
Process Monitoring
Usually, experiments are safer to run in the background, and they
will not stop because the terminal is closed. The command
to run in the background is:
1
nohup your_command >log_name.log 2>&1 &
The role of process monitoring is twofold:
Notify in time when the experiment ends abnormally.
Notify the result when the experiment ends normally.
It is relatively simple to implement. Just monitor whether the
process has ended. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import os from utils.wechat_bot import research_bot import time import datetime
whileTrue: if process_num() == 0: now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") send_report_text("The experiment is over, analyzing the results...") break time.sleep(60)
# do something
Tensorboard Data Extraction
The learning process data of deep learning is usually recorded in the
log file by tensorboard. Using tensorboard, it
is convenient to view training data, but it also reduces the convenience
of performing other operations on training data. The following script
can be used to extract the log data generated by
tensorboard and write it to an excel file.
Just call the tb_to_excel function to use it. The input
format is:
path_list: a list of strings, each item is the path of a log, such
as ['logs/0','logs/1']
excel_path: the exported excel path, such as
'export.xlsx'
one_sheet(default: False): whether to merge all data into one excel
sheet
Execution result: the log files in path_list are
exported to the target excel file, and each item in
path_list corresponds to each sheet in the excel file. If
on_sheet=True, all data will be merged into one sheet
through pd.concat.
import datetime import pandas as pd from tensorboard.backend.event_processing import event_accumulator import os from typing importDict import sys import collections
deftb_to_df(tb_data: event_accumulator.EventAccumulator): keys = tb_data.scalars.Keys() df = pd.DataFrame(columns=keys) for key in keys: data = tb_data.scalars.Items(key) df[key] = pd.Series([item.value for item in data]) if'step'notin df.columns: df['step'] = pd.Series([item.step for item in data]) if'wall_time'notin df.columns: df['wall_time'] = pd.Series([item.wall_time for item in data])
order = ['step', 'wall_time'] + keys df = df[order]
defextract_all_data(path_list:list) -> Dict[str, pd.DataFrame]: data_dict = {} for filepath in path_list: fn = filepath.split('/')[-1] if os.path.isdir(filepath): print("Reading and processing:", filepath) sheet_name = fn df = get_tb_df(filepath) data_dict[sheet_name] = df return data_dict
defdf_to_excel(df_dict, path, one_sheet=False): if one_sheet: df_all = pd.DataFrame() for df in df_dict.values(): df_all = pd.concat([df_all, df], ignore_index=True) df_all.to_excel(path, index=False) else: with pd.ExcelWriter(path) as writer: for sheet_name, df in df_dict.items(): print("Writing sheet:", sheet_name) df.to_excel(writer, sheet_name, index=False)
There are many ways to choose from for proactive notification,
including but not limited to:
SMTP sends email
Push services like Server Chan
QQ robot
WeChat robot
Enterprise WeChat application
Enterprise WeChat group robot
Each of these methods has its advantages and disadvantages. The
demand for result notification is:
Good real-time performance: the notification can be received
immediately after sending
No response: only one-way notification is required, and no reply
needs to be set up
Therefore, after weighing the above requirements and considering the
convenience of implementation, the enterprise WeChat group robot is
selected as the information push method.
First, create an enterprise WeChat group robot:
Create an enterprise WeChat group chat (>=3 people)
Kick out irrelevant people
Add a group robot
View the robot information and find the key value in
the Webhook address and record it
Then fill in the key in the following script to use
it.