VGG16提取图像特征 (torch7)
VGG16提取图像特征 (torch7)
下载pretrained model,保存到当前目录下
- th> caffemodel_url = 'http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel'
- th> proto_url='https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-vgg_ilsvrc_16_layers_deploy-prototxt'
- th> os.execute('wget VGG_ILSVRC_16_layers.caffemodel' .. caffemodel_url)
- th> os.execute('wget VGG_ILSVRC_16_layers_deploy.prototxt' .. proto_url)
使用loadcaffe提取图像特征
- require 'torch' -- 使用th命令,可忽略
- require 'nn' -- 修改model用到nn包
- require 'loadcaffe' -- 加在caffe训练的包
- require 'image' -- 加载图像,处理图像,可以使用cv中函数替代
- local loadSize = {3,256,256} -- 加载图像scale尺寸
- local sampleSize = {3,224,224} -- 样本尺寸,其实就是选取scale后图像的中间一块
- local function loadImage(input)
- -- 将图像缩放到loadSize尺寸,为了保证aspect ratio不变,将短边缩放后,长边按比例缩放
- if input:size(3) < input:size(2) then
- input = image.scale(input,loadSize[2],loadSize[3]*input:size(2)/input:size(3))
- -- 注意image.scale(src,width,height),width对应的是input:size[3],height对应的是input:size[2]
- else
- input = image.scale(input,loadSize[2]*input:size(3)/input:size(2),loadSize[3])
- end
- return input
- end
- local bgr_means = {103.939,116.779,123.68} --VGG预训练中的均值
- local function vggPreProcessing(img)
- local img2=img:clone()
- img2[{{1}}] =img2[{{3}}] -- image.load 加载图像是rgb格式,需转化维bgr
- img2[{{3}}] = img[{{1}}]
- img2:mul(255) -- image.load()加载的图像 pixel value \in [0,1]
- for i=1,3 do
- img2[i]:add(-bgr_means[i]) -- 中心化
- end
- return img2
- end
- local function centerCrop(input)
- local oH = sampleSize[2]
- local oW = sampleSize[3]
- local iW = input:size(3)
- local iH = input:size(2)
- local w1 = math.ceil((iW-oW)/2)
- local h1 = math.ceil((iH-oH)/2)
- local out = image.crop(input,w1,h1,w1+oW,h1+oH)
- return out
- end
- local function getPretrainedModel()
- local proto = 'VGG_ILSVRC_16_layers_deploy.prototxt'
- local caffemodel = '/home/zwzhou/modelZoo/VGG_ILSVRC_16_layers.caffemodel'
- local model = loadcaffe.load(proto,caffemodel,'nn') -- 加载pretrained model
- for i=1,3 do -- 将最后3层舍掉
- model.modules[#model.modules]=nil
- end
- -- 删除pretrained model的一些层官方方法
- -- ==========================
- -- for i= 40,38,-1 do
- -- model:remove(i)
- -- end
- -- ==========================
- model:add(nn.Normalize(2)) -- 添加一层正则化层,将输出向量归一化
- model:evaluate() -- self.training=false ,非训练,让网络参数不变
- return model
- end
- torch.setdefaulttensortype('torch.FloatTensor')
- model = getPretrainedModel()
- filepath = '/home/zwzhou/MOT16/train/MOT16-02/img1/000001.jpg'
- local img1=image.load(filepath) -- rgb图像
- local input = image.crop(img1,910,480,910+97,480+110) -- 里面参数时选择原图像的一个区域,boundingbox
- input = loadImage(input)
- local vggPreProcessed = vggPreProcessing(input)
- local out = centerCrop(vggPreProcessed)
- local outputs = model:forward(out)
- print(outputs)
- print(#outputs)
VGG16提取图像特征 (torch7)的更多相关文章
- CNN基础二:使用预训练网络提取图像特征
上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...
- 原来CNN是这样提取图像特征的。。。
对于即将到来的人工智能时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的领域,会不会感觉马上就out了?作为机器学习的一个分支,深度学习同样需要计算机获得强大的学 ...
- 深度学习tensorflow实战笔记 用预训练好的VGG-16模型提取图像特征
1.首先就要下载模型结构 首先要做的就是下载训练好的模型结构和预训练好的模型,结构地址是:点击打开链接 模型结构如下: 文件test_vgg16.py可以用于提取特征.其中vgg16.npy是需要单独 ...
- Pytorch如何用预训练模型提取图像特征
方法很简单,你只需要将模型最后的全连接层改成Dropout即可. import torch from torchvision import models # load data x, y = get_ ...
- opencv批处理提取图像的特征
____________________________________________________________________________________________________ ...
- paper 131:【图像算法】图像特征:GLCM【转载】
转载地址:http://www.cnblogs.com/skyseraph/archive/2011/08/27/2155776.html 一 原理 1 概念:GLCM,即灰度共生矩阵,GLCM是一个 ...
- 【图像算法】图像特征:GLCM灰度共生矩阵,纹理特征
[图像算法]图像特征:GLCM SkySeraph Aug 27th 2011 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modifie ...
- python实现gabor滤波器提取纹理特征 提取指静脉纹理特征 指静脉切割代码
参考博客:https://blog.csdn.net/xue_wenyuan/article/details/51533953 https://blog.csdn.net/jinshengtao/ar ...
- MATLAB·提取图像中多个目标
基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...
随机推荐
- SaltStack生产案例-系统初始化
需求分析 一,系统初始化 1.1 关闭SELinux 1.2 关闭默认iptables 1.3 时间同步(配置NTP) 1.4 文件描述符(必备/etc/security/limmits.c ...
- Oracle下select语句
先看scott下自带的emp表 empno:编号 ename:名字 Job:职位 mgr:上级编号 hiredate:入职时间 sal:薪水 comm:奖金 deptno:部门编号 部门表dep ...
- Oracle AWR之-enq: TX - allocate ITL entry
今天收到压力测试期间awr报告,测试人员要我看看数据库是否有可以优化的地方,数据库服务器配置信息:CPU:32*8,内存:480g 单实例数据库:oracle 11.2.0.4.具体分析过程如下: 可 ...
- android 导出数据库文件
1.打开dos窗口,进入自己SDK路径下,再进入platform-tools下边 2.进入shell模式: adb shell 3.获取所有root权限: su root 4.打开需要导出的数据库文件 ...
- android进入页面会定位到ListView问题解决方法
在我们的页面中如果存在有ListView,当我们进入这个activity时,页面会定位到ListView的位置去,而不是activity的头部,这是由于ListView会去默认获取焦点所造成的. 解决 ...
- MySQL的redo log结构和SQL Server的log结构对比
MySQL的redo log结构和SQL Server的log结构对比 innodb 存储引擎 mysql技术内幕 log buffer根据一定规则将内存中的log block刷写到磁盘,这个规则是 ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求 The serverRuntime@appConcurrentRequestLimit setting is being exceeded.
今天下午17点左右,博客园博客站点出现这样的错误信息: Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@a ...
- django开发项目的部署nginx
Django 部署(Nginx) 本文主要讲解 nginx + uwsgi socket 的方式来部署 Django,比 Apache mod_wsgi 要复杂一些,但这是目前主流的方法. 1. 运行 ...
- .net webform 把word转为html
首先添加引用 引用命名空间 using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; u ...
- Python 如何判断一个函数是generator函数?
如何判断一个函数是否是一个特殊的 generator 函数?可以利用 isgeneratorfunction 判断: >>>from inspect import isgenerat ...