https://github.com/ChrisWu1997/2D-Motion-Retargeting
Tip revision: f3454a1972a98b3a572f83c1c9ea0b0e5d9e7d00 authored by Rundi Wu on 28 December 2020, 02:08:07 UTC
Update README.md
Update README.md
Tip revision: f3454a1
utils.py
from PIL import Image
import os
import json
import logging
import shutil
import csv
class TrainClock(object):
def __init__(self):
self.epoch = 1
self.minibatch = 0
self.step = 0
def tick(self):
self.minibatch += 1
self.step += 1
def tock(self):
self.epoch += 1
self.minibatch = 0
def make_checkpoint(self):
return {
'epoch': self.epoch,
'minibatch': self.minibatch,
'step': self.step
}
def restore_checkpoint(self, clock_dict):
self.epoch = clock_dict['epoch']
self.minibatch = clock_dict['minibatch']
self.step = clock_dict['step']
class Table(object):
def __init__(self, filename):
'''
create a table to record experiment results that can be opened by excel
:param filename: using '.csv' as postfix
'''
assert '.csv' in filename
self.filename = filename
@staticmethod
def merge_headers(header1, header2):
#return list(set(header1 + header2))
if len(header1) > len(header2):
return header1
else:
return header2
def write(self, ordered_dict):
'''
write an entry
:param ordered_dict: something like {'name':'exp1', 'acc':90.5, 'epoch':50}
:return:
'''
if os.path.exists(self.filename) == False:
headers = list(ordered_dict.keys())
prev_rec = None
else:
with open(self.filename) as f:
reader = csv.DictReader(f)
headers = reader.fieldnames
prev_rec = [row for row in reader]
headers = self.merge_headers(headers, list(ordered_dict.keys()))
with open(self.filename, 'w', newline='') as f:
writer = csv.DictWriter(f, headers)
writer.writeheader()
if not prev_rec == None:
writer.writerows(prev_rec)
writer.writerow(ordered_dict)
class WorklogLogger:
def __init__(self, log_file):
logging.basicConfig(filename=log_file,
level=logging.DEBUG,
format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s')
self.logger = logging.getLogger()
def put_line(self, line):
self.logger.info(line)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name):
self.name = name
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 save_args(args, save_dir):
param_path = os.path.join(save_dir, 'params.json')
with open(param_path, 'w') as fp:
json.dump(args.__dict__, fp, indent=4, sort_keys=True)
def ensure_dir(path):
"""
create path by first checking its existence,
:param paths: path
:return:
"""
if not os.path.exists(path):
os.makedirs(path)
def ensure_dirs(paths):
"""
create paths by first checking their existence
:param paths: list of path
:return:
"""
if isinstance(paths, list) and not isinstance(paths, str):
for path in paths:
ensure_dir(path)
else:
ensure_dir(paths)
def remkdir(path):
"""
if dir exists, remove it and create a new one
:param path:
:return:
"""
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
def cycle(iterable):
while True:
for x in iterable:
yield x
def save_image(image_numpy, image_path):
image_pil = Image.fromarray(image_numpy)
image_pil.save(image_path)
def pad_to_16x(x):
if x % 16 > 0:
return x - x % 16 + 16
return x
def pad_to_height(tar_height, img_height, img_width):
scale = tar_height / img_height
h = pad_to_16x(tar_height)
w = pad_to_16x(int(img_width * scale))
return h, w, scale
def test():
pass
if __name__ == '__main__':
test()
