-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtrain.py
208 lines (173 loc) · 6.11 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
import sys, os
import numpy as np
import torch
from torch import nn
import torch.optim as optim
from torch.optim import lr_scheduler
import time
from time import perf_counter
import pickle
from model.config import load_config
from model.genconvit_ed import GenConViTED
from model.genconvit_vae import GenConViTVAE
from dataset.loader import load_data, load_checkpoint
import optparse
config = load_config()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def load_pretrained(pretrained_model_filename):
assert os.path.isfile(
pretrained_model_filename
), "Saved model file does not exist. Exiting."
model, optimizer, start_epoch, min_loss = load_checkpoint(
model, optimizer, filename=pretrained_model_filename
)
# now individually transfer the optimizer parts...
for state in optimizer.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.to(device)
return model, optimizer, start_epoch, min_loss
def train_model(
dir_path, mod, num_epochs, pretrained_model_filename, test_model, batch_size
):
print("Loading data...")
dataloaders, dataset_sizes = load_data(dir_path, batch_size)
print("Done.")
if mod == "ed":
from train.train_ed import train, valid
model = GenConViTED(config)
else:
from train.train_vae import train, valid
model = GenConViTVAE(config)
optimizer = optim.Adam(
model.parameters(),
lr=float(config["learning_rate"]),
weight_decay=float(config["weight_decay"]),
)
criterion = nn.CrossEntropyLoss()
criterion.to(device)
mse = nn.MSELoss()
min_val_loss = int(config["min_val_loss"])
scheduler = lr_scheduler.StepLR(optimizer, step_size=15, gamma=0.1)
if pretrained_model_filename:
model, optimizer, start_epoch, min_loss = load_pretrained(
pretrained_model_filename
)
model.to(device)
torch.manual_seed(1)
train_loss, train_acc, valid_loss, valid_acc = [], [], [], []
since = time.time()
for epoch in range(0, num_epochs):
train_loss, train_acc, epoch_loss = train(
model,
device,
dataloaders["train"],
criterion,
optimizer,
epoch,
train_loss,
train_acc,
mse,
)
valid_loss, valid_acc = valid(
model,
device,
dataloaders["validation"],
criterion,
epoch,
valid_loss,
valid_acc,
mse,
)
scheduler.step()
time_elapsed = time.time() - since
print(
"Training complete in {:.0f}m {:.0f}s".format(
time_elapsed // 60, time_elapsed % 60
)
)
print("\nSaving model...\n")
file_path = os.path.join(
"weight",
f'genconvit_{mod}_{time.strftime("%b_%d_%Y_%H_%M_%S", time.localtime())}',
)
with open(f"{file_path}.pkl", "wb") as f:
pickle.dump([train_loss, train_acc, valid_loss, valid_acc], f)
state = {
"epoch": num_epochs + 1,
"state_dict": model.state_dict(),
"optimizer": optimizer.state_dict(),
"min_loss": epoch_loss,
}
weight = f"{file_path}.pth"
torch.save(state, weight)
print("Done.")
if test_model:
test(model, dataloaders, dataset_sizes, mod, weight)
def test(model, dataloaders, dataset_sizes, mod, weight):
print("\nRunning test...\n")
model.eval()
checkpoint = torch.load(weight, map_location="cpu")
model.load_state_dict(checkpoint["state_dict"])
_ = model.eval()
Sum = 0
counter = 0
for inputs, labels in dataloaders["test"]:
inputs = inputs.to(device)
labels = labels.to(device)
if mod == "ed":
output = model(inputs).to(device).float()
else:
output = model(inputs)[0].to(device).float()
_, prediction = torch.max(output, 1)
pred_label = labels[prediction]
pred_label = pred_label.detach().cpu().numpy()
main_label = labels.detach().cpu().numpy()
bool_list = list(map(lambda x, y: x == y, pred_label, main_label))
Sum += sum(np.array(bool_list) * 1)
counter += 1
print(f"Pediction: {Sum}/{len(inputs)*counter}")
print(
f'Prediction: {Sum}/{dataset_sizes["test"]} {(Sum / dataset_sizes["test"]) * 100:.2f}%'
)
def gen_parser():
parser = optparse.OptionParser("Train GenConViT model.")
parser.add_option(
"-e",
"--epoch",
type=int,
dest="epoch",
help="Number of epochs used for training the GenConvNextViT model.",
)
parser.add_option("-v", "--version", dest="version", help="Version 0.1.")
parser.add_option("-d", "--dir", dest="dir", help="Training data path.")
parser.add_option(
"-m",
"--model",
dest="model",
help="model ed or model vae, model variant: genconvit (A) ed or genconvit (B) vae.",
)
parser.add_option(
"-p",
"--pretrained",
dest="pretrained",
help="Saved model file name. If you want to continue from the previous trained model.",
)
parser.add_option("-t", "--test", dest="test", help="run test on test dataset.")
parser.add_option("-b", "--batch_size", dest="batch_size", help="batch size.")
(options, _) = parser.parse_args()
dir_path = options.dir
epoch = options.epoch
mod = "ed" if options.model == "ed" else "vae"
test_model = "y" if options.test else None
pretrained_model_filename = options.pretrained if options.pretrained else None
batch_size = options.batch_size if options.batch_size else config["batch_size"]
return dir_path, mod, epoch, pretrained_model_filename, test_model, int(batch_size)
def main():
start_time = perf_counter()
path, mod, epoch, pretrained_model_filename, test_model, batch_size = gen_parser()
train_model(path, mod, epoch, pretrained_model_filename, test_model, batch_size)
end_time = perf_counter()
print("\n\n--- %s seconds ---" % (end_time - start_time))
if __name__ == "__main__":
main()