-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
265 lines (222 loc) · 8.77 KB
/
train.py
File metadata and controls
265 lines (222 loc) · 8.77 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
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 sys
sys.path.append("../")
import math
import os
import matplotlib.pyplot as plt
from argparse import ArgumentParser
from tqdm import tqdm
import torch
import torch.nn as nn
import torchvision.transforms.v2 as v2
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
from models.model import NeuralNetwork
from models.loss import *
from data.IAM import IAMPages, reconstruct_image, IAMPagesSplitted
from torch.amp.grad_scaler import GradScaler
from torch.amp.autocast_mode import autocast
device = (
"cuda"
if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available() else "cpu"
)
def collate_fn(batch):
# Extract and stack tensors for lines, noLines, etc.
linesImages = torch.stack([item["lines"] for item in batch])
noLines = torch.stack([item["noLines"] for item in batch])
shapes = [item["shape"] for item in batch]
return linesImages, noLines, shapes
data_dir = "./"
def getBestModelPath():
bestModelPath = None
bestModelEpoch = -1
for model in os.listdir(
os.path.join(os.path.realpath(os.path.dirname(__file__)), "models/saved/")
):
# epoch-x.pt
modelEpoch = model[6:].split(".")[0]
modelEpoch = int(modelEpoch)
if modelEpoch > bestModelEpoch:
bestModelEpoch = modelEpoch
bestModelPath = os.path.join(
os.path.realpath(os.path.dirname(__file__)),
f"models/saved/epoch-{bestModelEpoch}.pt",
)
return bestModelPath, bestModelEpoch
def loadBestModel(
device="cuda",
) -> tuple[nn.Module, torch.optim.Optimizer, GradScaler, int]:
bestModelPath, epochs = getBestModelPath()
if bestModelPath is None:
print(
"[LineRemoverNN] [Model Loader] FATAL: No model found to load, exiting..."
)
exit(1)
checkpoint = torch.load(bestModelPath, map_location=device)
network = NeuralNetwork().to(device)
network.load_state_dict(checkpoint["model_state"])
optimizer = torch.optim.Adam(network.parameters(), lr=1e-4)
optimizer.load_state_dict(checkpoint["optimizer_state"])
# Ensure optimizer tensors are on the correct device
for state in optimizer.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.to(device)
scaler = GradScaler("cuda")
if "scaler" in checkpoint:
scaler.load_state_dict(checkpoint["scaler"])
print(
f"[LineRemoverNN] [Model Loader] Loading {bestModelPath} which has {epochs + 1} epochs"
)
return network, optimizer, scaler, epochs
def init_weights(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, nonlinearity="leaky_relu")
def save_checkpoint(model, optimizer, scaler, epoch, path):
torch.save(
{
"model_state": model.state_dict(),
"optimizer_state": optimizer.state_dict(),
"scaler": scaler.state_dict(),
"epoch": epoch,
},
path,
)
print(f'[LineRemoverNN] [Trainer] Model saved at "{path}"')
if __name__ == "__main__":
writer = SummaryWriter()
argsParser = ArgumentParser(
prog="LinerRemoverNN - Trainer", description="A trainer for the model"
)
argsParser.add_argument(
"-e",
"--epoch",
type=int,
help="The number of epoch to train the model on",
required=True,
)
argsParser.add_argument("-d", "--dataset", help="Dataset location", required=True)
argsParser.add_argument(
"-l",
"--load",
help="Load the best model already saved before (continue training)",
action="store_true",
default=False,
)
args = argsParser.parse_args()
data_dir = args.dataset
# Get all needed directories paths
pages_dir = os.path.join(data_dir, "generated-pages")
nolines_dir = os.path.join(data_dir, "generated-nolines-pages")
json_dir = os.path.join(data_dir, "generated-words")
pages_blocks_dir = os.path.join(data_dir, "generated-pages-blocks")
nolines_blocks_dir = os.path.join(data_dir, "generated-nolines-pages-blocks")
# Init network, dataset, dataloader, loss, and optimizer
network = NeuralNetwork().to(device)
dataset = IAMPagesSplitted(
pages_blocks_dir,
nolines_blocks_dir,
"",
readJson=False,
transform=transforms.Compose(
[
# Instead of random rotation + random perspective : Random affine https://pytorch.org/vision/main/auto_examples/transforms/plot_transforms_illustrations.html#randomaffine
# v2.RandomRotation(degrees=(0, 180)),
# v2.RandomCrop(size=(128, 128)),
# v2.RandomPerspective(distortion_scale=0.6, p=0.75),
# v2.Resize(size=(512, 512)),
v2.RandomResizedCrop(size=(512, 512), scale=(0.6, 1.0)),
v2.ToDtype(torch.float32, scale=True),
]
),
sameTransform=True,
)
dataloader = DataLoader(
dataset,
batch_size=10,
shuffle=True,
collate_fn=collate_fn,
num_workers=2,
pin_memory=True,
)
optimizer = torch.optim.Adam(network.parameters(), lr=0.0001)
epochs = args.epoch
loss = combined_loss
startingEpoch = 0
network.apply(init_weights)
# Create saved folder if not already created
if not os.path.exists(os.path.join("./models", "saved")):
os.mkdir(os.path.join("./models", "saved"))
# Training...
scaler = GradScaler(device)
# Load model if specified
if args.load:
# Getting the bestModel
network, optimizer, scaler, startingEpoch = loadBestModel()
startingEpoch += 1 # We want to start the next epoch
network.to(device)
print(
"[LineRemoverNN] [Trainer] Starting training... for",
epochs - startingEpoch,
"epochs",
)
torch.backends.cudnn.benchmark = True # For speed improvement
for epoch in range(startingEpoch, epochs):
torch.cuda.empty_cache()
network.train()
totalLoss = 0
for batch in tqdm(dataloader, desc=f"Epoch : {epoch}"):
# Executing
linesImages, noLines, shapes = batch
linesImages, noLines = linesImages.to(device), noLines.to(device)
optimizer.zero_grad()
with autocast(device_type=device):
processedFilters = network(linesImages)
# Loss outside autocast to avoid issues with some loss functions
# Compute loss between the subtracted image and the target (noLines)
pixelLoss = loss(processedFilters.float(), noLines.float())
# Back prog
scaler.scale(pixelLoss).backward()
scaler.step(optimizer)
scaler.update()
# pixelLoss.backward()
# optimizer.step()
totalLoss += pixelLoss.item()
avgLoss = totalLoss / len(dataloader)
print(f"[LineRemoverNN] [Trainer] Epoch : {epoch}, Average Loss : {avgLoss}")
print(f"[LineRemoverNN] [Trainer] Saving model...")
writer.add_scalar("Loss/train", avgLoss, epoch)
# --- Logging 5 sample output images to TensorBoard ---
# Switch to evaluation mode to avoid randomness (like dropout or augmentation)
network.eval()
with torch.no_grad():
# Get a sample batch from the dataloader (you might want to use a validation set)
sample_batch = next(iter(dataloader))
sample_linesImages, sample_noLines, sample_shapes = sample_batch
sample_linesImages = sample_linesImages.to(device)
sample_outputs = network(sample_linesImages)
# Choose the first 5 outputs
# If your images are in [N, C, H, W] format, ensure they're in the [0, 1] range.
# Here we assume they are. Otherwise, you might need to apply a normalization.
writer.add_images(
"Processed Outputs",
torch.clamp(sample_outputs[:5], 0, 1),
epoch,
dataformats="NCHW",
)
# Save each epoch
save_checkpoint(
network,
optimizer,
scaler,
epoch,
os.path.join(
os.path.realpath(os.path.dirname(__file__)),
f"models/saved/epoch-{epoch}.pt",
),
)
print(
f'[LineremoverNN] [Trainer] Model saved at "./models/saved/epoch-{epoch}.pt"'
)
writer.flush()