forked from gradio-app/saliency
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
205 lines (147 loc) · 6.52 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import argparse
import os
import tensorflow as tf
import config
import data
import download
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
tf.logging.set_verbosity(tf.logging.ERROR)
def define_paths(current_path, args):
"""A helper function to define all relevant path elements for the
locations of data, weights, and the results from either training
or testing a model.
Args:
current_path (str): The absolute path string of this script.
args (object): A namespace object with values from command line.
Returns:
dict: A dictionary with all path elements.
"""
if args is None:
data_path = None
else:
if os.path.isfile(args.path):
data_path = args.path
else:
data_path = os.path.join(args.path, "")
results_path = current_path + "/results/"
weights_path = current_path + "/weights/"
history_path = results_path + "history/"
images_path = results_path + "images/"
ckpts_path = results_path + "ckpts/"
best_path = ckpts_path + "best/"
latest_path = ckpts_path + "latest/"
# if args.phase == "train":
# if args.data not in data_path:
# data_path += args.data + "/"
paths = {
"data": data_path,
"history": history_path,
"images": images_path,
"best": best_path,
"latest": latest_path,
"weights": weights_path
}
return paths
def get_tf_objects(paths):
dataset = 'mit1003'
device = config.PARAMS["device"]
model_name = "model_%s_%s.pb" % (dataset, device)
current_path = os.path.dirname(os.path.realpath(__file__))
paths = define_paths(current_path, None)
if os.path.isfile(paths["best"] + model_name):
with tf.gfile.Open(paths["best"] + model_name, "rb") as file:
graph_def.ParseFromString(file.read())
else:
if not os.path.isfile(paths["weights"] + model_name):
download.download_pretrained_weights(paths["weights"],
model_name[:-3])
with tf.gfile.Open(paths["weights"] + model_name, "rb") as file:
graph_def.ParseFromString(file.read())
[predicted_maps] = tf.import_graph_def(graph_def,
input_map={"input": input_images},
return_elements=["output:0"])
return
def better_test_model(dataset, paths, device):
"""The main function for executing network testing. It loads the specified
dataset iterator and optimized saliency model. By default, when no model
checkpoint is found locally, the pretrained weights will be downloaded.
Testing only works for models trained on the same device as specified in
the config file.
Args:
dataset (str): Denotes the dataset that was used during training.
paths (dict, str): A dictionary with all path elements.
device (str): Represents either "cpu" or "gpu".
"""
jpeg = data.postprocess_saliency_map(predicted_maps[0],
original_shape[0])
print(">> Start testing with %s %s model..." % (dataset.upper(), device))
with tf.Session() as sess:
sess.run(init_op)
while True:
try:
output_file, path = sess.run([jpeg, file_path])
except tf.errors.OutOfRangeError:
break
path = path[0][0].decode("utf-8")
filename = os.path.basename(path)
filename = os.path.splitext(filename)[0]
filename += ".jpeg"
os.makedirs(paths["images"], exist_ok=True)
with open(paths["images"] + filename, "wb") as file:
file.write(output_file)
def test_model(dataset, paths, device):
"""The main function for executing network testing. It loads the specified
dataset iterator and optimized saliency model. By default, when no model
checkpoint is found locally, the pretrained weights will be downloaded.
Testing only works for models trained on the same device as specified in
the config file.
Args:
dataset (str): Denotes the dataset that was used during training.
paths (dict, str): A dictionary with all path elements.
device (str): Represents either "cpu" or "gpu".
"""
tf.reset_default_graph()
iterator = data.get_dataset_iterator("test", dataset, paths["data"])
next_element, init_op = iterator
input_images, original_shape, file_path = next_element
graph_def = tf.GraphDef()
model_name = "model_%s_%s.pb" % (dataset, device)
if os.path.isfile(paths["best"] + model_name):
with tf.gfile.Open(paths["best"] + model_name, "rb") as file:
graph_def.ParseFromString(file.read())
else:
if not os.path.isfile(paths["weights"] + model_name):
download.download_pretrained_weights(paths["weights"],
model_name[:-3])
with tf.gfile.Open(paths["weights"] + model_name, "rb") as file:
graph_def.ParseFromString(file.read())
[predicted_maps] = tf.import_graph_def(graph_def,
input_map={"input": input_images},
return_elements=["output:0"])
jpeg = data.postprocess_saliency_map(predicted_maps[0],
original_shape[0])
print(">> Start testing with %s %s model..." % (dataset.upper(), device))
# tf.reset_default_graph()
with tf.Session() as sess:
sess.run(init_op)
while True:
try:
output_file, path = sess.run([jpeg, file_path])
except tf.errors.OutOfRangeError:
break
path = path[0][0].decode("utf-8")
filename = os.path.basename(path)
filename = os.path.splitext(filename)[0]
filename += ".jpeg"
os.makedirs(paths["images"], exist_ok=True)
with open(paths["images"] + filename, "wb") as file:
file.write(output_file)
def main(tmp_path):
"""The main function reads the command line arguments, invokes the
creation of appropriate path variables, and starts the training
or testing procedure for a model.
"""
current_path = os.path.dirname(os.path.realpath(__file__))
args = argparse.Namespace(path='{}'.format(tmp_path))
paths = define_paths(current_path, args)
test_model('mit1003', paths, config.PARAMS["device"])