diff --git a/.gitignore b/.gitignore index 71e456f1f3f142249271b1056b216132e34594af..6333d390a27d0ef8b189c1ab018b9753069676d0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ datasets/ Medical_Datasets/ lfw/ logs/ -model_data/ .temp_map_out/ 2007_train.txt 2007_val.txt diff --git a/get_map.py b/get_map.py index 7b7e502e39e504b6eeb7232ca17641a79329aede..76aa631761813d733e7940024e285f6e23446d17 100644 --- a/get_map.py +++ b/get_map.py @@ -1,12 +1,11 @@ import os import xml.etree.ElementTree as ET - +import cv2 from PIL import Image from tqdm import tqdm import numpy as np from utils.utils import get_classes from utils.utils_map import get_coco_map, get_map -from utils.utils_rbox import poly2hbb from yolo import YOLO if __name__ == "__main__": @@ -126,16 +125,12 @@ if __name__ == "__main__": x4 = bndbox.find('x4').text y4 = bndbox.find('y4').text poly = np.array([[x1, y1, x2, y2, x3, y3, x4, y4]], dtype=np.int32) - hbb = poly2hbb(poly) - xc, yc, w, h = hbb[0] - left = xc - w/2 - top = yc - h/2 - right = xc + w/2 - bottom = yc + h/2 + poly = poly.reshape(4, 2) + (x, y), (w, h), angle = cv2.minAreaRect(poly) # θ ∈ [0, 90] if difficult_flag: - new_f.write("%s %s %s %s %s difficult\n" % (obj_name, left, top, right, bottom)) + new_f.write("%s %s %s %s %s %s difficult\n" % (obj_name, int(x), int(y), int(w), int(h),angle)) else: - new_f.write("%s %s %s %s %s\n" % (obj_name, left, top, right, bottom)) + new_f.write("%s %s %s %s %s %s\n" % (obj_name, int(x), int(y), int(w), int(h),angle)) print("Get ground truth result done.") if map_mode == 0 or map_mode == 3: diff --git a/model_data/ssdd_classes.txt b/model_data/ssdd_classes.txt new file mode 100644 index 0000000000000000000000000000000000000000..ca76c80b5b2cd0b25047f75736656cfebc9da7aa --- /dev/null +++ b/model_data/ssdd_classes.txt @@ -0,0 +1,80 @@ +person +bicycle +car +motorbike +aeroplane +bus +train +truck +boat +traffic light +fire hydrant +stop sign +parking meter +bench +bird +cat +dog +horse +sheep +cow +elephant +bear +zebra +giraffe +backpack +umbrella +handbag +tie +suitcase +frisbee +skis +snowboard +sports ball +kite +baseball bat +baseball glove +skateboard +surfboard +tennis racket +bottle +wine glass +cup +fork +knife +spoon +bowl +banana +apple +sandwich +orange +broccoli +carrot +hot dog +pizza +donut +cake +chair +sofa +pottedplant +bed +diningtable +toilet +tvmonitor +laptop +mouse +remote +keyboard +cell phone +microwave +oven +toaster +sink +refrigerator +book +clock +vase +scissors +teddy bear +hair drier +toothbrush diff --git a/utils/nms_rotated/nms_rotated_ext.cp38-win_amd64.pyd b/utils/nms_rotated/nms_rotated_ext.cp38-win_amd64.pyd index 0ae23b824b9001eaa1b31f807eb45e10329da425..fd98ed9b98a8b633e3b148bd0acefb8852ccabda 100644 Binary files a/utils/nms_rotated/nms_rotated_ext.cp38-win_amd64.pyd and b/utils/nms_rotated/nms_rotated_ext.cp38-win_amd64.pyd differ diff --git a/utils/utils_map.py b/utils/utils_map.py index b49ddbae9176bde9530321f62eaebf3e370bde86..cff55a9dc0301ad3b62ede1377a0f2193bd07abc 100644 --- a/utils/utils_map.py +++ b/utils/utils_map.py @@ -5,6 +5,7 @@ import operator import os import shutil import sys + try: from pycocotools.coco import COCO from pycocotools.cocoeval import COCOeval @@ -28,6 +29,24 @@ import numpy as np (Right,Bottom) ''' +def iou_rotate_calculate(boxes1, boxes2): + """ + 计算旋转面积 + boxes1,boxes2格式为x,y,w,h,theta + """ + area1 = boxes1[2] * boxes1[3] + area2 = boxes2[2] * boxes2[3] + r1 = ((boxes1[0], boxes1[1]), (boxes1[2], boxes1[3]), boxes1[4]) + r2 = ((boxes2[0], boxes2[1]), (boxes2[2], boxes2[3]), boxes2[4]) + int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] + if int_pts is not None: + order_pts = cv2.convexHull(int_pts, returnPoints=True) + int_area = cv2.contourArea(order_pts) + ious = int_area * 1.0 / (area1 + area2 - int_area) + else: + ious = 0 + return ious + def log_average_miss_rate(precision, fp_cumsum, num_images): """ log-average miss rate: @@ -209,7 +228,7 @@ def draw_plot_func(dictionary, n_classes, window_title, plot_title, x_label, out """ fig = plt.gcf() # gcf - get current figure axes = plt.gca() - r = fig.canvas.get_renderer() + r = fig.canvas.manager.get_renderer() for i, val in enumerate(sorted_values): fp_val = fp_sorted[i] tp_val = tp_sorted[i] @@ -238,7 +257,7 @@ def draw_plot_func(dictionary, n_classes, window_title, plot_title, x_label, out if i == (len(sorted_values)-1): # largest bar adjust_axes(r, t, fig, axes) # set window title - fig.canvas.set_window_title(window_title) + fig.canvas.manager.set_window_title(window_title) # write classes in y axis tick_font_size = 12 plt.yticks(range(n_classes), sorted_keys, fontsize=tick_font_size) @@ -328,35 +347,37 @@ def get_map(MINOVERLAP, draw_plot, score_threhold=0.5, path = './map_out'): for line in lines_list: try: if "difficult" in line: - class_name, left, top, right, bottom, _difficult = line.split() + class_name, x, y, w, h,angle, _difficult = line.split() is_difficult = True else: - class_name, left, top, right, bottom = line.split() + class_name, x, y, w, h,angle = line.split() except: if "difficult" in line: line_split = line.split() _difficult = line_split[-1] - bottom = line_split[-2] - right = line_split[-3] - top = line_split[-4] - left = line_split[-5] + angle = line_split[-2] + h = line_split[-3] + w = line_split[-4] + y = line_split[-5] + x = line_split[-6] class_name = "" - for name in line_split[:-5]: + for name in line_split[:-6]: class_name += name + " " - class_name = class_name[:-1] + class_name = class_name[:-1] is_difficult = True else: line_split = line.split() - bottom = line_split[-1] - right = line_split[-2] - top = line_split[-3] - left = line_split[-4] + angle = line_split[-1] + h = line_split[-2] + w = line_split[-3] + y = line_split[-4] + x = line_split[-5] class_name = "" - for name in line_split[:-4]: + for name in line_split[:-5]: class_name += name + " " class_name = class_name[:-1] - bbox = left + " " + top + " " + right + " " + bottom + bbox = x + " " + y + " " + w + " " + h + " " + angle if is_difficult: bounding_boxes.append({"class_name":class_name, "bbox":bbox, "used":False, "difficult":True}) is_difficult = False @@ -396,21 +417,22 @@ def get_map(MINOVERLAP, draw_plot, score_threhold=0.5, path = './map_out'): lines = file_lines_to_list(txt_file) for line in lines: try: - tmp_class_name, confidence, left, top, right, bottom = line.split() + tmp_class_name, confidence, x, y, w, h,angle = line.split() except: line_split = line.split() - bottom = line_split[-1] - right = line_split[-2] - top = line_split[-3] - left = line_split[-4] - confidence = line_split[-5] + angle = line_split[-1] + h = line_split[-2] + w = line_split[-3] + y = line_split[-4] + x = line_split[-5] + confidence = line_split[-6] tmp_class_name = "" - for name in line_split[:-5]: + for name in line_split[:-6]: tmp_class_name += name + " " tmp_class_name = tmp_class_name[:-1] if tmp_class_name == class_name: - bbox = left + " " + top + " " + right + " " +bottom + bbox = x + " " + y + " " + w + " " + h + " " + angle bounding_boxes.append({"confidence":confidence, "file_id":file_id, "bbox":bbox}) bounding_boxes.sort(key=lambda x:float(x['confidence']), reverse=True) @@ -464,17 +486,13 @@ def get_map(MINOVERLAP, draw_plot, score_threhold=0.5, path = './map_out'): bb = [float(x) for x in detection["bbox"].split()] for obj in ground_truth_data: if obj["class_name"] == class_name: - bbgt = [ float(x) for x in obj["bbox"].split() ] - bi = [max(bb[0],bbgt[0]), max(bb[1],bbgt[1]), min(bb[2],bbgt[2]), min(bb[3],bbgt[3])] - iw = bi[2] - bi[0] + 1 - ih = bi[3] - bi[1] + 1 - if iw > 0 and ih > 0: - ua = (bb[2] - bb[0] + 1) * (bb[3] - bb[1] + 1) + (bbgt[2] - bbgt[0] - + 1) * (bbgt[3] - bbgt[1] + 1) - iw * ih - ov = iw * ih / ua - if ov > ovmax: - ovmax = ov - gt_match = obj + bbgt = [float(x) for x in obj["bbox"].split() ] + box1 = np.array([bb[0], bb[1], bb[2], bb[3], bb[4]], np.float32) + box2 = np.array([bbgt[0], bbgt[1], bbgt[2], bbgt[3], bbgt[4]], np.float32) + ov = iou_rotate_calculate(box1, box2) + if ov > ovmax: + ovmax = ov + gt_match = obj if show_animation: status = "NO MATCH FOUND!" @@ -606,7 +624,7 @@ def get_map(MINOVERLAP, draw_plot, score_threhold=0.5, path = './map_out'): plt.fill_between(area_under_curve_x, 0, area_under_curve_y, alpha=0.2, edgecolor='r') fig = plt.gcf() - fig.canvas.set_window_title('AP ' + class_name) + fig.canvas.manager.set_window_title('AP ' + class_name) plt.title('class: ' + text) plt.xlabel('Recall') @@ -782,7 +800,7 @@ def get_map(MINOVERLAP, draw_plot, score_threhold=0.5, path = './map_out'): plot_title = "mAP = {0:.2f}%".format(mAP*100) x_label = "Average Precision" output_path = RESULTS_FILES_PATH + "/mAP.png" - to_show = True + to_show = False plot_color = 'royalblue' draw_plot_func( ap_dictionary, diff --git a/yolo.py b/yolo.py index f5a5da46a31fe4885c641ee75acfaaae4479ef63..b4d0fd193ac0c1e953cd5ee6cb8f23a5c64540d1 100644 --- a/yolo.py +++ b/yolo.py @@ -376,22 +376,17 @@ class YOLO(object): top_label = np.array(results[0][:, 7], dtype = 'int32') top_conf = results[0][:, 5] * results[0][:, 6] top_rboxes = results[0][:, :5] - top_polys = rbox2poly(top_rboxes) - top_hbbs = poly2hbb(top_polys) for i, c in list(enumerate(top_label)): predicted_class = self.class_names[int(c)] - hbb = top_hbbs[i] + obb = top_rboxes[i] score = str(top_conf[i]) - xc, yc, w, h = hbb - left = xc - w/2 - top = yc - h/2 - right = xc + w/2 - bottom = yc + h/2 + xc, yc, w, h, angle = obb + if predicted_class not in class_names: continue - f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)),str(int(bottom)))) + f.write("%s %s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(xc)), str(int(yc)), str(int(w)), str(int(h)), str(math.degrees(angle)))) f.close() return