code: # -*- coding:utf-8 -*-
  
  from __future__ import absolute_import
  from __future__ import print_function
  from __future__ import division
  
  import os
  import sys
  import tensorflow as tf
  import cv2
  import numpy as np
  import math
  from tqdm import tqdm
  import argparse
  from multiprocessing import Queue, Process
  sys.path.append("../")
  
  from libs.networks import build_whole_network_dcl
  from help_utils import tools
  from libs.label_name_dict.label_dict import *
  from libs.box_utils import draw_box_in_img
  from libs.box_utils.coordinate_convert import forward_convert, backward_convert
  from libs.box_utils import nms_rotate
  from libs.box_utils.rotate_polygon_nms import rotate_gpu_nms
  
  
  def worker(gpu_id, images, det_net, result_queue):
      os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
      # 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)
  
      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_scores, detection_category, detection_boxes_angle = det_net.build_whole_detection_network(
          input_img_batch=img_batch,
          gtboxes_batch_h=None,
          gtboxes_batch_r=None,
          gt_encode_label=None,
          gpu_id=0)
  
      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 %d ...' % gpu_id)
          for a_img in images:
              raw_img = cv2.imread(a_img)
              raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]
  
              det_boxes_r_all, det_scores_r_all, det_category_r_all = [], [], []
  
              img_short_side_len_list = cfgs.IMG_SHORT_SIDE_LEN if isinstance(cfgs.IMG_SHORT_SIDE_LEN, list) else [
                  cfgs.IMG_SHORT_SIDE_LEN]
              img_short_side_len_list = [img_short_side_len_list[0]] if not args.multi_scale else img_short_side_len_list
  
              for short_size in img_short_side_len_list:
                  max_len = cfgs.IMG_MAX_LENGTH
                  if raw_h < raw_w:
                      new_h, new_w = short_size, min(int(short_size * float(raw_w) / raw_h), max_len)
                  else:
                      new_h, new_w = min(int(short_size * float(raw_h) / raw_w), max_len), short_size
                  img_resize = cv2.resize(raw_img, (new_w, new_h))
  
                  resized_img, detected_boxes, detected_scores, detected_categories = \
                      sess.run(
                          [img_batch, detection_boxes_angle, detection_scores, detection_category],
                          feed_dict={img_plac: img_resize[:, :, ::-1]}
                      )
  
                  detected_indices = detected_scores >= cfgs.VIS_SCORE
                  detected_scores = detected_scores[detected_indices]
                  detected_boxes = detected_boxes[detected_indices]
                  detected_categories = detected_categories[detected_indices]
  
                  if detected_boxes.shape[0] == 0:
                      continue
                  resized_h, resized_w = resized_img.shape[1], resized_img.shape[2]
                  detected_boxes = forward_convert(detected_boxes, False)
                  detected_boxes[:, 0::2] *= (raw_w / resized_w)
                  detected_boxes[:, 1::2] *= (raw_h / resized_h)
                  # detected_boxes = backward_convert(detected_boxes, False)
  
                  det_boxes_r_all.extend(detected_boxes)
                  det_scores_r_all.extend(detected_scores)
                  det_category_r_all.extend(detected_categories)
              det_boxes_r_all = np.array(det_boxes_r_all)
              det_scores_r_all = np.array(det_scores_r_all)
              det_category_r_all = np.array(det_category_r_all)
  
              box_res_rotate_ = []
              label_res_rotate_ = []
              score_res_rotate_ = []
  
              if det_scores_r_all.shape[0] != 0:
                  for sub_class in range(1, cfgs.CLASS_NUM + 1):
                      index = np.where(det_category_r_all == sub_class)[0]
                      if len(index) == 0:
                          continue
                      tmp_boxes_r = det_boxes_r_all[index]
                      tmp_label_r = det_category_r_all[index]
                      tmp_score_r = det_scores_r_all[index]
  
                      tmp_boxes_r_ = backward_convert(tmp_boxes_r, False)
  
                      try:
                          inx = nms_rotate.nms_rotate_cpu(boxes=np.array(tmp_boxes_r_),
                                                          scores=np.array(tmp_score_r),
                                                          iou_threshold=cfgs.NMS_IOU_THRESHOLD,
                                                          max_output_size=5000)
                      except:
                          tmp_boxes_r_ = np.array(tmp_boxes_r_)
                          tmp = np.zeros([tmp_boxes_r_.shape[0], tmp_boxes_r_.shape[1] + 1])
                          tmp[:, 0:-1] = tmp_boxes_r_
                          tmp[:, -1] = np.array(tmp_score_r)
                          # Note: the IoU of two same rectangles is 0, which is calculated by rotate_gpu_nms
                          jitter = np.zeros([tmp_boxes_r_.shape[0], tmp_boxes_r_.shape[1] + 1])
                          jitter[:, 0] += np.random.rand(tmp_boxes_r_.shape[0], ) / 1000
                          inx = rotate_gpu_nms(np.array(tmp, np.float32) + np.array(jitter, np.float32),
                                               float(cfgs.NMS_IOU_THRESHOLD), 0)
  
                      box_res_rotate_.extend(np.array(tmp_boxes_r)[inx])
                      score_res_rotate_.extend(np.array(tmp_score_r)[inx])
                      label_res_rotate_.extend(np.array(tmp_label_r)[inx])
  
              box_res_rotate_ = np.array(box_res_rotate_)
              score_res_rotate_ = np.array(score_res_rotate_)
              label_res_rotate_ = np.array(label_res_rotate_)
  
              result_dict = {'scales': [1, 1], 'boxes': box_res_rotate_,
                             'scores': score_res_rotate_, 'labels': label_res_rotate_,
                             'image_id': a_img}
              result_queue.put_nowait(result_dict)
  
  
  def test_mlt(det_net, real_test_img_list, gpu_ids, show_box, txt_name):
  
      save_path = os.path.join('./test_mlt', cfgs.VERSION)
      tools.mkdir(save_path)
  
      nr_records = len(real_test_img_list)
      pbar = tqdm(total=nr_records)
      gpu_num = len(gpu_ids.strip().split(','))
  
      nr_image = math.ceil(nr_records / gpu_num)
      result_queue = Queue(500)
      procs = []
  
      for i, gpu_id in enumerate(gpu_ids.strip().split(',')):
          start = i * nr_image
          end = min(start + nr_image, nr_records)
          split_records = real_test_img_list[start:end]
          proc = Process(target=worker, args=(int(gpu_id), split_records, det_net, result_queue))
          print('process:%d, start:%d, end:%d' % (i, start, end))
          proc.start()
          procs.append(proc)
  
      for i in range(nr_records):
          res = result_queue.get()
          if res['boxes'].shape[0] == 0:
              fw_txt_dt = open(os.path.join(save_path, 'res_{}.txt'.format(
                  res['image_id'].split('/')[-1].split('.')[0].split('ts_')[1])), 'w')
              fw_txt_dt.close()
              pbar.update(1)
  
              fw = open(txt_name, 'a+')
              fw.write('{}\n'.format(res['image_id'].split('/')[-1]))
              fw.close()
  
              continue
          x1, y1, x2, y2, x3, y3, x4, y4 = res['boxes'][:, 0], res['boxes'][:, 1], res['boxes'][:, 2], res['boxes'][:, 3],\
                                           res['boxes'][:, 4], res['boxes'][:, 5], res['boxes'][:, 6], res['boxes'][:, 7]
  
          x1, y1 = x1 * res['scales'][0], y1 * res['scales'][1]
          x2, y2 = x2 * res['scales'][0], y2 * res['scales'][1]
          x3, y3 = x3 * res['scales'][0], y3 * res['scales'][1]
          x4, y4 = x4 * res['scales'][0], y4 * res['scales'][1]
  
          boxes = np.transpose(np.stack([x1, y1, x2, y2, x3, y3, x4, y4]))
  
          if show_box:
              boxes = backward_convert(boxes, False)
              nake_name = res['image_id'].split('/')[-1]
              draw_path = os.path.join(save_path, nake_name)
              draw_img = np.array(cv2.imread(res['image_id']), np.float32)
  
              final_detections = draw_box_in_img.draw_boxes_with_label_and_scores(draw_img,
                                                                                  boxes=boxes,
                                                                                  labels=res['labels'],
                                                                                  scores=res['scores'],
                                                                                  method=1,
                                                                                  in_graph=False)
              cv2.imwrite(draw_path, final_detections)
  
          else:
              fw_txt_dt = open(os.path.join(save_path, 'res_{}.txt'.format(
                  res['image_id'].split('/')[-1].split('.')[0].split('ts_')[1])), 'w')
  
              for ii, box in enumerate(boxes):
                  line = '%d,%d,%d,%d,%d,%d,%d,%d,%.3f\n' % (box[0], box[1], box[2], box[3],
                                                             box[4], box[5], box[6], box[7], res['scores'][ii])
                  fw_txt_dt.write(line)
              fw_txt_dt.close()
  
              fw = open(txt_name, 'a+')
              fw.write('{}\n'.format(res['image_id'].split('/')[-1]))
              fw.close()
  
          pbar.set_description("Test image %s" % res['image_id'].split('/')[-1])
  
          pbar.update(1)
  
      for p in procs:
          p.join()
  
  
  def eval(num_imgs, test_dir, gpu_ids, show_box):
  
      txt_name = '{}.txt'.format(cfgs.VERSION)
      if not args.show_box:
          if not os.path.exists(txt_name):
              fw = open(txt_name, 'w')
              fw.close()
  
          fr = open(txt_name, 'r')
          img_filter = fr.readlines()
          print('****************************' * 3)
          print('Already tested imgs:', img_filter)
          print('****************************' * 3)
          fr.close()
  
          test_imgname_list = [os.path.join(test_dir, img_name) for img_name in os.listdir(args.test_dir)
                               if img_name.endswith(('.jpg', '.JPG', '.png', '.jpeg', '.tif', '.tiff')) and
                               (img_name + '\n' not in img_filter)]
      else:
          test_imgname_list = [os.path.join(test_dir, img_name) for img_name in os.listdir(args.test_dir)
                               if img_name.endswith(('.jpg', '.JPG', '.png', '.jpeg', '.tif', '.tiff'))]
  
      assert len(test_imgname_list) != 0, 'test_dir has no imgs there.' \
                                          ' Note that, we only support img format of (.jpg, .png, and .tiff) '
  
      if num_imgs == np.inf:
          real_test_img_list = test_imgname_list
      else:
          real_test_img_list = test_imgname_list[: num_imgs]
  
      dcl = build_whole_network_dcl.DetectionNetwork(base_network_name=cfgs.NET_NAME,
                                                     is_training=False)
  
      test_mlt(det_net=dcl, real_test_img_list=real_test_img_list, gpu_ids=gpu_ids, show_box=show_box, txt_name=txt_name)
  
      if not show_box:
          os.remove(txt_name)
  
  
  def parse_args():
  
      parser = argparse.ArgumentParser('evaluate the result with Pascal2007 strand')
  
      parser.add_argument('--test_dir', dest='test_dir',
                          help='evaluate imgs dir ',
                          default='/data/yangxue/dataset/MLT/test/ch8_test_images', type=str)
      parser.add_argument('--gpus', dest='gpus',
                          help='gpu id',
                          default='0,1,2,3,4,5,6,7', type=str)
      parser.add_argument('--eval_num', dest='eval_num',
                          help='the num of eval imgs',
                          default=np.inf, type=int)
      parser.add_argument('--show_box', '-s', default=False,
                          action='store_true')
      parser.add_argument('--multi_scale', '-ms', default=False,
                          action='store_true')
      args = parser.parse_args()
      return args
  
  
  if __name__ == '__main__':
  
      args = parse_args()
      print(20*"--")
      print(args)
      print(20*"--")
      eval(args.eval_num,
           test_dir=args.test_dir,
           gpu_ids=args.gpus,
           show_box=args.show_box)
  
  
  
apis: tensorflow.local_variables_initializer tensorflow.constant tensorflow.cast tensorflow.expand_dims tensorflow.placeholder numpy.stack tensorflow.ConfigProto tensorflow.global_variables_initializer numpy.random.rand tensorflow.Session numpy.array numpy.where numpy.zeros
api_extract: [(16, 'sys.path.append', 'sys.path.append', (['"""../"""'], {}), False, 'import sys\n'), (30, 'tensorflow.placeholder', 'tf.placeholder', ([], {'dtype': 'tf.uint8', 'shape': '[None, None, 3]'}), True, 'import tensorflow as tf\n'), (31, 'tensorflow.cast', 'tf.cast', (['img_plac', 'tf.float32'], {}), True, 'import tensorflow as tf\n'), (38, 'tensorflow.expand_dims', 'tf.expand_dims', (['img_batch'], {'axis': '(0)'}), True, 'import tensorflow as tf\n'), (54, 'tensorflow.ConfigProto', 'tf.ConfigProto', ([], {}), True, 'import tensorflow as tf\n'), (153, 'os.path.join', 'os.path.join', (['"""./test_mlt"""', 'cfgs.VERSION'], {}), False, 'import os\n'), (154, 'help_utils.tools.mkdir', 'tools.mkdir', (['save_path'], {}), False, 'from help_utils import tools\n'), (157, 'tqdm.tqdm', 'tqdm', ([], {'total': 'nr_records'}), False, 'from tqdm import tqdm\n'), (160, 'math.ceil', 'math.ceil', (['(nr_records / gpu_num)'], {}), False, 'import math\n'), (161, 'multiprocessing.Queue', 'Queue', (['(500)'], {}), False, 'from multiprocessing import Queue, Process\n'), (262, 'libs.networks.build_whole_network_dcl.DetectionNetwork', 'build_whole_network_dcl.DetectionNetwork', ([], {'base_network_name': 'cfgs.NET_NAME', 'is_training': '(False)'}), False, 'from libs.networks import build_whole_network_dcl\n'), (273, 'argparse.ArgumentParser', 'argparse.ArgumentParser', (['"""evaluate the result with Pascal2007 strand"""'], {}), False, 'import argparse\n'), (48, 'tensorflow.global_variables_initializer', 'tf.global_variables_initializer', ([], {}), True, 'import tensorflow as tf\n'), (49, 'tensorflow.local_variables_initializer', 'tf.local_variables_initializer', ([], {}), True, 'import tensorflow as tf\n'), (57, 'tensorflow.Session', 'tf.Session', ([], {'config': 'config'}), True, 'import tensorflow as tf\n'), (268, 'os.remove', 'os.remove', (['txt_name'], {}), False, 'import os\n'), (34, 'tensorflow.constant', 'tf.constant', (['cfgs.PIXEL_STD'], {}), True, 'import tensorflow as tf\n'), (36, 'tensorflow.constant', 'tf.constant', (['cfgs.PIXEL_MEAN'], {}), True, 'import tensorflow as tf\n'), (63, 'cv2.imread', 'cv2.imread', (['a_img'], {}), False, 'import cv2\n'), (102, 'numpy.array', 'np.array', (['det_boxes_r_all'], {}), True, 'import numpy as np\n'), (103, 'numpy.array', 'np.array', (['det_scores_r_all'], {}), True, 'import numpy as np\n'), (104, 'numpy.array', 'np.array', (['det_category_r_all'], {}), True, 'import numpy as np\n'), (141, 'numpy.array', 'np.array', (['box_res_rotate_'], {}), True, 'import numpy as np\n'), (142, 'numpy.array', 'np.array', (['score_res_rotate_'], {}), True, 'import numpy as np\n'), (143, 'numpy.array', 'np.array', (['label_res_rotate_'], {}), True, 'import numpy as np\n'), (194, 'numpy.stack', 'np.stack', (['[x1, y1, x2, y2, x3, y3, x4, y4]'], {}), True, 'import numpy as np\n'), (197, 'libs.box_utils.coordinate_convert.backward_convert', 'backward_convert', (['boxes', '(False)'], {}), False, 'from libs.box_utils.coordinate_convert import forward_convert, backward_convert\n'), (199, 'os.path.join', 'os.path.join', (['save_path', 'nake_name'], {}), False, 'import os\n'), (202, 'libs.box_utils.draw_box_in_img.draw_boxes_with_label_and_scores', 'draw_box_in_img.draw_boxes_with_label_and_scores', (['draw_img'], {'boxes': 'boxes', 'labels': "res['labels']", 'scores': "res['scores']", 'method': '(1)', 'in_graph': '(False)'}), False, 'from libs.box_utils import draw_box_in_img\n'), (208, 'cv2.imwrite', 'cv2.imwrite', (['draw_path', 'final_detections'], {}), False, 'import cv2\n'), (236, 'os.path.exists', 'os.path.exists', (['txt_name'], {}), False, 'import os\n'), (247, 'os.path.join', 'os.path.join', (['test_dir', 'img_name'], {}), False, 'import os\n'), (251, 'os.path.join', 'os.path.join', (['test_dir', 'img_name'], {}), False, 'import os\n'), (34, 'tensorflow.constant', 'tf.constant', (['cfgs.PIXEL_MEAN_'], {}), True, 'import tensorflow as tf\n'), (78, 'cv2.resize', 'cv2.resize', (['raw_img', '(new_w, new_h)'], {}), False, 'import cv2\n'), (94, 'libs.box_utils.coordinate_convert.forward_convert', 'forward_convert', (['detected_boxes', '(False)'], {}), False, 'from libs.box_utils.coordinate_convert import forward_convert, backward_convert\n'), (200, 'cv2.imread', 'cv2.imread', (["res['image_id']"], {}), False, 'import cv2\n'), (247, 'os.listdir', 'os.listdir', (['args.test_dir'], {}), False, 'import os\n'), (251, 'os.listdir', 'os.listdir', (['args.test_dir'], {}), False, 'import os\n'), (119, 'libs.box_utils.coordinate_convert.backward_convert', 'backward_convert', (['tmp_boxes_r', '(False)'], {}), False, 'from libs.box_utils.coordinate_convert import forward_convert, backward_convert\n'), (112, 'numpy.where', 'np.where', (['(det_category_r_all == sub_class)'], {}), True, 'import numpy as np\n'), (127, 'numpy.array', 'np.array', (['tmp_boxes_r_'], {}), True, 'import numpy as np\n'), (128, 'numpy.zeros', 'np.zeros', (['[tmp_boxes_r_.shape[0], tmp_boxes_r_.shape[1] + 1]'], {}), True, 'import numpy as np\n'), (130, 'numpy.array', 'np.array', (['tmp_score_r'], {}), True, 'import numpy as np\n'), (132, 'numpy.zeros', 'np.zeros', (['[tmp_boxes_r_.shape[0], tmp_boxes_r_.shape[1] + 1]'], {}), True, 'import numpy as np\n'), (137, 'numpy.array', 'np.array', (['tmp_boxes_r'], {}), True, 'import numpy as np\n'), (138, 'numpy.array', 'np.array', (['tmp_score_r'], {}), True, 'import numpy as np\n'), (139, 'numpy.array', 'np.array', (['tmp_label_r'], {}), True, 'import numpy as np\n'), (122, 'numpy.array', 'np.array', (['tmp_boxes_r_'], {}), True, 'import numpy as np\n'), (123, 'numpy.array', 'np.array', (['tmp_score_r'], {}), True, 'import numpy as np\n'), (133, 'numpy.random.rand', 'np.random.rand', (['tmp_boxes_r_.shape[0]'], {}), True, 'import numpy as np\n'), (134, 'numpy.array', 'np.array', (['tmp', 'np.float32'], {}), True, 'import numpy as np\n'), (134, 'numpy.array', 'np.array', (['jitter', 'np.float32'], {}), True, 'import numpy as np\n')]