-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (58 loc) · 1.84 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from visdom import Visdom
import socket
import numpy as np
import os
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class VisdomLinePlotter(object):
"""Plots to Visdom"""
def __init__(self, env_name='main'):
self.viz = Visdom()
self.env = env_name
self.plots = {}
def plot(self, var_name, split_name, title_name, x, y):
if var_name not in self.plots:
self.plots[var_name] = self.viz.line(X=np.array([x,x]), Y=np.array([y,y]), env=self.env, opts=dict(
legend=[split_name],
title=title_name,
xlabel='Epochs',
ylabel=var_name
))
else:
self.viz.line(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name, update = 'append')
def get_avail_gpu():
'''
works for linux
'''
result = os.popen("nvidia-smi").readlines()
try:
# get Processes Line
for i in range(len(result)):
if 'Processes' in result[i]:
process_idx = i
# get # of gpus
num_gpu = 0
for i in range(process_idx+1):
if 'MiB' in result[i]:
num_gpu += 1
gpu_list = list(range(num_gpu))
# dedect which one is busy
for i in range(process_idx, len(result)):
if result[i][22] == 'C':
gpu_list.remove(int(result[i][5]))
return (gpu_list[0])
except:
print('no gpu available, return 0')
return 0