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 ...
随机推荐
- WebGL画一个10px大小的点
WebGL程序在屏幕上同时使用HTML和javascript来创建和显示三维图形.WebGL中新引入的<canvas>元素标签,它定义了网页上的绘图区域. 由于<canvas> ...
- 【数据库_Mysql】JAVA-数据库Date格式在前台JSP页面的获取
问题: 数据库保存的为date格式的日期 在前台JSP页面显示的为一串数字1487897 解决办法: 数据库表中字段对应的实体对象属性的get方法上添加一行代码 页面即可正常显示
- BZOJ5072 小A的树(树形dp)
容易猜到能选择的黑点个数是一个连续区间.那么设f[i][j]为i子树内选j个点形成包含根的连通块,最多有几个黑点,g[i][j]为最少有几个黑点,暴力dp是O(n2)的,求出每个连通块大小对应的黑点数 ...
- 信息收集利器——Nmap
环境:kali2.0 常用的Nmap命令总结: 1.扫描单个IP地址 nmap 192.168.56.1 2.扫描一个网络中IP地址范围 nmap 192.168.56.1-255 3.扫描目标主机的 ...
- SAS数据步与过程步,数据步语句
SAS数据步与过程步,数据步语句http://www.biostatistic.net/thread-2045-1-1.html ---转载---原文作者:biostar(出处: 生物统计家园) 数 ...
- Nginx多个配置文件共用location配置
一.应用情况 很多时候我们在一台服务器上部署了不止 一个项目,我们通过Nginx来代理,为了方便管理往往会将各个项目的配置分开写到不同的配置文件中,如: 在nginx.conf 文件中加上 incl ...
- [机器学习]-Adaboost提升算法从原理到实践
1.基本思想: 综合某些专家的判断,往往要比一个专家单独的判断要好.在”强可学习”和”弱可学习”的概念上来说就是我们通过对多个弱可学习的算法进行”组合提升或者说是强化”得到一个性能赶超强可学习算法的算 ...
- Java修饰符 public、protected、default、private
2.访问修饰符public,private,protected,以及不写(默认)时的区别?答: 修饰符 当前类 同 包 子 类 其他包 public √ √ √ √ protected √ √ √ × ...
- JS事件大全及兼容
一般事件 事件 浏览器支持 描述 onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseDown ...
- 预处理 Gym - 101128H
题目链接:http://codeforces.com/gym/101128 题目大意:给你一个区间[x,y],找出这个区间有多少个seldom的数字. seldom的数字定义如下:该数值的二进制数字符 ...