Skip to content

Commit

Permalink
perf(util): 移除不相关函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zjZSTU committed Apr 13, 2020
1 parent b21a1fe commit bba1d8b
Showing 1 changed file with 0 additions and 67 deletions.
67 changes: 0 additions & 67 deletions py/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"""

import os
import numpy as np
import xmltodict
import torch
import random
import matplotlib.pyplot as plt
Expand All @@ -29,71 +27,6 @@ def num_model(model):
return sum(param.numel() for param in model.parameters())


def parse_car_csv(csv_dir):
csv_path = os.path.join(csv_dir, 'car.csv')
samples = np.loadtxt(csv_path, dtype=np.str)
return samples


def parse_xml(xml_path):
"""
解析xml文件,返回标注边界框坐标
"""
# print(xml_path)
with open(xml_path, 'rb') as f:
xml_dict = xmltodict.parse(f)
# print(xml_dict)

bndboxs = list()
objects = xml_dict['annotation']['object']
if isinstance(objects, list):
for obj in objects:
obj_name = obj['name']
difficult = int(obj['difficult'])
if 'car'.__eq__(obj_name) and difficult != 1:
bndbox = obj['bndbox']
bndboxs.append((int(bndbox['xmin']), int(bndbox['ymin']), int(bndbox['xmax']), int(bndbox['ymax'])))
elif isinstance(objects, dict):
obj_name = objects['name']
difficult = int(objects['difficult'])
if 'car'.__eq__(obj_name) and difficult != 1:
bndbox = objects['bndbox']
bndboxs.append((int(bndbox['xmin']), int(bndbox['ymin']), int(bndbox['xmax']), int(bndbox['ymax'])))
else:
pass

return np.array(bndboxs)


def iou(pred_box, target_box):
"""
计算候选建议和标注边界框的IoU
:param pred_box: 大小为[4]
:param target_box: 大小为[N, 4]
:return: [N]
"""
xA = np.maximum(pred_box[0], target_box[:, 0])
yA = np.maximum(pred_box[1], target_box[:, 1])
xB = np.minimum(pred_box[2], target_box[:, 2])
yB = np.minimum(pred_box[3], target_box[:, 3])
# 计算交集面积
intersection = np.maximum(0.0, xB - xA) * np.maximum(0.0, yB - yA)
# 计算两个边界框面积
boxAArea = (pred_box[2] - pred_box[0]) * (pred_box[3] - pred_box[1])
boxBArea = (target_box[:, 2] - target_box[:, 0]) * (target_box[:, 3] - target_box[:, 1])

scores = intersection / (boxAArea + boxBArea - intersection)
return scores


def compute_ious(rects, bndboxs):
iou_list = list()
for rect in rects:
scores = iou(rect, bndboxs)
iou_list.append(max(scores))
return iou_list


def save_model(model, model_save_path):
# 保存最好的模型参数
check_dir('./models')
Expand Down

0 comments on commit bba1d8b

Please sign in to comment.