forked from aiff22/PyNET
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
155 lines (104 loc) · 4.18 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright 2020 by Andrey Ignatov. All Rights Reserved.
from functools import reduce
import tensorflow.compat.v1 as tf
import numpy as np
import sys
import os
NUM_DEFAULT_TRAIN_ITERS = [500, 500, 500, 500, 500]
def process_command_args(arguments):
# Specifying the default parameters
level = 0
batch_size = 20
train_size = 5000
learning_rate = 5e-5
prefix = ''
model_path = 'model.py'
eval_step = 1000
restore_iter = None
num_train_iters = None
dataset_dir = 'raw_images/'
vgg_dir = 'vgg_pretrained/imagenet-vgg-verydeep-19.mat'
for args in arguments:
if args.startswith("model_path"):
model_path = str(args.split("=")[1])
if args.startswith("dir_prefix"):
prefix = str(args.split("=")[1])
if args.startswith("level"):
level = int(args.split("=")[1])
if args.startswith("batch_size"):
batch_size = int(args.split("=")[1])
if args.startswith("train_size"):
train_size = int(args.split("=")[1])
if args.startswith("learning_rate"):
learning_rate = float(args.split("=")[1])
if args.startswith("restore_iter"):
restore_iter = int(args.split("=")[1])
if args.startswith("num_train_iters"):
num_train_iters = int(args.split("=")[1])
# -----------------------------------
if args.startswith("dataset_dir"):
dataset_dir = args.split("=")[1]
if args.startswith("vgg_dir"):
vgg_dir = args.split("=")[1]
if args.startswith("loss_fn"):
loss_fn = args.split("=")[1]
if restore_iter is None and level < 4:
restore_iter = get_last_iter(level + 1)
if restore_iter == -1:
print("Error: Cannot find any pre-trained models for PyNET's level " + str(level + 1) + ".")
print("Aborting the training.")
sys.exit()
if num_train_iters is None:
num_train_iters = NUM_DEFAULT_TRAIN_ITERS[level]
if loss_fn is None:
loss_fn = 'vgg+ssim'
print("The following parameters will be applied for CNN training:")
print("Training level: " + str(level))
print("Batch size: " + str(batch_size))
print("Learning rate: " + str(learning_rate))
print("Training iterations: " + str(num_train_iters))
print("Evaluation step: " + str(eval_step))
print("Restore Iteration: " + str(restore_iter))
print("Path to the dataset: " + dataset_dir)
print("Path to VGG-19 network: " + vgg_dir)
print("loss_fn: " + loss_fn)
return prefix, model_path, level, batch_size, train_size, learning_rate, restore_iter, num_train_iters,\
dataset_dir, vgg_dir, loss_fn
def process_test_model_args(arguments):
level = 0
restore_iter = None
dataset_dir = 'raw_images/'
use_gpu = "true"
orig_model = "false"
for args in arguments:
if args.startswith("level"):
level = int(args.split("=")[1])
if args.startswith("dataset_dir"):
dataset_dir = args.split("=")[1]
if args.startswith("restore_iter"):
restore_iter = int(args.split("=")[1])
if args.startswith("use_gpu"):
use_gpu = args.split("=")[1]
if args.startswith("orig"):
orig_model = args.split("=")[1]
if restore_iter is None and orig_model == "false":
restore_iter = get_last_iter(level)
if restore_iter == -1:
print("Error: Cannot find any pre-trained models for PyNET's level " + str(level) + ".")
sys.exit()
return level, restore_iter, dataset_dir, use_gpu, orig_model
def get_last_iter(level):
saved_models = [int((model_file.split("_")[-1]).split(".")[0])
for model_file in os.listdir("models/")
if model_file.startswith("pynet_level_" + str(level))]
if len(saved_models) > 0:
return np.max(saved_models)
else:
return -1
def log10(x):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
def _tensor_size(tensor):
from operator import mul
return reduce(mul, (d for d in tensor.get_shape()[1:]), 1)