-
Notifications
You must be signed in to change notification settings - Fork 138
/
utils.py
52 lines (39 loc) · 1.43 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import json
import logging
import numpy as np
from datetime import datetime
import tensorflow as tf
import tensorflow.contrib.slim as slim
def prepare_dirs_and_logger(config):
formatter = logging.Formatter(
"%(asctime)s:%(levelname)s::%(message)s")
logger = logging.getLogger('tensorflow')
for hdlr in logger.handlers:
logger.removeHandler(hdlr)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(tf.logging.INFO)
if config.load_path:
if config.load_path.startswith(config.task):
config.model_name = config.load_path
else:
config.model_name = "{}_{}".format(config.task, config.load_path)
else:
config.model_name = "{}_{}".format(config.task, get_time())
config.model_dir = os.path.join(config.log_dir, config.model_name)
for path in [config.log_dir, config.data_dir, config.model_dir]:
if not os.path.exists(path):
os.makedirs(path)
def get_time():
return datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
def show_all_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def save_config(model_dir, config):
param_path = os.path.join(model_dir, "params.json")
tf.logging.info("MODEL dir: %s" % model_dir)
tf.logging.info("PARAM path: %s" % param_path)
with open(param_path, 'w') as fp:
json.dump(config.__dict__, fp, indent=4, sort_keys=True)