-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
224 lines (188 loc) · 7.9 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import argparse
import os
import pandas as pd
import numpy as np
from sklearn.metrics import f1_score
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from read_file import read_csv
from dataloader import (
get_loader,
LOSO_sequence_generate
)
from models.FMER import FMER
def train(epochs: int, criterion: nn.Module, optimizer: torch.optim,
model: nn.Module, scheduler: torch.optim.lr_scheduler, train_loader: DataLoader,
device: torch.device, model_best_name: str):
"""Train the model
Parameters
----------
epochs : int
Epochs for training the model
model : DSSN
Model to be trained
train_loader : DataLoader
DataLoader to load in the data
device: torch.device
Device to be trained on
model_best_name: str
Name of the weight file to be saved
"""
best_accuracy = -1
# Set model in training mode
model.train()
for epoch in range(epochs):
train_loss = 0.0
train_accuracy = 0.0
for patches, labels in train_loader:
patches = patches.to(device)
labels = labels.to(device)
output = model(patches)
# Compute the loss
loss = criterion(output, labels)
train_loss += loss.item()
# Update the parameters
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Compute the accuracy
prediction = (output.argmax(-1) == labels)
train_accuracy += prediction.sum().item() / labels.size(0)
if scheduler is not None:
scheduler.step()
train_loss /= len(train_loader)
train_accuracy /= len(train_loader)
print(f"Epoch: {epoch + 1}")
print(f"Loss: {train_loss}")
print(f"Accuracy: {train_accuracy}")
if train_accuracy > best_accuracy:
torch.save(model.state_dict(), model_best_name)
best_accuracy = train_accuracy
print("Save model")
def evaluate(test_loader: DataLoader, model: nn.Module, device: torch.device):
# Set into evaluation mode
model.eval()
test_accuracy = 0.0
test_f1_score = 0.0
with torch.no_grad():
for patches, labels in test_loader:
# Move data to device and compute the output
patches = patches.to(device)
labels = labels.to(device)
output = model(patches)
# Compute the accuracy
prediction = (output.argmax(-1) == labels)
test_accuracy += prediction.sum().item() / labels.size(0)
test_f1_score += f1_score(labels.cpu().numpy(), output.argmax(-1).cpu().numpy(),
average="weighted")
return test_accuracy / len(test_loader), test_f1_score / len(test_loader)
def LOSO_train(data: pd.DataFrame, sub_column: str, args,
label_mapping: dict, device: torch.device):
log_file = open("train.log", "w")
# Create different DataFrame for each subject
train_list, test_list = LOSO_sequence_generate(data, sub_column)
test_accuracy = 0.0
test_f1_score = 0.0
for idx in range(len(train_list)):
npz_file = np.load(f"{args.npz_file}/{idx}.npz")
adj_matrix = torch.FloatTensor(npz_file["adj_matrix"]).to(device)
print(f"=================LOSO {idx + 1}=====================")
train_csv = train_list[idx]
test_csv = test_list[idx]
# Create dataset and dataloader
_, train_loader = get_loader(csv_file=train_csv,
image_root=args.image_root,
label_mapping=label_mapping,
batch_size=args.batch_size,
device=device,
catego=args.catego)
_, test_loader = get_loader(csv_file=test_csv,
image_root=args.image_root,
label_mapping=label_mapping,
batch_size=len(test_csv),
device=device,
catego=args.catego,
train=False)
# Read in the model
model = FMER(adj_matrix=adj_matrix,
num_classes=args.num_classes,
device=device).to(device)
# Create criterion and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),
lr=args.learning_rate)
# Train the data
train(epochs=args.epochs,
criterion=criterion,
optimizer=optimizer,
scheduler=None,
model=model,
train_loader=train_loader,
device=device,
model_best_name=f"{args.weight_save_path}/model_best_{idx}.pt")
model.load_state_dict(torch.load(f"{args.weight_save_path}/model_best_{idx}.pt",
map_location=device))
temp_test_accuracy, temp_f1_score = evaluate(test_loader=test_loader,
model=model,
device=device)
print(f"In LOSO {idx + 1}, test accuracy: {temp_test_accuracy:.4f}, f1-score: {temp_f1_score:.4f}")
log_file.write(f"LOSO {idx + 1}: Accuracy: {temp_test_accuracy:.4f}, F1-Score: {temp_f1_score:.4f}\n")
test_accuracy += temp_test_accuracy
test_f1_score += temp_f1_score
print(f"LOSO accuracy: {test_accuracy / len(train_list):.4f}, f1-score: {test_f1_score / len(train_list):.4f}")
log_file.write(
f"Total: Accuracy {test_accuracy / len(train_list):.4f}, F1-Score: {test_f1_score / len(train_list):.4f}\n")
log_file.close()
if __name__ == "__main__":
# Argument parse
parser = argparse.ArgumentParser()
parser.add_argument("--csv_path",
type=str,
required=True,
help="Path for the csv file for training data")
parser.add_argument("--image_root",
type=str,
required=True,
help="Root for the training images")
parser.add_argument("--npz_file",
type=str,
required=True,
help="Place for the npz file")
parser.add_argument("--catego",
type=str,
required=True,
help="SAMM or CASME dataset")
parser.add_argument("--num_classes",
type=int,
default=5,
help="Classes to be trained")
parser.add_argument("--batch_size",
type=int,
default=32,
help="Training batch size")
parser.add_argument("--weight_save_path",
type=str,
default="model",
help="Path for the saving weight")
parser.add_argument("--epochs",
type=int,
default=15,
help="Epochs for training the model")
parser.add_argument("--learning_rate",
type=float,
default=1e-4,
help="Learning rate for training the model")
args = parser.parse_args()
# Training device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Read in the data
data, label_mapping = read_csv(args.csv_path)
# Create folders for the saving weight
os.makedirs(args.weight_save_path, exist_ok=True)
# Train the model
LOSO_train(data=data,
sub_column="Subject",
label_mapping=label_mapping,
args=args,
device=device)