#encoding: utf-8
"""
对于il2012来说,
分类问题一共有1000各类别
检测问题有200个类别
"""
""" 0 产生数据框架包 ; """
"""
这里执行了python脚本:
利用 权重文件,部署文件,标签文件,待检测的目标图像 , 作为输入,产生了一个以检测为目的的数据框架包。
"""
"""
1、 Rcnn简介
原文连接:http://people.eecs.berkeley.edu/~rbg/papers/r-cnn-cvpr.pdf
首先使用selective search算法,从图片中提取出2000个可能包含有目标的区域,
再将这2000个候选区(ROI:region of interest)压缩到统一大小(227*227)送入卷积神经网络中进行特征提取,
在最后一层将特征向量输入svm分类器,得到该候选区域的种类。整体上看R-cnn比较简单,与此同时也有两个 重大缺陷:
(1)selective search进行候选区域提取的过程在cpu内计算完成,占用了大量计算时间。
(2)对2000个候选框进行卷积计算,提取特征的时候,存在大量的重复计算,进一步增加了计算复杂度。
针对以上两个缺点,R Girshick分别在fast-Rcnn和faster-rcnn中进行了改进。
"""
# !mkdir -p _temp
# !echo `pwd`/images/fish-bike.jpg > _temp/det_input.txt
# !../python/detect.py --crop_mode=selective_search\
# --pretrained_model=../models/bvlc_reference_rcnn_ilsvrc13/bvlc_reference_rcnn_ilsvrc13.caffemodel\
# --model_def=../models/bvlc_reference_rcnn_ilsvrc13/deploy.prototxt\
# --gpu --raw_scale=255 _temp/det_input_person.txt _temp/det_output_person.h5
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline
nameImg = "/home/sea/caffeM/caffe/examples/images/fish-bike.jpg"
# nameImg ="/home/sea/Downloads/1.jpeg"
# nameImg ="/home/sea/Downloads/2.jpeg"
# nameImg ="/home/sea/Downloads/3.jpeg"
nameImg ="/home/sea/Downloads/4.jpeg"
nameImg ="/home/sea/Downloads/person.jpeg"
"""1 准备模型"""
print """1 准备模型"""
df = pd.read_hdf('/home/sea/caffeM/caffe/examples/_temp/det_output_person.h5', 'df')
print "(df.shape) = ", (df.shape) # (df.shape) = (1565, 5)
print "type(df.iloc[0]) = ", type(df.iloc[0]) # type(df.iloc[0]) = <class 'pandas.core.series.Series'>
print "df.iloc[0].shape = ", df.iloc[0].shape #df.iloc[0].shape = (5,)
print "(df.iloc[0]) = ", (df.iloc[0]) #
print 'aaaaa'
with open( '/home/sea/caffeM/caffe/data/ilsvrc12/det_synset_words.txt' ) as f:
labels_df = pd.DataFrame([
{
'synset_id': l.strip().split(' ')[0],
'name': ' '.join(l.strip().split(' ')[1:]).split(',')[0]
}
for l in f.readlines()
])
print 'aaaaa'
print "type(labels_df) = ", type(labels_df) # type(labels_df) = <class 'pandas.core.frame.DataFrame'>
print "labels_df.shape = ", labels_df.shape # labels_df.shape = (200, 2)
# print "labels_df = ", labels_df
# print "labels_df sort_index= ", labels_df.sort_index( )
# labels_df.sort_values(by, axis, ascending, inplace, kind, na_position)
# labels_df.sort('synset_id')
predictions_df = pd.DataFrame(np.vstack(df.prediction.values), columns=labels_df['name'])
""" 这个是所有的大类"""
# print "(predictions_df.iloc[0]) = ", (predictions_df.iloc[0])
aa = predictions_df.iloc[0]
print "type( aa ) = ", type( aa ) #
if 0:
dt = aa.to_dict()
dt = sorted( dt.items(), lambda x, y: cmp( abs(x[1] ), abs( y[1] ) ), reverse=False )
print 'dt = ', dt #
plt.gray()
plt.matshow(predictions_df.values)
plt.xlabel('Classes')
plt.ylabel('Windows')
# plt.show(3)
print "type( predictions_df ) = ", type( predictions_df ) #
print "这个是总表来着 : "
pprr = predictions_df.sort_index( )
print "pprr.shape = ", pprr.shape ## 1565* 200 这个是其规格
# print " predictions_df.sort_index( ) = ", pprr
max_s = predictions_df.max(0)
print "type( max_s ) = ", type( max_s )
# print "max_s = ", max_s
# max_s.sort(ascending=False)
# print(max_s[:10])
# Find, print, and display the top detections: person and bicycle.
i = predictions_df['person'].idxmax()
j = predictions_df['bicycle'].idxmax()
# Show top predictions for top detection.
print "f : "
f = pd.Series(df['prediction'].iloc[i], index=labels_df['name'])
"""f是200个 类别"""
f.sort_values( )
print "f.shape = ", f.shape
# print 'f.sort_values( ) = ', f.sort_values( )
print('Top detection:')
print f.sort_values(ascending=False)[:5]
# Show top predictions for second-best detection.
f = pd.Series(df['prediction'].iloc[j], index=labels_df['name'])
print ('Second-best detection:')
print f.sort_values(ascending=False)[:5]
"""2 读图开始"""
print """2 读图开始"""
# Show top detection in red, second-best top detection in blue.
im = plt.imread( nameImg )
plt.imshow(im)
# plt.show(3)
currentAxis = plt.gca()
det = df.iloc[i]
coords = (det['xmin'], det['ymin']), det['xmax'] - det['xmin'], det['ymax'] - det['ymin']
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor='r', linewidth=5))
det = df.iloc[j]
coords = (det['xmin'], det['ymin']), det['xmax'] - det['xmin'], det['ymax'] - det['ymin']
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor='b', linewidth=5))
# exit()
def nms_detections(dets, overlap=0.3):
"""
Non-maximum suppression: Greedily select high-scoring detections and
skip detections that are significantly covered by a previously
selected detection.
This version is translated from Matlab code by Tomasz Malisiewicz,
who sped up Pedro Felzenszwalb's code.
Parameters
----------
dets: ndarray
each row is ['xmin', 'ymin', 'xmax', 'ymax', 'score']
overlap: float
minimum overlap ratio (0.3 default)
Output
------
dets: ndarray
remaining after suppression.
"""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
ind = np.argsort(dets[:, 4])
w = x2 - x1
h = y2 - y1
area = (w * h).astype(float)
pick = []
while len(ind) > 0:
i = ind[-1]
pick.append(i)
ind = ind[:-1]
xx1 = np.maximum(x1[i], x1[ind])
yy1 = np.maximum(y1[i], y1[ind])
xx2 = np.minimum(x2[i], x2[ind])
yy2 = np.minimum(y2[i], y2[ind])
w = np.maximum(0., xx2 - xx1)
h = np.maximum(0., yy2 - yy1)
wh = w * h
o = wh / (area[i] + area[ind] - wh)
ind = ind[np.nonzero(o <= overlap)[0]]
return dets[pick, :]
"""3 检测"""
print """3 检测"""
scores = predictions_df['bicycle'] ## 这个有1565 各数据的长度
print "scores : "
# print "scores = predictions_df['bicycle'] : ", scores
windows = df[['xmin', 'ymin', 'xmax', 'ymax']].values
dets = np.hstack((windows, scores[:, np.newaxis]))
nms_dets = nms_detections(dets)
"""4 显示"""
print """4 显示"""
plt.imshow(im)
plt.show(3)
currentAxis = plt.gca()
colors = ['r', 'b', 'y']
for c, det in zip(colors, nms_dets[:3]):
currentAxis.add_patch(
plt.Rectangle((det[0], det[1]), det[2]-det[0], det[3]-det[1],
fill=False, edgecolor=c, linewidth=5)
)
print 'scores:', nms_dets[:3, 4]
# !rm -rf _temp
caffe--detection--python--select-search
最新推荐文章于 2021-03-15 17:24:15 发布