51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
import torch
|
|
import os
|
|
import time
|
|
|
|
ROOT_PATH = ''
|
|
BASE_ADV_PATH = os.path.join(ROOT_PATH, 'advimages')
|
|
|
|
class AverageMeter:
|
|
def __init__(self):
|
|
self.reset()
|
|
|
|
def reset(self):
|
|
self.val = 0
|
|
self.avg = 0
|
|
self.sum = 0
|
|
self.count = 0
|
|
|
|
def update(self, val, n=1):
|
|
self.val = val
|
|
self.sum += val * n
|
|
self.count += n
|
|
self.avg = self.sum / self.count
|
|
|
|
def accuracy(output, target, topk=(1,)):
|
|
maxk = max(topk)
|
|
batch_size = target.size(0)
|
|
_, pred = output.topk(maxk, 1, True, True)
|
|
pred = pred.t()
|
|
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
|
|
return [correct[:k].reshape(-1).float().sum(0) * 100. / batch_size for k in topk]
|
|
|
|
class Logger(object):
|
|
|
|
def __init__(self, path, header):
|
|
self.log_file = open(path, 'w')
|
|
self.logger = csv.writer(self.log_file, delimiter='\t')
|
|
|
|
self.logger.writerow(header)
|
|
self.header = header
|
|
|
|
def __del(self):
|
|
self.log_file.close()
|
|
|
|
def log(self, values):
|
|
write_values = []
|
|
for col in self.header:
|
|
assert col in values
|
|
write_values.append(values[col])
|
|
|
|
self.logger.writerow(write_values)
|
|
self.log_file.flush() |