-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.py
120 lines (96 loc) · 3.75 KB
/
location.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
# test for final results
# using 1...n index to access image
from __future__ import print_function
import argparse
import os
import sys
import time
import tensorflow as tf
import numpy as np
import glob
import cv2
import pdb
from model.model import *
from model.average_gradients import *
from utils.colored import *
from utils.tools import *
from utils.params import *
def main():
"""Create the model and start the training."""
args = get_arguments()
h=args.h
w=args.w
coord = tf.train.Coordinator()
img = tf.placeholder(tf.float32, shape=[None, h, w, 3])
mask = tf.placeholder(tf.float32, shape=[None, h, w, 1])
model = TyphoonModel(args, img, None)
# Gets moving_mean and moving_variance update operations from
# COUNT PARAMS
total_num_parameters = 0
for variable in tf.trainable_variables():
total_num_parameters += np.array(variable.get_shape().as_list()).prod()
print(toCyan('number of trainable parameters: {}'.format(total_num_parameters)))
# Set up tf session and initialize variables.
#
config = tf.ConfigProto(allow_soft_placement=True) # Chong
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.4
sess = tf.Session(config=config)
init_local = tf.local_variables_initializer()
init = tf.global_variables_initializer()
# init
sess.run([init_local, init])
# Saver for storing checkpoints of the model.
var = tf.global_variables()
# fine_tune_var=[val for val in var if ('conv6_cls' not in val.name and 'sub4_out' not in val.name and 'sub24_out' not in val.name )]
saver = tf.train.Saver(var_list=var, max_to_keep=5)
print(toGreen('./ckpt/location'))
loader = tf.train.Saver(var_list=var)
load(loader, sess, './ckpt/location')
# Start queue threads.
threads = tf.train.start_queue_runners(coord=coord, sess=sess)
# Iterate over training steps.
files=glob.glob(os.path.join(args.img_dir,'08/*.jpg'))
txtfile0=os.path.join(args.img_dir,'recognition.txt')
txtfile='location.txt'
ff=open(os.path.join(args.img_dir,'location.txt'),'w')
ff0_=open(txtfile0,'r')
ff0=ff0_.readlines()
for ii in range(len(files)):
start_time = time.time()
ss=ff0[ii].strip().split(' ')
if int(ss[0])==-1:
ff.write(ff0[ii])
continue
image=read_tri_image_by_index(ii+1,args.img_dir,args.h,args.w)
feed_dict = {img:image}
heatmap = sess.run(
model.mask_pred, feed_dict=feed_dict)
pred,result=parse_heatmap(heatmap,args.threshold)
orig=image[0,:,:]
# pdb.set_trace()
pairs=np.concatenate([orig*255,pred],1)
cv2.imwrite('./output/'+str(ii+1)+'.jpg',pairs)
duration = time.time() - start_time
print('\r',toCyan('{:d}:{:f}'.format(ii+1,duration)),end='')
ff.write(result)
ff.close()
if os.path.exists(os.path.join(args.img_dir,'label.txt')):
f1=open(os.path.join(args.img_dir,'label.txt'),'r')
f2=open(os.path.join(args.img_dir,txtfile),'r')
s1,s2,s,ets,mae=calc_score(f1,f2)
f1.close()
f2.close()
os.rename(os.path.join(args.img_dir,txtfile),\
os.path.join(args.img_dir,str(round(ets,4))+'-'+str(round(mae,4))+'-'+txtfile))
print('\n')
print(toGreen('--'*10))
print(toGreen('ETS: {:f}'.format(ets)))
print(toGreen('MAE: {:f}'.format(mae)))
print(toGreen('Score is : {:f} + {:f} = {:f}'.format(s1,s2,s)))
print(toGreen('--'*10))
coord.request_stop()
coord.join(threads)
sess.close()
if __name__ == '__main__':
main()