-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
30 lines (25 loc) · 825 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import random
import subprocess
import numpy as np
import psutil
import torch
def start_tensorboard(logdir: str, port: int = 6066):
host = "127.0.0.1"
try:
for conn in psutil.net_connections(kind="inet"):
if conn.laddr.port == port:
pid = conn.pid
if pid != 0:
psutil.Process(pid).terminate()
except NameError:
...
finally:
cmd = ["tensorboard", "--logdir", str(logdir), "--port", str(port), "--host", host]
with open(".tb_log.txt", "w") as f:
subprocess.Popen(cmd, stdout=f, stderr=f, shell=False)
print(f"Tensorboard has started at http://{host}:{port}")
def setup_seed(seed: int):
"""设置随机种子"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)