Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data #275

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open

data #275

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ed735ca
Update ssd_vgg_300.py
Tangzixia May 6, 2018
a3c55bb
Update ssd_vgg_300.py
Tangzixia May 6, 2018
cfa0eb4
Update ssd_vgg_300.py
Tangzixia May 6, 2018
5cb1131
Update train_ssd_network.py
Tangzixia May 6, 2018
357d1c5
Update train_ssd_network.py
Tangzixia May 6, 2018
118b6a5
Update ssd_common.py
Tangzixia May 6, 2018
390d18d
Update ssd_common.py
Tangzixia May 7, 2018
16720f9
Update ssd_vgg_preprocessing.py
Tangzixia May 8, 2018
a07b96c
Update ssd_vgg_preprocessing.py
Tangzixia May 8, 2018
ca53926
Update ssd_vgg_preprocessing.py
Tangzixia May 8, 2018
aee5be0
Update tf_image.py
Tangzixia May 8, 2018
7ce5cbe
Update vgg_preprocessing.py
Tangzixia May 8, 2018
0b3e728
Update vgg_preprocessing.py
Tangzixia May 8, 2018
fdfd27f
Update ssd_common.py
Tangzixia May 9, 2018
265d51b
Update ssd_common.py
Tangzixia May 9, 2018
e2d1f56
Update train_ssd_network.py
Tangzixia May 10, 2018
5c5eee6
Update ssd_common.py
Tangzixia May 10, 2018
9474813
Update ssd_vgg_300.py
Tangzixia May 10, 2018
b9e29d6
Update ssd_common.py
Tangzixia May 10, 2018
9e7d9a6
Create eval_video.py
Tangzixia Aug 6, 2018
591afa3
Create ssd_notebook.py
Tangzixia Aug 6, 2018
e348481
Create label2xml.py
Tangzixia Aug 7, 2018
7b61975
Create train_val_split.py
Tangzixia Aug 7, 2018
53fa111
Create mAP_tutorial.py
Tangzixia Aug 11, 2018
7285a91
Create mAP_tutorial_std.py
Tangzixia Aug 27, 2018
5ad9a1c
Update mAP_tutorial_std.py
Tangzixia Aug 27, 2018
a82c7c0
Create criteria.txt
Tangzixia Aug 27, 2018
5968a92
Update README.md
Tangzixia Oct 30, 2018
6dc0e8c
Update README.md
Tangzixia Oct 30, 2018
68c8aeb
Update README.md
Tangzixia Oct 30, 2018
336d3ea
Update README.md
Tangzixia Oct 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# 参考:https://zhuanlan.zhihu.com/p/33544892 讲解,注意SSD是属于Faster RCNN那一挂的,都有设置先验框,然后预测的仅仅只是偏移而已~
# SSD: Single Shot MultiBox Detector in TensorFlow
## 参考py-faster-rcnn的设置,可以发现其滤除的是confidence score小于0.05和nms thresh>0.3的值,然后生成检测文件~


SSD is an unified framework for object detection with a single network. It has been originally introduced in this research [article](http://arxiv.org/abs/1512.02325).

Expand All @@ -25,7 +28,13 @@ and then start a jupyter notebook with
```bash
jupyter notebook notebooks/ssd_notebook.ipynb
```
## 预测流程

1、根据置信度确定其类别(置信度最大的为对应的类别),滤除背景类别及其置信度小于阈值的预测框.
2、根据先验框和预测的偏移还原回预测框,同时clip操作,防止预测框超出图片.
3、根据置信度对预测框由大到小进行排序,选择top-k个预测框.
4、运行NMS算法,设置NMS阈值,滤除掉那些重叠较大的框,得到最后的预测框.
### 注意NMS算法有两种理解,针对每一类进行NMS和针对所有类别进行NMS操作~

## Datasets

Expand Down
2 changes: 2 additions & 0 deletions criteria.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
关于目标检测相关的东西,标准的检测方案如下:
http://host.robots.ox.ac.uk/pascal/VOC/voc2012/htmldoc/devkit_doc.html#SECTION00044000000000000000
264 changes: 264 additions & 0 deletions mAP_tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
#coding=utf-8
"""
参照voc_detection的标准的mAP计算方法
用来计算我们自己的数据的值,
mean Average Precision指的是多个类的AP的平均,
怎么平均的mAP,
怎么弄呢,其实就是将多个类的AP求和,然后进行平均,即可得到检测的mAP值。
"""
import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
from pylab import mpl
import sys
reload(sys)
sys.setdefaultencoding('utf8')

def voc_ap(rec, prec, use_07_metric=False):
if use_07_metric:
ap = 0.
for t in np.arange(0., 1.1, 0.1):
if np.sum(rec >= t) == 0:
p = 0
else:
p = np.max(prec[rec >= t])
ap = ap + p / 11.
else:
mrec = np.concatenate(([0.], rec, [1.]))
mpre = np.concatenate(([0.], prec, [0.]))

for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
i = np.where(mrec[1:] != mrec[:-1])[0]

ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap

#如果是self_data数据,则需要对label进行转化,讲中心点加宽高转化为左上加右下
def read_file(file,classname):
with open(file,'r') as f:
lines=f.readlines()
if classname==-1:
lines=[line.strip().split(" ") for line in lines]
bboxes_to=[]
for item in lines:
bboxes_to.append(item[1:])
else:
lines_=[line.strip().split(" ") for line in lines]
lines=[]
bboxes_to=[]
for i,line in enumerate(lines_):
if int(line[0])==classname:
lines.append(line)
bboxes_to.append(line[1:])
# print(bboxes_to_[i])
# bboxes=[]
# for bbox in bboxes_to:
# item=[float(bbox[0]),float(bbox[1]),float(bbox[2]),float(bbox[3])]
# bboxes.append(item)
# return np.array(bboxes),lines
bboxes = []
for bbox in bboxes_to:
item = [float(bbox[0])-float(bbox[2])/2.0, float(bbox[1])-float(bbox[3])/2.0, float(bbox[0])+float(bbox[2])/2.0, float(bbox[1])+float(bbox[3])/2.0]
bboxes.append(item)
return np.array(bboxes),lines

def convert(gt_dir,classname):
class_recs={}
npos=0
for root,dirs,files in os.walk(gt_dir):
for i,file in enumerate(files):
cur_path=os.path.join(root,file)
bbox,R=read_file(cur_path,classname)
if classname!=-1:
det=[False]*len(R)
npos+=len(R)
class_recs[file]={"bbox":bbox,'det':det}
else:
gt_cls_id=[]
for item in R:
gt_cls_id.append(item[0])
det = [False] * len(R)
npos += len(R)
class_recs[file] = {"bbox": bbox, 'det': det, "gt_cls_id":gt_cls_id}
print("正在转化中。。。"+str(len(files)-i))
return class_recs,npos
#更加详细的资料可以查看https://github.com/Tangzixia/Object-Detection-Metrics#average-precision
#计算某个类的AP,class=-1时候代表计算所有类的mAP
def gen_ap(gt_dir,pred_res,classname,iou=0.5):
class_recs,npos=convert(gt_dir,classname)
with open(pred_res,'r') as f:
lines=f.readlines()
#img_id,confidence,BB,
#得分
splitlines=[item.strip().split(" ") for item in lines]
img_ids=[x[0] for x in splitlines]
cls_flgs=np.array([x[1] for x in splitlines])
confidence=np.array([float(x[2]) for x in splitlines])
BB=np.array([[float(z) for z in x[3:]] for x in splitlines])

# 找出每一类对应的预测候选框
# 如果是classname==-1,则说明需要计算所有类的mAP值,这时候需要得到所有的类别标签
if classname!=-1:
inds=np.zeros(len(splitlines))
for i,item in enumerate(splitlines):
if int(item[1])==classname:
inds[i]=1
img_ids_=[]
confidence_=[]
BB_=[]
for i,item in enumerate(splitlines):
if inds[i]==1:
img_ids_.append(img_ids[i])
confidence_.append(confidence[i])
BB_.append(BB[i])
img_ids=img_ids_
confidence=np.array(confidence_)
BB=np.array(BB_)
# img_ids=list(np.array(img_ids[np.array(inds)]))
# confidence=list(np.array(confidence[np.array(inds)]))
# BB=list(np.array(BB[np.array(inds)]))


#confidence由大到小排序
sorted_ind=np.argsort(-confidence)
# np.argsort(-confidence<=-.3)
sorted_ind1 = np.where(confidence[sorted_ind] >= .0)[0]
sorted_ind = sorted_ind[sorted_ind1]
print(len(sorted_ind))
BB=BB[sorted_ind,:]
img_ids=[img_ids[x] for x in sorted_ind]

# sorted_ind = np.argsort(-confidence)
# print(len(sorted_ind))
# BB = BB[sorted_ind, :]
# img_ids = [img_ids[x] for x in sorted_ind]

nd=len(img_ids)
print(nd)
tp=np.zeros(nd)
fp=np.zeros(nd)

for d in range(nd):
R=class_recs[img_ids[d]]
bb=BB[d,:].astype(float)
ovmax = -np.inf
BBGT=R['bbox'].astype(float)

if BBGT.size>0:
ixmin = np.maximum(BBGT[:, 0], bb[0])
iymin = np.maximum(BBGT[:, 1], bb[1])
ixmax = np.minimum(BBGT[:, 2], bb[2])
iymax = np.minimum(BBGT[:, 3], bb[3])
iw = np.maximum(ixmax - ixmin + 1., 0.)
ih = np.maximum(iymax - iymin + 1., 0.)
inters = iw * ih

uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +
(BBGT[:, 2] - BBGT[:, 0] + 1.) *
(BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)
overlaps=inters/uni
ovmax=np.max(overlaps)
# print(ovmax)
jmax=np.argmax(overlaps)
if ovmax>iou:
if not R['det'][jmax]:
tp[d]=1
R['det'][jmax]=1
else:
fp[d]=1
else:
fp[d]=1
else:
# confidence由大到小排序
sorted_ind = np.argsort(-confidence)
# np.argsort(-confidence<=-.3)
sorted_ind1 = np.where(confidence[sorted_ind] >= .3)[0]
sorted_ind = sorted_ind[sorted_ind1]
BB = BB[sorted_ind, :]
img_ids = [img_ids[x] for x in sorted_ind]
cls_flgs=cls_flgs[sorted_ind]

# sorted_ind = np.argsort(-confidence)
# print(len(sorted_ind))
# BB = BB[sorted_ind, :]
# img_ids = [img_ids[x] for x in sorted_ind]

nd = len(img_ids)
print(nd)
tp = np.zeros(nd)
fp = np.zeros(nd)

for d in range(nd):
R = class_recs[img_ids[d]]
bb = BB[d, :].astype(float)
ovmax = -np.inf
BBGT = R['bbox'].astype(float)
if BBGT.size > 0:
ixmin = np.maximum(BBGT[:, 0], bb[0])
iymin = np.maximum(BBGT[:, 1], bb[1])
ixmax = np.minimum(BBGT[:, 2], bb[2])
iymax = np.minimum(BBGT[:, 3], bb[3])
iw = np.maximum(ixmax - ixmin + 1., 0.)
ih = np.maximum(iymax - iymin + 1., 0.)
inters = iw * ih

uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +
(BBGT[:, 2] - BBGT[:, 0] + 1.) *
(BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)
overlaps = inters / uni
ovmax = np.max(overlaps)
# print(ovmax)
jmax = np.argmax(overlaps)
if ovmax > iou and R['gt_cls_id'][jmax]==cls_flgs[d]:
if not R['det'][jmax]:
tp[d] = 1
R['det'][jmax] = 1
else:
fp[d] = 1
else:
fp[d] = 1

fp=np.cumsum(fp)
tp=np.cumsum(tp)
rec=tp/float(npos)
prec=tp/np.maximum(tp+fp,np.finfo(np.float64).eps)

ap = voc_ap(rec, prec)
return rec,prec,ap
def draw_plot(rec,prec,ap,name,path):
if os.path.exists(path)==False:
os.mkdir(path)
myfont = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSansCJK.ttc")
mpl.rcParams['axes.unicode_minus'] = False
tick=np.arange(0,1.1,0.1)
plt.figure()
plt.title(name+":"+str(ap),fontproperties=myfont)
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.axis([0,1,0,1.05])
plt.xticks(tick)
plt.yticks(tick)
plt.plot(rec,prec)
# plt.show()

plt.savefig(os.path.join(path,name+".png"))
if __name__=="__main__":
gt_dir = "/home/hp/Data/house_data/train/Data_valid/labels_initial/"
pred_res = "../res_self_data_0.0.txt"
mAP_file="/home/hp/Desktop/yolov3-res/map.txt"

dict_ = {"0": u"迷彩建筑", "1": u"一般建筑", "2": u"迷彩油罐", "3": u"一般油罐", "4": u"迷彩雷达", "5": u"一般雷达"}
ap_list=[]
for i in range(6):
classname =i
rec, prec, ap = gen_ap(gt_dir, pred_res, classname)
draw_plot(rec,prec,ap,dict_[str(classname)],path="/home/hp/Desktop/yolov3-res/")
ap_list.append(ap)
print(rec, prec, ap)
with open(mAP_file,'w') as f:
for i,ap in enumerate(ap_list):
f.write(str(dict_[str(i)].decode('utf8'))+":"+str(ap)+"\n")
f.write("mAP:"+str(round(np.array(ap_list).mean(),4)))
print("mAP50的值为:",round(np.array(ap_list).mean(),4))
Loading