-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_anchor_datamanager_config_module.py
208 lines (137 loc) · 8.04 KB
/
test_anchor_datamanager_config_module.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
from configs.configs import parsing_configs
from net.box_utils import anchor_np_op
from net.box_utils import boxes_np_op
from data_manager import data_manager
from net.base_net import ssd_net
import cv2
np.set_printoptions(suppress = True)
def render_boxs_info_for_display(image, anchors, labels, scores, encode_box, original_ground_truth, scales, net_out, anchor_pos_iou = 0):
postive_number = 0
for index, score in enumerate(scores):
# print("current score is %f" % (score))
# print("current label :" + str(labels[index]))
if score > 0:
postive_number += 1
# print("encode gt box is " + str(encode_box[index]))
# # print("net out encode is " + str(net_out[index]))
#
# decode_box = boxes_np_op.decode_boxes(np.expand_dims(encode_box[index],axis=0), np.expand_dims(anchors[index], axis=0), scales)
#
# print("current score is %f"%(score))
# print("anchor box : " + str(anchors[index]))
# print("decode gt box : " + str(decode_box))
# print("original gt box" + str(original_ground_truth[index]))
# print("current label :" + str(labels[index]))
#
# image = render_rectangle_box(image, anchors[index], colour=(0, 0, 255))
# image = render_rectangle_box(image, decode_box[0], colour=(0, 255, 0))
# image = render_rectangle_box(image, original_ground_truth[index], colour=(255, 0, 0), offset = 3, thickness=2)
print("Total postive number is %d"%(postive_number))
return image
def render_rectangle_box(image, box, colour = (255, 255, 255), offset = 0, thickness = 1):
"""
:param image: 需要显示的图片
:param box: box信息
:param colour: 颜色信息
:param offset: box偏移
:param thickness: 线条宽度
:return:
"""
height,width, channel = image.shape
y_start = int(height * box[0]) + offset if int(height * box[0]) + offset > 0 else 0
x_start = int(width * box[1]) + offset if int(width * box[1]) + offset > 0 else 0
y_end = int(height * box[2]) + offset if int(height * box[2]) + offset < height else height - 1
x_end = int(width * box[3]) + offset if int(width * box[3]) + offset < width else width - 1
image = cv2.rectangle(image,(x_start,y_start), (x_end,y_end), color=colour, thickness= thickness)
return image
def judge_data_valid(image_name , gt_box_label, image =None, extra = None):
if np.any(np.isnan(gt_box_label)) == True:
nan_index = np.where(np.isnan(gt_box_label))
print("current image name is %s"%(image_name))
print(gt_box_label[nan_index[0],:])
print("***************************************")
if extra is not None:
print(extra)
current_gt_label = gt_box_label[nan_index[0],:]
if image is not None:
image = render_rectangle_box(image, current_gt_label[:, 26:30][0], colour=(255, 0, 0), offset=0, thickness=2)
cv2.imshow("vliad", image)
cv2.waitKey()
# print(nan_index)
if __name__=="__main__":
config_path = "./configs/ssd_dsod_300.yaml"
# tf_record_path = "/home/tcl/ImageSet/voc/tf_record/test"
configs = parsing_configs(config_path)
base_net_info = configs[0]
anchor_info = configs[1]
extract_feature_info = configs[2]
loss_info = configs[3]
extra_info = configs[4]
ground_truth = np.array([[0.25, 0.25, 0.5,0.5, 0],
[0.5, 0.5, 0.75, 0.75, 3],
[0.3, 0.3, 0.7 , 0.7, 4]])
ground_truth_tensor = tf.convert_to_tensor(ground_truth,dtype=tf.float32)
base_anchor_sizes = anchor_info["anchor_size"]
anchor_ratios = anchor_info["anchor_ratios"]
feat_shapes = anchor_info["feature_shape"]
anchor_strides = anchor_info["anchor_strides"]
scale_factors = anchor_info["prior_scaling"]
anchor_offset = anchor_info["anchor_offset"]
anchor_pos_iou = anchor_info["anchor_pos_iou_threshold"]
class_numer = base_net_info["class_number"]
image_shape = base_net_info["base_net_size"]
is_training = base_net_info["train_step"]
tf_record_path = extra_info["tf_record_path"]
total_number = 0
#ymin xmin ymax xmax
all_anchors, total_number = anchor_np_op.make_anchors_for_all_layer(image_shape, image_shape, base_anchor_sizes, anchor_ratios, anchor_strides, feat_shapes,
anchor_offset=anchor_offset)
batch_size = 2
image_size = 300
data_provider = data_manager.Data_Manager(tf_record_path, batch_size, is_training, image_size, all_anchors, class_numer, scale_factors, anchor_pos_iou)
net = ssd_net.SSD_Net(base_net_info, anchor_info, extract_feature_info, all_anchors, loss_info)
print("总共的anchor个数为: %d"%(total_number))
all_anchors = np.concatenate(all_anchors, axis=0)
all_anchors_tensor = tf.constant(all_anchors, dtype=tf.float32)
total_localization_loss, total_classification_loss, total_loss = net.build_loss(net.multibox_layer_out, net.labels, net.total_anchor_number)
regular_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
total_loss = total_loss + tf.add_n(regular_loss)
init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]
neg_number = tf.get_collection('neg_number')
pos_number = tf.get_collection('pos_number')
with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for batch_number in range(100):
print("*****************************************************************************")
image_name_batch, image_batch, gt_label_batch, num_object, img_height, img_width = \
sess.run((data_provider.next_batch()))
for i in range(len(image_name_batch)):
print("-----------------------------%s display start-------------------------------------------------"%(image_name_batch[i]))
image = render_boxs_info_for_display(image_batch[i], all_anchors, gt_label_batch[i][:,:21], gt_label_batch[i][:, 25], gt_label_batch[i][:, 21:25], gt_label_batch[i][:, 26:30], scale_factors, None, anchor_pos_iou)
print("------------------------------%s display end -------------------------------------------------------"%(image_name_batch[i]))
#
# cv2.imshow("boxs_info_display", image.astype(np.uint8))
# cv2.waitKey(0)
# judge_data_valid(image_name_batch[i], gt_label_batch[i], image_batch[i])
r_total_localization_loss, r_total_classification_loss, r_total_loss, r_localization,r_neg_number, r_pos_number = sess.run([total_localization_loss, total_classification_loss, total_loss , net.net_out, neg_number, pos_number],feed_dict={net.labels :gt_label_batch, net.inputs : image_batch , net.is_training:True})
# for i in range(len(image_name_batch)):
#
# print("-------------------------------------------------------------------------------------")
#
# image = render_boxs_info_for_display(image_batch[i], all_anchors, gt_label_batch[i][:,:21], gt_label_batch[i][:, 25], gt_label_batch[i][:, 21:25], gt_label_batch[i][:, 26:30], scale_factors, r_localization[i][:,21:25], anchor_pos_iou)
#
# print("-------------------------------------------------------------------------------------")
#
# cv2.imshow("boxs_info_display", image)
# cv2.waitKey(0)
print("localization loss is %f classification loss is %f total loss is %f"%(r_total_localization_loss, r_total_classification_loss, r_total_loss))
print("r_neg_number is " + str(r_neg_number))
print("r_pos_number is " + str(r_pos_number))