VGG16提取图像特征 (torch7)

VGG16
loadcaffe
torch7
  1. 下载pretrained model,保存到当前目录下

  1. th> caffemodel_url = 'http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel' 

  2. th> proto_url='https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-vgg_ilsvrc_16_layers_deploy-prototxt' 

  3. th> os.execute('wget VGG_ILSVRC_16_layers.caffemodel' .. caffemodel_url) 

  4. th> os.execute('wget VGG_ILSVRC_16_layers_deploy.prototxt' .. proto_url) 

  1. 使用loadcaffe提取图像特征


  1. require 'torch' -- 使用th命令,可忽略 

  2. require 'nn' -- 修改model用到nn包 

  3. require 'loadcaffe' -- 加在caffe训练的包 

  4. require 'image' -- 加载图像,处理图像,可以使用cv中函数替代 


  5. local loadSize = {3,256,256} -- 加载图像scale尺寸 

  6. local sampleSize = {3,224,224} -- 样本尺寸,其实就是选取scale后图像的中间一块 


  7. local function loadImage(input) 

  8. -- 将图像缩放到loadSize尺寸,为了保证aspect ratio不变,将短边缩放后,长边按比例缩放 

  9. if input:size(3) < input:size(2) then 

  10. input = image.scale(input,loadSize[2],loadSize[3]*input:size(2)/input:size(3)) 

  11. -- 注意image.scale(src,width,height),width对应的是input:size[3],height对应的是input:size[2] 

  12. else 

  13. input = image.scale(input,loadSize[2]*input:size(3)/input:size(2),loadSize[3]) 

  14. end 

  15. return input 

  16. end 


  17. local bgr_means = {103.939,116.779,123.68} --VGG预训练中的均值 

  18. local function vggPreProcessing(img) 

  19. local img2=img:clone() 

  20. img2[{{1}}] =img2[{{3}}] -- image.load 加载图像是rgb格式,需转化维bgr 

  21. img2[{{3}}] = img[{{1}}] 

  22. img2:mul(255) -- image.load()加载的图像 pixel value \in [0,1] 

  23. for i=1,3 do 

  24. img2[i]:add(-bgr_means[i]) -- 中心化 

  25. end 

  26. return img2 

  27. end 


  28. local function centerCrop(input) 

  29. local oH = sampleSize[2] 

  30. local oW = sampleSize[3] 

  31. local iW = input:size(3) 

  32. local iH = input:size(2) 

  33. local w1 = math.ceil((iW-oW)/2) 

  34. local h1 = math.ceil((iH-oH)/2) 

  35. local out = image.crop(input,w1,h1,w1+oW,h1+oH) 

  36. return out 

  37. end 


  38. local function getPretrainedModel() 

  39. local proto = 'VGG_ILSVRC_16_layers_deploy.prototxt' 

  40. local caffemodel = '/home/zwzhou/modelZoo/VGG_ILSVRC_16_layers.caffemodel' 


  41. local model = loadcaffe.load(proto,caffemodel,'nn') -- 加载pretrained model 

  42. for i=1,3 do -- 将最后3层舍掉 

  43. model.modules[#model.modules]=nil 

  44. end 

  45. -- 删除pretrained model的一些层官方方法 

  46. -- ========================== 

  47. -- for i= 40,38,-1 do 

  48. -- model:remove(i) 

  49. -- end 

  50. -- ========================== 

  51. model:add(nn.Normalize(2)) -- 添加一层正则化层,将输出向量归一化 


  52. model:evaluate() -- self.training=false ,非训练,让网络参数不变 

  53. return model 

  54. end 


  55. torch.setdefaulttensortype('torch.FloatTensor') 

  56. model = getPretrainedModel() 


  57. filepath = '/home/zwzhou/MOT16/train/MOT16-02/img1/000001.jpg' 

  58. local img1=image.load(filepath) -- rgb图像 

  59. local input = image.crop(img1,910,480,910+97,480+110) -- 里面参数时选择原图像的一个区域,boundingbox 


  60. input = loadImage(input) 

  61. local vggPreProcessed = vggPreProcessing(input) 

  62. local out = centerCrop(vggPreProcessed) 


  63. local outputs = model:forward(out) 


  64. print(outputs) 

  65. print(#outputs) 

  1. 参考项目

    VGG-19-feature-extractor

    Torch 7 利用已有VGG模型提取图片特征

VGG16提取图像特征 (torch7)的更多相关文章

  1. CNN基础二:使用预训练网络提取图像特征

    上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...

  2. 原来CNN是这样提取图像特征的。。。

    对于即将到来的人工智能时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的领域,会不会感觉马上就out了?作为机器学习的一个分支,深度学习同样需要计算机获得强大的学 ...

  3. 深度学习tensorflow实战笔记 用预训练好的VGG-16模型提取图像特征

    1.首先就要下载模型结构 首先要做的就是下载训练好的模型结构和预训练好的模型,结构地址是:点击打开链接 模型结构如下: 文件test_vgg16.py可以用于提取特征.其中vgg16.npy是需要单独 ...

  4. Pytorch如何用预训练模型提取图像特征

    方法很简单,你只需要将模型最后的全连接层改成Dropout即可. import torch from torchvision import models # load data x, y = get_ ...

  5. opencv批处理提取图像的特征

    ____________________________________________________________________________________________________ ...

  6. paper 131:【图像算法】图像特征:GLCM【转载】

    转载地址:http://www.cnblogs.com/skyseraph/archive/2011/08/27/2155776.html 一 原理 1 概念:GLCM,即灰度共生矩阵,GLCM是一个 ...

  7. 【图像算法】图像特征:GLCM灰度共生矩阵,纹理特征

    [图像算法]图像特征:GLCM SkySeraph Aug 27th 2011  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modifie ...

  8. python实现gabor滤波器提取纹理特征 提取指静脉纹理特征 指静脉切割代码

    参考博客:https://blog.csdn.net/xue_wenyuan/article/details/51533953 https://blog.csdn.net/jinshengtao/ar ...

  9. MATLAB·提取图像中多个目标

    基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...

随机推荐

  1. VM+CentOS+hadoop2.7搭建hadoop完全分布式集群

    写在前边的话: 最近找了一个云计算开发的工作,本以为来了会直接做一些敲代码,处理数据的活,没想到师父给了我一个课题“基于质量数据的大数据分析”,那么问题来了首先要做的就是搭建这样一个平台,毫无疑问,底 ...

  2. dongle --NFC

    A dongle is a small piece of hardware that attaches to a computer, TV, or other electronic device in ...

  3. 寻找最小(最大)的k个数

    题目描述:输入n个整数,输出其中最小的k个元素. 例如:输入1,2,3,4,5,6,7,8这8个数字,则最小的4个数字为1,2,3,4. 思路1:最容易想到的方法:先对这个序列从小到大排序,然后输出前 ...

  4. Scrapy框架(3)

    一.如何提升scrapy框架的爬取效率 增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings配置文件中修改CONCURRENT_REQUESTS = 100,并发设 ...

  5. centos linux 系统日常管理4 scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法 第十七节课

    centos linux 系统日常管理4  scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法  第十七节课 rsync可以增量同步,scp不行 ...

  6. 给所有开发者的React Native详细入门指南

    建议先下载好资料后,再阅读本文.demo代码和资料下载 目录 一.前言 二.回答一些问题 1.为什么写此教程 2.本文适合哪些人看 3.如何使用本教程 4.需要先学习JavaScript.HTML.C ...

  7. java序列化与反序列化(转)

    Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列 ...

  8. #C++初学记录(初识汉诺塔)

    汉诺塔 题目 用1,2,...,n表示n个盘子,称为1号盘,2号盘,....号数大盘子就大.经典的汉诺塔问 题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于 印度传说的 ...

  9. HDU 1879 继续畅通工程(Prim||Kruscal模板题)

    原题链接 Prim(点归并) //异或运算:相同为假,不同为真 #include<cstdio> #include<algorithm> #define maxn 105 us ...

  10. 33Sql数据删除与遍历

    数据库的创建.添加.修改.查询.删除都是利用SQL语句和类QSqlQuery的结合. QSqlDatabase::database().可返回当前正在打开的数据库对象. 数据库的删除 //获取删除的名 ...