def compareXml(testPath, dstPath, savePath, num_classes=16, conf_thresh=0.3, IOU_thresh=0.3): """ 计算混淆矩阵 Args: testPath:预测结果文件 dstPath:真实标注文件 savePath:混淆矩阵保存路径 num_classes:细胞种类 conf_thresh:混淆矩阵置信度阈值 IOU_thresh:混淆矩阵IOU阈值 Returns: """ table = np.zeros((num_classes+1, num_classes+1)) for subdir in os.listdir(testPath): xmllist_test = os.listdir(testPath + subdir) for xmlfile in xmllist_test: testfile = testPath + subdir + "/" + xmlfile dstfile = dstPath + subdir + "/" + xmlfile DOMTree_test = xml.dom.minidom.parse(testfile) # 打开xml文档 DOMTree_dst = xml.dom.minidom.parse(dstfile) collection_test = DOMTree_test.documentElement # 得到文档元素对象 collection_dst = DOMTree_dst.documentElement objectlist_test = collection_test.getElementsByTagName("object") # 得到标签名为object的信息 objectlist_dst = collection_dst.getElementsByTagName("object") conf_mat = ConfusionMatrix(num_classes=num_classes, CONF_THRESHOLD=conf_thresh, IOU_THRESHOLD=IOU_thresh) my_result = [] gt_bbox_label = [] for objects_test in objectlist_test: namelist_test = objects_test.getElementsByTagName('name') name_test = namelist_test[0].childNodes[0].data bndbox_test = objects_test.getElementsByTagName('bndbox') p1 = [] for box in bndbox_test: p_list = box.getElementsByTagName('xmin') p1.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymin') p1.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('xmax') p1.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymax') p1.append(int(p_list[0].childNodes[0].data)) p1.append(1) p1.append(findIndex(name_test)) my_result.append(p1) for objects_dst in objectlist_dst: namelist_dst = objects_dst.getElementsByTagName('name') name_dst = namelist_dst[0].childNodes[0].data bndbox_dst = objects_dst.getElementsByTagName('bndbox') p2 = [] p2.append(findIndex(name_dst)) for box in bndbox_dst: p_list = box.getElementsByTagName('xmin') p2.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymin') p2.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('xmax') p2.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymax') p2.append(int(p_list[0].childNodes[0].data)) gt_bbox_label.append(p2) my_result = np.array(my_result) gt_bbox_label = np.array(gt_bbox_label) if gt_bbox_label.shape == (0,): continue # print("my_result = ", my_result) # print("gt = ", gt_bbox_label) conf_mat.process_batch(my_result, gt_bbox_label) tmp = conf_mat.return_matrix() table = table + tmp np.savetxt(savePath + "table.txt", table, fmt="%d", delimiter="\t")
def compareXml(testPath, dstPath): '''比较测试集预测xml和标注的xml,求出混淆矩阵''' table = np.zeros((17,17)) nums = np.zeros((16,), dtype=np.int16) for subdir in os.listdir(testPath): xmllist_test = os.listdir(testPath+subdir) for xmlfile in xmllist_test: testfile = testPath + subdir + "/" + xmlfile dstfile = dstPath + subdir + "/" + xmlfile DOMTree_test = xml.dom.minidom.parse(testfile) # 打开xml文档 DOMTree_dst = xml.dom.minidom.parse(dstfile) collection_test = DOMTree_test.documentElement # 得到文档元素对象 collection_dst = DOMTree_dst.documentElement objectlist_test = collection_test.getElementsByTagName("object") # 得到标签名为object的信息 objectlist_dst = collection_dst.getElementsByTagName("object") conf_mat = ConfusionMatrix(num_classes=16, CONF_THRESHOLD=0.3, IOU_THRESHOLD=0.3) my_result = [] gt_bbox_label = [] for objects_test in objectlist_test: namelist_test = objects_test.getElementsByTagName('name') name_test = namelist_test[0].childNodes[0].data bndbox_test = objects_test.getElementsByTagName('bndbox') p1 = [] nums[findIndex(name_test)] += 1 for box in bndbox_test: p_list = box.getElementsByTagName('xmin') p1.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymin') p1.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('xmax') p1.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymax') p1.append(int(p_list[0].childNodes[0].data)) p1.append(1) p1.append(findIndex(name_test)) my_result.append(p1) for objects_dst in objectlist_dst: namelist_dst = objects_dst.getElementsByTagName('name') name_dst = namelist_dst[0].childNodes[0].data if findIndex(name_dst)==15: continue bndbox_dst = objects_dst.getElementsByTagName('bndbox') p2 = [] p2.append(findIndex(name_dst)) for box in bndbox_dst: p_list = box.getElementsByTagName('xmin') p2.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymin') p2.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('xmax') p2.append(int(p_list[0].childNodes[0].data)) p_list = box.getElementsByTagName('ymax') p2.append(int(p_list[0].childNodes[0].data)) gt_bbox_label.append(p2) my_result = np.array(my_result) gt_bbox_label = np.array(gt_bbox_label) # print("my_result.shape =", my_result.shape) conf_mat.process_batch(my_result, gt_bbox_label) tmp = conf_mat.return_matrix() # input() table = table + tmp np.savetxt(savePath+"table.txt", table, fmt="%d", delimiter="\t") # 混淆矩阵 np.savetxt(savePath+"nums.txt", table, fmt="%d", delimiter="\t") # 预测各类细胞个数 return 0