Skip to content

Commit b62b523

Browse files
committed
Revise remove indexes for label
1 parent 26e6d44 commit b62b523

File tree

4 files changed

+63
-32
lines changed

4 files changed

+63
-32
lines changed

cython_util/bbox_overlap.pyx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ctypedef np.int_t DTYPE_int_t
1313
def bbox_overlaps(
1414
np.ndarray[DTYPE_t, ndim=4] anchors,
1515
np.ndarray[DTYPE_int_t, ndim=3] is_inside,
16-
np.ndarray[DTYPE_t, ndim=3] gt_boxes):
16+
object gt_boxes):
1717
"""
1818
Parameters
1919
----------
@@ -27,7 +27,7 @@ def bbox_overlaps(
2727
cdef unsigned int K = anchors.shape[1]
2828
cdef unsigned int A = anchors.shape[2]
2929
cdef unsigned int G
30-
cdef np.ndarray[DTYPE_t, ndim=4] overlaps = np.zeros((Batch_Size, K, A, 10), dtype=DTYPE)
30+
cdef np.ndarray[DTYPE_t, ndim=4] overlaps = np.zeros((Batch_Size, K, A, 15), dtype=DTYPE)
3131
cdef np.ndarray[DTYPE_int_t, ndim=3] true_index = np.zeros((Batch_Size, K, A), dtype=DTYPE_int)
3232
cdef np.ndarray[DTYPE_int_t, ndim=3] false_index = np.zeros((Batch_Size, K, A), dtype=DTYPE_int)
3333
cdef DTYPE_t iw, ih, box_area
@@ -40,8 +40,8 @@ def bbox_overlaps(
4040
G = gt_boxes[b].shape[0]
4141
for g in range(G):
4242
box_area = (
43-
(gt_boxes[b, g, 2] - gt_boxes[b, g, 0] + 1) *
44-
(gt_boxes[b, g, 3] - gt_boxes[b, g, 1] + 1)
43+
(gt_boxes[b][g, 2] - gt_boxes[b][g, 0] + 1) *
44+
(gt_boxes[b][g, 3] - gt_boxes[b][g, 1] + 1)
4545
)
4646
max_overlap = 0
4747
max_k = 0
@@ -50,13 +50,13 @@ def bbox_overlaps(
5050
for a in range(A):
5151
if is_inside[b, k, a] == 1:
5252
iw = (
53-
min(anchors[b, k, a, 2], gt_boxes[b, g, 2]) -
54-
max(anchors[b, k, a, 0], gt_boxes[b, g, 0]) + 1
53+
min(anchors[b, k, a, 2], gt_boxes[b][g, 2]) -
54+
max(anchors[b, k, a, 0], gt_boxes[b][g, 0]) + 1
5555
)
5656
if iw > 0:
5757
ih = (
58-
min(anchors[b, k, a, 3], gt_boxes[b, g, 3]) -
59-
max(anchors[b, k, a, 1], gt_boxes[b, g, 1]) + 1
58+
min(anchors[b, k, a, 3], gt_boxes[b][g, 3]) -
59+
max(anchors[b, k, a, 1], gt_boxes[b][g, 1]) + 1
6060
)
6161
if ih > 0:
6262
ua = float(
@@ -65,7 +65,7 @@ def bbox_overlaps(
6565
box_area - iw * ih
6666
)
6767
overlaps[b, k, a, g] = iw * ih / ua
68-
if max_overlap < ((iw * ih / ua):
68+
if max_overlap < ((iw * ih / ua)):
6969
max_overlap = iw * ih / ua
7070
max_k = k
7171
max_a = a
@@ -79,7 +79,7 @@ def bbox_overlaps(
7979
max_g = 0
8080
for g in range(G):
8181
if overlaps[b, k, a, g] > 0:
82-
if max_overlap < (overlaps[b, k, a, g):
82+
if max_overlap < (overlaps[b, k, a, g]):
8383
max_overlap = overlaps[b, k, a, g]
8484
max_g = g
8585
if max_overlap > 0.7:
@@ -93,10 +93,10 @@ def bbox_overlaps(
9393
ex_height = anchors[b, k, a, 3] - anchors[b, k, a, 1] + 1
9494
ex_center_x = anchors[b, k, a, 0] + ex_width / 2.0
9595
ex_center_y = anchors[b, k, a, 1] + ex_height / 2.0
96-
gt_width = gt_boxes[b, max_g, 2] - gt_boxes[b, max_g, 0] + 1
97-
gt_height = gt_boxes[b, max_g, 3] - gt_boxes[b, max_g, 1] + 1
98-
gt_center_x = gt_boxes[b, max_g, 0] + gt_width / 2.0
99-
gt_center_y = gt_boxes[b, max_g, 1] + gt_height / 2.0
96+
gt_width = gt_boxes[b][max_g, 2] - gt_boxes[b][max_g, 0] + 1
97+
gt_height = gt_boxes[b][max_g, 3] - gt_boxes[b][max_g, 1] + 1
98+
gt_center_x = gt_boxes[b][max_g, 0] + gt_width / 2.0
99+
gt_center_y = gt_boxes[b][max_g, 1] + gt_height / 2.0
100100

101101
anchors[b, k, a, 0] = (gt_center_x - ex_center_x) / (ex_width)
102102
anchors[b, k, a, 1] = (gt_center_y - ex_center_y) / (ex_height)

rpn/rpn.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# -*- coding: utf-8 -*-
22

33
import sys
4+
sys.path.append("/home/katou01/code/Faster_RCNN_tensorflow")
5+
sys.path.append("/home/katou01/code/Faster_RCNN_tensorflow/util")
6+
sys.path.append("/home/katou01/code/Faster_RCNN_tensorflow/cython_util")
47
import glob
58
import cv2
69
import numpy as np
710
# from vgg16 import vgg16
811
from input_kitti import *
9-
from util import *
12+
from data_util import *
1013
from parse_xml import parseXML
1114
from base_vgg16 import Vgg16
1215
import tensorflow as tf
@@ -16,7 +19,7 @@
1619
from bool_anchors_inside_image import batch_inside_image
1720
from generate_anchors import generate_anchors
1821
# from utility.image.data_augmentation.flip import Flip
19-
sys.path.append("/Users/tsujiyuuki/env_python/code/my_code/Data_Augmentation")
22+
# sys.path.append("/Users/tsujiyuuki/env_python/code/my_code/Data_Augmentation")
2023

2124
"""
2225
・collect dataset of cars
@@ -164,9 +167,9 @@ def rpn_loss(rpn_cls, rpn_bbox, g_bbox_regression, true_index, false_index):
164167
return total_loss, cls_loss, bbox_loss
165168

166169

167-
def create_Labels_For_Loss(gt_boxes, feat_stride=16, feature_shape=(64, 64), \
170+
def create_Labels_For_Loss(gt_boxes, feat_stride=16, feature_shape=(64, 19), \
168171
scales=np.array([8, 16, 32]), ratios=[0.5, 0.8, 1], \
169-
image_size=(500, 1000)):
172+
image_size=(300, 1000)):
170173
"""This Function is processed before network input
171174
Number of Candicate Anchors is Feature Map width * heights
172175
Number of Predicted Anchors is Batch Num * Feature Map Width * Heights * 9
@@ -180,8 +183,8 @@ def create_Labels_For_Loss(gt_boxes, feat_stride=16, feature_shape=(64, 64), \
180183
candicate_anchors, true_index, false_index = create_Labels_For_Loss(gt_boxes)
181184
candicate_anchors[true_index==1][97]
182185
"""
183-
# import time
184-
# func_start = time.time()
186+
import time
187+
func_start = time.time()
185188
width = feature_shape[0]
186189
height = feature_shape[1]
187190
batch_size = gt_boxes.shape[0]
@@ -201,25 +204,35 @@ def create_Labels_For_Loss(gt_boxes, feat_stride=16, feature_shape=(64, 64), \
201204
candicate_anchors = centers.reshape(batch_size, K, 1, 4) + anchors # [Batch, K, A, 4]
202205

203206
# shape is [B, K, A]
204-
is_inside = batch_inside_image(candicate_anchors, image_size[0], image_size[1])
207+
is_inside = batch_inside_image(candicate_anchors, image_size[1], image_size[0])
205208

206209
# candicate_anchors: Shape is [Batch, K, A, 4]
207210
# gt_boxes: Shape is [Batch, G, 4]
208211
# true_index: Shape is [Batch, K, A]
209212
# false_index: Shape is [Batch, K, A]
213+
# print gt_boxes.shape
214+
# print gt_boxes
215+
# print np.array(gt_boxes,dtype=np.float64).dtype
216+
# print gt_boxes.dtype
217+
print gt_boxes[0]
218+
# box_area = (
219+
# (gt_boxes[b, g, 2] - gt_boxes[b, g, 0] + 1) *
220+
# (gt_boxes[b, g, 3] - gt_boxes[b, g, 1] + 1)
221+
# )
210222
candicate_anchors, true_index, false_index = bbox_overlaps(
211223
np.ascontiguousarray(candicate_anchors, dtype=np.float),
212224
is_inside,
213-
np.ascontiguousarray(gt_boxes, dtype=np.float))
225+
gt_boxes)
214226

215227
for i in range(batch_size):
216228
true_where = np.where(true_index[i] == 1)
217229
num_true = len(true_where[0])
218230
if num_true > 64:
219231
select = np.random.choice(num_true, num_true - 64, replace=False)
220232
num_true = 64
221-
true_where = remove_extraboxes(true_where[0], true_where[1], select)
222-
true_index[i, true_where] = 0
233+
batch = np.ones((select.shape[0]), dtype=np.int) * i
234+
true_where = remove_extraboxes(true_where[0], true_where[1], select, batch)
235+
true_index[true_where] = 0
223236

224237
false_where = np.where(false_index[i] == 1)
225238
num_false = len(false_where[0])
@@ -228,7 +241,7 @@ def create_Labels_For_Loss(gt_boxes, feat_stride=16, feature_shape=(64, 64), \
228241
false_where = remove_extraboxes(false_where[0], false_where[1], select, batch)
229242
false_index[false_where] = 0
230243

231-
# print "time", time.time() - func_start
244+
print "time", time.time() - func_start
232245
return candicate_anchors, true_index, false_index
233246

234247
def create_ROIs_For_RCNN(model, g_labels, feat_stride=16):
@@ -291,11 +304,24 @@ def train_rpn(batch_size, image_dir, label_dir, epoch=101, label_type="txt", lr=
291304

292305
image_dir = "/home/katou01/download/training/image_2/*.png"
293306
label_dir = "/home/katou01/download/training/label_2/*.txt"
307+
import time
308+
start = time.time()
294309
images, labels = get_ALL_Image(image_dir, label_dir)
295-
create_Labels_For_Loss(gt_boxes, feat_stride=16, feature_shape=(64, 64), \
296-
scales=np.array([8, 16, 32]), ratios=[0.5, 0.8, 1], \
297-
image_size=(500, 1000)):
298-
print images.shape, labels.shape
310+
print "labels"
311+
print labels.shape
312+
print "images"
313+
print images.shape
314+
candicate_anchors, true_index, false_index = create_Labels_For_Loss(labels, feat_stride=16, feature_shape=(64, 19), \
315+
scales=np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32]), ratios=[0.1, 0.2, 0.3, 0.5, 0.8, 1, 1.2], \
316+
image_size=(302, 1000))
317+
print time.time() - start
318+
print images[0].shape, labels.shape
319+
320+
print true_index[true_index==1].shape
321+
print false_index[false_index==1].shape
322+
323+
print true_index[0, true_index[0]==1].shape
324+
print false_index[false_index==1].shape
299325
#
300326
# image = im.open("./test_images/test1.jpg")
301327
# image = np.array(image, dtype=np.float32)

util/util.py renamed to util/data_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import cv2
66
import numpy as np
77
# from vgg16 import vgg16
8-
from input_kitti import *
8+
# from input_kitti import *
99
from parse_xml import parseXML
1010
from bbox_transform import *
1111
from base_vgg16 import Vgg16

util/input_kitti.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import glob
99
import math
1010
from parse_xml import parseXML
11-
from util import *
11+
from data_util import *
1212
import matplotlib.pyplot as plt
1313

1414
def read_label_from_txt(label_path):
@@ -86,6 +86,8 @@ def get_ALL_Image(image_dir, label_dir):
8686
images (list): ndarray Images of datasets.
8787
pred_bboxes(ndarray): rescaled bbox Label. Shapeis [Batch, ?, 4](x, y, w, h)
8888
"""
89+
import time
90+
start = time.time()
8991
# 車が含まれている画像のみラベルと一緒に読み込む
9092
image_pathlist = 0 #load_for_detection(label_dir)
9193
dataset_img_list = [] # len(dataset_img_list) == Number of Datasets Images
@@ -98,7 +100,9 @@ def get_ALL_Image(image_dir, label_dir):
98100
label_pathlist.sort()
99101

100102
for index, (image_path, label_path) in enumerate(zip(image_pathlist, label_pathlist)):
101-
if index == 100:
103+
if index < 64:
104+
continue
105+
if index == 128:
102106
break
103107
img = cv2.imread(image_path)
104108
label = read_label_from_txt(label_path)
@@ -108,4 +112,5 @@ def get_ALL_Image(image_dir, label_dir):
108112
dataset_img_list.append(img)
109113
g_bboxes.append(label)
110114

115+
print time.time() - start
111116
return np.array(dataset_img_list), np.array(g_bboxes)

0 commit comments

Comments
 (0)