In [1]:
import numpy as np
import tensorflow as tf
import pandas as pd
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
data = pd.read_csv('FDDB2XYWH.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())


                        Frame  x_center  y_center      w      h    s  \
0  2002/08/18/big/img_181.jpg     462.0     357.0  258.0  149.0  1.0   
1  2002/08/18/big/img_181.jpg     674.0      73.0   98.0   54.0  1.0   
2  2002/08/18/big/img_181.jpg     849.0     127.0  111.0   61.0  1.0   
3  2002/08/10/big/img_520.jpg     530.0     274.0  828.0  458.0  1.0   
4  2002/08/25/big/img_705.jpg     459.0     337.0  918.0  489.0  1.0   

                                    FileName  
0  ./originalPics/2002/08/18/big/img_181.jpg  
1  ./originalPics/2002/08/18/big/img_181.jpg  
2  ./originalPics/2002/08/18/big/img_181.jpg  
3  ./originalPics/2002/08/10/big/img_520.jpg  
4  ./originalPics/2002/08/25/big/img_705.jpg  

In [3]:
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),dataset = 'FDDB')
copy_img = img
print(bb_boxes)
gta = batch_generate.bbox_transform(bb_boxes)
print(gta)
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))


   index                       Frame  x_center  y_center      w      h    s  \
0   1591  2003/01/17/big/img_794.jpg     358.0     196.0  192.0  198.0  1.0   
1   1592  2003/01/17/big/img_794.jpg     733.0     136.0  201.0  210.0  1.0   

                                    FileName  
0  ./originalPics/2003/01/17/big/img_794.jpg  
1  ./originalPics/2003/01/17/big/img_794.jpg  
[[ 262.    97.   454.   295. ]
 [ 632.5   31.   833.5  241. ]]

In [4]:
#Inference
import config
from netarch import *
img_channel_mean = [103.939, 116.779, 123.68]
with tf.Graph().as_default():
    mc = config.model_parameters()
    mc.LOAD_PRETRAINED_MODEL = False
    model = ResNet50(mc, '0')
    saver = tf.train.Saver(model.model_params)
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        saver.restore(sess, './tf_detection/model.ckpt-2000')
        img = img.astype(np.float32)
        img[:, :, 0] -= img_channel_mean[0]
        img[:, :, 1] -= img_channel_mean[1]
        img[:, :, 2] -= img_channel_mean[2]
        #img_per_batch = np.expand_dims(img, axis = 0)
        det_probs, det_boxes = sess.run([model.det_probs, model.det_boxes],feed_dict={model.image_input:[img], model.keep_prob: 1.0})


Input tensor shape to rpn: (1, 40, 60, 1024)
Input tensor shape to rpn_cls_score: (1, 40, 60, 512)
Input tensor shape to rpn_bbox_pred: (1, 40, 60, 512)
/home/walter/.local/lib/python3.5/site-packages/tensorflow/python/ops/gradients_impl.py:91: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "

In [5]:
print(det_probs.shape, det_boxes.shape)
box_probs = np.reshape(det_probs[0],[-1,2])[:,1]
box_delta = np.reshape(det_boxes[0],[21600,4])
print(box_probs.shape, box_delta.shape)


(1, 40, 60, 18) (1, 40, 60, 36)
(21600,) (21600, 4)

In [8]:
import utils
anchor_box = mc.ANCHOR_BOX
pred_box_xyxy = utils.bbox_delta_convert_inv(anchor_box, box_delta)
box_nms, probs_nms = utils.non_max_suppression_fast(pred_box_xyxy, box_probs, 5, overlap_thresh=0.5)

In [9]:
box = box_nms
plt.figure(figsize=(10,10))
plt.imshow(copy_img)
currentAxis = plt.gca()
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 [ ]: