Skip to content

Commit 20c1a8f

Browse files
authored
Add files via upload
1 parent a2a6da7 commit 20c1a8f

28 files changed

Lines changed: 3284 additions & 3 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Bubbliiiing
3+
Copyright (c) 2020 JiaQi Xu
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
# faster-rcnn-tf2
2-
这是一个faster-rcnn的tensorflow2实现的库,可以利用voc数据集格式的数据进行训练。
1+
## Faster-Rcnn:Two-Stage目标检测模型在Keras当中的实现
2+
---
3+
4+
### 目录
5+
1. [所需环境 Environment](#所需环境)
6+
2. [文件下载 Download](#文件下载)
7+
3. [训练步骤 How2train](#训练步骤)
8+
4. [参考资料 Reference](#Reference)
9+
10+
### 所需环境
11+
tensorflow-gpu==1.13.1
12+
keras==2.1.5
13+
14+
### 文件下载
15+
训练所需的voc_weights.h5可以去百度网盘下载
16+
链接: https://pan.baidu.com/s/1xDRhw0U4dWfy_2rceH-YnA 提取码: cm3q
17+
18+
### 训练步骤
19+
1、本文使用VOC格式进行训练。
20+
2、训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。
21+
3、训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。
22+
4、在训练前利用voc2faster-rcnn.py文件生成对应的txt。
23+
5、再运行根目录下的voc_annotation.py,运行前需要将classes改成你自己的classes。
24+
```python
25+
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
26+
```
27+
6、就会生成对应的2007_train.txt,每一行对应其图片位置及其真实框的位置。
28+
7、在训练前需要修改model_data里面的voc_classes.txt文件,需要将classes改成你自己的classes。
29+
8、运行train.py即可开始训练。
30+
31+
### mAP目标检测精度计算更新
32+
更新了get_gt_txt.py、get_dr_txt.py和get_map.py文件。
33+
get_map文件克隆自https://github.com/Cartucho/mAP
34+
具体mAP计算过程可参考:https://www.bilibili.com/video/BV1zE411u7Vw
35+
36+
### Reference
37+
https://github.com/qqwweee/keras-yolo3/
38+
https://github.com/pierluigiferrari/ssd_keras
39+
https://github.com/kuhung/SSD_keras
40+
https://github.com/jinfagang/keras_frcnn
41+
https://github.com/Cartucho/mAP
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
存放标签文件
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
存放训练索引文件
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
存放图片文件
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import random
3+
4+
xmlfilepath=r'./VOCdevkit/VOC2007/Annotations'
5+
saveBasePath=r"./VOCdevkit/VOC2007/ImageSets/Main/"
6+
7+
trainval_percent=1
8+
train_percent=1
9+
10+
temp_xml = os.listdir(xmlfilepath)
11+
total_xml = []
12+
for xml in temp_xml:
13+
if xml.endswith(".xml"):
14+
total_xml.append(xml)
15+
16+
num=len(total_xml)
17+
list=range(num)
18+
tv=int(num*trainval_percent)
19+
tr=int(tv*train_percent)
20+
trainval= random.sample(list,tv)
21+
train=random.sample(trainval,tr)
22+
23+
print("train and val size",tv)
24+
print("traub suze",tr)
25+
ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w')
26+
ftest = open(os.path.join(saveBasePath,'test.txt'), 'w')
27+
ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')
28+
fval = open(os.path.join(saveBasePath,'val.txt'), 'w')
29+
30+
for i in list:
31+
name=total_xml[i][:-4]+'\n'
32+
if i in trainval:
33+
ftrainval.write(name)
34+
if i in train:
35+
ftrain.write(name)
36+
else:
37+
fval.write(name)
38+
else:
39+
ftest.write(name)
40+
41+
ftrainval.close()
42+
ftrain.close()
43+
fval.close()
44+
ftest .close()

Vision_for_anchor.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from tensorflow import keras
4+
from utils.config import Config
5+
6+
config = Config()
7+
8+
def generate_anchors(sizes=None, ratios=None):
9+
if sizes is None:
10+
sizes = config.anchor_box_scales
11+
12+
if ratios is None:
13+
ratios = config.anchor_box_ratios
14+
15+
num_anchors = len(sizes) * len(ratios)
16+
17+
anchors = np.zeros((num_anchors, 4))
18+
# print(anchors)
19+
anchors[:, 2:] = np.tile(sizes, (2, len(ratios))).T
20+
21+
for i in range(len(ratios)):
22+
anchors[3*i:3*i+3, 2] = anchors[3*i:3*i+3, 2]*ratios[i][0]
23+
anchors[3*i:3*i+3, 3] = anchors[3*i:3*i+3, 3]*ratios[i][1]
24+
25+
26+
anchors[:, 0::2] -= np.tile(anchors[:, 2] * 0.5, (2, 1)).T
27+
anchors[:, 1::2] -= np.tile(anchors[:, 3] * 0.5, (2, 1)).T
28+
# print(anchors)
29+
return anchors
30+
31+
def shift(shape, anchors, stride=config.rpn_stride):
32+
# [0,1,2,3,4,5……37]
33+
# [0.5,1.5,2.5……37.5]
34+
# [8,24,……]
35+
shift_x = (np.arange(0, shape[0], dtype=keras.backend.floatx()) + 0.5) * stride
36+
shift_y = (np.arange(0, shape[1], dtype=keras.backend.floatx()) + 0.5) * stride
37+
38+
shift_x, shift_y = np.meshgrid(shift_x, shift_y)
39+
40+
shift_x = np.reshape(shift_x, [-1])
41+
shift_y = np.reshape(shift_y, [-1])
42+
# print(shift_x,shift_y)
43+
shifts = np.stack([
44+
shift_x,
45+
shift_y,
46+
shift_x,
47+
shift_y
48+
], axis=0)
49+
50+
shifts = np.transpose(shifts)
51+
number_of_anchors = np.shape(anchors)[0]
52+
53+
k = np.shape(shifts)[0]
54+
55+
shifted_anchors = np.reshape(anchors, [1, number_of_anchors, 4]) + np.array(np.reshape(shifts, [k, 1, 4]), keras.backend.floatx())
56+
shifted_anchors = np.reshape(shifted_anchors, [k * number_of_anchors, 4])
57+
58+
59+
fig = plt.figure()
60+
ax = fig.add_subplot(111)
61+
plt.ylim(-300,900)
62+
plt.xlim(-300,900)
63+
# plt.ylim(0,600)
64+
# plt.xlim(0,600)
65+
plt.scatter(shift_x,shift_y)
66+
box_widths = shifted_anchors[:,2]-shifted_anchors[:,0]
67+
box_heights = shifted_anchors[:,3]-shifted_anchors[:,1]
68+
69+
initial = 0
70+
for i in [initial+0,initial+1,initial+2,initial+3,initial+4,initial+5,initial+6,initial+7,initial+8]:
71+
rect = plt.Rectangle([shifted_anchors[i, 0],shifted_anchors[i, 1]],box_widths[i],box_heights[i],color="r",fill=False)
72+
ax.add_patch(rect)
73+
plt.show()
74+
75+
return shifted_anchors
76+
77+
def get_anchors(shape,width,height):
78+
anchors = generate_anchors()
79+
network_anchors = shift(shape,anchors)
80+
network_anchors[:,0] = network_anchors[:,0]/width
81+
network_anchors[:,1] = network_anchors[:,1]/height
82+
network_anchors[:,2] = network_anchors[:,2]/width
83+
network_anchors[:,3] = network_anchors[:,3]/height
84+
network_anchors = np.clip(network_anchors,0,1)
85+
print(network_anchors)
86+
return network_anchors
87+
88+
if __name__ == "__main__":
89+
get_anchors([38,38],600,600)

0 commit comments

Comments
 (0)