Skip to content

Commit

Permalink
Add fix for issue amdegroot#173
Browse files Browse the repository at this point in the history
  • Loading branch information
ashiks-qb committed Oct 19, 2021
1 parent 6eec635 commit b05ea6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions layers/modules/multibox_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def forward(self, predictions, targets):
loss_c = log_sum_exp(batch_conf) - batch_conf.gather(1, conf_t.view(-1, 1))

# Hard Negative Mining
loss_c[pos] = 0 # filter out pos boxes for now
loss_c = loss_c.view(num, -1)
loss_c[pos] = 0 # filter out pos boxes for now
_, loss_idx = loss_c.sort(1, descending=True)
_, idx_rank = loss_idx.sort(1)
num_pos = pos.long().sum(1, keepdim=True)
Expand All @@ -111,7 +111,10 @@ def forward(self, predictions, targets):

# Sum of losses: L(x,c,l,g) = (Lconf(x, c) + αLloc(x,l,g)) / N

N = num_pos.data.sum()
N = num_pos.data.sum().double()
loss_l = loss_l.double()
loss_c = loss_c.double()
loss_l /= N
loss_c /= N

return loss_l, loss_c
10 changes: 5 additions & 5 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def train():

data_loader = data.DataLoader(dataset, args.batch_size,
num_workers=args.num_workers,
shuffle=True, collate_fn=detection_collate,
shuffle=False, collate_fn=detection_collate,
pin_memory=True)
# create batch iterator
batch_iterator = iter(data_loader)
Expand Down Expand Up @@ -180,15 +180,15 @@ def train():
loss.backward()
optimizer.step()
t1 = time.time()
loc_loss += loss_l.data[0]
conf_loss += loss_c.data[0]
loc_loss += loss_l.data
conf_loss += loss_c.data

if iteration % 10 == 0:
print('timer: %.4f sec.' % (t1 - t0))
print('iter ' + repr(iteration) + ' || Loss: %.4f ||' % (loss.data[0]), end=' ')
print('iter ' + repr(iteration) + ' || Loss: %.4f ||' % (loss.data), end=' ')

if args.visdom:
update_vis_plot(iteration, loss_l.data[0], loss_c.data[0],
update_vis_plot(iteration, loss_l.data, loss_c.data,
iter_plot, epoch_plot, 'append')

if iteration != 0 and iteration % 5000 == 0:
Expand Down

0 comments on commit b05ea6d

Please sign in to comment.