-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
66 lines (55 loc) · 1.98 KB
/
tools.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
import torch
import numpy as np
import math
import matplotlib.pyplot as plt
import datetime
def get_gpu_memory():
import nvidia_smi
nvidia_smi.nvmlInit()
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
print("Used GPU memory: {}%".format((info.used*100)//info.total))
nvidia_smi.nvmlShutdown()
class Logs():
def __init__(self, file_name='log/logs.txt'):
self.file_name = file_name
self.f = open(self.file_name, 'a+')
def save(self):
self.f.close()
def write(self, line):
if self.f.closed:
self.f = open(self.file_name, 'a+')
self.f.write(str(datetime.datetime.now()) + ':: ' + line + '\n')
def cal_acc(targets, output):
avg = np.sum(
np.sqrt(np.square(output[:, :, 0] - targets[:, :, 0]) + np.square(output[:, :, 1] - targets[:, :, 1]))) / \
targets.shape[1]
return avg
def get_preds(scores,img_h=224,img_w=224):
# scores = torch.Tensor.cpu(scores).detach().numpy()
scores = np.reshape(scores, (scores.shape[0], scores.shape[1], -1))
max_val, indx = np.max(scores, -1), np.argmax(scores, -1)
preds = np.zeros((scores.shape[0], scores.shape[1], 2), int)
preds[:, :, 0] = indx[:, :] % img_h
preds[:, :, 1] = indx[:, :] // img_w
return preds
def show_targets(img, targets, points, isShow=False, isSave=True):
columns = 4
rows = 5
pred = get_preds(targets)
for k in range(len(targets)):
plt.figure(figsize=(15, 15))
print(pred[k])
print(points[k])
for i in range(0, 17):
if i == 16:
plt.subplot(rows, columns, i + 1)
plt.imshow(img[k])
plt.scatter(pred[k][:, 0], pred[k][:, 1])
else:
plt.subplot(rows, columns, i + 1)
plt.imshow(targets[k][i])
if isSave:
plt.savefig('output/targets_{}.png'.format(str(k)))
if isShow:
plt.show()