forked from jarrycyx/UNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
184 lines (151 loc) · 7.31 KB
/
train.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
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
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
import shutil
import os
def train_CUTS(model, mask, optimizer, criterion, train_loader, val_loader, device, save_path, n_epochs, summary_writer=None):
model.to(device)
val_losses = []
min_loss = np.inf
mask = torch.Tensor(mask).to(device)
for epoch in range(n_epochs):
model.train()
train_losses = []
i = 0
for seq, target in train_loader:
seq = torch.Tensor(seq).to(device)
target = torch.Tensor(target).squeeze().to(device)
print(seq.shape, target.shape)
masks = mask.to(device)
masks = masks.unsqueeze(0)
masks = masks.repeat(seq.shape[0], 1, 1)
output = model(seq, masks).to(device)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % 10 == 0:
# print(f"Epoch {epoch+1}/{n_epochs}, Batch {i+1}/{len(train_loader)}, Loss: {loss.item():.6f}")
summary_writer.add_scalar('train_loss', loss.item(), epoch*len(train_loader)+i)
i += 1
train_losses.append(loss.item())
model.eval()
with torch.no_grad():
for seq, target in val_loader:
seq = torch.Tensor(seq).to(device)
target = torch.Tensor(target).squeeze().to(device)
masks = mask.to(device)
masks = masks.unsqueeze(0)
masks = masks.repeat(seq.shape[0], 1, 1)
output = model(seq, masks).to(device)
loss = criterion(output, target)
val_losses.append(loss.item())
if loss.item() < min_loss:
min_loss = loss.item()
# 清空文件夹
shutil.rmtree(save_path, ignore_errors=True)
torch.save(model.state_dict(), save_path + f'epoch_{epoch+1}_loss_{loss.item()}.pth')
print(f'Epoch {epoch+1}/{n_epochs}, Train Loss: {np.mean(train_losses)}')
print(f'Epoch {epoch+1}/{n_epochs}, Val Loss: {np.mean(val_losses)}')
summary_writer.add_scalar('train_loss_epoch', np.mean(train_losses), epoch)
summary_writer.add_scalar('val_loss', np.mean(val_losses), epoch)
summary_writer.flush()
torch.save(model.state_dict(), save_path + f'epoch_{epoch+1}_loss_{loss.item()}.pth')
return model
def train(model, optimizer, criterion, train_loader, val_loader, device, save_path, n_epochs, summary_writer=None):
model.to(device)
# check path
if not os.path.exists(save_path + 'model'):
os.makedirs(save_path + 'model')
val_losses = []
min_loss = np.inf
for epoch in range(n_epochs):
model.train()
train_losses = []
i = 0
for seq, target in train_loader:
seq = torch.Tensor(seq).to(device)
target = torch.Tensor(target).squeeze().to(device)
output = model(seq).to(device)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % 10 == 0:
# print(f"Epoch {epoch+1}/{n_epochs}, Batch {i+1}/{len(train_loader)}, Loss: {loss.item():.6f}")
summary_writer.add_scalar('train_loss', loss.item(), epoch*len(train_loader)+i)
i += 1
train_losses.append(loss.item())
model.eval()
with torch.no_grad():
for seq, target in val_loader:
seq = torch.Tensor(seq)
target = torch.Tensor(target).squeeze().to(device)
output = model(seq)
loss = criterion(output, target)
val_losses.append(loss.item())
if loss.item() < min_loss:
min_loss = loss.item()
shutil.rmtree(save_path + 'model/', ignore_errors=True)
if not os.path.exists(save_path + 'model'):
os.makedirs(save_path + 'model')
torch.save(model.state_dict(), save_path + f'model/epoch_{epoch+1}_loss_{loss.item()}.pth')
print(f'Epoch {epoch+1}/{n_epochs}, Train Loss: {np.mean(train_losses)}')
print(f'Epoch {epoch+1}/{n_epochs}, Val Loss: {np.mean(val_losses)}')
summary_writer.add_scalar('train_loss_epoch', np.mean(train_losses), epoch)
summary_writer.add_scalar('val_loss', np.mean(val_losses), epoch)
summary_writer.flush()
torch.save(model.state_dict(), save_path + f'epoch_{epoch+1}_loss_{loss.item()}.pth')
return model
def stable_train(model, optimizer, criterion, train_loader, val_loader, device, save_path, n_epochs, summary_writer=None):
model.to(device)
if not os.path.exists(save_path + 'model'):
os.makedirs(save_path + 'model')
val_losses = []
min_loss = np.inf
for epoch in range(n_epochs):
model.train()
train_losses = []
i = 0
for seq, target in train_loader:
seq = torch.Tensor(seq).to(device)
target = torch.Tensor(target).squeeze().to(device)
output = model(seq, target)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % 10 == 0:
print(f"Epoch {epoch+1}/{n_epochs}, Batch {i+1}/{len(train_loader)}, Loss: {loss.item():.6f}")
summary_writer.add_scalar('train_loss', loss.item(), epoch*len(train_loader)+i)
i += 1
train_losses.append(loss.item())
model.eval()
with torch.no_grad():
for seq, target in val_loader:
seq = torch.Tensor(seq)
target = torch.Tensor(target).squeeze().to(device)
output = model(seq, target)
loss = criterion(output, target)
val_losses.append(loss.item())
if loss.item() < min_loss:
min_loss = loss.item()
shutil.rmtree(save_path + 'model/', ignore_errors=True)
if not os.path.exists(save_path + 'model'):
os.makedirs(save_path + 'model')
torch.save(model.state_dict(), save_path + f'model/epoch_{epoch+1}_loss_{loss.item()}.pth')
print(f'Epoch {epoch+1}/{n_epochs}, Train Loss: {np.mean(train_losses)}')
print(f'Epoch {epoch+1}/{n_epochs}, Val Loss: {np.mean(val_losses)}')
summary_writer.add_scalar('train_loss_epoch', np.mean(train_losses), epoch)
summary_writer.add_scalar('val_loss', np.mean(val_losses), epoch)
summary_writer.flush()
torch.save(model.state_dict(), save_path + f'epoch_{epoch+1}_loss_{loss.item()}.pth')
return model
def train_using_residual(model, optimizer, criterion, train_loader, val_loader, device, save_path, n_epochs, summary_writer=None):
full_model = model.full_model
masked_model = model.masked_model
full_model.to(device)
masked_model.to(device)
train(full_model, optimizer, criterion, train_loader, val_loader, device, save_path + 'full/', n_epochs, summary_writer)
train(masked_model, optimizer, criterion, train_loader, val_loader, device, save_path + 'masked/', n_epochs, summary_writer)
return model