-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
205 lines (161 loc) · 6.46 KB
/
utils.py
File metadata and controls
205 lines (161 loc) · 6.46 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import logging
import os
import random
import sys
import torch
import numpy as np
from tqdm import tqdm
from torch.utils.data.sampler import SubsetRandomSampler
from Mydataset import Mydataset
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
def clf_m(predictions, labels, k=5.0):
diff = torch.mean(torch.abs(predictions - labels))
lossout = torch.mean(torch.where(diff <= k, (0.000001 * diff), (diff ** 3) + 0.1))
return (lossout)
def metric(predictions, labels, k=5.0):
diff = torch.abs(predictions - labels)
return diff <= k
def train_one_epoch(model, optimizer, data_loader, device, epoch, loss_function):
model.train()
accu_loss = torch.zeros(1).to(device) # 累计损失
accu_num = torch.zeros(1).to(device) # 累计预测正确的样本数
optimizer.zero_grad()
sample_num = 0
data_loader = tqdm(data_loader, file=sys.stdout)
for step, data in enumerate(data_loader):
images, labels = data
sample_num += images.shape[0]
pred = model(images.to(device))
accu_num += metric(pred, labels.to(device)).sum().item()
loss = loss_function(pred, labels.to(device))
loss.backward()
accu_loss += loss.item()
data_loader.desc = "[train epoch {}] loss: {:.3f}, acc: {:.3f}".format(epoch,
accu_loss.item() / (step + 1),
accu_num.item() / sample_num)
if not torch.isfinite(loss):
logging.info('WARNING: non-finite loss, ending training ', loss)
sys.exit(1)
optimizer.step()
optimizer.zero_grad()
return accu_loss.item() / (step + 1), accu_num.item() / sample_num
@torch.no_grad()
def evaluate(model, data_loader, device, epoch, loss_function):
model.eval()
accu_num = torch.zeros(1).to(device) # 累计预测正确的样本数
accu_loss = torch.zeros(1).to(device) # 累计损失
sample_num = 0
data_loader = tqdm(data_loader, file=sys.stdout)
for step, data in enumerate(data_loader):
images, labels = data
sample_num += images.shape[0]
pred = model(images.to(device))
accu_num += metric(pred, labels.to(device)).sum()
loss = loss_function(pred, labels.to(device))
accu_loss += loss
data_loader.desc = "[valid epoch {}] loss: {:.3f}, acc: {:.3f}".format(epoch,
accu_loss.item() / (step + 1),
accu_num.item() / sample_num)
return accu_loss.item() / (step + 1), accu_num.item() / sample_num
def model_onnx(model, model_name: str):
"""
visualize the network architecture through onnx
Args:
model: the network. eg resnet18
model_name: the name of network.
"""
print(f"make {model_name}'s onnx file")
x = torch.zeros(1, 3, 224, 224)
torch.onnx.export(model, x, model_name)
print("finished!")
def setup_seed(seed):
"""
set random seeds
Args:
seed: seed
"""
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def split_dataset(data_path,split_proption=0.2,shuffle_or_not=True):
"""
split the datasets to train_datasets and val_datasets
Args:
data_path: the path to datasets
split_proption: the proption of train and val
shuffle_or_not:
"""
data_npy = np.load(data_path,allow_pickle=True)
# set random seed
random_seed = 1234
# create data indices for trainning and testing splits
dataset_size = len(data_npy)
indices = list(range(dataset_size))
# count out split size
split = int(np.floor(split_proption * dataset_size))
# split the train_indices and val_indices
if shuffle_or_not:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_indices, val_indices = indices[split:],indices[:split]
print(f"the length of train_datasets is {len(train_indices)} and the length of val_datasets is {len(val_indices)}")
train_sampler = SubsetRandomSampler(train_indices)
val_sampler = SubsetRandomSampler(val_indices)
return train_sampler, val_sampler
def getLogger(path):
"""
making a logger object to records.
Args:
path: the way log_file locates
"""
logger = logging.getLogger()
logger.setLevel(logging.INFO)
rf_handler = logging.FileHandler(path)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s')
rf_handler.setFormatter(formatter)
logger.addHandler(rf_handler)
sh_handler = logging.StreamHandler()
sh_handler.setFormatter(formatter)
logger.addHandler(sh_handler)
return logger
def plt_res(path:str,train_losses:list, train_accuracys:list, eval_losses:list, eval_accuracys:list,learning_rates:list)->None:
"""
plots the result of every experiment.
Args:
path: the way to store the result pictures location.
train_losses: the list contains all train_loss for this experiment.
train_accuracys: the list contains all train_accuracys for this experiment.
eval_losses: the list contains all eval_losses for this experiment.
eval_accuracys: the list contains all eval_accuracys for this experiment.
"""
epochs = range(1,len(train_losses) + 1)
plt.figure(figsize=(18, 6))
plt.subplot(1, 3, 1)
plt.plot(epochs, train_losses, 'b-', label='Training Loss')
plt.plot(epochs, eval_losses, 'r-', label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.subplot(1, 3, 2)
plt.plot(epochs, train_accuracys, 'b-', label='Training Accuracy')
plt.plot(epochs, eval_accuracys, 'r-', label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 3, 3)
plt.plot(epochs, learning_rates, 'b-', label='Learning Rate')
plt.title('Learning Rate Curve')
plt.xlabel('Epochs')
plt.ylabel('learning_rates')
plt.legend()
if not os.path.exists(path):
os.makedirs(path)
filename = "training_plot.png"
save_path = os.path.join(path,filename)
plt.tight_layout()
plt.savefig(save_path)