-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
198 lines (165 loc) · 6.85 KB
/
test.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
# Import the converted model's class
import numpy as np
import random
import tensorflow as tf
from ResNet_work import *
import cv2
from tqdm import tqdm
import time
import os
from multiprocessing import Process, Queue, Pool
import math
batch_size = 2
# Set this path to your dataset directory
directory = '/home/sky/PoseNet-master/posenet/KingsCollege/'
dataset = 'dataset_train.txt'
class datasource(object):
def __init__(self, images, poses):
self.images = images
self.poses = poses
def centeredCrop(img, output_side_length):
height, width, depth = img.shape
new_height = output_side_length
new_width = output_side_length
if height > width:
new_height = output_side_length * height / width
else:
new_width = output_side_length * width / height
height_offset = (new_height - output_side_length) / 2
width_offset = (new_width - output_side_length) / 2
cropped_img = img[height_offset:height_offset + output_side_length,
width_offset:width_offset + output_side_length]
return cropped_img
def preprocess(images):
images_out = [] # final result
# Resize and crop and compute mean!
images_cropped = []
for i in tqdm(range(len(images))):
X = cv2.imread(images[i])
X = cv2.resize(X, (455, 256))
X = centeredCrop(X, 224)
images_cropped.append(X)
# compute images mean
N = 0
mean = np.zeros((1, 3, 224, 224))
for X in tqdm(images_cropped):
mean[0][0] += X[:, :, 0]
mean[0][1] += X[:, :, 1]
mean[0][2] += X[:, :, 2]
N += 1
mean[0] /= N
# Subtract mean from all images
for X in tqdm(images_cropped):
X = np.transpose(X, (2, 0, 1))
X = X - mean
X = np.squeeze(X)
X = np.transpose(X, (1, 2, 0))
images_out.append(X)
return images_out
def get_data():
poses = []
images = []
with open(directory + dataset) as f:
next(f) # skip the 3 header lines
next(f)
next(f)
for line in f:
fname, p0, p1, p2, p3, p4, p5, p6 = line.split()
p0 = float(p0)
p1 = float(p1)
p2 = float(p2)
p3 = float(p3)
p4 = float(p4)
p5 = float(p5)
p6 = float(p6)
poses.append((p0, p1, p2, p3, p4, p5, p6))
images.append(directory + fname)
images = preprocess(images)
return datasource(images, poses)
def gen_data(source):
while True:
indices = range(len(source.images))
random.shuffle(indices)
for i in indices:
image = source.images[i]
pose_x = source.poses[i][0:3]
pose_q = source.poses[i][3:7]
yield image, pose_x, pose_q
def gen_data_batch(source):
data_gen = gen_data(source)
while True:
image_batch = []
pose_x_batch = []
pose_q_batch = []
for _ in range(batch_size):
image, pose_x, pose_q = next(data_gen)
image_batch.append(image)
pose_x_batch.append(pose_x)
pose_q_batch.append(pose_q)
yield np.array(image_batch), np.array(pose_x_batch), np.array(pose_q_batch)
class Train(object):
def __init__(self):
self.placeholders()
def placeholders(self):
self.images_placeholder = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])
self.poses_x_placeholder = tf.placeholder(tf.float32, [batch_size, 3])
self.poses_q_placeholder = tf.placeholder(tf.float32, [batch_size, 4])
def build_train_graph(self):
global_step = tf.Variable(0, trainable=False)
pose = inference(self.images_placeholder, FLAGS.num_residual_blocks, reuse=False)
regu_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
loss = self.loss(pose, self.poses_x_placeholder, self.poses_q_placeholder)
self.full_loss = tf.add_n([loss] + regu_losses)
self.train_op = self.train_operation(global_step, self.full_loss)
def train_operation(self, global_step, total_loss):
opt = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=0.00000001,
use_locking=False, name='Adam').minimize(self.full_loss, global_step=global_step)
return opt
def loss(self, logits, translation, rotation):
# lx,lq = logits[0:3],logits[3:7]
lx = tf.slice(logits, [0, 0], [FLAGS.batch_size, 3])
lq = tf.slice(logits, [0, 3], [FLAGS.batch_size, 4])
# lx, lq = logits[0:3], logits[3:7]
l1_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(lx, translation)))) * 0.3
l1_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(lq, rotation)))) * 150
cost = l1_x + l1_q
return cost
def test(self):
datasource = get_data()
results = np.zeros((len(datasource.images), 2))
self.test_image_placeholder = tf.placeholder(tf.float32,[2,224,224,3])
pre = inference(self.test_image_placeholder, FLAGS.num_residual_blocks, reuse=False)
pre_x,pre_q = tf.slice(pre, [0, 0], [2, 3]),tf.slice(pre, [0, 3], [2, 4])
saver = tf.train.Saver()
with tf.Session() as sess:
checkpoint_path = os.path.join(train_dir, 'model.ckpt-1500')
saver.restore(sess, checkpoint_path)
data_gen = gen_data_batch(datasource)
for i in range(0,len(datasource.images),2):
#np_image = datasource.images[i:i+2]
np_images, np_poses_x, np_poses_q = next(data_gen)
feed = {self.test_image_placeholder: np_images}
pose_q = np.asarray(np_poses_q)
#print('Valid:',pose_q)
pose_x = np.asarray(np_poses_x)
predicted_x,predicted_q = sess.run([pre_x,pre_q],feed_dict=feed)
#print('image_{}:{}'.format(i,i+1),predicted_x,predicted_q)
#print predicted_x[0,:],predicted_x[1,:]
#print pose_q
for row in range(2):
pose_q_ = np.squeeze(pose_q[row,:])
pose_x_ = np.squeeze(pose_x[row,:])
predicted_q_ = np.squeeze(predicted_q[row,:])
predicted_x_ = np.squeeze(predicted_x[row,:])
q1 = pose_q_ / np.linalg.norm(pose_q_)
q2 = predicted_q_ / np.linalg.norm(predicted_q_)
d = abs(np.sum(np.multiply(q1, q2)))
theta = 2 * np.arccos(d) * 180 / math.pi
error_x = np.linalg.norm(pose_x_ - predicted_x_)
results[i+row, :] = [error_x, theta]
print 'Iteration: ', i, ' Error XYZ (m): ', error_x, ' Error Q (degrees): ', theta
median_result = np.median(results, axis=0)
print 'Median error ', median_result[0], 'm and ', median_result[1], 'degrees.'
if __name__ == '__main__':
train = Train()
train.test()