#以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的更多相关文章

  1. caffe Python API 之中值转换

    # 编写一个函数,将二进制的均值转换为python的均值 def convert_mean(binMean,npyMean): blob = caffe.proto.caffe_pb2.BlobPro ...

  2. caffe Python API 之激活函数ReLU

    import sys import os sys.path.append("/projects/caffe-ssd/python") import caffe net = caff ...

  3. caffe Python API 之 数据输入层(Data,ImageData,HDF5Data)

    import sys sys.path.append('/projects/caffe-ssd/python') import caffe4 net = caffe.NetSpec() 一.Image ...

  4. caffe Python API 之BatchNormal

    net.bn = caffe.layers.BatchNorm( net.conv1, batch_norm_param=dict( moving_average_fraction=0.90, #滑动 ...

  5. caffe Python API 之上卷积层(Deconvolution)

    对于convolution: output = (input + 2 * p  - k)  / s + 1; 对于deconvolution: output = (input - 1) * s + k ...

  6. caffe Python API 之可视化

    一.显示各层 # params显示:layer名,w,b for layer_name, param in net.params.items(): print layer_name + '\t' + ...

  7. caffe Python API 之图片预处理

    # 设定图片的shape格式为网络data层格式 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) ...

  8. caffe Python API 之Model训练

    # 训练设置 # 使用GPU caffe.set_device(gpu_id) # 若不设置,默认为0 caffe.set_mode_gpu() # 使用CPU caffe.set_mode_cpu( ...

  9. caffe Python API 之Solver定义

    from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...

随机推荐

  1. python 内置函数02

    1. lambda 匿名函数 lambda 参数: 返回值 #常规计算两个数相加的函数 def func(a,b): return a+b print(func(1,9)) #lambda函数 my_ ...

  2. 编写高效SQL语句(转)

    转至http://blog.csdn.net/u012150457/article/details/41846299 一.编写高效SQL语句 1) 选择最有效的表名顺序(仅适用于RBO模式) ORAC ...

  3. 为什么我再也不想和 Google HR 交谈了

    英文:yegor256,编译:伯乐在线/心灵是一棵开花的树 http://blog.jobbole.com/110340/ [伯乐在线导读]: 关于程序员面试时现场写代码,估计大家还记得 2015 年 ...

  4. bzoj5118: Fib数列2(费马小定理+矩阵快速幂)

    题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...

  5. 【bzoj3687】简单题

    #3687. 简单题 内存限制:512 MiB时间限制:10 Sec 提交提交记录讨论 题目描述 小呆开始研究集合论了,他提出了关于一个数集四个问题:1.子集的异或和的算术和.2.子集的异或和的异或和 ...

  6. 【agc006C】Rabbit Exercise

    Portal --> agc006C Solution 啊感觉是好有意思的一道题qwq官方题解里面的说辞也是够皮的哈哈哈..(大概就是说如果你没有意识到那个trick的话这题这辈子都做不出来qw ...

  7. 【bzoj1002】轮状病毒

    Portal-->bzoj1002 Solution 虽然说看上去是一道矩阵树定理的题但是 但是! 没有模数了解一下,\(n=100\)了解一下 开心愉快敲了一个高消之后发现跑到\(80\)都已 ...

  8. poj 3254 状态压缩

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15285   Accepted: 8033 Desc ...

  9. git 列出两个 commit 之间变更的文件列表

    git diff <commit1> <commit2> --stat 如: git diff 74ecf17dc 1ee25ed3c --stat src/assets 上面 ...

  10. centos pure-ftpd配置及错误解决

    使用yum安装pure-ftpd Pure-FTPd是Linux上的一个开源的FTP服务程序,在易用性.配置性上比vsftp较方便,下面我们使用centos6演示安装和配置pure-ftpd. 安装e ...