Caffe官网->Notebook Examples->R-CNN detection : 地址
由于使用的是docker环境,并且创建前没有加--privilege参数,所以安装不了Matlab,因此无法使用selective_search_ijcv_with_python模块来进行selective search.
还好demo里也支持使用第三方的selective search工具。(只需要使用csv类型作为input)
整个工程用的python 3.6的环境。
- 先在github上找到了一个selective search的python版本,而且它已经内置在pip的包里了。可以直接使用pip安装。
pip install selectivesearch
- 先用这个ss库对图片进行处理,提取region proposals. 具体代码如下:
from skimage import io
import selectivesearch
import pandas as pd
# 图片路径,(demo中只测试了一张图片)
image_path = '/root/project/caffe/examples/images/fish-bike.jpg'
output_path = '_temp/csvtest.csv'
img = io.imread(image_path)
img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=1000)
# 具体参数细节可查看github
candidates = set()
for r in regions:
# excluding same rectangle (with different segments)
if r['rect'] in candidates:
continue
# excluding regions smaller than 2000 pixels
if r['size'] < 2000:
continue
# distorted rects
x, y, w, h = r['rect']
if w / h > 10 or h / w > 10:
continue
candidates.add(r['rect'])
windows = []
for c in candidates:
x, y, w, h = c
windows.append([x,y, x+w,y+h])
print('[windows after ss : ', len(windows), ' ]')
df = pd.DataFrame(windows,columns=['xmin', 'ymin', 'xmax', 'ymax'])
df.insert(0,'filename',[image_path for i in range(len(windows))])
df.to_csv(output_path,index=False)
-
Caffe demo的/root/project/caffe/python/detect.py文件关于csv处理的部分有误,需要修改:
/root/project/caffe/python/detect.py - detection.py的执行代码修改如下:
!/root/project/caffe/python/detect.py --crop_mode=list --pretrained_model=/root/project/caffe/models/bvlc_reference_rcnn_ilsvrc13/bvlc_reference_rcnn_ilsvrc13.caffemodel --model_def=/root/project/caffe/models/bvlc_reference_rcnn_ilsvrc13/deploy.prototxt --gpu --raw_scale=255 _temp/csvtest.csv _temp/csvtestoutput.csv
- 预测后的分析和可视化需要修改的部分,其他的不动。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('_temp/csvtestoutput.csv')
print(df.shape)
# print(df.iloc[0])
with open('/root/project/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()
])
labels_df.sort_values('synset_id')
NUM_OUTPUT = 200
class_cols = ['class{}'.format(x) for x in range(NUM_OUTPUT)]
predictions_df = pd.DataFrame(np.vstack(df[class_cols].values), columns=labels_df['name'])
print(predictions_df.iloc[0])
max_s = predictions_df.max(0)
max_s.sort_values(ascending=False, inplace=True)
print(max_s[:10])
# image_path = '/root/cvdata/myData/mylab.jpg'
image_path = '/root/project/caffe/examples/images/fish-bike.jpg'
# Find, print, and display the top detections: person and bicycle.
i = predictions_df['person'].argmax()
j = predictions_df['bicycle'].argmax()
# Show top predictions for top detection.
f = pd.Series(df.iloc[i][class_cols].values, index=labels_df['name'])
print('Top detection:')
print(f.sort_values(ascending=False)[:5])
print('')
# Show top predictions for second-best detection.
f = pd.Series(df.iloc[j][class_cols].values, index=labels_df['name'])
print('Second-best detection:')
print(f.sort_values(ascending=False)[:5])
# Show top detection in red, second-best top detection in blue.
im = plt.imread(image_path)
plt.imshow(im)
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))
网友评论