-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainUse.py
104 lines (89 loc) · 2.86 KB
/
TrainUse.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import torch
import random
import os
import numpy as np
from sklearn.metrics import precision_recall_fscore_support as prfs
import time
def seed_torch(seed):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
class CTime:
def __init__(self):
self.time_born = ""
self.time_start = ""
self.time_end = ""
def born(self):
self.time_born = time.time()
def start(self):
self.time_start = time.time()
def end(self):
self.time_end = time.time()
def showtime(self, delta):
# delta = end-start
delta = int(delta + 0.5)
hour = delta // 60 // 60
delta = delta - hour * 60 * 60
if hour >= 100:
hour = str(hour)
else:
hour = "0" + str(hour)
hour = hour[-2:]
minu = delta // 60
minu = "0" + str(minu)
minu = minu[-2:]
sec = delta % 60
sec = "0" + str(sec)
sec = sec[-2:]
return hour + ":" + minu + ":" + sec
def show(self, epoch, PRI_EPOCH, EPOCH):
print(
" Time: "
+ self.showtime(self.time_end - self.time_start)
+ " | "
+ self.showtime(self.time_end - self.time_born)
+ " + "
+ self.showtime((self.time_end - self.time_start) * (EPOCH - epoch - 1))
)
def calF1(preds, labels):
preds = preds.data.cpu().numpy().flatten()
labels = labels.data.cpu().numpy().flatten()
sumNum = labels.shape[0]
TP = np.sum(preds * labels) / sumNum
TN = np.sum((1 - preds) * (1 - labels)) / sumNum
FP = np.sum(preds * (1 - labels)) / sumNum
FN = np.sum((1 - preds) * labels) / sumNum
return TP, TN, FP, FN
class CDMetrics:
def __init__(self):
self.metrics = {
"loss": [],
"matrix": [],
"lr": [],
}
def set(self, cd_loss, cd_preds, labels, lr=[0.0]):
TP, TN, FP, FN = calF1(cd_preds, labels)
self.metrics["loss"].append(cd_loss.item())
self.metrics["matrix"].append([TP, TN, FP, FN])
self.metrics["lr"].append(lr[0])
def get(self):
[TP, TN, FP, FN] = np.mean(self.metrics["matrix"], axis=0)
oa = TP + TN
precision = TP / (TP + FP + 1e-10)
recall = TP / (TP + FN + 1e-10)
f1 = 2 * precision * recall / (precision + recall + 1e-10)
iou = TP / (1 - TN + 1e-10)
return {
"loss": np.mean(self.metrics["loss"]),
"lr": self.metrics["lr"][-1],
"oa": oa,
"precision": precision,
"recall": recall,
"f1": f1,
"iou": iou,
}