-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
185 lines (138 loc) · 5.34 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
"""This module implements the training of RCTNet."""
import argparse
import json
from datetime import datetime
from pathlib import Path
import numpy as np
import torch
from torch.optim import Adam
from torch.utils.data import DataLoader
from dataset import RCTDataset
from RCTNet.loss import Loss
from RCTNet.model import RCTNet
def save_checkpoint(model: RCTNet, root: Path, epoch: int) -> None:
"""Save checkpoint for the model.
This method is called at the end of each training epoch in order to
save a checkpoint for the model's weights.
Args:
- model (RCTNet): The trained RCTNet model
- root (Path): Root path for the checkpoints
- epoch (int): The current epoch of training
"""
save_path = root / Path(f"epoch-{epoch}/")
save_path.mkdir()
# Save model weights
torch.save(
model.state_dict(),
save_path / "checkpoint.pt"
)
def load_checkpoint(model: RCTNet, path: Path, device: str) -> None:
"""Load model weights from checkpoint.
This method is called when the user specifies a checkpoint path to load
the model's weights.
Args:
- model (RCTNet): The trained RCTNet model
- path (Path): Path for the checkpoint
"""
return model
def main(args):
# Set path for checkpoints
root = Path(f"checkpoints/checkpoint-"
f"{datetime.today().strftime('%Y-%m-%d-%H-%M')}/")
root.mkdir(parents=True, exist_ok=True)
# Unless otherwise specified, model runs on CUDA if available
if args.device == None:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
else:
device = args.device
# Initialize dataloader
dataset = RCTDataset(args.images, args.targets)
dataloader = DataLoader(dataset, batch_size=args.batch_size, shuffle=True)
# Initialize RCT model
if args.config:
with open(args.config) as fp:
cfg = json.load(fp) # load model configurations
model = RCTNet(
in_channels=cfg["in_channels"],
hidden_dims=cfg["hidden_dims"],
c_prime=cfg["c_prime"],
epsilon=cfg["epsilon"],
c_G=cfg["c_G"],
n_G=cfg["n_G"],
c_L=cfg["c_L"],
n_L=cfg["n_L"],
grid_size=cfg["grid_size"],
device=device
)
else:
model = RCTNet(device=device)
# Move model to device selected
model = model.to(device)
# Load model's weights if checkpoint is given
if args.checkpoint:
model.load_state_dict(torch.load(
args.checkpoint, map_location=torch.device(device)))
# Initialize optimizer
optimizer = Adam(model.parameters(), lr=args.lr,
weight_decay=args.weight_decay)
# Initialize scheduler
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, T_max=10)
# Initialize loss
loss = Loss(device=device)
# Training loop
print('Training...')
losses = np.empty([])
for epoch in range(args.epochs):
l = 0.0
# Loop through batches
for i, (x, target) in enumerate(dataloader):
x = x.to(device)
target = target.to(device)
optimizer.zero_grad()
# Forward pass
enhanced = model(x)
# Calculate loss
batch_loss = loss.estimate_loss(enhanced, target)
# Backward pass
batch_loss.backward()
# Update weights
optimizer.step()
# Update scheduler
scheduler.step()
l += batch_loss.detach().cpu().item()
print(
(f"Epoch: {epoch+1}/{args.epochs}, "
f"Iter: {i+1}/{len(dataloader)}, Loss: {l/(i+1)}"),
end='\r')
# Save checkpoint
np.append(losses, l/len(dataloader))
if ((epoch+1) % args.checkpoint_interval == 0):
save_checkpoint(model=model, root=root, epoch=epoch+1)
np.save(root / "losses.npy", losses)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--images', required=True,
help='Path to the directory of images to be enhanced')
parser.add_argument(
'--targets', required=True,
help='Path to the directory of groundtruth enhanced images')
parser.add_argument('--epochs', default=500, type=int,
help='Number of epochs')
parser.add_argument('--batch_size', default=8, type=int,
help='Number of samples per minibatch')
parser.add_argument('--lr', default=5e-4, type=float,
help='Initial Learning rate of Adam optimizer')
parser.add_argument('--weight_decay', default=1e-5, type=float,
help='Weight decay of Adam optimizer')
parser.add_argument(
'--config', default=None, type=str,
help="Path to configurations file for the RCTNet model")
parser.add_argument('--checkpoint', default=None, type=str,
help='Path to previous checkpoint')
parser.add_argument('--checkpoint_interval', type=int, default=10,
help='Interval for saving checkpoints')
parser.add_argument('--device', default=None, choices=["cpu", "cuda"],
type=str, help='Device to use for training')
args = parser.parse_args()
main(args)