-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
85 lines (67 loc) · 2.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
import argparse
import torch
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import os
from torch.utils.data import Dataset, DataLoader
from dataset import ImageDataset
from utils import generate_model_name, get_model_by_name, log_print, setup_logging
import torch.nn.functional as F
from model import CropperNet
import torch.nn as nn
def bbox_loss(pred, target):
return F.smooth_l1_loss(pred, target)
def main(dir: str, num_epochs: int, checkpoint: int, base: str):
cropper_dir = os.path.join(dir, 'cropper')
setup_logging(cropper_dir)
log_print("training started ...")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
log_print(f"using {device}")
models_dir = os.path.join(cropper_dir, 'models')
os.makedirs(models_dir, exist_ok=True)
low_res_dir = os.path.join(cropper_dir, 'input', '256p')
labels_file = os.path.join(cropper_dir, 'labels.json')
transform = transforms.Compose([
transforms.ToTensor(),
])
dataset = ImageDataset(low_res_dir, labels_file, transform=transform)
print(f"loaded {len(dataset)} images")
dataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=4)
if base:
model = get_model_by_name(device=device, directory=models_dir, name=base)
else:
model = CropperNet().to(device)
model.train()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # type: ignore
for epoch in range(num_epochs):
running_loss = 0.0
for images, bbox in dataloader:
images = images.to(device)
bbox = bbox.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, bbox)
loss.backward()
optimizer.step()
running_loss += loss.item()
avg_loss = running_loss / len(dataloader)
log_print(f"epoch [{epoch + 1}/{num_epochs}], loss: {avg_loss}")
if (epoch + 1) % checkpoint == 0:
checkpoint_name = generate_model_name(base, len(dataset), epoch + 1)
checkpoint_path = os.path.join(models_dir, f"{checkpoint_name}.pth")
torch.save(model.state_dict(), checkpoint_path)
log_print(f"model saved for {checkpoint_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Train.")
parser.add_argument("-w", "--directory", type=str, required=True, help="Working Directory.")
parser.add_argument("-e", "--epochs", type=int, required=True, help="Epochs.")
parser.add_argument("-c", "--checkpoint", type=int, required=True, help="Checkpoint.")
parser.add_argument("-b", "--base", type=str, required=False, help="Base Model.")
args = parser.parse_args()
main(
dir=args.directory,
num_epochs=args.epochs,
checkpoint=args.checkpoint,
base=args.base,
)