-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
140 lines (104 loc) · 4.14 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
import torch
import config
import utils
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from tqdm import tqdm
from models import CACNet
from dataset import KUPCPDataset, FCDBDataset
from torch.utils.data import DataLoader
import warnings
warnings.filterwarnings('ignore')
def train_epoch(fcdb_loader, kupcp_loader, model, cropper_loss_fn, classifier_loss_fn, optimizer, epoch, lr_scheduler=None):
model.train()
loader = tqdm(fcdb_loader)
cropper_losses = []
classifier_losses = []
classifier_accuracies = []
for boxes, images, width, height in loader:
# Train Cropper Branch
boxes = boxes.to(config.DEVICE).squeeze(1)
images = images.to(config.DEVICE)
height = height.to(config.DEVICE)
width = width.to(config.DEVICE)
boxes[:, 0::2] = boxes[:, 0::2] / width[:, None] * images.shape[-1]
boxes[:, 1::2] = boxes[:, 1::2] / height[:, None] * images.shape[-2]
model.classifier.eval()
_, _, target_boxes = model(images)
cropper_loss = cropper_loss_fn(target_boxes, boxes)
optimizer.zero_grad()
cropper_loss.backward()
cropper_losses.append(cropper_loss.item())
# Train Classifier Branch
labels, images = next(iter(kupcp_loader))
labels = labels.to(config.DEVICE)
images = images.to(config.DEVICE)
model.classifier.train()
scores, _, _ = model(images)
_, predictions = torch.max(scores, dim=1)
classifier_accuracy = torch.mean((predictions == labels).float()) * 100
classifier_loss = classifier_loss_fn(scores, labels)
classifier_loss.backward()
optimizer.step()
if lr_scheduler is not None:
lr_scheduler.step()
loader.set_postfix(cropper_loss=cropper_loss.item(), classifier_loss=classifier_loss.item(), classifier_accuracy=classifier_accuracy.item())
classifier_losses.append(classifier_loss.item())
classifier_accuracies.append(classifier_accuracy.item())
if config.SAVE_MODEL:
utils.save_checkpoint({
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
'cropper_losses': np.mean(cropper_losses),
'classifier_losses': np.mean(classifier_losses)
})
print(f'Epoch[{epoch}] => Classifier Loss [{np.mean(classifier_losses):.2f}], Cropper Loss [{np.mean(cropper_losses):.2f}], Classifier Accuracy [{np.mean(classifier_accuracies):.2f}]')
if __name__ == '__main__':
fcdb_dataset = FCDBDataset(
root=config.DATASETS['FCDB']['DATASET'],
annotations=config.DATASETS['FCDB']['ANNOTATIONS']['TRAIN'],
transform=config.DATASETS['FCDB']['TRANSFORMS'],
augmentation=True,
)
fcdb_loader = DataLoader(
dataset=fcdb_dataset,
batch_size=config.BATCH_SIZE,
pin_memory=config.PIN_MEMORY,
drop_last=False,
shuffle=True,
)
kupcp_dataset = KUPCPDataset(
root=config.DATASETS['KUPCP']['TRAIN'],
root_labels=config.DATASETS['KUPCP']['LABELS']['TRAIN'],
transform=config.DATASETS['KUPCP']['TRANSFORMS']['TRAIN'],
)
kupcp_loader = DataLoader(
dataset=kupcp_dataset,
batch_size=config.BATCH_SIZE,
pin_memory=config.PIN_MEMORY,
drop_last=False,
shuffle=True,
)
model = CACNet()
model = model.to(config.DEVICE)
cropper_loss_fn = nn.SmoothL1Loss()
classifier_loss_fn = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=config.LEARNING_RATE, weight_decay=config.WEIGHT_DECAY)
lr_scheduler = lr_scheduler.MultiStepLR(optimizer=optimizer, milestones=config.LR_DECAY_EPOCH, gamma=config.LR_DECAY)
starting_epoch = 1
if utils.can_load_checkpoint():
utils.load_checkpoint(model)
for epoch in range(starting_epoch, config.EPOCHS):
train_epoch(
fcdb_loader,
kupcp_loader,
model,
cropper_loss_fn,
classifier_loss_fn,
optimizer,
epoch,
lr_scheduler,
)