-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
368 lines (291 loc) · 14 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
from model.model_Tatarchenko15_attention import ModelTatarchenko15Attention
from model.model_Zhou16_attention import ModelZhou16Attention
from model.model_interface import ModelInterface
from data_container import *
import json
import multiprocessing
import os
import glob
import collections
import pandas as pd
from test_utils import *
dataset = None
current_test_input_images = None
current_test_target_images = None
current_test_poses = None
def initialize_tensorflow():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
config.log_device_placement = True # to log device placement (on which device the operation ran)
# (nothing gets printed in Jupyter, only if you run it standalone)
sess = tf.Session(config=config)
set_session(sess)
def build_model_from_dictionary(data: DataLoader, **kwargs):
model_type = kwargs["model_type"]
model_class = ModelInterface
if model_type == 't':
model_class = ModelTatarchenko15Attention
elif model_type == 'z':
model_class = ModelZhou16Attention
attention_strategy = kwargs.get("attention_strategy", None)
attention_strategy_details = kwargs.get("attention_strategy_details", None)
random_seed_index = kwargs.get("random_seed_index", None)
image_size = kwargs.get("image_size", 256)
k = kwargs.get("k", 2)
pose_input_size = None
if data.name == 'kitti' or data.name == 'synthia':
pose_input_size = data.pose_size
model = model_class(
image_size=image_size,
attention_strategy=attention_strategy,
attention_strategy_details=attention_strategy_details,
additional_name=random_seed_index,
pose_input_size=pose_input_size,
k=k
)
return model
def find_load_model_in_folder(model, parent_folder, dataset_name):
print(model.name)
target_name = "%s/%s_%s*/*.h5" % (parent_folder, model.name, dataset_name)
files = glob.glob(target_name)
print(target_name)
if len(files) > 1:
min_file = None
min_len = 100000
for f in files:
s = len(f.split("_"))
if s < min_len:
min_len = s
min_file = f
load_file = min_file
else:
load_file = files[0]
return load_file
def load_dataset_from_config(**kwargs):
dataset_name = kwargs["dataset"]
dataset_format = kwargs["dataset_format"]
image_size = kwargs.get("image_size", 256)
is_pose_matrix = kwargs.get("is_pose_matrix", False)
train_or_test = kwargs.get("train_or_test", "train")
if dataset_name == "kitti" or dataset_name == "synthia":
return SceneDataLoaderNumpy(dataset_name, use_pose_matrix=is_pose_matrix, image_size=image_size)
elif dataset_name == "car" or dataset_name == "chair":
return ObjectDataLoaderNumpy(dataset_name, image_size=image_size, train_or_test=train_or_test)
def train_single_model(x):
i, gpu_id, config_file_name = x
kwargs = json.load(open(config_file_name))
ith_model_info = kwargs["model_list"][i]
model = build_model_from_dictionary(dataset, **ith_model_info)
print("model constructed!")
additional_name = kwargs.get("additional_name", None)
if additional_name is not None:
random.seed(additional_name * 4219 + 123)
np.random.seed(additional_name * 4219 + 123)
else:
random.seed(1000)
np.random.seed(1000)
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
initialize_tensorflow()
model.train(dataset, **kwargs)
def train_all_using_multiprocessing(config_file_name):
global dataset
print("start to load dataset")
config = json.load(open(config_file_name))
model_counts = len(config["model_list"])
dataset = load_dataset_from_config(**config)
print("dataset loading finished")
available_gpu_ids = config["available_gpu_ids"]
gpu_ids = [available_gpu_ids[i % len(available_gpu_ids)] for i in range(model_counts)]
train_infos = [(i, gpu_ids[i], config_file_name) for i in range(model_counts)]
i = 0
k = config.get("multiprocess_max", model_counts)
print("start multiprocessing training")
while i < model_counts:
with multiprocessing.Pool(k) as p:
p.map(train_single_model, train_infos[i:min(i + k, model_counts)], chunksize=1)
i += k
def test_single_model(x):
i, gpu_id, config_file_name = x
kwargs = json.load(open(config_file_name))
ith_model_info = kwargs["model_list"][i]
model = build_model_from_dictionary(dataset, **ith_model_info)
try:
print("model constructed!")
random.seed(883222)
np.random.seed(883222)
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
initialize_tensorflow()
parent_folder = kwargs["parent_folder"]
load_file = kwargs.get("load_file", find_load_model_in_folder(model, parent_folder, dataset.name))
model.build_model()
model.load_model(load_file)
batch_size = kwargs.get("batch_size", 16)
test_method = kwargs.get("test_method", "exhaustive")
mae_all = None
ssim_all = None
# scene
if dataset.name == 'kitti' or dataset.name == 'synthia':
if test_method == 'exhaustive':
mae, ssim, mae_all, ssim_all = test_for_all_scenes(dataset, model, batch_size=batch_size)
else:
mae, ssim = test_for_random_scene(dataset, model, N=kwargs.get("max_iter", 20000), batch_size=batch_size)
# object
else:
if test_method == 'exhaustive':
mae, ssim, mae_all, ssim_all = test_for_all_objects(dataset, model, batch_size=batch_size)
else:
mae, ssim = test_for_random_scene(dataset, model, N=kwargs.get("max_iter", 20000), batch_size=batch_size)
return mae, ssim, mae_all, ssim_all, model.name
except Exception as ex:
print(ex)
return 0, 0, None, None, model.name
def test_all_using_multiprocessing(config_file_name):
global dataset
config = json.load(open(config_file_name))
model_counts = len(config["model_list"])
config["train_or_test"] = "test"
dataset = load_dataset_from_config(**config)
print("dataset loading finished")
available_gpu_ids = config["available_gpu_ids"]
gpu_ids = [available_gpu_ids[i % len(available_gpu_ids)] for i in range(model_counts)]
train_infos = [(i, gpu_ids[i], config_file_name) for i in range(model_counts)]
k = config.get("multiprocess_max", model_counts)
with multiprocessing.Pool(k) as p:
results = p.map(test_single_model, train_infos, chunksize=1)
maes, ssims, mae_alls, ssim_alls, names = zip(*results)
raw_data = collections.OrderedDict()
raw_data['name'] = names
raw_data['mae'] = maes
raw_data['ssim'] = ssims
df = pd.DataFrame(raw_data)
df = df.set_index("name")
mae_alls = np.array(mae_alls)
ssim_alls = np.array(ssim_alls)
diff_N = mae_alls.shape[1]
mae_all_df = pd.DataFrame(mae_alls, index=names, columns=[i - (diff_N // 2) for i in range(diff_N)])
ssim_all_df = pd.DataFrame(ssim_alls, index=names, columns=[i - (diff_N // 2) for i in range(diff_N)])
result_export_folder = config["result_export_folder"]
if not os.path.exists(result_export_folder):
os.makedirs(result_export_folder)
started_time_date = time.strftime("%Y%m%d_%H%M%S")
df.to_csv("%s/%s_%s.csv" % (result_export_folder, "total_result", started_time_date))
mae_all_df.to_csv("%s/%s_%s.csv" % (result_export_folder, "total_result_mae", started_time_date))
ssim_all_df.to_csv("%s/%s_%s.csv" % (result_export_folder, "total_result_ssim", started_time_date))
def test_and_export_picture_for_single_model(x):
i, gpu_id, config_file_name = x
kwargs = json.load(open(config_file_name))
ith_model_info = kwargs["model_list"][i]
model = build_model_from_dictionary(dataset, **ith_model_info)
try:
print("model constructed!")
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
initialize_tensorflow()
parent_folder = kwargs["parent_folder"]
load_file = kwargs.get("load_file", find_load_model_in_folder(model, parent_folder, dataset.name))
model.build_model()
model.load_model(load_file)
poseinfo_processed = model.process_pose_info(dataset, current_test_poses)
pred_images = model.get_predicted_image((current_test_input_images, poseinfo_processed))
#pred_image_tensor = tf.convert_to_tensor(pred_images, dtype=tf.float32)
#target_image_original_tensor = tf.convert_to_tensor(target_image_original, dtype=tf.float32)
#ssim_values = K.eval(ssim_custom(pred_image_tensor, target_image_original_tensor))
#mae_values = K.eval(mae_custom(pred_image_tensor, target_image_original_tensor))
return pred_images, None, None, model.name
except Exception as ex:
print(ex)
return None, None, None, model.name
def test_and_export_picture_for_models_using_multiprocessing(config_file_name):
global dataset, current_test_input_images, current_test_target_images, current_test_poses
config = json.load(open(config_file_name))
model_counts = len(config["model_list"])
config["train_or_test"] = "test"
dataset = load_dataset_from_config(**config)
print("dataset loading finished")
available_gpu_ids = config["available_gpu_ids"]
gpu_ids = [available_gpu_ids[i % len(available_gpu_ids)] for i in range(model_counts)]
train_infos = [(i, gpu_ids[i], config_file_name) for i in range(model_counts)]
k = config.get("multiprocess_max", model_counts)
target_scene_infos = config.get("target_scene_infos", None)
target_scene_n = config.get("target_scene_n", 5)
result_export_folder = config.get("result_export_folder", None)
index_info = None
if target_scene_infos is None:
test_data, index_info = dataset.get_batched_data(target_scene_n, single_model=False, return_info=True, is_train=False)
print(index_info)
else:
test_data = dataset.get_specific_data(target_scene_infos)
current_test_input_images, current_test_target_images, current_test_poses = test_data
with multiprocessing.Pool(k) as p:
results = p.map(test_and_export_picture_for_single_model, train_infos, chunksize=1)
images, maes, ssims, names = zip(*results)
# 1. export images
xs = []
xs.append(np.concatenate(current_test_input_images, axis=0))
xs.append(np.concatenate(current_test_target_images, axis=0))
pred_image_temp = None
for pred_image in images:
if pred_image is not None:
xs.append(np.concatenate(pred_image, axis=0))
elif pred_image_temp is not None:
xs.append(np.concatenate(np.zeros_like(pred_image_temp), axis=0))
pred_image_temp = pred_image
total_image = np.concatenate(tuple(xs), axis=1)
if not os.path.exists(result_export_folder):
os.makedirs(result_export_folder)
started_time_date = time.strftime("%Y%m%d_%H%M%S")
save_pred_images(total_image, "%s/%s_%s" % (result_export_folder, "total_images", started_time_date))
# export model names
raw_data = collections.OrderedDict()
raw_data['name'] = names
df = pd.DataFrame(raw_data)
df = df.set_index("name")
df.to_csv("%s/%s_%s.csv" % (result_export_folder, "total_images_models", started_time_date))
if index_info is not None:
if dataset.name == 'kitti' or dataset.name == 'synthia':
scene_ids, input_ids, target_ids = zip(*index_info)
raw_data = collections.OrderedDict()
raw_data['scene_id'] = scene_ids
raw_data['input_id'] = input_ids
raw_data['target_id'] = target_ids
df = pd.DataFrame(raw_data)
df.to_csv("%s/%s_%s.csv" % (result_export_folder, "tested_samples_index_info", started_time_date), index=False)
def test_and_export_feature_map_for_single_model(x):
i, gpu_id, config_file_name = x
kwargs = json.load(open(config_file_name))
ith_model_info = kwargs["model_list"][i]
model = build_model_from_dictionary(dataset, **ith_model_info)
print("model constructed!")
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
initialize_tensorflow()
parent_folder = ith_model_info["parent_folder"]
result_export_folder = kwargs["result_export_folder"]
load_file = kwargs.get("load_file", find_load_model_in_folder(model, parent_folder, dataset.name))
model.build_model()
model.load_model(load_file)
poseinfo_processed = model.process_pose_info(dataset, current_test_poses)
current_test_data = (current_test_input_images, current_test_target_images, poseinfo_processed)
feature_map = show_feature_map(current_test_data, model)
started_time_date = time.strftime("%Y%m%d_%H%M%S")
print(feature_map.shape)
save_pred_images(feature_map, "%s/%s_%s" % (result_export_folder, model.name, started_time_date))
def test_and_export_feature_map_for_models_using_multiprocessing(config_file_name):
global dataset, current_test_input_images, current_test_target_images, current_test_poses
config = json.load(open(config_file_name))
model_counts = len(config["model_list"])
dataset = load_dataset_from_config(**config)
print("dataset loading finished")
available_gpu_ids = config["available_gpu_ids"]
gpu_ids = [available_gpu_ids[i % len(available_gpu_ids)] for i in range(model_counts)]
train_infos = [(i, gpu_ids[i], config_file_name) for i in range(model_counts)]
k = config.get("multiprocess_max", model_counts)
target_scene_infos = config.get("target_scene_infos", None)
if target_scene_infos is None:
test_data, index_info = dataset.get_batched_data(1, single_model=False, return_info=True)
print(index_info)
else:
test_data = dataset.get_specific_data(target_scene_infos)
current_test_input_images, current_test_target_images, current_test_poses = test_data
with multiprocessing.Pool(k) as p:
p.map(test_and_export_feature_map_for_single_model, train_infos, chunksize=1)