forked from MG2033/ShuffleNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
74 lines (61 loc) · 2.39 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from bunch import Bunch
import json
import argparse
import os
import tensorflow as tf
def parse_args():
"""
Parse the arguments of the program
:return: (config_args)
:rtype: tuple
"""
# Create a parser
parser = argparse.ArgumentParser(description="ShuffleNet Tensorflow implementation")
parser.add_argument('--version', action='version', version='%(prog)s 0.0.1')
parser.add_argument('--config', default=None, type=str, help='Configuration file')
# Parse the arguments
args = parser.parse_args()
# parse the configurations from the config json file provided
with open(args.config, 'r') as config_file:
config_args_dict = json.load(config_file)
# convert the dictionary to a namespace using bunch lib
config_args = Bunch(config_args_dict)
print(config_args)
return config_args
def create_experiment_dirs(exp_dir):
"""
Create Directories of a regular tensorflow experiment directory
:param exp_dir:
:return summary_dir, checkpoint_dir:
"""
experiment_dir = os.path.realpath(os.path.join(os.path.dirname(__file__))) + "/experiments/" + exp_dir + "/"
summary_dir = experiment_dir + 'summaries/'
checkpoint_dir = experiment_dir + 'checkpoints/'
# output_dir = experiment_dir + 'output/'
# test_dir = experiment_dir + 'test/'
# dirs = [summary_dir, checkpoint_dir, output_dir, test_dir]
dirs = [summary_dir, checkpoint_dir]
try:
for dir_ in dirs:
if not os.path.exists(dir_):
os.makedirs(dir_)
print("Experiment directories created!")
# return experiment_dir, summary_dir, checkpoint_dir, output_dir, test_dir
return experiment_dir, summary_dir, checkpoint_dir
except Exception as err:
print("Creating directories error: {0}".format(err))
exit(-1)
def calculate_flops():
# Print to stdout an analysis of the number of floating point operations in the
# model broken down by individual operations.
tf.profiler.profile(
tf.get_default_graph(),
options=tf.profiler.ProfileOptionBuilder.float_operation(), cmd='scope')
def show_parameters():
tf.profiler.profile(
tf.get_default_graph(),
options=tf.profiler.ProfileOptionBuilder.trainable_variables_parameter(), cmd='scope')
def load_obj(filename):
import pickle
with open(filename, 'rb') as file:
return pickle.load(file)