Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.93 KB | None | 0 0
  1. from validation.pycocotools.coco import COCO
  2. from validation.pycocotools.cocoeval import COCOeval
  3. import copy
  4. import numpy as np
  5.  
  6. def _process_into_results(boxes, scores, orig_image_size=[900, 900], threshold=0.3, cpu=False):
  7.     """
  8.    Process the raw results of the detection model into an object that can be deserialized to JSON
  9.    """
  10.     #scores = scores[:, 0]
  11.     scale = np.array([orig_image_size[1], orig_image_size[0], orig_image_size[1], orig_image_size[0]])  ##################
  12.  
  13.     inds = np.where(scores > threshold)
  14.     if len(inds) == 0:
  15.         all_boxes = np.empty([0, 7], dtype=np.float32)
  16.         return []
  17.    
  18.     #print(inds)
  19.     #print(scores)
  20.     c_bboxes = boxes[inds]
  21.     c_scores = scores[inds]
  22.     c_dets = np.hstack((c_bboxes, c_scores[:, np.newaxis])).astype(np.float32, copy=False)
  23.  
  24.     #keep = soft_nms(c_dets)#, 0.45, force_cpu=cpu)
  25.     #keep = nms(c_dets, 0.45)
  26.     #keep = keep[:50]
  27.     #c_dets = c_dets[keep, :]
  28.     #_points = c_points[keep, :]
  29.     all_boxes = c_dets #np.hstack((c_dets, c_points))
  30.  
  31.     results = []
  32.  
  33.     for det in all_boxes:
  34.         conf = det[4]
  35.         if conf > threshold:
  36.             xmin = int(det[0])
  37.             ymin = int(det[1])
  38.             xmax = int(det[2])
  39.             ymax = int(det[3])
  40.  
  41.             #pt_x = int(round(det[5]))
  42.             #pt_y = int(round(det[6]))
  43.  
  44.             result = {
  45.                 'bbox': [xmin, ymin, xmax, ymax],
  46.                 #'ground': [pt_x, pt_y],
  47.                 'conf': conf
  48.             }
  49.             results += [result]
  50.     return results
  51.  
  52. gen_ids = 1
  53. def process_into_coco(people, image_id, is_truth, orig_size=[1., 1.], ignore_mask=None):
  54.     global gen_ids
  55.     image_id = 0
  56.     results = []
  57.     for person in people:
  58.         #print(person)
  59.         if is_truth:
  60.             score = 1.0
  61.             bbox = person[:4] #get_bbox(person['bbox'])
  62.             #floor_point = person['floor_point'][:2]
  63.             #bbox = clip_bbox(bbox, orig_size[1], orig_size[0])
  64.             bbox[2] -= bbox[0]
  65.             bbox[3] -= bbox[1]
  66.  
  67.             bbox[0] = int(bbox[0])
  68.             bbox[1] = int(bbox[1])
  69.             bbox[2] = int(bbox[2])
  70.             bbox[3] = int(bbox[3])
  71.             # Normalize by orig_size
  72.             #bbox /= np.tile(orig_size[::-1], (2,))
  73.             #floor_point /= np.array(orig_size[::-1])
  74.  
  75.             area = bbox[2] * bbox[3]
  76.         else:
  77.             score = person['conf']
  78.             xmin = person['bbox'][0]
  79.             ymin = person['bbox'][1]
  80.             xmax = person['bbox'][2]
  81.             ymax = person['bbox'][3]
  82.  
  83.             bbox = [xmin, ymin, xmax, ymax]
  84.             #bbox = clip_bbox(bbox, orig_size[1], orig_size[0])
  85.             bbox[2] -= bbox[0]
  86.             bbox[3] -= bbox[1]
  87.  
  88.             # Normalize by orig_size
  89.             #bbox /= np.tile(orig_size[::-1], (2,))
  90.  
  91.  
  92.         floor_point = [bbox[0], bbox[1]]
  93.         if is_truth:
  94.             # coco truth wants `id` and its predicted input wants `image_id`...
  95.             coco = {'id': gen_ids, 'image_id': image_id, 'category_id': 1, 'score': float(score), 'bbox': list(bbox),
  96.                     'iscrowd': 0, 'area': area, 'points': [list(floor_point)] }
  97.             gen_ids += 1
  98.         else:
  99.             coco = {'image_id': image_id, 'category_id': 1, 'score': float(score), 'bbox': list(bbox), 'points': [list(floor_point), ] }
  100.         #print(coco)
  101.         results.append(coco)
  102.  
  103.     if len(results) == 0:
  104.         # TODO: fix issue with missing gts for some images
  105.         if is_truth:
  106.             results = [{'id': gen_ids, 'image_id': image_id, 'category_id': 1, 'score': 0., 'bbox': [0, 0, 0, 0],
  107.                         'iscrowd': 0, 'area': 0, 'ignore': 1}]
  108.             gen_ids += 1
  109.  
  110.     return results
  111.  
  112. def run_coco_validation(truth_data, validation_results, use_coco_val, conf_type_step):
  113.     truths = COCO()
  114.     truths.dataset['images'] = [{'id': gt['image_id']} for gt in truth_data]
  115.     # it uses images but we just need the ID
  116.     truths.dataset['annotations'] = truth_data
  117.     truths.dataset['categories'] = [{
  118.         "supercategory": "peplum",
  119.         "id": 1,
  120.         "name": "peplum",
  121.     }]
  122.  
  123.     truths.createIndex()
  124.     results = truths.loadRes(copy.deepcopy(validation_results))
  125.     if results is None:
  126.         logging.info("No validation to be run. Stopping program here.")
  127.         return None, None
  128.  
  129.     if conf_type_step:
  130.         coco_eval = COCOeval(truths, results, 'bbox', 'step', None)
  131.     else:
  132.         coco_eval = COCOeval(truths, results, 'bbox', None, None)
  133.     coco_eval.evaluate()
  134.     distances, _ = coco_eval.compute_distance_points()
  135.     coco_eval.accumulate()
  136.     coco_eval.summarize(use_coco_val, print_=False)
  137.  
  138.     return coco_eval.stats, distances
  139.  
  140. def evaluate(output, filenames, gt_bboxes, mode):
  141.     bboxes, scores = [], []
  142.     validation_results = []
  143.     truth_data = []
  144.     for pict_idx in range(len(filenames)):
  145.         bb, s = output[pict_idx][:, 1:], output[pict_idx][:, 0]
  146.         #print(bb)
  147.         #print(s)
  148.         #for pict_idx in range(len(bb)):
  149.             #for det_indx in range(len(bb[pict_idx])):
  150.         result = _process_into_results(bb, s, threshold=0.01)
  151.                 #score.append(dets)
  152.         #print(process_into_coco(gt_bboxes[pict_idx], filenames[pict_idx], True))
  153.         validation_results += process_into_coco(result, filenames, False)
  154.         truth_data += process_into_coco(gt_bboxes[pict_idx], filenames[pict_idx], True) ###################### разобраться с индексацией
  155.         #plot_det(cv2.imread(filenames[batch_idx][pict_idx]), validation_results, mode)
  156.     #print(truth_data)
  157.     #print(validation_results)
  158.     mscoco_summary_stats, distances = run_coco_validation(truth_data, validation_results, True, conf_type_step=0.5)
  159.     print(mode + ' mAP :  ')
  160.     #print('IoU = 0.0    ' + str(mscoco_summary_stats['precision'][0.05]['all'][0.0]))
  161.     print('IoU = 0.2    ' + str(mscoco_summary_stats['precision'][0.05]['all'][0.2]))
  162.     print('IoU = 0.5    ' + str(mscoco_summary_stats['precision'][0.05]['all'][0.5]))
  163.     print('IoU = 0.8    ' + str(mscoco_summary_stats['precision'][0.05]['all'][0.8]))
  164.     return mscoco_summary_stats['precision'][0.05]['all'][0.2], mscoco_summary_stats['precision'][0.05]['all'][0.5], mscoco_summary_stats['precision'][0.05]['all'][0.8]
  165.     # print(images[0].detach().numpy().transpose().shape
  166.  
  167. output_to_eval = list(np.load('pred.npy'))
  168. gt_bboxes_to_eval = list(np.load('gt.npy'))
  169. filenames_to_eval = np.arange(len(output_to_eval))
  170.  
  171. for i in range(len(gt_bboxes_to_eval)):
  172.     for j in range(len(gt_bboxes_to_eval[i])):
  173.         gt_bboxes_to_eval[i][j][4] = 0
  174.        
  175. for i in range(len(output_to_eval)):
  176.     for j in range(len(output_to_eval[i])):
  177.         output_to_eval[i][j] = [output_to_eval[i][j][4], output_to_eval[i][j][0], output_to_eval[i][j][1], output_to_eval[i][j][2], output_to_eval[i][j][3]]
  178.        
  179. evaluate(output_to_eval, filenames_to_eval, gt_bboxes_to_eval, 'Train')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement