text
stringlengths 1
93.6k
|
|---|
scr[i] += output.cpu().numpy()
|
gts = np.concatenate(gts, axis=0).T
|
scr = np.concatenate(scr, axis=0).T
|
aps = []
|
for i in range(20):
|
# Subtract eps from score to make AP work for tied scores
|
ap = metrics.average_precision_score(gts[i][gts[i]<=1], scr[i][gts[i]<=1]-1e-5*gts[i][gts[i]<=1])
|
aps.append( ap )
|
print(np.mean(aps), ' ', ' '.join(['%0.2f'%a for a in aps]))
|
def train(loader, model, optimizer, criterion, fc6_8, losses, it=0, total_iterations=None, stepsize=None, verbose=True):
|
# to log
|
batch_time = AverageMeter()
|
data_time = AverageMeter()
|
top1 = AverageMeter()
|
end = time.time()
|
current_iteration = it
|
# use dropout for the MLP
|
model.train()
|
# in the batch norms always use global statistics
|
model.features.eval()
|
for (input, target) in loader:
|
# measure data loading time
|
data_time.update(time.time() - end)
|
# adjust learning rate
|
if current_iteration != 0 and current_iteration % stepsize == 0:
|
for param_group in optimizer.param_groups:
|
param_group['lr'] = param_group['lr'] * 0.5
|
print('iter {0} learning rate is {1}'.format(current_iteration, param_group['lr']))
|
# move input to gpu
|
input = input.cuda(non_blocking=True)
|
# forward pass with or without grad computation
|
output = model(input)
|
target = target.float().cuda()
|
mask = (target == 255)
|
loss = torch.sum(criterion(output, target).masked_fill_(mask, 0)) / target.size(0)
|
# backward
|
optimizer.zero_grad()
|
loss.backward()
|
# clip gradients
|
torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
|
# and weights update
|
optimizer.step()
|
# measure accuracy and record loss
|
losses.update(loss.item(), input.size(0))
|
# measure elapsed time
|
batch_time.update(time.time() - end)
|
end = time.time()
|
if verbose is True and current_iteration % 25 == 0:
|
print('Iteration[{0}]\t'
|
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
|
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
|
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
|
current_iteration, batch_time=batch_time,
|
data_time=data_time, loss=losses))
|
current_iteration = current_iteration + 1
|
if total_iterations is not None and current_iteration == total_iterations:
|
break
|
return current_iteration
|
class VOC2007_dataset(torch.utils.data.Dataset):
|
def __init__(self, voc_dir, split='train', transform=None):
|
# Find the image sets
|
image_set_dir = os.path.join(voc_dir, 'ImageSets', 'Main')
|
image_sets = glob.glob(os.path.join(image_set_dir, '*_' + split + '.txt'))
|
assert len(image_sets) == 20
|
# Read the labels
|
self.n_labels = len(image_sets)
|
images = defaultdict(lambda:-np.ones(self.n_labels, dtype=np.uint8))
|
for k, s in enumerate(sorted(image_sets)):
|
for l in open(s, 'r'):
|
name, lbl = l.strip().split()
|
lbl = int(lbl)
|
# Switch the ignore label and 0 label (in VOC -1: not present, 0: ignore)
|
if lbl < 0:
|
lbl = 0
|
elif lbl == 0:
|
lbl = 255
|
images[os.path.join(voc_dir, 'JPEGImages', name + '.jpg')][k] = lbl
|
self.images = [(k, images[k]) for k in images.keys()]
|
np.random.shuffle(self.images)
|
self.transform = transform
|
def __len__(self):
|
return len(self.images)
|
def __getitem__(self, i):
|
img = Image.open(self.images[i][0])
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.