def detect(det_net, inference_save_path, real_test_imgname_list): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) # [1, None, None, 3] detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(a_img_name) start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) show_indices = detected_scores >= 0.7 show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( np.squeeze(resized_img, 0), boxes=show_boxes, labels=show_categories, scores=show_scores) nake_name = a_img_name.split('/')[-1] # print (inference_save_path + '/' + nake_name) cv2.imwrite(inference_save_path + '/' + nake_name, final_detections[:, :, ::-1]) tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(real_test_imgname_list))
def eval_ship(img_num, mode): with tf.Graph().as_default(): img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \ next_batch(dataset_name=cfgs.DATASET_NAME, batch_size=cfgs.BATCH_SIZE, shortside_len=cfgs.SHORT_SIDE_LEN, is_training=False) gtboxes_and_label = tf.py_func( back_forward_convert, inp=[tf.squeeze(gtboxes_and_label_batch, 0)], Tout=tf.float32) gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6]) gtboxes_and_label_minAreaRectangle = get_horizen_minAreaRectangle( gtboxes_and_label) gtboxes_and_label_minAreaRectangle = tf.reshape( gtboxes_and_label_minAreaRectangle, [-1, 5]) # *********************************************************************************************** # * share net * # *********************************************************************************************** _, share_net = get_network_byname(net_name=cfgs.NET_NAME, inputs=img_batch, num_classes=None, is_training=True, output_stride=None, global_pool=False, spatial_squeeze=False) # *********************************************************************************************** # * RPN * # *********************************************************************************************** rpn = build_rpn.RPN( net_name=cfgs.NET_NAME, inputs=img_batch, gtboxes_and_label=None, is_training=False, share_head=False, share_net=share_net, stride=cfgs.STRIDE, anchor_ratios=cfgs.ANCHOR_RATIOS, anchor_scales=cfgs.ANCHOR_SCALES, scale_factors=cfgs.SCALE_FACTORS, base_anchor_size_list=cfgs. BASE_ANCHOR_SIZE_LIST, # P2, P3, P4, P5, P6 level=cfgs.LEVEL, top_k_nms=cfgs.RPN_TOP_K_NMS, rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD, max_proposals_num=cfgs.MAX_PROPOSAL_NUM, rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD, rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD, rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE, rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE, remove_outside_anchors=False, # whether remove anchors outside rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME]) # rpn predict proposals rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals( ) # rpn_score shape: [300, ] # *********************************************************************************************** # * Fast RCNN * # *********************************************************************************************** fast_rcnn = build_fast_rcnn1.FastRCNN( feature_pyramid=rpn.feature_pyramid, rpn_proposals_boxes=rpn_proposals_boxes, rpn_proposals_scores=rpn_proposals_scores, img_shape=tf.shape(img_batch), roi_size=cfgs.ROI_SIZE, roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE, scale_factors=cfgs.SCALE_FACTORS, gtboxes_and_label=None, gtboxes_and_label_minAreaRectangle= gtboxes_and_label_minAreaRectangle, fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, fast_rcnn_maximum_boxes_per_img=100, fast_rcnn_nms_max_boxes_per_class=cfgs. FAST_RCNN_NMS_MAX_BOXES_PER_CLASS, show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD, # show detections which score >= 0.6 num_classes=cfgs.CLASS_NUM, fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE, fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE, fast_rcnn_positives_iou_threshold=cfgs. FAST_RCNN_IOU_POSITIVE_THRESHOLD, # iou>0.5 is positive, iou<0.5 is negative use_dropout=cfgs.USE_DROPOUT, weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], is_training=False, level=cfgs.LEVEL) fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category, \ fast_rcnn_decode_boxes_rotate, fast_rcnn_score_rotate, num_of_objects_rotate, detection_category_rotate = \ fast_rcnn.fast_rcnn_predict() if mode == 0: fast_rcnn_decode_boxes_rotate = get_horizen_minAreaRectangle( fast_rcnn_decode_boxes_rotate, False) # train init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = restore_model.get_restorer() with tf.Session() as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) gtboxes_horizontal_dict = {} predict_horizontal_dict = {} gtboxes_rotate_dict = {} predict_rotate_dict = {} for i in range(img_num): start = time.time() _img_name_batch, _img_batch, _gtboxes_and_label, _gtboxes_and_label_minAreaRectangle, \ _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category, _fast_rcnn_decode_boxes_rotate, \ _fast_rcnn_score_rotate, _detection_category_rotate \ = sess.run([img_name_batch, img_batch, gtboxes_and_label, gtboxes_and_label_minAreaRectangle, fast_rcnn_decode_boxes, fast_rcnn_score, detection_category, fast_rcnn_decode_boxes_rotate, fast_rcnn_score_rotate, detection_category_rotate]) end = time.time() # gtboxes convert dict gtboxes_horizontal_dict[str(_img_name_batch[0])] = [] predict_horizontal_dict[str(_img_name_batch[0])] = [] gtboxes_rotate_dict[str(_img_name_batch[0])] = [] predict_rotate_dict[str(_img_name_batch[0])] = [] gtbox_horizontal_list, predict_horizontal_list = \ make_dict_packle(_gtboxes_and_label_minAreaRectangle, _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category) if mode == 0: gtbox_rotate_list, predict_rotate_list = \ make_dict_packle(_gtboxes_and_label_minAreaRectangle, _fast_rcnn_decode_boxes_rotate, _fast_rcnn_score_rotate, _detection_category_rotate) else: gtbox_rotate_list, predict_rotate_list = \ make_dict_packle(_gtboxes_and_label, _fast_rcnn_decode_boxes_rotate, _fast_rcnn_score_rotate, _detection_category_rotate) gtboxes_horizontal_dict[str( _img_name_batch[0])].extend(gtbox_horizontal_list) predict_horizontal_dict[str( _img_name_batch[0])].extend(predict_horizontal_list) gtboxes_rotate_dict[str( _img_name_batch[0])].extend(gtbox_rotate_list) predict_rotate_dict[str( _img_name_batch[0])].extend(predict_rotate_list) view_bar( '{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i + 1, img_num) fw1 = open('gtboxes_horizontal_dict.pkl', 'w') fw2 = open('predict_horizontal_dict.pkl', 'w') fw3 = open('gtboxes_rotate_dict.pkl', 'w') fw4 = open('predict_rotate_dict.pkl', 'w') pickle.dump(gtboxes_horizontal_dict, fw1) pickle.dump(predict_horizontal_dict, fw2) pickle.dump(gtboxes_rotate_dict, fw3) pickle.dump(predict_rotate_dict, fw4) fw1.close() fw2.close() fw3.close() fw4.close() coord.request_stop() coord.join(threads)
def eval_with_plac(img_dir, det_net, image_ext, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, is_resize=False) det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_h_batch=None, gtboxes_r_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') all_boxes_h = [] all_boxes_r = [] imgs = os.listdir(img_dir) for i, a_img_name in enumerate(imgs): a_img_name = a_img_name.split(image_ext)[0] recs = {} recs[a_img_name] = parse_rec( os.path.join(test_annotation_path, a_img_name + '.xml')) #R = [obj for obj in recs[a_img_name]] bbox = np.squeeze(np.array([x['bbox'] for x in recs[a_img_name]])) #labels = bbox[:, -1] if len(bbox.shape) == 1: bbox = np.expand_dims(bbox, axis=0) labels = bbox[:, -1] raw_img = cv2.imread(os.path.join(img_dir, a_img_name + image_ext)) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, \ det_boxes_r_, det_scores_r_, det_category_r_ = \ sess.run( [img_batch, det_boxes_r, det_scores_r, det_category_r], feed_dict={img_plac: raw_img} ) end = time.time() det_boxes_r_ = det_boxes_r_[det_scores_r_ >= 0.4] det_category_r_ = det_category_r_[det_scores_r_ >= 0.4] det_scores_r_ = det_scores_r_[det_scores_r_ >= 0.4] keep = nms_rotate.nms_rotate_cpu(det_boxes_r_, det_scores_r_, 0.3, 20) det_boxes_r_ = det_boxes_r_[keep] det_scores_r_ = det_scores_r_[keep] det_category_r_ = det_category_r_[keep] ##### box_ratio > 2 or < 1/2 index = (det_boxes_r_[:, 2] / det_boxes_r_[:, 3] > 2) | (det_boxes_r_[:, 2] / det_boxes_r_[:, 3] <= 0.5) det_boxes_r_ = det_boxes_r_[index] det_scores_r_ = det_scores_r_[index] det_category_r_ = det_category_r_[index] # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: det_detections_h = draw_box_in_img.draw_rotate_box_cv1( np.squeeze(resized_img, 0), boxes=bbox, labels=labels, scores=np.ones(bbox.shape[0])) det_detections_r = draw_box_in_img.draw_rotate_box_cv( np.squeeze(resized_img, 0), boxes=det_boxes_r_, labels=det_category_r_, scores=det_scores_r_) save_dir = os.path.join(cfgs.TEST_SAVE_PATH, cfgs.VERSION) tools.mkdir(save_dir) cv2.imwrite(save_dir + '/' + a_img_name + '_h.jpg', det_detections_h[:, :, ::-1]) cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg', det_detections_r[:, :, ::-1]) # xmin, ymin, xmax, ymax = det_boxes_h_[:, 0], det_boxes_h_[:, 1], \ # det_boxes_h_[:, 2], det_boxes_h_[:, 3] if det_boxes_r_.shape[0] != 0: resized_h, resized_w = resized_img.shape[1], resized_img.shape[ 2] det_boxes_r_ = forward_convert(det_boxes_r_, False) det_boxes_r_[:, 0::2] *= (raw_w / resized_w) det_boxes_r_[:, 1::2] *= (raw_h / resized_h) det_boxes_r_ = back_forward_convert(det_boxes_r_, False) x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \ det_boxes_r_[:, 3], det_boxes_r_[:, 4] # xmin = xmin * raw_w / resized_w # xmax = xmax * raw_w / resized_w # ymin = ymin * raw_h / resized_h # ymax = ymax * raw_h / resized_h # boxes_h = np.transpose(np.stack([xmin, ymin, xmax, ymax])) boxes_r = np.transpose(np.stack([x_c, y_c, w, h, theta])) # dets_h = np.hstack((det_category_h_.reshape(-1, 1), # det_scores_h_.reshape(-1, 1), # boxes_h)) dets_r = np.hstack((det_category_r_.reshape(-1, 1), det_scores_r_.reshape(-1, 1), boxes_r)) # all_boxes_h.append(dets_h) all_boxes_r.append(dets_r) tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(imgs)) fw1 = open(cfgs.VERSION + '_detections_h.pkl', 'w') fw2 = open(cfgs.VERSION + '_detections_r.pkl', 'w') pickle.dump(all_boxes_h, fw1) pickle.dump(all_boxes_r, fw2)
def eval_with_plac(det_net, real_test_imgname_list, img_root, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not BGR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') all_boxes = [] for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(os.path.join(img_root, a_img_name)) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( np.squeeze(resized_img, 0), boxes=show_boxes, labels=show_categories, scores=show_scores) if not os.path.exists(cfgs.TEST_SAVE_PATH): os.makedirs(cfgs.TEST_SAVE_PATH) nake_name = os.path.split(a_img_name)[1] cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + nake_name, final_detections[:, :, ::-1]) # cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + a_img_name + '.jpg', # final_detections[:, :, ::-1]) xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) dets = np.hstack((detected_categories.reshape(-1, 1), detected_scores.reshape(-1, 1), boxes)) all_boxes.append(dets) tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(real_test_imgname_list)) save_dir = os.path.join(cfgs.EVALUATE_DIR, cfgs.VERSION) if not os.path.exists(save_dir): os.makedirs(save_dir) fw1 = open(os.path.join(save_dir, 'detections.pkl'), 'wb') pickle.dump(all_boxes, fw1)
def test(img_num): with tf.Graph().as_default(): # img = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8) img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \ next_batch(dataset_name=cfgs.DATASET_NAME, batch_size=cfgs.BATCH_SIZE, shortside_len=cfgs.SHORT_SIDE_LEN, is_training=False) gtboxes_and_label = tf.py_func(back_forward_convert, inp=[tf.squeeze(gtboxes_and_label_batch, 0)], Tout=tf.float32) gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6]) # *********************************************************************************************** # * share net * # *********************************************************************************************** _, share_net = get_network_byname(net_name=cfgs.NET_NAME, inputs=img_batch, num_classes=None, is_training=True, output_stride=None, global_pool=False, spatial_squeeze=False) # *********************************************************************************************** # * RPN * # *********************************************************************************************** rpn = build_rpn.RPN(net_name=cfgs.NET_NAME, inputs=img_batch, gtboxes_and_label=gtboxes_and_label, is_training=False, share_head=False, share_net=share_net, anchor_ratios=cfgs.ANCHOR_RATIOS, anchor_scales=cfgs.ANCHOR_SCALES, anchor_angles=cfgs.ANCHOR_ANGLES, scale_factors=cfgs.SCALE_FACTORS, base_anchor_size_list=cfgs.BASE_ANCHOR_SIZE_LIST, # P2, P3, P4, P5, P6 level=cfgs.LEVEL, anchor_stride=cfgs.ANCHOR_STRIDE, top_k_nms=cfgs.RPN_TOP_K_NMS, kernel_size=cfgs.KERNEL_SIZE, use_angles_condition=True, anchor_angle_threshold=cfgs.RPN_ANCHOR_ANGLES_THRESHOLD, nms_angle_threshold=cfgs.RPN_NMS_ANGLES_THRESHOLD, rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD, max_proposals_num=cfgs.MAX_PROPOSAL_NUM, rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD, rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD, rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE, rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE, remove_outside_anchors=False, # whether remove anchors outside rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], scope='') # rpn predict proposals rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals() # rpn_score shape: [300, ] # _, _, rpn_top_k_boxes, rpn_top_k_scores = rpn.rpn_losses() # *********************************************************************************************** # * Fast RCNN * # *********************************************************************************************** fast_rcnn = build_fast_rcnn.FastRCNN(img_batch=img_batch, feature_pyramid=rpn.feature_pyramid, rpn_proposals_boxes=rpn_proposals_boxes, rpn_proposals_scores=rpn_proposals_scores, stop_gradient_for_proposals=False, img_shape=tf.shape(img_batch), roi_size=cfgs.ROI_SIZE, roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE, scale_factors=cfgs.SCALE_FACTORS, gtboxes_and_label=None, fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, top_k_nms=cfgs.FAST_RCNN_TOP_K_NMS, nms_angle_threshold=cfgs.FAST_RCNN_NMS_ANGLES_THRESHOLD, use_angle_condition=False, level=cfgs.LEVEL, fast_rcnn_maximum_boxes_per_img=100, fast_rcnn_nms_max_boxes_per_class=cfgs.FAST_RCNN_NMS_MAX_BOXES_PER_CLASS, show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD, # show detections which score >= 0.6 num_classes=cfgs.CLASS_NUM, fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE, fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE, fast_rcnn_positives_iou_threshold=cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD, boxes_angle_threshold=cfgs.FAST_RCNN_BOXES_ANGLES_THRESHOLD, use_dropout=cfgs.USE_DROPOUT, weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], is_training=False) fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \ fast_rcnn.fast_rcnn_predict() # test init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt = restore_model.get_restorer() config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) for i in range(img_num): start = time.time() _img_name_batch, _img_batch, _gtboxes_and_label, _fast_rcnn_decode_boxes, \ _fast_rcnn_score, _detection_category \ = sess.run([img_name_batch, img_batch, gtboxes_and_label, fast_rcnn_decode_boxes, fast_rcnn_score, detection_category]) end = time.time() _img_batch = np.squeeze(_img_batch, axis=0) _img_batch_fpn = help_utils.draw_box_cv(_img_batch, boxes=_fast_rcnn_decode_boxes, labels=_detection_category, scores=_fast_rcnn_score) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/{}_fpn.jpg'.format(str(_img_name_batch[0]).split('.tif')[0]), _img_batch_fpn) temp_label = np.reshape(_gtboxes_and_label[:, -1:], [-1, ]).astype(np.int64) _img_batch_gt = help_utils.draw_box_cv(_img_batch, boxes=_gtboxes_and_label[:, :-1], labels=temp_label, scores=None) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/{}_gt.jpg'.format(str(_img_name_batch[0]).split('.tif')[0]), _img_batch_gt) view_bar('{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i+1, img_num) coord.request_stop() coord.join(threads)
def eval_with_plac(det_net, real_test_imgname_list, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not BGR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) restorer, restore_ckpt = det_net.get_restorer() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') all_boxes = [] for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(a_img_name) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() if cfgs.SOFT_NMS: boxes_soft_nms = [] scores_soft_nms = [] category_soft_nms = [] tmp_boxes = np.reshape(detected_boxes, (cfgs.CLASS_NUM, -1, 4), order='C') # print("tmp_boxes is ", tmp_boxes.shape, 'type is ', type(tmp_boxes)) tmp_scores = np.reshape(detected_scores, (cfgs.CLASS_NUM, -1), order='C') tmp_category = np.reshape(detected_categories, (cfgs.CLASS_NUM, -1), order='C') for ind in range(cfgs.CLASS_NUM): tmp_class_boxes = tmp_boxes[ind, :, :] tmp_class_scores = tmp_scores[ind, :] tmp_class_category = tmp_category[ind, :] # print("tmp_boxes is ",tmp_class_boxes.shape,'type is ',type(tmp_class_boxes)) # print('tmp score is ',tmp_class_scores.shape) dets = np.hstack( (tmp_class_boxes, tmp_class_scores[:, np.newaxis])).astype(np.float32, copy=False) # keep=py_cpu_softnms(tmp_class_boxes,tmp_class_scores,method=4) keep = py_soft_nms(dets, 'greedy') # keep=gpu_nms(dets,cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,0) class_boxes = tmp_class_boxes[keep] class_scores = tmp_class_scores[keep] class_category = tmp_class_category[keep] boxes_soft_nms.append(class_boxes) scores_soft_nms.append(class_scores) category_soft_nms.append(class_category) detected_boxes = np.concatenate(boxes_soft_nms, axis=0) detected_scores = np.concatenate(scores_soft_nms, axis=0) detected_categories = np.concatenate(category_soft_nms, axis=0) # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( np.squeeze(resized_img, 0), boxes=show_boxes, labels=show_categories, scores=show_scores) if not os.path.exists(cfgs.TEST_SAVE_PATH): os.makedirs(cfgs.TEST_SAVE_PATH) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + a_img_name + '.jpg', final_detections[:, :, ::-1]) xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) dets = np.hstack((detected_categories.reshape(-1, 1), detected_scores.reshape(-1, 1), boxes)) all_boxes.append(dets) tools.view_bar( '{} image cost {}s'.format(os.path.basename(a_img_name), (end - start)), i + 1, len(real_test_imgname_list)) save_dir = os.path.join(cfgs.EVALUATE_DIR, cfgs.DATASET_NAME, cfgs.VERSION) if not os.path.exists(save_dir): os.makedirs(save_dir) fw1 = open(os.path.join(save_dir, 'detections.pkl'), 'wb') pickle.dump(all_boxes, fw1) return all_boxes
def detect(det_net, src_dir, res_dir, draw_imgs): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) # [1, None, None, 3] result_dict = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') sub_folders = os.listdir(src_dir) for sub_folder in sub_folders: folder_dir = os.path.join(src_dir, sub_folder) real_test_imgname_list = [ os.path.join(folder_dir, img_name) for img_name in os.listdir(folder_dir) ] tools.mkdir(os.path.join(res_dir, sub_folder)) for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(a_img_name) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, result_dict_ = \ sess.run( [img_batch, result_dict], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() detected_boxes, detected_scores, detected_categories = merge_result( result_dict_) nake_name = a_img_name.split('/')[-1] xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[ 2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) dets = np.hstack((detected_categories.reshape(-1, 1), detected_scores.reshape(-1, 1), boxes)) show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = boxes[show_indices] show_categories = detected_categories[show_indices] f = open( os.path.join(res_dir, sub_folder) + '/' + nake_name.split('.')[0] + '.txt', 'w') f.write('{}\n'.format(nake_name.split('.')[0])) # f.write('{}\n'.format(dets.shape[0])) # for inx in range(dets.shape[0]): # # f.write('%d %d %d %d %.3f\n' % (int(dets[inx][2]), # int(dets[inx][3]), # int(dets[inx][4]) - int(dets[inx][2]), # int(dets[inx][5]) - int(dets[inx][3]), # dets[inx][1])) f.write('{}\n'.format(show_boxes.shape[0])) for inx in range(show_boxes.shape[0]): f.write('%d %d %d %d %.3f\n' % (int(show_boxes[inx][0]), int(show_boxes[inx][1]), int(show_boxes[inx][2]) - int(show_boxes[inx][0]), int(show_boxes[inx][3]) - int(show_boxes[inx][1]), show_scores[inx])) if draw_imgs: final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( raw_img - np.array(cfgs.PIXEL_MEAN), boxes=show_boxes, labels=show_categories, scores=show_scores) tools.mkdir(cfgs.TEST_SAVE_PATH) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + nake_name, final_detections) tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(real_test_imgname_list))
def eval_coco(det_net, real_test_img_list, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not BGR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data(img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH, is_resize=False) if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: img_batch = (img_batch / 255 - tf.constant(cfgs.PIXEL_MEAN_)) / tf.constant(cfgs.PIXEL_STD) else: img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) # img_batch = (img_batch - tf.constant(cfgs.PIXEL_MEAN)) / (tf.constant(cfgs.PIXEL_STD)*255) img_batch = tf.expand_dims(img_batch, axis=0) detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') save_path = os.path.join('./eval_coco', cfgs.VERSION) tools.mkdir(save_path) fw_json_dt = open(os.path.join(save_path, 'coco_minival_ms.json'), 'w') coco_det = [] for i, a_img in enumerate(real_test_img_list): record = json.loads(a_img) raw_img = cv2.imread(record['fpath']) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() detected_scores_, detected_boxes_, detected_categories_ = [], [], [] for ss in [600, 800, 1000, 1200]: # cfgs.IMG_SHORT_SIDE_LEN: img_resize = cv2.resize(raw_img, (ss, ss)) resized_img, tmp_detected_boxes, tmp_detected_scores, tmp_detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: img_resize[:, :, ::-1]} # cv is BGR. But need RGB ) eval_indices = tmp_detected_scores >= 0.01 tmp_detected_scores = tmp_detected_scores[eval_indices] tmp_detected_boxes = tmp_detected_boxes[eval_indices] tmp_detected_categories = tmp_detected_categories[eval_indices] xmin, ymin, xmax, ymax = tmp_detected_boxes[:, 0], tmp_detected_boxes[:, 1], \ tmp_detected_boxes[:, 2], tmp_detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h resize_boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) detected_scores_.append(tmp_detected_scores) detected_boxes_.append(resize_boxes) detected_categories_.append(tmp_detected_categories) detected_scores_ = np.concatenate(detected_scores_) detected_boxes_ = np.concatenate(detected_boxes_) detected_categories_ = np.concatenate(detected_categories_) detected_scores, detected_boxes, detected_categories = [], [], [] for sub_class in range(1, cfgs.CLASS_NUM + 1): index = np.where(detected_categories_ == sub_class)[0] if len(index) == 0: continue tmp_boxes_h = detected_boxes_[index] tmp_label_h = detected_categories_[index] tmp_score_h = detected_scores_[index] tmp_boxes_h = np.array(tmp_boxes_h) tmp = np.zeros([tmp_boxes_h.shape[0], tmp_boxes_h.shape[1] + 1]) tmp[:, 0:-1] = tmp_boxes_h tmp[:, -1] = np.array(tmp_score_h) inx = nms.py_cpu_nms(dets=np.array(tmp, np.float32), thresh=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, max_output_size=500) detected_boxes.extend(np.array(tmp_boxes_h)[inx]) detected_scores.extend(np.array(tmp_score_h)[inx]) detected_categories.extend(np.array(tmp_label_h)[inx]) detected_scores = np.array(detected_scores) detected_boxes = np.array(detected_boxes) detected_categories = np.array(detected_categories) # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] # if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: # draw_img = (raw_img * np.array(cfgs.PIXEL_STD) + np.array(cfgs.PIXEL_MEAN_)) * 255 # else: # draw_img = raw_img + np.array(cfgs.PIXEL_MEAN) # draw_img = draw_img * (np.array(cfgs.PIXEL_STD)*255) + np.array(cfgs.PIXEL_MEAN) raw_img = np.array(raw_img, np.float32) final_detections = draw_box_in_img.draw_boxes_with_label_and_scores(raw_img, boxes=show_boxes, labels=show_categories, scores=show_scores, in_graph=False) if not os.path.exists(cfgs.TEST_SAVE_PATH): os.makedirs(cfgs.TEST_SAVE_PATH) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + record['ID'], final_detections) # cost much time for j, box in enumerate(detected_boxes): coco_det.append({'bbox': [float(box[0]), float(box[1]), float(box[2]-box[0]), float(box[3]-box[1])], 'score': float(detected_scores[j]), 'image_id': int(record['ID'].split('.jpg')[0].split('_000000')[-1]), 'category_id': int(classes_originID[LABEl_NAME_MAP[detected_categories[j]]])}) end = time.time() tools.view_bar('%s image cost %.3fs' % (record['ID'], (end - start)), i + 1, len(real_test_img_list)) json.dump(coco_det, fw_json_dt) fw_json_dt.close() return os.path.join(save_path, 'coco_minival_ms.json')
def detect(det_net, inference_save_path, real_test_imgname_list): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: img_batch = (img_batch / 255 - tf.constant( cfgs.PIXEL_MEAN_)) / tf.constant(cfgs.PIXEL_STD) else: img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) # [1, None, None, 3] detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(a_img_name) start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h detected_boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] # if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: # raw_img = (raw_img / 255 - np.array(cfgs.PIXEL_MEAN_)) /np.array(cfgs.PIXEL_STD) # else: # raw_img = raw_img - np.array(cfgs.PIXEL_MEAN) final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( raw_img, boxes=show_boxes, labels=show_categories, scores=show_scores, in_graph=False) nake_name = a_img_name.split('/')[-1] # print (inference_save_path + '/' + nake_name) cv2.imwrite(inference_save_path + '/' + nake_name, final_detections[:, :, ::-1]) tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(real_test_imgname_list))
def eval_with_plac(det_net, real_test_imgname_list, img_root, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not BGR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data(img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: img_batch = (img_batch / 255 - tf.constant(cfgs.PIXEL_MEAN_)) / tf.constant(cfgs.PIXEL_STD) else: img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) # img_batch = (img_batch - tf.constant(cfgs.PIXEL_MEAN)) / (tf.constant(cfgs.PIXEL_STD)*255) img_batch = tf.expand_dims(img_batch, axis=0) detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(os.path.join(img_root, a_img_name)) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] draw_img = np.squeeze(resized_img, 0) if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: draw_img = (draw_img * np.array(cfgs.PIXEL_STD) + np.array(cfgs.PIXEL_MEAN_)) * 255 else: draw_img = draw_img + np.array(cfgs.PIXEL_MEAN) # draw_img = draw_img * (np.array(cfgs.PIXEL_STD)*255) + np.array(cfgs.PIXEL_MEAN) final_detections = draw_box_in_img.draw_boxes_with_label_and_scores(draw_img, boxes=show_boxes, labels=show_categories, scores=show_scores, in_graph=False) if not os.path.exists(cfgs.TEST_SAVE_PATH): os.makedirs(cfgs.TEST_SAVE_PATH) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + a_img_name, final_detections[:, :, ::-1]) xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) dets = np.hstack((detected_categories.reshape(-1, 1), detected_scores.reshape(-1, 1), boxes)) # all_boxes.append(dets) # eval txt CLASS_VOC = NAME_LABEL_MAP.keys() write_handle = {} txt_dir = os.path.join('voc2012_eval', cfgs.VERSION, 'results', 'VOC2012', 'Main') tools.mkdir(txt_dir) for sub_class in CLASS_VOC: if sub_class == 'back_ground': continue write_handle[sub_class] = open(os.path.join(txt_dir, 'comp3_det_test_%s.txt' % sub_class), 'a+') for det in dets: command = '%s %.6f %.6f %.6f %.6f %.6f\n' % (a_img_name.split('/')[-1].split('.')[0], det[1], det[2], det[3], det[4], det[5]) write_handle[LABEl_NAME_MAP[det[0]]].write(command) for sub_class in CLASS_VOC: if sub_class == 'back_ground': continue write_handle[sub_class].close() tools.view_bar('%s image cost %.3fs' % (a_img_name, (end - start)), i + 1, len(real_test_imgname_list))
def eval_with_plac(det_net, imgId_list, coco, out_json_root, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) if cfgs.NET_NAME in ['resnet152_v1d', 'resnet101_v1d', 'resnet50_v1d']: img_batch = (img_batch / 255 - tf.constant( cfgs.PIXEL_MEAN_)) / tf.constant(cfgs.PIXEL_STD) else: img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True # coco_test_results = [] with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') for i, imgid in enumerate(imgId_list): imgname = coco.loadImgs(ids=[imgid])[0]['file_name'] raw_img = cv2.imread( os.path.join("/home/yjr/DataSet/COCO/2017/test2017", imgname)) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() if draw_imgs: show_indices = detected_scores >= cfgs.VIS_SCORE show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] draw_img = np.squeeze(resized_img, 0) if cfgs.NET_NAME in [ 'resnet152_v1d', 'resnet101_v1d', 'resnet50_v1d' ]: draw_img = (draw_img * np.array(cfgs.PIXEL_STD) + np.array(cfgs.PIXEL_MEAN_)) * 255 else: draw_img = draw_img + np.array(cfgs.PIXEL_MEAN) final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( draw_img, boxes=show_boxes, labels=show_categories, scores=show_scores, in_graph=False) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + str(imgid) + '.jpg', final_detections[:, :, ::-1]) xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes = np.transpose( np.stack([xmin, ymin, xmax - xmin, ymax - ymin])) dets = np.hstack((detected_categories.reshape(-1, 1), detected_scores.reshape(-1, 1), boxes)) a_img_detect_result = [] for a_det in dets: label, score, bbox = a_det[0], a_det[1], a_det[2:] cat_id = classes_originID[LABEL_NAME_MAP[label]] if score < 0.00001: continue det_object = { "image_id": imgid, "category_id": cat_id, "bbox": bbox.tolist(), "score": float(score) } # print (det_object) a_img_detect_result.append(det_object) f = open( os.path.join(out_json_root, 'each_img', str(imgid) + '.json'), 'w') json.dump(a_img_detect_result, f) # , indent=4 f.close() del a_img_detect_result del dets del boxes del resized_img del raw_img tools.view_bar('{} image cost {}s'.format(imgid, (end - start)), i + 1, len(imgId_list))
def detect(det_net, inference_save_path, rgb_real_test_imgname_list, ir_real_test_imgname_list): # 1. preprocess img rgb_img_plac = tf.placeholder(tf.uint8, [None, None, 3], 'rgb') # is RGB. not GBR rgb_img_batch = tf.cast(rgb_img_plac, tf.float32) rgb_img_batch = short_side_resize_for_inference_data(img_tensor=rgb_img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) ir_img_plac = tf.placeholder(tf.uint8, [None, None, 3], 'ir') # is RGB. not GBR ir_img_batch = tf.cast(ir_img_plac, tf.float32) ir_img_batch = short_side_resize_for_inference_data(img_tensor=ir_img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) rgb_img_batch = rgb_img_batch - tf.constant(cfgs.RGB_PIXEL_MEAN) ir_img_batch = ir_img_batch - tf.constant(cfgs.IR_PIXEL_MEAN) #img_batch = (img_batch - tf.constant(cfgs.PIXEL_MEAN)) / (tf.constant(cfgs.PIXEL_STD)*255) rgb_img_batch = tf.expand_dims(rgb_img_batch, axis=0) # [1, None, None, 3] ir_img_batch = tf.expand_dims(ir_img_batch, axis=0) # [1, None, None, 3] detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( rgb_input_img_batch=rgb_img_batch, ir_input_img_batch=ir_img_batch, seg_mask_batch = None, gtboxes_batch=None) init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt, model_variables = det_net.get_restorer_test() print(restore_ckpt) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') for i, rgb_img_name in enumerate(rgb_real_test_imgname_list): rgb_raw_img = cv2.imread(rgb_img_name)[:, :, ::-1] ir_raw_img = cv2.imread(ir_real_test_imgname_list[i])[:, :, ::-1] start = time.time() rgb_resized_img, ir_resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [rgb_img_batch, ir_img_batch, detection_boxes, detection_scores, detection_category], feed_dict={rgb_img_plac: rgb_raw_img, ir_img_plac: ir_raw_img} ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) raw_h, raw_w = ir_raw_img.shape[0], rgb_raw_img.shape[1] xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = rgb_resized_img.shape[1], ir_resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h detected_boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax])) show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] nake_name = rgb_img_name.split('/')[-1] f1 = open(inference_save_path + '/txt/' + nake_name.split('.')[0]+'.txt', 'w') final_detections = draw_box_in_img.draw_boxes_with_label_and_scores(rgb_raw_img - np.array(cfgs.RGB_PIXEL_MEAN), boxes=show_boxes, labels=show_categories, scores=show_scores, txt_file=f1, img_name=nake_name.split('.')[0]) # print (inference_save_path + '/' + nake_name) cv2.imwrite(inference_save_path + '/img/' + nake_name, final_detections[:, :, ::-1]) tools.view_bar('{} image cost {}s'.format(rgb_img_name, (end - start)), i + 1, len(ir_real_test_imgname_list))
def eval_dict_convert(img_num, mode): with tf.Graph().as_default(): # img = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8) img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \ next_batch(dataset_name=cfgs.DATASET_NAME, batch_size=cfgs.BATCH_SIZE, shortside_len=cfgs.SHORT_SIDE_LEN, is_training=False) gtboxes_and_label = tf.py_func( back_forward_convert, inp=[tf.squeeze(gtboxes_and_label_batch, 0)], Tout=tf.float32) gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6]) gtboxes_and_label_minAreaRectangle = get_horizon_minAreaRectangle( gtboxes_and_label) gtboxes_and_label_minAreaRectangle = tf.reshape( gtboxes_and_label_minAreaRectangle, [-1, 5]) # *********************************************************************************************** # * share net * # *********************************************************************************************** _, share_net = get_network_byname(net_name=cfgs.NET_NAME, inputs=img_batch, num_classes=None, is_training=True, output_stride=None, global_pool=False, spatial_squeeze=False) # *********************************************************************************************** # * RPN * # *********************************************************************************************** rpn = build_rpn.RPN( net_name=cfgs.NET_NAME, inputs=img_batch, gtboxes_and_label=gtboxes_and_label, is_training=False, share_head=False, share_net=share_net, anchor_ratios=cfgs.ANCHOR_RATIOS, anchor_scales=cfgs.ANCHOR_SCALES, anchor_angles=cfgs.ANCHOR_ANGLES, scale_factors=cfgs.SCALE_FACTORS, base_anchor_size_list=cfgs. BASE_ANCHOR_SIZE_LIST, # P2, P3, P4, P5, P6 level=cfgs.LEVEL, anchor_stride=cfgs.ANCHOR_STRIDE, top_k_nms=cfgs.RPN_TOP_K_NMS, kernel_size=cfgs.KERNEL_SIZE, use_angles_condition=False, anchor_angle_threshold=cfgs.RPN_ANCHOR_ANGLES_THRESHOLD, nms_angle_threshold=cfgs.RPN_NMS_ANGLES_THRESHOLD, rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD, max_proposals_num=cfgs.MAX_PROPOSAL_NUM, rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD, rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD, # iou>=0.7 is positive box, iou< 0.3 is negative rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE, rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE, remove_outside_anchors=cfgs. IS_FILTER_OUTSIDE_BOXES, # whether remove anchors outside rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], scope='') rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals( ) # rpn_score shape: [300, ] _, _, rpn_predict_boxes, rpn_predict_scores = rpn.rpn_losses() # *********************************************************************************************** # * Fast RCNN * # *********************************************************************************************** fast_rcnn = build_fast_rcnn.FastRCNN( img_batch=img_batch, feature_pyramid=rpn.feature_pyramid, rpn_proposals_boxes=rpn_proposals_boxes, rpn_proposals_scores=rpn_proposals_scores, stop_gradient_for_proposals=False, img_shape=tf.shape(img_batch), roi_size=cfgs.ROI_SIZE, roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE, scale_factors=cfgs.SCALE_FACTORS, gtboxes_and_label=None, fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, top_k_nms=cfgs.FAST_RCNN_TOP_K_NMS, nms_angle_threshold=cfgs.FAST_RCNN_NMS_ANGLES_THRESHOLD, use_angle_condition=False, level=cfgs.LEVEL, fast_rcnn_maximum_boxes_per_img=100, fast_rcnn_nms_max_boxes_per_class=cfgs. FAST_RCNN_NMS_MAX_BOXES_PER_CLASS, show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD, # show detections which score >= 0.6 num_classes=cfgs.CLASS_NUM, fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE, fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE, fast_rcnn_positives_iou_threshold=cfgs. FAST_RCNN_IOU_POSITIVE_THRESHOLD, boxes_angle_threshold=cfgs.FAST_RCNN_BOXES_ANGLES_THRESHOLD, use_dropout=cfgs.USE_DROPOUT, weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], is_training=False) fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \ fast_rcnn.fast_rcnn_predict() ############################################################################################## if cfgs.NEED_AUXILIARY: predict_boxes = tf.concat( [fast_rcnn_decode_boxes, rpn_predict_boxes], axis=0) predict_scores = tf.concat( [fast_rcnn_score, rpn_predict_scores - 0.2], axis=0) rpn_predict_label = tf.ones([ tf.shape(rpn_predict_scores)[0], ], tf.int64) labels = tf.concat([detection_category, rpn_predict_label], axis=0) # valid_indices = nms_rotate.nms_rotate(decode_boxes=predict_boxes, # scores=predict_scores, # iou_threshold=0.15, # max_output_size=30, # use_angle_condition=False, # angle_threshold=15, # use_gpu=True) valid_indices = tf.py_func(nms_rotate.nms_rotate_cpu, inp=[ predict_boxes, predict_scores, tf.constant(0.15, tf.float32), tf.constant(30, tf.float32) ], Tout=tf.int64) fast_rcnn_decode_boxes = tf.gather(predict_boxes, valid_indices) fast_rcnn_score = tf.gather(predict_scores, valid_indices) detection_category = tf.gather(labels, valid_indices) ############################################################################################## if mode == 0: fast_rcnn_decode_boxes = get_horizon_minAreaRectangle( fast_rcnn_decode_boxes, False) # train init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = restore_model.get_restorer() config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) gtboxes_dict = {} predict_dict = {} for i in range(img_num): start = time.time() _img_name_batch, _img_batch, _gtboxes_and_label, _fast_rcnn_decode_boxes, \ _gtboxes_and_label_minAreaRectangle, _fast_rcnn_score, _detection_category \ = sess.run([img_name_batch, img_batch, gtboxes_and_label, fast_rcnn_decode_boxes, gtboxes_and_label_minAreaRectangle, fast_rcnn_score, detection_category]) end = time.time() # gtboxes convert dict gtboxes_dict[str(_img_name_batch[0])] = [] predict_dict[str(_img_name_batch[0])] = [] # for j, box in enumerate(_gtboxes_and_label): # bbox_dict = {} # bbox_dict['bbox'] = np.array(_gtboxes_and_label[j, :-1], np.float64) # bbox_dict['name'] = LABEl_NAME_MAP[int(_gtboxes_and_label[j, -1])] # gtbox_dict[str(_img_name_batch[0])].append(bbox_dict) # # for label in NAME_LABEL_MAP.keys(): # if label == 'back_ground': # continue # else: # temp_dict = {} # temp_dict['name'] = label # # ind = np.where(_detection_category == NAME_LABEL_MAP[label])[0] # temp_boxes = _fast_rcnn_decode_boxes[ind] # temp_score = np.reshape(_fast_rcnn_score[ind], [-1, 1]) # temp_dict['bbox'] = np.array(np.concatenate([temp_boxes, temp_score], axis=1), np.float64) # predict_dict[str(_img_name_batch[0])].append(temp_dict) if mode == 0: gtboxes_list, predict_list = \ make_dict_packle(_gtboxes_and_label_minAreaRectangle, _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category) else: gtboxes_list, predict_list = \ make_dict_packle(_gtboxes_and_label, _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category) gtboxes_dict[str(_img_name_batch[0])].extend(gtboxes_list) predict_dict[str(_img_name_batch[0])].extend(predict_list) view_bar( '{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i + 1, img_num) fw1 = open('gtboxes_dict.pkl', 'w') fw2 = open('predict_dict.pkl', 'w') pickle.dump(gtboxes_dict, fw1) pickle.dump(predict_dict, fw2) fw1.close() fw2.close() coord.request_stop() coord.join(threads)
def eval_with_plac(img_dir, det_net, num_imgs, image_ext, draw_imgs, test_annotation_path): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, is_resize=False) det_boxes_h, det_scores_h, det_category_h, \ det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_h_batch=None, gtboxes_r_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) global_step_tensor = slim.get_or_create_global_step() eval_result = [] last_checkpoint_name = None while True: restorer, restore_ckpt = det_net.get_restorer() #saver = tf.train.Saver(max_to_keep=10) start_time = time.time() model_path = os.path.splitext(os.path.basename(restore_ckpt))[0] if model_path == None: print("Wait for available checkpoint") elif last_checkpoint_name == model_path: print( "Already evaluated checkpoint {}, we will try evaluation in {} seconds" .format(model_path, EVAL_INTERVAL)) #continue else: print('Last ckpt was {}, new ckpt is {}'.format( last_checkpoint_name, model_path)) last_checkpoint_name = model_path config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) sess.run(global_step_tensor.initializer) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model', restore_ckpt) global_stepnp = tf.train.global_step(sess, global_step_tensor) print('#########################', global_stepnp) all_boxes_h = [] all_boxes_r = [] imgs = os.listdir(img_dir) imgs_len = len(imgs) none_detected_image = [] for i, a_img_name in enumerate(imgs[:]): a_img_name = a_img_name.split(image_ext)[0] image_name = a_img_name + image_ext print('\n', a_img_name) raw_img = cv2.imread( os.path.join(img_dir, a_img_name + image_ext)) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, det_boxes_h_, det_scores_h_, det_category_h_, \ det_boxes_r_, det_scores_r_, det_category_r_ = \ sess.run( [img_batch, det_boxes_h, det_scores_h, det_category_h, det_boxes_r, det_scores_r, det_category_r], feed_dict={img_plac: raw_img} ) end = time.time() print("det category H : ", det_category_h_) print("det category R : ", det_category_r_) # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: det_detections_h = draw_box_in_img.draw_box_cv( np.squeeze(resized_img, 0), boxes=det_boxes_h_, labels=det_category_h_, scores=det_scores_h_) det_detections_r = draw_box_in_img.draw_rotate_box_cv( np.squeeze(resized_img, 0), boxes=det_boxes_r_, labels=det_category_r_, scores=det_scores_r_) save_dir = os.path.join(cfgs.TEST_SAVE_PATH, cfgs.VERSION) tools.mkdir(save_dir) cv2.imwrite(save_dir + '/' + a_img_name + '_h.jpg', det_detections_h[:, :, ::-1]) cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg', det_detections_r[:, :, ::-1]) xmin, ymin, xmax, ymax = det_boxes_h_[:, 0], det_boxes_h_[:, 1], \ det_boxes_h_[:, 2], det_boxes_h_[:, 3] if det_boxes_r_.shape[0] != 0: #print('### Has box ###') resized_h, resized_w = resized_img.shape[ 1], resized_img.shape[2] det_boxes_r_ = forward_convert(det_boxes_r_, False) det_boxes_r_[:, 0::2] *= (raw_w / resized_w) det_boxes_r_[:, 1::2] *= (raw_h / resized_h) det_boxes_r_ = back_forward_convert( det_boxes_r_, False) x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \ det_boxes_r_[:, 3], det_boxes_r_[:, 4] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes_h = np.transpose( np.stack([xmin, ymin, xmax, ymax])) boxes_r = np.transpose( np.stack([x_c, y_c, w, h, theta])) dets_h = np.hstack((det_category_h_.reshape(-1, 1), det_scores_h_.reshape(-1, 1), boxes_h)) dets_r = np.hstack((det_category_r_.reshape(-1, 1), det_scores_r_.reshape(-1, 1), boxes_r)) all_boxes_h.append(dets_h) all_boxes_r.append(dets_r) else: imgs.remove(image_name) none_detected_image.append(image_name) print('No detected') tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, imgs_len) fw1 = open(cfgs.VERSION + '_detections_h.pkl', 'wb') fw2 = open(cfgs.VERSION + '_detections_r.pkl', 'wb') pickle.dump(all_boxes_h, fw1) pickle.dump(all_boxes_r, fw2) # with open(cfgs.VERSION + '_detections_h.pkl', 'rb') as f1: # all_boxes_h = pickle.load(f1, encoding='unicode') # print(10 * "###") # print(len(all_boxes_h)) # # with open(cfgs.VERSION + '_detections_r.pkl', 'rb') as f2: # all_boxes_r = pickle.load(f2, encoding='unicode') # # print(len(all_boxes_r)) # imgs = os.listdir(img_dir) real_test_imgname_list = [i.split(image_ext)[0] for i in imgs] print(10 * "**") print('horizon eval:') # print(len(all_boxes_h), len(all_boxes_r)) # print(len(real_test_imgname_list)) mAP_h, recall_h, precision_h, total_mAP_h, total_recall_h, total_precision_h = voc_eval.voc_evaluate_detections( all_boxes=all_boxes_h, test_imgid_list=real_test_imgname_list, test_annotation_path=test_annotation_path) print('mAP_h: ', mAP_h) print('mRecall_h:', recall_h) print('mPrecision_h:', precision_h) print('total_mAP_h: ', total_mAP_h) print('total_recall_h_list:', total_recall_h) print('total_precision_h_list:', total_precision_h) print(10 * "**") print('rotation eval:') mAP_r, recall_r, precision_r, total_mAP_r, total_recall_r, total_precision_r = voc_eval_r.voc_evaluate_detections( all_boxes=all_boxes_r, test_imgid_list=real_test_imgname_list, test_annotation_path=test_annotation_path) f1score_h_check = (1 + 1**2) * precision_h * recall_h / ( 1**2 * precision_h + recall_h) f1score_h = calc_fscore(precision_h, recall_h, 1) f1score_r_check = (1 + 1**2) * precision_r * recall_r / ( 1**2 * precision_r + recall_r) f1score_r = calc_fscore(precision_r, recall_r, 1) print(10 * '##') print('mAP_r:', mAP_r) print('mRecall_r:', recall_r) print('mPrecision_r:', precision_r) print('total_mAP_r_list: ', total_mAP_r) print('total_recall_r_list:', total_recall_r) print('total_precision_r_list:', total_precision_r) print('f1score_r:', f1score_r) summary_path = os.path.join(cfgs.SUMMARY_PATH, cfgs.VERSION + '/eval_0') tools.mkdir(summary_path) summary_writer = tf.summary.FileWriter(summary_path, graph=sess.graph) mAP_h_summ = tf.Summary() mAP_h_summ.value.add(tag='EVAL_Global/mAP_h', simple_value=mAP_h) summary_writer.add_summary(mAP_h_summ, global_stepnp) mAP_r_summ = tf.Summary() mAP_r_summ.value.add(tag='EVAL_Global/mAP_r', simple_value=mAP_r) summary_writer.add_summary(mAP_r_summ, global_stepnp) mRecall_h_summ = tf.Summary() mRecall_h_summ.value.add(tag='EVAL_Global/Recall_h', simple_value=recall_h) summary_writer.add_summary(mRecall_h_summ, global_stepnp) mRecall_r_summ = tf.Summary() mRecall_r_summ.value.add(tag='EVAL_Global/Recall_r', simple_value=recall_r) summary_writer.add_summary(mRecall_r_summ, global_stepnp) mPrecision_h_summ = tf.Summary() mPrecision_h_summ.value.add(tag='EVAL_Global/Precision_h', simple_value=precision_h) summary_writer.add_summary(mPrecision_h_summ, global_stepnp) mPrecision_r_summ = tf.Summary() mPrecision_r_summ.value.add(tag='EVAL_Global/Precision_r', simple_value=precision_r) summary_writer.add_summary(mPrecision_r_summ, global_stepnp) mF1Score_h_summ = tf.Summary() mF1Score_h_summ.value.add(tag='EVAL_Global/F1Score_h', simple_value=f1score_h) summary_writer.add_summary(mF1Score_h_summ, global_stepnp) mF1Score_r_summ = tf.Summary() mF1Score_r_summ.value.add(tag='EVAL_Global/F1Score_r', simple_value=f1score_r) summary_writer.add_summary(mF1Score_r_summ, global_stepnp) mAP_h_class_dict = {} mAP_r_class_dict = {} recall_h_class_dict = {} recall_r_class_dict = {} precision_h_class_dict = {} precision_r_class_dict = {} f1score_h_class_dict = {} f1score_r_class_dict = {} label_list = list(NAME_LABEL_MAP.keys()) label_list.remove('back_ground') for cls in label_list: mAP_h_class_dict["cls_%s_mAP_h_summ" % cls] = tf.Summary() mAP_r_class_dict["cls_%s_mAP_r_summ" % cls] = tf.Summary() recall_h_class_dict["cls_%s_recall_h_summ" % cls] = tf.Summary() recall_r_class_dict["cls_%s_recall_r_summ" % cls] = tf.Summary() precision_h_class_dict["cls_%s_precision_h_summ" % cls] = tf.Summary() precision_r_class_dict["cls_%s_precision_r_summ" % cls] = tf.Summary() f1score_h_class_dict["cls_%s_f1score_h_summ" % cls] = tf.Summary() f1score_r_class_dict["cls_%s_f1score_r_summ" % cls] = tf.Summary() for cls in label_list: mAP_h_class_dict["cls_%s_mAP_h_summ" % cls].value.add( tag='EVAL_Class_mAP/{}_mAP_h'.format(cls), simple_value=total_mAP_h[cls]) mAP_r_class_dict["cls_%s_mAP_r_summ" % cls].value.add( tag='EVAL_Class_mAP/{}_mAP_r'.format(cls), simple_value=total_mAP_r[cls]) recall_h_class_dict[ "cls_%s_recall_h_summ" % cls].value.add( tag='EVAL_Class_recall/{}_recall_h'.format(cls), simple_value=total_recall_h[cls]) recall_r_class_dict[ "cls_%s_recall_r_summ" % cls].value.add( tag='EVAL_Class_recall/{}_recall_r'.format(cls), simple_value=total_recall_r[cls]) precision_h_class_dict[ "cls_%s_precision_h_summ" % cls].value.add( tag='EVAL_Class_precision/{}_precision_h'.format( cls), simple_value=total_precision_h[cls]) precision_r_class_dict[ "cls_%s_precision_r_summ" % cls].value.add( tag='EVAL_Class_precision/{}_precision_r'.format( cls), simple_value=total_precision_r[cls]) f1score_h_cls = calc_fscore(total_precision_h[cls], total_recall_h[cls], 1) f1score_r_cls = calc_fscore(total_precision_r[cls], total_recall_r[cls], 1) f1score_h_class_dict[ "cls_%s_f1score_h_summ" % cls].value.add( tag='EVAL_Class_f1score/{}_f1score_h'.format(cls), simple_value=f1score_h_cls) f1score_r_class_dict[ "cls_%s_f1score_r_summ" % cls].value.add( tag='EVAL_Class_f1score/{}_f1score_r'.format(cls), simple_value=f1score_r_cls) for cls in label_list: summary_writer.add_summary( mAP_h_class_dict["cls_%s_mAP_h_summ" % cls], global_stepnp) summary_writer.add_summary( mAP_r_class_dict["cls_%s_mAP_r_summ" % cls], global_stepnp) summary_writer.add_summary( recall_h_class_dict["cls_%s_recall_h_summ" % cls], global_stepnp) summary_writer.add_summary( recall_r_class_dict["cls_%s_recall_r_summ" % cls], global_stepnp) summary_writer.add_summary( precision_h_class_dict["cls_%s_precision_h_summ" % cls], global_stepnp) summary_writer.add_summary( precision_r_class_dict["cls_%s_precision_r_summ" % cls], global_stepnp) summary_writer.add_summary( f1score_h_class_dict["cls_%s_f1score_h_summ" % cls], global_stepnp) summary_writer.add_summary( f1score_r_class_dict["cls_%s_f1score_r_summ" % cls], global_stepnp) summary_writer.flush() if not os.path.exists(save_dir): os.makedirs(save_dir) save_ckpt = os.path.join(save_dir, 'voc_' + str(global_stepnp) + 'model.ckpt') #saver.save(sess, save_ckpt) print(' weights had been saved') time_to_next_eval = start_time + EVAL_INTERVAL - time.time() if time_to_next_eval > 0: time.sleep(time_to_next_eval)
def test(img_num): with tf.Graph().as_default(): img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \ next_batch(dataset_name=cfgs.DATASET_NAME, batch_size=cfgs.BATCH_SIZE, shortside_len=cfgs.SHORT_SIDE_LEN, is_training=False) gtboxes_and_label, head = get_head( tf.squeeze(gtboxes_and_label_batch, 0)) gtboxes_and_label = tf.py_func(back_forward_convert, inp=[gtboxes_and_label], Tout=tf.float32) gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6]) head_quadrant = tf.py_func(get_head_quadrant, inp=[head, gtboxes_and_label], Tout=tf.float32) head_quadrant = tf.reshape(head_quadrant, [-1, 1]) gtboxes_and_label_minAreaRectangle = get_horizen_minAreaRectangle( gtboxes_and_label) gtboxes_and_label_minAreaRectangle = tf.reshape( gtboxes_and_label_minAreaRectangle, [-1, 5]) # *********************************************************************************************** # * share net * # *********************************************************************************************** _, share_net = get_network_byname(net_name=cfgs.NET_NAME, inputs=img_batch, num_classes=None, is_training=True, output_stride=None, global_pool=False, spatial_squeeze=False) # *********************************************************************************************** # * RPN * # *********************************************************************************************** rpn = build_rpn.RPN( net_name=cfgs.NET_NAME, inputs=img_batch, gtboxes_and_label=None, is_training=False, share_head=cfgs.SHARE_HEAD, share_net=share_net, stride=cfgs.STRIDE, anchor_ratios=cfgs.ANCHOR_RATIOS, anchor_scales=cfgs.ANCHOR_SCALES, scale_factors=cfgs.SCALE_FACTORS, base_anchor_size_list=cfgs. BASE_ANCHOR_SIZE_LIST, # P2, P3, P4, P5, P6 level=cfgs.LEVEL, top_k_nms=cfgs.RPN_TOP_K_NMS, rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD, max_proposals_num=cfgs.MAX_PROPOSAL_NUM, rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD, rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD, rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE, rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE, remove_outside_anchors=False, # whether remove anchors outside rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME]) # rpn predict proposals rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals( ) # rpn_score shape: [300, ] # *********************************************************************************************** # * Fast RCNN * # *********************************************************************************************** fast_rcnn = build_fast_rcnn.FastRCNN( feature_pyramid=rpn.feature_pyramid, rpn_proposals_boxes=rpn_proposals_boxes, rpn_proposals_scores=rpn_proposals_scores, img_shape=tf.shape(img_batch), img_batch=img_batch, roi_size=cfgs.ROI_SIZE, roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE, scale_factors=cfgs.SCALE_FACTORS, gtboxes_and_label=None, gtboxes_and_label_minAreaRectangle= gtboxes_and_label_minAreaRectangle, fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, fast_rcnn_maximum_boxes_per_img=100, fast_rcnn_nms_max_boxes_per_class=cfgs. FAST_RCNN_NMS_MAX_BOXES_PER_CLASS, show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD, # show detections which score >= 0.6 num_classes=cfgs.CLASS_NUM, fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE, fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE, fast_rcnn_positives_iou_threshold=cfgs. FAST_RCNN_IOU_POSITIVE_THRESHOLD, # iou>0.5 is positive, iou<0.5 is negative use_dropout=cfgs.USE_DROPOUT, weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], is_training=False, level=cfgs.LEVEL, head_quadrant=head_quadrant) fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category, \ fast_rcnn_decode_boxes_rotate, fast_rcnn_score_rotate, fast_rcnn_head_quadrant, \ num_of_objects_rotate, detection_category_rotate = fast_rcnn.fast_rcnn_predict() # train init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = restore_model.get_restorer() config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) for i in range(img_num): start = time.time() _img_name_batch, _img_batch, _gtboxes_and_label, _gtboxes_and_label_minAreaRectangle, _head_quadrant, \ _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category, _fast_rcnn_decode_boxes_rotate, \ _fast_rcnn_score_rotate, _fast_rcnn_head_quadrant, _detection_category_rotate \ = sess.run([img_name_batch, img_batch, gtboxes_and_label, gtboxes_and_label_minAreaRectangle, head_quadrant, fast_rcnn_decode_boxes, fast_rcnn_score, detection_category, fast_rcnn_decode_boxes_rotate, fast_rcnn_score_rotate, fast_rcnn_head_quadrant, detection_category_rotate]) end = time.time() _img_batch = np.squeeze(_img_batch, axis=0) _img_batch_fpn_horizonal = help_utils.draw_box_cv( _img_batch, boxes=_fast_rcnn_decode_boxes, labels=_detection_category, scores=_fast_rcnn_score) _img_batch_fpn_rotate = help_utils.draw_rotate_box_cv( _img_batch, boxes=_fast_rcnn_decode_boxes_rotate, labels=_detection_category_rotate, scores=_fast_rcnn_score_rotate, head=np.argmax(_fast_rcnn_head_quadrant, axis=1)) mkdir(cfgs.TEST_SAVE_PATH) cv2.imwrite( cfgs.TEST_SAVE_PATH + '/{}_horizontal_fpn.jpg'.format(str(_img_name_batch[0])), _img_batch_fpn_horizonal) cv2.imwrite( cfgs.TEST_SAVE_PATH + '/{}_rotate_fpn.jpg'.format(str(_img_name_batch[0])), _img_batch_fpn_rotate) temp_label_horizontal = np.reshape(_gtboxes_and_label[:, -1:], [ -1, ]).astype(np.int64) temp_label_rotate = np.reshape(_gtboxes_and_label[:, -1:], [ -1, ]).astype(np.int64) _img_batch_gt_horizontal = help_utils.draw_box_cv( _img_batch, boxes=_gtboxes_and_label_minAreaRectangle[:, :-1], labels=temp_label_horizontal, scores=None) _img_batch_gt_rotate = help_utils.draw_rotate_box_cv( _img_batch, boxes=_gtboxes_and_label[:, :-1], labels=temp_label_rotate, scores=None, head=np.reshape(_head_quadrant, [ -1, ])) cv2.imwrite( cfgs.TEST_SAVE_PATH + '/{}_horizontal_gt.jpg'.format(str(_img_name_batch[0])), _img_batch_gt_horizontal) cv2.imwrite( cfgs.TEST_SAVE_PATH + '/{}_rotate_gt.jpg'.format(str(_img_name_batch[0])), _img_batch_gt_rotate) view_bar( '{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i + 1, img_num) coord.request_stop() coord.join(threads)
def eval_text(img_num, mode): with tf.Graph().as_default(): faster_rcnn = build_whole_network.DetectionNetwork( base_network_name=cfgs.NET_NAME, is_training=False) # 通过readtfrecord来读取文件 img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \ next_batch(dataset_name=cfgs.DATASET_NAME, batch_size=cfgs.BATCH_SIZE, shortside_len=cfgs.IMG_SHORT_SIDE_LEN, is_training=False) gtboxes_and_label = tf.py_func( back_forward_convert, inp=[tf.squeeze(gtboxes_and_label_batch, 0)], Tout=tf.float32) gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6]) gtboxes_and_label_minAreaRectangle = get_horizen_minAreaRectangle( gtboxes_and_label) gtboxes_and_label_minAreaRectangle = tf.reshape( gtboxes_and_label_minAreaRectangle, [-1, 5]) final_boxes_h, final_scores_h, final_category_h, \ final_boxes_r, final_scores_r, final_category_r= faster_rcnn.build_whole_detection_network( input_img_batch=img_batch, gtboxes_r_batch=gtboxes_and_label, gtboxes_h_batch=gtboxes_and_label_minAreaRectangle) if mode == 0: final_boxes_r = get_horizen_minAreaRectangle(final_boxes_r, False) # train init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) restorer, restore_ckpt = faster_rcnn.get_restorer() config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) gtboxes_horizontal_dict = {} predict_horizontal_dict = {} gtboxes_rotate_dict = {} predict_rotate_dict = {} for i in range(img_num): start = time.time() _img_name_batch, _img_batch, _gtboxes_and_label, _gtboxes_and_label_minAreaRectangle, \ _final_boxes_h, _final_scores_h, _final_category_h, _final_boxes_r, \ _final_scores_r, _final_category_r \ = sess.run([img_name_batch, img_batch, gtboxes_and_label, gtboxes_and_label_minAreaRectangle, final_boxes_h, final_scores_h, final_category_h, final_boxes_r, final_scores_r, final_category_r]) end = time.time() # det_detections_h = draw_box_in_img.draw_box_cv(np.squeeze(_img_batch, 0), # boxes=_final_boxes_h, # labels=_final_category_h, # scores=final_scores_h) # det_detections_r = draw_box_in_img.draw_rotate_box_cv(np.squeeze(_img_batch, 0), # boxes=_final_boxes_r, # labels=_final_category_r, # scores= _final_scores_r) # save_dir = os.path.join('/home/sws/code/Tfrecord/', cfgs.VERSION) # tools.mkdir(save_dir) # cv2.imwrite(save_dir + '/' + _img_name_batch[0] + '_h.jpg', # det_detections_h) # cv2.imwrite(save_dir + '/' + _img_name_batch[0] + '_r.jpg', # det_detections_r) # gtboxes convert dict gtboxes_horizontal_dict[str(_img_name_batch[0])] = [] predict_horizontal_dict[str(_img_name_batch[0])] = [] gtboxes_rotate_dict[str(_img_name_batch[0])] = [] predict_rotate_dict[str(_img_name_batch[0])] = [] gtbox_horizontal_list, predict_horizontal_list = \ make_dict_packle(_gtboxes_and_label_minAreaRectangle, _final_boxes_h, _final_scores_h, _final_category_h) # if mode == 0: # gtbox_rotate_list, predict_rotate_list = \ # make_dict_packle(_gtboxes_and_label_minAreaRectangle, _fast_rcnn_decode_boxes_rotate, # _fast_rcnn_score_rotate, _detection_category_rotate) if mode == 0: gtbox_rotate_list, predict_rotate_list = \ make_dict_packle(_gtboxes_and_label_minAreaRectangle, _final_boxes_r, _final_scores_r, _final_category_r) else: gtbox_rotate_list, predict_rotate_list = \ make_dict_packle(_gtboxes_and_label, _final_boxes_r, _final_scores_r, _final_category_r) gtboxes_horizontal_dict[str( _img_name_batch[0])].extend(gtbox_horizontal_list) predict_horizontal_dict[str( _img_name_batch[0])].extend(predict_horizontal_list) gtboxes_rotate_dict[str( _img_name_batch[0])].extend(gtbox_rotate_list) predict_rotate_dict[str( _img_name_batch[0])].extend(predict_rotate_list) view_bar( '{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i + 1, img_num) print('\n') fw1 = open('gtboxes_horizontal_dict.pkl', 'w') fw2 = open('predict_horizontal_dict.pkl', 'w') fw3 = open('gtboxes_rotate_dict.pkl', 'w') fw4 = open('predict_rotate_dict.pkl', 'w') pickle.dump(gtboxes_horizontal_dict, fw1) pickle.dump(predict_horizontal_dict, fw2) pickle.dump(gtboxes_rotate_dict, fw3) pickle.dump(predict_rotate_dict, fw4) fw1.close() fw2.close() fw3.close() fw4.close() coord.request_stop() coord.join(threads)
def test_coco(det_net, real_test_img_list, eval_data, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not BGR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data(img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: img_batch = (img_batch / 255 - tf.constant(cfgs.PIXEL_MEAN_)) / tf.constant(cfgs.PIXEL_STD) else: img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) # img_batch = (img_batch - tf.constant(cfgs.PIXEL_MEAN)) / (tf.constant(cfgs.PIXEL_STD)*255) img_batch = tf.expand_dims(img_batch, axis=0) detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') save_path = os.path.join('./eval_coco', cfgs.VERSION) tools.mkdir(save_path) fw_json_dt = open(os.path.join(save_path, 'coco_test-dev.json'), 'w') coco_det = [] for i, a_img in enumerate(real_test_img_list): raw_img = cv2.imread(os.path.join(eval_data, a_img['file_name'])) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() eval_indices = detected_scores >= 0.01 detected_scores = detected_scores[eval_indices] detected_boxes = detected_boxes[eval_indices] detected_categories = detected_categories[eval_indices] # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] draw_img = np.squeeze(resized_img, 0) if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']: draw_img = (draw_img * np.array(cfgs.PIXEL_STD) + np.array(cfgs.PIXEL_MEAN_)) * 255 else: draw_img = draw_img + np.array(cfgs.PIXEL_MEAN) # draw_img = draw_img * (np.array(cfgs.PIXEL_STD)*255) + np.array(cfgs.PIXEL_MEAN) final_detections = draw_box_in_img.draw_boxes_with_label_and_scores(draw_img, boxes=show_boxes, labels=show_categories, scores=show_scores, in_graph=False) if not os.path.exists(cfgs.TEST_SAVE_PATH): os.makedirs(cfgs.TEST_SAVE_PATH) cv2.imwrite(cfgs.TEST_SAVE_PATH + '/' + '{}.jpg'.format(a_img['id']), final_detections[:, :, ::-1]) xmin, ymin, xmax, ymax = detected_boxes[:, 0], detected_boxes[:, 1], \ detected_boxes[:, 2], detected_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h boxes = np.transpose(np.stack([xmin, ymin, xmax-xmin, ymax-ymin])) # cost much time for j, box in enumerate(boxes): coco_det.append({'bbox': [float(box[0]), float(box[1]), float(box[2]), float(box[3])], 'score': float(detected_scores[j]), 'image_id': a_img['id'], 'category_id': int(classes_originID[LABEl_NAME_MAP[detected_categories[j]]])}) tools.view_bar('%s image cost %.3fs' % (a_img['id'], (end - start)), i + 1, len(real_test_img_list)) json.dump(coco_det, fw_json_dt) fw_json_dt.close()
def eval_with_plac(img_dir, det_net, num_imgs, image_ext, draw_imgs=False): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = short_side_resize_for_inference_data(img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, is_resize=False) det_boxes_h, det_scores_h, det_category_h, \ det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_h_batch=None, gtboxes_r_batch=None) init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt = det_net.get_restorer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') all_boxes_r = [] imgs = os.listdir(img_dir) for i, a_img_name in enumerate(imgs): a_img_name = a_img_name.split(image_ext)[0] raw_img = cv2.imread(os.path.join(img_dir, a_img_name + image_ext)) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, det_boxes_r_, det_scores_r_, det_category_r_ = \ sess.run( [img_batch, det_boxes_h, det_scores_h, det_category_h, det_boxes_r, det_scores_r, det_category_r], feed_dict={img_plac: raw_img} ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) if draw_imgs: det_detections_r = draw_box_in_img.draw_rotate_box_cv(np.squeeze(resized_img, 0), boxes=det_boxes_r_, labels=det_category_r_, scores=det_scores_r_) save_dir = os.path.join(cfgs.TEST_SAVE_PATH, cfgs.VERSION) tools.mkdir(save_dir) cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg', det_detections_r[:, :, ::-1]) resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] det_boxes_r_ = forward_convert(det_boxes_r_, False) det_boxes_r_[:, 0::2] *= (raw_w / resized_w) det_boxes_r_[:, 1::2] *= (raw_h / resized_h) det_boxes_r_ = back_forward_convert(det_boxes_r_, False) x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \ det_boxes_r_[:, 3], det_boxes_r_[:, 4] boxes_r = np.transpose(np.stack([x_c, y_c, w, h, theta])) dets_r = np.hstack((det_category_r_.reshape(-1, 1), det_scores_r_.reshape(-1, 1), boxes_r)) all_boxes_r.append(dets_r) tools.view_bar('{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(imgs)) fw2 = open(cfgs.VERSION + '_detections_r.pkl', 'w') pickle.dump(all_boxes_r, fw2)
def detect(det_net, inference_save_path, real_test_imgname_list): # 1. preprocess img img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3]) # is RGB. not GBR img_batch = tf.cast(img_plac, tf.float32) img_batch = short_side_resize_for_inference_data( img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN, length_limitation=cfgs.IMG_MAX_LENGTH) img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN) img_batch = tf.expand_dims(img_batch, axis=0) # [1, None, None, 3] detection_boxes, detection_scores, detection_category = det_net.build_whole_detection_network( input_img_batch=img_batch, gtboxes_batch=None) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) ################################################################ ###根据checkpoint恢复最新的 # restorer, restore_ckpt = det_net.get_restorer() ###恢复指定的 restore_ckpt = os.path.join(cfgs.TRAINED_CKPT, cfgs.VERSION) + '/voc_16000model.ckpt' restorer = tf.train.Saver() print("model restore from :", restore_ckpt) print(20 * "****") ################################################################ config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') all_images_detect_result = [] for i, a_img_name in enumerate(real_test_imgname_list): raw_img = cv2.imread(a_img_name) raw_h, raw_w = raw_img.shape[0], raw_img.shape[1] start = time.time() resized_img, detected_boxes, detected_scores, detected_categories = \ sess.run( [img_batch, detection_boxes, detection_scores, detection_category], feed_dict={img_plac: raw_img[:, :, ::-1]} # cv is BGR. But need RGB ) end = time.time() # print("{} cost time : {} ".format(img_name, (end - start))) show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD show_scores = detected_scores[show_indices] show_boxes = detected_boxes[show_indices] show_categories = detected_categories[show_indices] final_detections = draw_box_in_img.draw_boxes_with_label_and_scores( np.squeeze(resized_img, 0), boxes=show_boxes, labels=show_categories, scores=show_scores) nake_name = os.path.split(a_img_name)[1] print(inference_save_path + '/' + nake_name) # cv2.imwrite(inference_save_path + '/' + nake_name, # final_detections[:, :, ::-1]) xmin, ymin, xmax, ymax = show_boxes[:, 0], show_boxes[:, 1], \ show_boxes[:, 2], show_boxes[:, 3] resized_h, resized_w = resized_img.shape[1], resized_img.shape[2] xmin = xmin * raw_w / resized_w xmax = xmax * raw_w / resized_w ymin = ymin * raw_h / resized_h ymax = ymax * raw_h / resized_h a_img_detect_result = [] for idex in range(len(show_scores)): # label, score, bbox = a_det[0], a_det[1], a_det[2:] det_object = { "xmin": int(xmin[idex]), "xmax": int(xmax[idex]), "ymin": int(ymin[idex]), "ymax": int(ymax[idex]), "label": int(show_categories[idex]), "confidence": float(show_scores[idex]) } # print (det_object) a_img_detect_result.append(det_object) image_result = { "filename": nake_name, "rects": a_img_detect_result } all_images_detect_result.append(image_result) all_images_result_dict = {"results": all_images_detect_result} f = open('result_jinlian20190314.json', 'w') json.dump(all_images_result_dict, f) # , indent=4 f.close() tools.view_bar( '{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(real_test_imgname_list))
def eval_dict_convert(img_num, mode): with tf.Graph().as_default(): # img = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8) img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \ next_batch(dataset_name=cfgs.DATASET_NAME, batch_size=cfgs.BATCH_SIZE, shortside_len=cfgs.SHORT_SIDE_LEN, is_training=False) gtboxes_and_label = tf.py_func(back_forward_convert, inp=[tf.squeeze(gtboxes_and_label_batch, 0)], Tout=tf.float32) gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6]) gtboxes_and_label_minAreaRectangle = get_horizon_minAreaRectangle(gtboxes_and_label) gtboxes_and_label_minAreaRectangle = tf.reshape(gtboxes_and_label_minAreaRectangle, [-1, 5]) # *********************************************************************************************** # * share net * # *********************************************************************************************** _, share_net = get_network_byname(net_name=cfgs.NET_NAME, inputs=img_batch, num_classes=None, is_training=True, output_stride=None, global_pool=False, spatial_squeeze=False) # *********************************************************************************************** # * RPN * # *********************************************************************************************** rpn = build_rpn.RPN(net_name=cfgs.NET_NAME, inputs=img_batch, gtboxes_and_label=gtboxes_and_label, is_training=False, share_head=False, share_net=share_net, anchor_ratios=cfgs.ANCHOR_RATIOS, anchor_scales=cfgs.ANCHOR_SCALES, anchor_angles=cfgs.ANCHOR_ANGLES, scale_factors=cfgs.SCALE_FACTORS, base_anchor_size_list=cfgs.BASE_ANCHOR_SIZE_LIST, # P2, P3, P4, P5, P6 level=cfgs.LEVEL, anchor_stride=cfgs.ANCHOR_STRIDE, top_k_nms=cfgs.RPN_TOP_K_NMS, kernel_size=cfgs.KERNEL_SIZE, use_angles_condition=False, anchor_angle_threshold=cfgs.RPN_ANCHOR_ANGLES_THRESHOLD, nms_angle_threshold=cfgs.RPN_NMS_ANGLES_THRESHOLD, rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD, max_proposals_num=cfgs.MAX_PROPOSAL_NUM, rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD, rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD, # iou>=0.7 is positive box, iou< 0.3 is negative rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE, rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE, remove_outside_anchors=cfgs.IS_FILTER_OUTSIDE_BOXES, # whether remove anchors outside rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], scope='') rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals() # rpn_score shape: [300, ] _, _, rpn_predict_boxes, rpn_predict_scores = rpn.rpn_losses() # *********************************************************************************************** # * Fast RCNN * # *********************************************************************************************** fast_rcnn = build_fast_rcnn.FastRCNN(img_batch=img_batch, feature_pyramid=rpn.feature_pyramid, rpn_proposals_boxes=rpn_proposals_boxes, rpn_proposals_scores=rpn_proposals_scores, stop_gradient_for_proposals=False, img_shape=tf.shape(img_batch), roi_size=cfgs.ROI_SIZE, roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE, scale_factors=cfgs.SCALE_FACTORS, gtboxes_and_label=None, fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, top_k_nms=cfgs.FAST_RCNN_TOP_K_NMS, nms_angle_threshold=cfgs.FAST_RCNN_NMS_ANGLES_THRESHOLD, use_angle_condition=False, level=cfgs.LEVEL, fast_rcnn_maximum_boxes_per_img=100, fast_rcnn_nms_max_boxes_per_class=cfgs.FAST_RCNN_NMS_MAX_BOXES_PER_CLASS, show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD, # show detections which score >= 0.6 num_classes=cfgs.CLASS_NUM, fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE, fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE, fast_rcnn_positives_iou_threshold=cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD, boxes_angle_threshold=cfgs.FAST_RCNN_BOXES_ANGLES_THRESHOLD, use_dropout=cfgs.USE_DROPOUT, weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME], is_training=False) fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \ fast_rcnn.fast_rcnn_predict() ############################################################################################## if cfgs.NEED_AUXILIARY: predict_boxes = tf.concat([fast_rcnn_decode_boxes, rpn_predict_boxes], axis=0) predict_scores = tf.concat([fast_rcnn_score, rpn_predict_scores - 0.2], axis=0) rpn_predict_label = tf.ones([tf.shape(rpn_predict_scores)[0], ], tf.int64) labels = tf.concat([detection_category, rpn_predict_label], axis=0) # valid_indices = nms_rotate.nms_rotate(decode_boxes=predict_boxes, # scores=predict_scores, # iou_threshold=0.15, # max_output_size=30, # use_angle_condition=False, # angle_threshold=15, # use_gpu=True) valid_indices = tf.py_func(nms_rotate.nms_rotate_cpu, inp=[predict_boxes, predict_scores, tf.constant(0.15, tf.float32), tf.constant(30, tf.float32)], Tout=tf.int64) fast_rcnn_decode_boxes = tf.gather(predict_boxes, valid_indices) fast_rcnn_score = tf.gather(predict_scores, valid_indices) detection_category = tf.gather(labels, valid_indices) ############################################################################################## if mode == 0: fast_rcnn_decode_boxes = get_horizon_minAreaRectangle(fast_rcnn_decode_boxes, False) # train init_op = tf.group( tf.global_variables_initializer(), tf.local_variables_initializer() ) restorer, restore_ckpt = restore_model.get_restorer() config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init_op) if not restorer is None: restorer.restore(sess, restore_ckpt) print('restore model') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) gtboxes_dict = {} predict_dict = {} for i in range(img_num): start = time.time() _img_name_batch, _img_batch, _gtboxes_and_label, _fast_rcnn_decode_boxes, \ _gtboxes_and_label_minAreaRectangle, _fast_rcnn_score, _detection_category \ = sess.run([img_name_batch, img_batch, gtboxes_and_label, fast_rcnn_decode_boxes, gtboxes_and_label_minAreaRectangle, fast_rcnn_score, detection_category]) end = time.time() # gtboxes convert dict gtboxes_dict[str(_img_name_batch[0])] = [] predict_dict[str(_img_name_batch[0])] = [] # for j, box in enumerate(_gtboxes_and_label): # bbox_dict = {} # bbox_dict['bbox'] = np.array(_gtboxes_and_label[j, :-1], np.float64) # bbox_dict['name'] = LABEl_NAME_MAP[int(_gtboxes_and_label[j, -1])] # gtbox_dict[str(_img_name_batch[0])].append(bbox_dict) # # for label in NAME_LABEL_MAP.keys(): # if label == 'back_ground': # continue # else: # temp_dict = {} # temp_dict['name'] = label # # ind = np.where(_detection_category == NAME_LABEL_MAP[label])[0] # temp_boxes = _fast_rcnn_decode_boxes[ind] # temp_score = np.reshape(_fast_rcnn_score[ind], [-1, 1]) # temp_dict['bbox'] = np.array(np.concatenate([temp_boxes, temp_score], axis=1), np.float64) # predict_dict[str(_img_name_batch[0])].append(temp_dict) if mode == 0: gtboxes_list, predict_list = \ make_dict_packle(_gtboxes_and_label_minAreaRectangle, _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category) else: gtboxes_list, predict_list = \ make_dict_packle(_gtboxes_and_label, _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category) gtboxes_dict[str(_img_name_batch[0])].extend(gtboxes_list) predict_dict[str(_img_name_batch[0])].extend(predict_list) view_bar('{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i + 1, img_num) fw1 = open('gtboxes_dict.pkl', 'w') fw2 = open('predict_dict.pkl', 'w') pickle.dump(gtboxes_dict, fw1) pickle.dump(predict_dict, fw2) fw1.close() fw2.close() coord.request_stop() coord.join(threads)