-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
63 lines (46 loc) · 1.98 KB
/
engine.py
File metadata and controls
63 lines (46 loc) · 1.98 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
# Copyright (c) 2022-present, Js2hou.
# All rights reserved.
# Train and eval functions used in main.py
import torch
import time
from timm.utils import accuracy
from metrics import MetricLogger
def train_one_epoch(model, criterion, data_loader, optimizer, lr_scheduler, epoch, device_id, logger):
model.train()
metriclogger = MetricLogger(['loss'])
# t_start = time.time() # test time
for idx, batch in enumerate(data_loader):
# logger.info(f'batch length: {len(batch[0])}')
# t2 = time.time()
# logger.info(f'load batch data: {t2 - t_start:.4f}')
samples, targets = [_.cuda(device_id) for _ in batch]
with torch.cuda.amp.autocast():
outputs = model(samples)
loss = criterion(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# torch.cuda.synchronize()
metriclogger.update(n=samples.size(0), loss=loss.item())
t3 = time.time()
# logger.info(f'train one batch: {t3 - t2:.4f}')
# break
lr_scheduler.step()
metriclogger.synchronize_between_processes()
# t_end = time.time() # test time
return {k: v.avg for k, v in metriclogger.meters.items()}
@torch.no_grad()
def validate(model, criterion, data_loader, device_id):
model.eval()
metriclogger = MetricLogger(['loss', 'acc1', 'acc5'])
for idx, batch in enumerate(data_loader):
samples, targets = [_.cuda(device_id) for _ in batch]
with torch.cuda.amp.autocast():
outputs = model(samples)
loss = criterion(outputs, targets)
acc1, acc5 = accuracy(outputs, targets, topk=(1, 5))
# torch.cuda.synchronize()
metriclogger.update(n=samples.size(0), loss=loss.item(),
acc1=acc1.item(), acc5=acc5.item())
metriclogger.synchronize_between_processes()
return {k: v.avg for k, v in metriclogger.meters.items()}