#以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. BZOJ4887 Tjoi2017可乐(动态规划+矩阵快速幂)

    设f[i][j]为第i天到达j号城市的方案数,转移显然,答案即为每天在每个点的方案数之和.矩乘一发即可. #include<iostream> #include<cstdio> ...

  2. Ubuntu 10.04 配置TQ2440交叉编译环境

    一.解压交叉编译开发工具包  EABI_4.3.3_EmbedSky_20100610.tar.bz2 $ sudo mkdir /opt/EmbedSky/     $ sudo cp -r /ho ...

  3. 后渗透提权辅助工具BeRoot详解

    0x00 工具介绍 前言 BeRoot是一个后期开发工具,用于检查常见的Windows的配置错误,以方便找到提高我们提权的方法.其二进制编译地址为: https://github.com/Alessa ...

  4. bzoj1969: [Ahoi2005]LANE 航线规划(树链剖分)

    只有删边,想到时间倒流. 倒着加边,因为保证图连通,所以一开始一定至少是一棵树.我们先建一棵树出来,对于每一条非树边,两个端点在树上这段路径的边就不会变成关键边了,所以将它们对答案的贡献删去,那么直接 ...

  5. 【bzoj4195】【NOI2015】程序自动分析

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3470  Solved: 1626[Submit][Sta ...

  6. 51nod求助

    求助dalao们,51nod1170实在是不会了,有没有大佬讲一下,有兴趣的可以告诉我,我提供AC代码. using System; using System.Collections.Generic; ...

  7. springboot缓存开发

    前言:缓存在开发中是一个必不可少的优化点,近期在公司的项目重构中,关于缓存优化了很多点,比如在加载一些数据比较多的场景中,会大量使用缓存机制提高接口响应速度,简介提升用户体验.关于缓存,很多人对它都是 ...

  8. ASCLL表

    ASCII码表完整版 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 ...

  9. JSP2 特性

    JSP2 新特性 1.直接配置 JSP 属性 2.表达式语言 3.简化的自定义标签 API 4.Tag 文件语法 如果要使用 JSP2 语法,web.xml 文件必须使用 Servlet2.4 以上版 ...

  10. [DeeplearningAI笔记]序列模型2.3-2.5余弦相似度/嵌入矩阵/学习词嵌入

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.3词嵌入的特性 properties of word embedding Mikolov T, Yih W T, Zwe ...