caffe Python API 之Inference
#以SSD的检测测试为例
def detetion(image_dir,weight,deploy,resolution=300):
caffe.set_mode_gpu()
net = caffe.Net(weight,deploy,caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data',(2,0,1))
transformer.set_mean('data', np.array([104, 117, 123])) # mean pixel images = os.listdir(image_dir)
target_dir = "det_results"
if not os.path.exists(target_dir):
os.mkdir(target_dir)
for image in images:
image_path = os.path.join(image_dir,image)
target_path = os.path.join(target_dir,image)
croped = cut(image_path,resolution)
net.blobs['data'].reshape(1, 3, resolution, resolution)
transformed_image = transformer.preprocess('data',croped)
net.blobs['data'].data[...]=transformed_image
start = time.time()
net.forward()
end = time.time()
print "Forward time is {} s.".format(int(end-start))
out_put = net.blobs["detection_out"].data out_put = np.squeeze(out_put)
# label,conf,xmin,ymin,xmax,ymax
for box in out_put:
conf = box[2]
# if conf < 0.1:
# continue
xmin = int(box[3]*resolution) if box[3] > 0 else 0
ymin = int(box[4]*resolution) if box[4] > 0 else 0
xmax = int(box[5]*resolution) if box[5] > 0 else 0
ymax = int(box[6]*resolution) if box[6] > 0 else 0
cv2.rectangle(croped,(xmin,ymin),(xmax,ymax),(0,255,0),1)
cv2.imwrite(target_path,croped)
print target_path
caffe Python API 之Inference的更多相关文章
- caffe Python API 之中值转换
# 编写一个函数,将二进制的均值转换为python的均值 def convert_mean(binMean,npyMean): blob = caffe.proto.caffe_pb2.BlobPro ...
- caffe Python API 之激活函数ReLU
import sys import os sys.path.append("/projects/caffe-ssd/python") import caffe net = caff ...
- caffe Python API 之 数据输入层(Data,ImageData,HDF5Data)
import sys sys.path.append('/projects/caffe-ssd/python') import caffe4 net = caffe.NetSpec() 一.Image ...
- caffe Python API 之BatchNormal
net.bn = caffe.layers.BatchNorm( net.conv1, batch_norm_param=dict( moving_average_fraction=0.90, #滑动 ...
- caffe Python API 之上卷积层(Deconvolution)
对于convolution: output = (input + 2 * p - k) / s + 1; 对于deconvolution: output = (input - 1) * s + k ...
- caffe Python API 之可视化
一.显示各层 # params显示:layer名,w,b for layer_name, param in net.params.items(): print layer_name + '\t' + ...
- caffe Python API 之图片预处理
# 设定图片的shape格式为网络data层格式 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) ...
- caffe Python API 之Model训练
# 训练设置 # 使用GPU caffe.set_device(gpu_id) # 若不设置,默认为0 caffe.set_mode_gpu() # 使用CPU caffe.set_mode_cpu( ...
- caffe Python API 之Solver定义
from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...
随机推荐
- 题解 P1308 【统计单词数】
小金羊发一篇不一样的题解: 这个题解不是讲解法的,是讲算法的... 众所周知,string在中被定义为是类型, 这意味着我们可以将它作为int一样的类型使用. 并且还有神奇的加减法: string s ...
- P1107 [BJWC2008]雷涛的小猫
题目描述 雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样的行为是违反学生宿舍管理条例的).在他的照顾下,小猫很快恢复了健康,并且愈发的活泼可爱了. 可是有一天,雷涛下课回 ...
- Expect the Expected UVA - 11427(概率dp)
题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...
- 【BZOJ1493】【NOI2007】项链工厂(线段树)
[BZOJ1493]项链工厂(线段树) 题面 BZOJ 洛谷 Description T公司是一家专门生产彩色珠子项链的公司,其生产的项链设计新颖.款式多样.价格适中,广受青年人的喜爱. 最近T公司打 ...
- CF1100E
i207M给的题 省选前-小题解合集 给定一张有向图,每条边有边权.你可以花费边权的代价反转一条边,使得原图中没有环.最小化反转的边权的最大值. 首先二分,然后考虑判定. 转化为有些边可以翻转,有些边 ...
- SVN Server 500 NotLicensed 错误的解决方法
SVN Server 500 NotLicensed 错误的HTML页面显示 Not licensed The server encountered an internal error or misc ...
- 图像处理之中值滤波介绍及C实现
1 中值滤波概述 中值滤波是基于排序统计理论的一种能有效抑制噪声的非线性信号平滑处理技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值. 中值滤波的基本原理是把数字图像或数字序 ...
- STL源码分析-deque
http://note.youdao.com/noteshare?id=66f21dca07c1984f41848700021644fd
- Jenkins CI Pipeline scripting
Jenkins pipeline is a suite of Jenkins plugins. Pipelines can be seen as a sequence of stages to per ...
- width: calc(100% - 20px);
width: calc(100% - 20px); css3 的 calc()函数.这里的意思是设置宽度比100%的宽度少20px. calc()函数用于动态计算长度值. calc()函数支持 &qu ...