har_kimyeon / HAR_code /util /utils.py
huantran2004's picture
Upload 48 files
862e0b5 verified
Raw History Blame Contribute Delete
2.15 kB
# Forked from https://github.com/yolish/transposenet
import logging
import logging.config
import PIL
import json
from os.path import join, exists, split, realpath
import time
from os import mkdir, getcwd
import matplotlib.pyplot as plt
import numpy as np
# Logging and output utils
##########################
def get_stamp_from_log():
"""
Get the time stamp from the log file
:return:
"""
return split(logging.getLogger().handlers[0].baseFilename)[-1].replace(".log","")
def create_output_dir(name):
"""
Create a new directory for outputs, if it does not already exist
:param name: (str) the name of the directory
:return: the path to the outpur directory
"""
out_dir = join(getcwd(), name)
if not exists(out_dir):
mkdir(out_dir)
return out_dir
def init_logger(name_of_model = ""):
"""
Initialize the logger and create a time stamp for the file
"""
path = split(realpath(__file__))[0]
with open(join(path, 'log_config.json')) as json_file:
log_config_dict = json.load(json_file)
#filename = log_config_dict.get('handlers').get('file_handler').get('filename')
#filename = ''.join([filename, "_", time.strftime("%d_%m_%y_%H%M", time.localtime()), ".log"])
filename = ''.join([time.strftime("%y%m%d_%H%M", time.localtime()), ".log"])
# Creating logs' folder is needed
log_path = create_output_dir(''.join(['/workspace/HAR_code/out/',name_of_model,'_',filename[:-4], '/']))
log_config_dict.get('handlers').get('file_handler')['filename'] = join(log_path, ''.join([name_of_model,'_',filename]))
logging.config.dictConfig(log_config_dict)
# disable external modules' loggers (level warning and below)
logging.getLogger(PIL.__name__).setLevel(logging.WARNING)
return log_path
# Plotting utils
##########################
def plot_loss_func(sample_count, loss_vals, loss_fig_path):
plt.figure()
plt.plot(sample_count, loss_vals)
plt.ylim(0,0.7)
plt.grid()
plt.title('Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.savefig(loss_fig_path)