VGG16提取图像特征 (torch7)

时间:2024-01-16 15:44:50

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模型提取图片特征