-
Notifications
You must be signed in to change notification settings - Fork 18
/
softmax.py
97 lines (81 loc) · 2.8 KB
/
softmax.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
from __future__ import division, print_function, absolute_import
from torchreid import metrics
from torchreid.losses import CrossEntropyLoss
from ..engine import Engine
class ImageSoftmaxEngine(Engine):
r"""Softmax-loss engine for image-reid.
Args:
datamanager (DataManager): an instance of ``torchreid.data.ImageDataManager``
or ``torchreid.data.VideoDataManager``.
model (nn.Module): model instance.
optimizer (Optimizer): an Optimizer.
scheduler (LRScheduler, optional): if None, no learning rate decay will be performed.
use_gpu (bool, optional): use gpu. Default is True.
label_smooth (bool, optional): use label smoothing regularizer. Default is True.
Examples::
import torchreid
datamanager = torchreid.data.ImageDataManager(
root='path/to/reid-data',
sources='market1501',
height=256,
width=128,
combineall=False,
batch_size=32
)
model = torchreid.models.build_model(
name='resnet50',
num_classes=datamanager.num_train_pids,
loss='softmax'
)
model = model.cuda()
optimizer = torchreid.optim.build_optimizer(
model, optim='adam', lr=0.0003
)
scheduler = torchreid.optim.build_lr_scheduler(
optimizer,
lr_scheduler='single_step',
stepsize=20
)
engine = torchreid.engine.ImageSoftmaxEngine(
datamanager, model, optimizer, scheduler=scheduler
)
engine.run(
max_epoch=60,
save_dir='log/resnet50-softmax-market1501'
)
"""
def __init__(
self,
datamanager,
model,
optimizer,
writer,
engine_state,
scheduler=None,
use_gpu=True,
label_smooth=True,
save_model_flag=False
):
super(ImageSoftmaxEngine, self).__init__(datamanager, writer, engine_state, use_gpu, save_model_flag)
self.model = model
self.optimizer = optimizer
self.scheduler = scheduler
self.register_model('model', model, optimizer, scheduler)
self.criterion = CrossEntropyLoss(
label_smooth=label_smooth
)
def forward_backward(self, data):
imgs, pids = self.parse_data_for_train(data)
if self.use_gpu:
imgs = imgs.cuda()
pids = pids.cuda()
outputs = self.model(imgs)
loss = self.compute_loss(self.criterion, outputs, pids)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
loss_summary = {
'loss': loss.item(),
'acc': metrics.accuracy(outputs, pids)[0].item()
}
return loss, {'glb_ft': loss_summary}