In [6]:
import keras
import cv2
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
%matplotlib inline
In [7]:
data = pd.read_csv('voc.csv')
data = data.drop('Unnamed: 0', 1)
data['File_Path'] = './VOCdevkit2007/VOC2007/JPEGImages/' + data['Frame']
#data = data[(data['label'] == 0)].reset_index()
print(data.head())
In [10]:
def csv2bbox(bbox):
gta = np.zeros((len(bb_boxes), 4))
#print(gta.shape)
#bbox groundtruth before bbox_encode
for i in range(len(bb_boxes)):
gta[i, 0] = int(bb_boxes.iloc[i]['xmin'])
gta[i, 1] = int(bb_boxes.iloc[i]['ymin'])
gta[i, 2] = int(bb_boxes.iloc[i]['xmax'])
gta[i, 3] = int(bb_boxes.iloc[i]['ymax'])
return gta
In [15]:
#load img and bbox
import batch_generate
i_line = np.random.randint(len(data))
name_str, img, bb_boxes = batch_generate.get_img_by_name(data, i_line, size = (960, 640))
gta = csv2bbox(bb_boxes)
plt.figure(figsize=(10,10))
plt.imshow(img)
currentAxis = plt.gca()
for i in range(len(gta)):
currentAxis.add_patch(plt.Rectangle((gta[i,0], gta[i,1]), gta[i,2]-gta[i,0], gta[i,3]-gta[i,1], fill=False, edgecolor= 'r', linewidth=1))
In [18]:
#applied anchors here
(output_width, output_height) = (60, 40)
num_anchors = 9
anchor_box = batch_generate._generate_all_bbox(output_width, output_height)
total_anchors = anchor_box.shape[0]
#print('the shape of anchor_box', np.asarray(anchor_box).shape)
#print('the total number os anchors',total_anchors)
#Only inside anchors are valid
_allowed_border = 0
im_info = img.shape[:2]
inds_inside = np.where(
(anchor_box[:, 0] >= -_allowed_border) &
(anchor_box[:, 1] >= -_allowed_border) &
(anchor_box[:, 2] < im_info[1] + _allowed_border) & # width
(anchor_box[:, 3] < im_info[0] + _allowed_border) # height
)[0]
plt.figure(figsize=(10,10))
plt.imshow(img)
currentAxis = plt.gca()
box = anchor_box[inds_inside]
for i in range(len(box)):
currentAxis.add_patch(plt.Rectangle((box[i,0], box[i,1]), box[i,2]-box[i,0], box[i,3]-box[i,1], fill=False, edgecolor= 'r', linewidth=1))
In [33]:
import utils
y_rpn_overlap = np.zeros((output_height, output_width, num_anchors))
y_is_box_valid = np.zeros((output_height, output_width, num_anchors))
y_rpn_regr = np.zeros((output_height * output_width * num_anchors , 4))
y_rpn_regr[inds_inside] = anchor_box[inds_inside, :]
overlaps = utils.bbox_overlaps(np.ascontiguousarray(y_rpn_regr, dtype=np.float),np.ascontiguousarray(gta, dtype=np.float))
argmax_overlaps = overlaps.argmax(axis=1)
max_overlaps = np.zeros((output_height * output_width * num_anchors))
max_overlaps[inds_inside] = overlaps[np.arange(len(inds_inside)), argmax_overlaps[inds_inside]]
gt_argmax_overlaps = overlaps.argmax(axis=0)
gt_max_overlaps = overlaps[gt_argmax_overlaps,np.arange(overlaps.shape[1])]
gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]
print(gt_argmax_overlaps)
print(gt_max_overlaps)
In [21]:
plt.figure(figsize=(10,10))
plt.imshow(img)
currentAxis = plt.gca()
box = y_rpn_regr[gt_argmax_overlaps]
for i in range(len(box)):
currentAxis.add_patch(plt.Rectangle((box[i,0], box[i,1]), box[i,2]-box[i,0], box[i,3]-box[i,1], fill=False, edgecolor= 'r', linewidth=1))
In [39]:
y_rpn_overlap = y_rpn_overlap.reshape(output_height * output_width * num_anchors)
y_is_box_valid = y_is_box_valid.reshape(output_height * output_width * num_anchors)
#negative
#print('shape of y_rpn_overlap', y_rpn_overlap.shape)
#print('shape of y_is_box_valid',y_is_box_valid.shape)
y_rpn_overlap[max_overlaps < neg_min_overlaps] = 0
y_is_box_valid[inds_inside] = 1
#y_is_box_valid[max_overlaps < neg_min_overlaps] = 1#not good way to set all box as valid, because we also have outside box here
#neutral
#np.logical_and
y_rpn_overlap[np.logical_and(neg_min_overlaps < max_overlaps, max_overlaps < pos_max_overlaps)] = 0
y_is_box_valid[np.logical_and(neg_min_overlaps < max_overlaps, max_overlaps < pos_max_overlaps)] = 0
#positive
y_rpn_overlap[gt_argmax_overlaps] = 1
y_is_box_valid[gt_argmax_overlaps] = 1
y_rpn_overlap[max_overlaps >= pos_max_overlaps] = 1
y_is_box_valid[max_overlaps >= pos_max_overlaps] = 1
#y_rpn_overlap[neg_min_overlaps < max_overlaps and max_overlaps < pos_max_overlaps] = 0
#y_is_box_valid[neg_min_overlaps < max_overlaps and max_overlaps < pos_max_overlaps] = 0
# subsample positive labels if we have too many
num_fg = int(fraction * batchsize)
#print('balanced fg',num_fg)
disable_inds = []
fg_inds = np.where(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 1))[0]
#print('fg number',len(fg_inds))
if len(fg_inds) > num_fg:
disable_inds = npr.choice(fg_inds, size=(len(fg_inds) - num_fg), replace=False)
#labels[disable_inds] = -1
y_is_box_valid[disable_inds] = 0
y_rpn_overlap[disable_inds] = 0
# subsample negative labels if we have too many
num_bg = batchsize - np.sum(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 1))
bg_inds = np.where(np.logical_and(y_rpn_overlap == 0, y_is_box_valid == 1))[0]
#print('bg number',len(bg_inds))
if len(bg_inds) > num_bg:
disable_inds = npr.choice(bg_inds, size=(len(bg_inds) - num_bg), replace=False)
#labels[disable_inds] = -1
y_is_box_valid[disable_inds] = 0
y_rpn_overlap[disable_inds] = 0
#print ("was %s inds, disabling %s, now %s %sinds" % (len(bg_inds), len(disable_inds), np.sum(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 0))))
print('negative samples',np.where(np.logical_and(y_rpn_overlap == 0, y_is_box_valid == 1))[0])
print('postive samples',np.where(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 1))[0])
print('number of postive samples',len(np.where(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 1))[0]))
print('number of negative samples',len(np.where(np.logical_and(y_rpn_overlap == 0, y_is_box_valid == 1))[0]))
In [40]:
#postive samples
plt.figure(figsize=(10,10))
plt.imshow(img)
currentAxis = plt.gca()
postive = np.where(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 1))[0]
print(np.where(np.logical_and(y_rpn_overlap == 1, y_is_box_valid == 1))[0])
box = y_rpn_regr[postive,:]
for i in range(len(box)):
currentAxis.add_patch(plt.Rectangle((box[i,0], box[i,1]), box[i,2]-box[i,0], box[i,3]-box[i,1], fill=False, edgecolor= 'r', linewidth=1))
In [41]:
#negative samples
plt.figure(figsize=(10,10))
plt.imshow(img)
currentAxis = plt.gca()
negative = np.where(np.logical_and(y_rpn_overlap == 0, y_is_box_valid == 1))[0]
print(np.where(np.logical_and(y_rpn_overlap == 0, y_is_box_valid == 1))[0])
box = y_rpn_regr[negative,:]
for i in range(len(box)):
currentAxis.add_patch(plt.Rectangle((box[i,0], box[i,1]), box[i,2]-box[i,0], box[i,3]-box[i,1], fill=False, edgecolor= 'r', linewidth=1))
In [42]:
#bbox transfer for all valid postive samples
rpn_regr = np.zeros((output_height * output_width * num_anchors , 4))
y_rpn_regr[postive,:] = utils._compute_targets(y_rpn_regr[postive,:], gta[argmax_overlaps[postive], :])
print(len(y_rpn_regr[postive,:]))
In [49]:
import bbox_decode
anchor_box = batch_generate._generate_all_bbox(output_width, output_height)
proposals = bbox_decode.bbox_transform_inv_cpu(anchor_box[postive], y_rpn_regr[postive,:])
#proposals = bbox_decode.bbox_transform_inv_cpu(anchor_box, y_rpn_regr)
print(proposals.shape)
In [50]:
#bbox transfer
plt.figure(figsize=(10,10))
plt.imshow(img)
currentAxis = plt.gca()
box = proposals
for i in range(len(box)):
currentAxis.add_patch(plt.Rectangle((box[i,0], box[i,1]), box[i,2]-box[i,0], box[i,3]-box[i,1], fill=False, edgecolor= 'r', linewidth=1))
In [ ]: