-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_v9.py
265 lines (219 loc) · 7.86 KB
/
train_v9.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import random
import re
import zipfile
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader, Subset, ConcatDataset, random_split
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import argparse
from transform_data import JigsawModel
class JigsawDataset(Dataset):
def __init__(self, data, labels):
self.data = data
self.labels = labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
puzzle = self.data[idx]
label = self.labels[idx]
return puzzle, label
# save checkpoint function
def checkpoint_save(model, save_path, epoch):
filename = "checkpoint-{:03d}.pth".format(epoch)
f = os.path.join(save_path, filename)
torch.save(model.state_dict(), f)
print("saved checkpoint:", f, flush=True)
# save checkpoint function
def checkpoint_load(model, save_path, epoch):
print("loading checkpoint:", epoch, flush=True)
filename = "checkpoint-{:03d}.pth".format(epoch)
f = os.path.join(save_path, filename)
model.load_state_dict(torch.load(f))
print("loaded checkpoint:", f, flush=True)
def checkpoint_delete(save_path, epoch):
filename = "checkpoint-{:03d}.pth".format(epoch)
f = os.path.join(save_path, filename)
os.remove(f)
def load_training_data(epoch, max_fold=10):
# Load the labels
labels = np.loadtxt(f"data/train/label_train.txt")
labels = torch.from_numpy(labels).long()
data = None
filename = (
"data/distance_timm_preprocessed_train.npy"
if max_fold == 0
else f"data/distance_timm_preprocessed_train_{epoch % max_fold}.npy"
)
preprocessed_data = np.load(filename)
# Convert the NumPy array to PyTorch tensors
data = torch.from_numpy(preprocessed_data).float()
print(f"loaded training data from: {filename}")
return data, labels
def train_model(
model,
optimizer,
scheduler,
criterion,
num_epochs,
max_fold,
load_checkpoint=False,
):
save_path = os.path.join(os.getcwd(), "data", "checkpoints/")
os.makedirs(save_path, exist_ok=True)
for _, _, files in os.walk(save_path):
for filename in files:
f = os.path.join(save_path, filename)
os.unlink(f)
loaded_checkpoint = -1
if load_checkpoint:
for _, _, files in os.walk(save_path):
for filename in files:
checkpoint = int(re.split("[-.]", filename)[-2])
if checkpoint > loaded_checkpoint:
loaded_checkpoint = checkpoint
checkpoint_load(model, save_path, loaded_checkpoint)
highest_accuracy = 0.9
best_epoch = -1
for epoch in range(num_epochs):
if epoch <= loaded_checkpoint:
scheduler.step()
continue
model.train()
total_loss = 0.0
correct_predictions = 0
total_predictions = 0
learning_rate = optimizer.state_dict()["param_groups"][0]["lr"]
print(f"Epoch {epoch + 1}, Learning rate: {learning_rate:.10f}", flush=True)
data, labels = load_training_data(epoch, max_fold=max_fold)
# Define the dataset and dataloader
dataset = JigsawDataset(data, labels)
print(f"dataset len: {len(dataset)}")
train_set, val_set = random_split(dataset, [0.7, 0.3])
print(f"train_set len: {len(train_set)}")
print(f"val_set len: {len(val_set)}", flush=True)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False)
for puzzle, label in train_loader:
puzzle, label = puzzle.to(device), label.to(device)
optimizer.zero_grad()
outputs = model(puzzle)
loss = criterion(outputs, label)
loss.backward()
optimizer.step()
total_loss += loss.item()
# Calculate accuracy for this batch
_, predicted = torch.max(outputs, 1)
total_predictions += label.size(0)
correct_predictions += (predicted == label).sum().item()
scheduler.step()
avg_loss = total_loss / len(train_loader)
accuracy = correct_predictions / total_predictions
print(
f"Epoch {epoch + 1}, Training Loss: {avg_loss:.4f}, Training Accuracy: {accuracy * 100:.2f}%",
flush=True,
)
# Validation for this epoch
model.eval()
with torch.no_grad():
correct_predictions = 0
total_predictions = 0
val_loss = []
for inputs, targets in val_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
batch_loss_value = loss.item()
val_loss.append(batch_loss_value)
# Calculate accuracy for this batch
_, predicted = torch.max(outputs, 1)
total_predictions += targets.size(0)
correct_predictions += (predicted == targets).sum().item()
# Print statistics
loss_value = np.mean(val_loss)
accuracy = correct_predictions / total_predictions
print(
f"Epoch {epoch + 1}, Validation Loss: {loss_value:.4f}, Validation Accuracy: {accuracy * 100:.2f}%",
flush=True,
)
if accuracy > highest_accuracy:
if best_epoch >= 0:
checkpoint_delete(save_path, best_epoch)
highest_accuracy = accuracy
best_epoch = epoch
checkpoint_save(model, save_path, epoch)
print(
f"Best epoch {best_epoch + 1}, Highest Validation Accuracy: {highest_accuracy * 100:.2f}%",
flush=True,
)
if __name__ == "__main__":
RANDOM_SEED = 193
# RANDOM_SEED = 194
# RANDOM_SEED = 1940
# initialising seed for reproducibility
torch.manual_seed(RANDOM_SEED)
torch.cuda.manual_seed(RANDOM_SEED)
seeded_generator = torch.Generator().manual_seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)
random.seed(RANDOM_SEED)
torch.backends.cudnn.deterministic = True
# Check if GPU is available
if torch.cuda.is_available():
device = torch.device("cuda") # Use GPU
else:
device = torch.device("cpu") # Use CPU
parser = argparse.ArgumentParser()
parser.add_argument(
"-e", "--epochs", type=int, help="Number of epochs", default=300
)
parser.add_argument("-b", "--batch", type=int, help="Batch size", default=16)
parser.add_argument(
"-f",
"--fold",
type=int,
help="Number of folds of data augmentation",
default=20,
)
parser.add_argument(
"-r",
"--resume",
type=int,
help="Resume from previous training checkpoint",
default=0,
)
parser.add_argument(
"-l",
"--learning_rate",
type=float,
help="Learning rate",
default=0.0001,
)
# Parse the arguments
args = parser.parse_args()
print(f"device: {device}")
num_classes = 50
batch_size = args.batch
num_epochs = args.epochs
load_checkpoint = args.resume > 0
learning_rate = args.learning_rate
max_fold = args.fold
print("epochs: ", num_epochs, "batch:", batch_size, "max_fold:", max_fold)
# Create the model
model = JigsawModel(num_classes=num_classes).to(device)
# Define the optimizer and loss function
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=182, gamma=0.1)
criterion = nn.CrossEntropyLoss()
# Train the model
train_model(
model,
optimizer,
scheduler,
criterion,
num_epochs,
max_fold,
load_checkpoint,
)