把vgg-face.mat权重迁移到pytorch模型示例


Posted in Python onDecember 27, 2019

最近使用pytorch时,需要用到一个预训练好的人脸识别模型提取人脸ID特征,想到很多人都在用用vgg-face,但是vgg-face没有pytorch的模型,于是写个vgg-face.mat转到pytorch模型的代码

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu May 10 10:41:40 2018
@author: hy
"""
import torch
import math
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
from scipy.io import loadmat
import scipy.misc as sm
import matplotlib.pyplot as plt
 
class vgg16_face(nn.Module):
  def __init__(self,num_classes=2622):
    super(vgg16_face,self).__init__()
    inplace = True
    self.conv1_1 = nn.Conv2d(3,64,kernel_size=(3,3),stride=(1,1),padding=(1,1))
    self.relu1_1 = nn.ReLU(inplace)
    self.conv1_2 = nn.Conv2d(64,64,kernel_size=(3,3),stride=(1,1),padding=(1,1))
    self.relu1_2 = nn.ReLU(inplace)
    self.pool1 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
      
    self.conv2_1 = nn.Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu2_1 = nn.ReLU(inplace)
    self.conv2_2 = nn.Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu2_2 = nn.ReLU(inplace)
    self.pool2 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
      
    self.conv3_1 = nn.Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu3_1 = nn.ReLU(inplace)
    self.conv3_2 = nn.Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu3_2 = nn.ReLU(inplace)
    self.conv3_3 = nn.Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu3_3 = nn.ReLU(inplace)
    self.pool3 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
      
    self.conv4_1 = nn.Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu4_1 = nn.ReLU(inplace)
    self.conv4_2 = nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu4_2 = nn.ReLU(inplace)
    self.conv4_3 = nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu4_3 = nn.ReLU(inplace)
    self.pool4 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
      
    self.conv5_1 = nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu5_1 = nn.ReLU(inplace)
    self.conv5_2 = nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu5_2 = nn.ReLU(inplace)
    self.conv5_3 = nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    self.relu5_3 = nn.ReLU(inplace)
    self.pool5 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False) 
      
    self.fc6 = nn.Linear(in_features=25088, out_features=4096, bias=True)
    self.relu6 = nn.ReLU(inplace)
    self.drop6 = nn.Dropout(p=0.5)
    
    self.fc7 = nn.Linear(in_features=4096, out_features=4096, bias=True)
    self.relu7 = nn.ReLU(inplace)
    self.drop7 = nn.Dropout(p=0.5)
    self.fc8 = nn.Linear(in_features=4096, out_features=num_classes, bias=True)
      
    self._initialize_weights()
  def forward(self,x):
    out = self.conv1_1(x)
    x_conv1 = out
    out = self.relu1_1(out)
    out = self.conv1_2(out)
    out = self.relu1_2(out)
    out = self.pool1(out)
    x_pool1 = out
    
    out = self.conv2_1(out)
    out = self.relu2_1(out)
    out = self.conv2_2(out)
    out = self.relu2_2(out)
    out = self.pool2(out)
    x_pool2 = out
    
    out = self.conv3_1(out)
    out = self.relu3_1(out)
    out = self.conv3_2(out)
    out = self.relu3_2(out)
    out = self.conv3_3(out)
    out = self.relu3_3(out)
    out = self.pool3(out)
    x_pool3 = out
    
    out = self.conv4_1(out)
    out = self.relu4_1(out)
    out = self.conv4_2(out)
    out = self.relu4_2(out)
    out = self.conv4_3(out)
    out = self.relu4_3(out)
    out = self.pool4(out)
    x_pool4 = out
    
    out = self.conv5_1(out)
    out = self.relu5_1(out)
    out = self.conv5_2(out)
    out = self.relu5_2(out)
    out = self.conv5_3(out)
    out = self.relu5_3(out)
    out = self.pool5(out)
    x_pool5 = out
    
    out = out.view(out.size(0),-1)
    
    out = self.fc6(out)
    out = self.relu6(out)
    out = self.fc7(out)
    out = self.relu7(out)
    out = self.fc8(out)
    
    return out, x_pool1, x_pool2, x_pool3, x_pool4, x_pool5
 
  def _initialize_weights(self):
    for m in self.modules():
      if isinstance(m, nn.Conv2d):
        n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
        m.weight.data.normal_(0, math.sqrt(2. / n))
        if m.bias is not None:
          m.bias.data.zero_()
      elif isinstance(m, nn.BatchNorm2d):
        m.weight.data.fill_(1)
        m.bias.data.zero_()
      elif isinstance(m, nn.Linear):
        m.weight.data.normal_(0, 0.01)
        m.bias.data.zero_()
     
def copy(vgglayers, dstlayer,idx):
  layer = vgglayers[0][idx]
  kernel, bias = layer[0]['weights'][0][0]
  if idx in [33,35]: # fc7, fc8
    kernel = kernel.squeeze()
    dstlayer.weight.data.copy_(torch.from_numpy(kernel.transpose([1,0]))) # matrix format: axb -> bxa
  elif idx == 31: # fc6
    kernel = kernel.reshape(-1,4096)
    dstlayer.weight.data.copy_(torch.from_numpy(kernel.transpose([1,0]))) # matrix format: axb -> bxa
  else:
    dstlayer.weight.data.copy_(torch.from_numpy(kernel.transpose([3,2,1,0]))) # matrix format: axbxcxd -> dxcxbxa
  dstlayer.bias.data.copy_(torch.from_numpy(bias.reshape(-1)))
 
def get_vggface(vgg_path):
  """1. define pytorch model"""   
  model = vgg16_face()   
  
  """2. get pre-trained weights and other params"""     
  #vgg_path = "/home/hy/vgg-face.mat" # download from http://www.vlfeat.org/matconvnet/pretrained/
  vgg_weights = loadmat(vgg_path)
  data = vgg_weights
  meta = data['meta']
  classes = meta['classes']
  class_names = classes[0][0]['description'][0][0]
  normalization = meta['normalization']
  average_image = np.squeeze(normalization[0][0]['averageImage'][0][0][0][0])
  image_size = np.squeeze(normalization[0][0]['imageSize'][0][0])
  layers = data['layers']
  # =============================================================================
  # for idx,layer in enumerate(layers[0]):
  #   name = layer[0]['name'][0][0]
  #   print idx,name
  # """
  # 0 conv1_1
  # 1 relu1_1
  # 2 conv1_2
  # 3 relu1_2
  # 4 pool1
  # 5 conv2_1
  # 6 relu2_1
  # 7 conv2_2
  # 8 relu2_2
  # 9 pool2
  # 10 conv3_1
  # 11 relu3_1
  # 12 conv3_2
  # 13 relu3_2
  # 14 conv3_3
  # 15 relu3_3
  # 16 pool3
  # 17 conv4_1
  # 18 relu4_1
  # 19 conv4_2
  # 20 relu4_2
  # 21 conv4_3
  # 22 relu4_3
  # 23 pool4
  # 24 conv5_1
  # 25 relu5_1
  # 26 conv5_2
  # 27 relu5_2
  # 28 conv5_3
  # 29 relu5_3
  # 30 pool5
  # 31 fc6
  # 32 relu6
  # 33 fc7
  # 34 relu7
  # 35 fc8
  # 36 prob
  # """
  # =============================================================================
  
  """3. load weights to pytorch model"""  
  copy(layers,model.conv1_1,0)
  copy(layers,model.conv1_2,2)
  copy(layers,model.conv2_1,5)
  copy(layers,model.conv2_2,7)
  copy(layers,model.conv3_1,10)
  copy(layers,model.conv3_2,12)
  copy(layers,model.conv3_3,14)
  copy(layers,model.conv4_1,17)
  copy(layers,model.conv4_2,19)
  copy(layers,model.conv4_3,21)
  copy(layers,model.conv5_1,24)
  copy(layers,model.conv5_2,26)
  copy(layers,model.conv5_3,28)
  copy(layers,model.fc6,31)
  copy(layers,model.fc7,33)
  copy(layers,model.fc8,35)
  return model,class_names,average_image,image_size
 
if __name__ == '__main__':
  """test""" 
  vgg_path = "/home/hy/vgg-face.mat" # download from http://www.vlfeat.org/matconvnet/pretrained/ 
  model,class_names,average_image,image_size = get_vggface(vgg_path) 
  imgpath = "/home/hy/e/avg_face.jpg"
  img = sm.imread(imgpath)
  img = sm.imresize(img,[image_size[0],image_size[1]])
  input_arr = np.float32(img)#-average_image # h,w,c
  x = torch.from_numpy(input_arr.transpose((2,0,1))) # c,h,w
  avg = torch.from_numpy(average_image) # 
  avg = avg.view(3,1,1).expand(3,224,224)
  x = x - avg
  x = x.contiguous()
  x = x.view(1, x.size(0), x.size(1), x.size(2))
  x = Variable(x)
  out, x_pool1, x_pool2, x_pool3, x_pool4, x_pool5 = model(x)
#  plt.imshow(x_pool1.data.numpy()[0,45]) # plot

以上这篇把vgg-face.mat权重迁移到pytorch模型示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python进行稳定可靠的文件操作详解
Dec 31 Python
Python+Opencv识别两张相似图片
Mar 23 Python
python去除空格和换行符的实现方法(推荐)
Jan 04 Python
15行Python代码带你轻松理解令牌桶算法
Mar 21 Python
通过实例解析Python调用json模块
Dec 11 Python
Django项目使用ckeditor详解(不使用admin)
Dec 17 Python
在python tkinter界面中添加按钮的实例
Mar 04 Python
Python计算指定日期是今年的第几天(三种方法)
Mar 26 Python
PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
Apr 15 Python
python新手学习使用库
Jun 11 Python
如何更换python默认编辑器的背景色
Aug 10 Python
Python入门之使用pandas分析excel数据
May 12 Python
Pytorch 多维数组运算过程的索引处理方式
Dec 27 #Python
Pytorch 之修改Tensor部分值方式
Dec 27 #Python
pytorch 实现tensor与numpy数组转换
Dec 27 #Python
Numpy与Pytorch 矩阵操作方式
Dec 27 #Python
基于python及pytorch中乘法的使用详解
Dec 27 #Python
pytorch:torch.mm()和torch.matmul()的使用
Dec 27 #Python
pytorch点乘与叉乘示例讲解
Dec 27 #Python
You might like
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
PHP封装cURL工具类与应用示例
2019/07/01 PHP
40个有创意的jQuery图片和内容滑动及弹出插件收藏集之三
2012/01/03 Javascript
Javascript中数组sort和reverse用法分析
2014/12/30 Javascript
jQuery实现dialog设置focus焦点的方法
2015/06/10 Javascript
ionic实现可滑动的tab选项卡切换效果
2020/04/15 Javascript
JavaScript 限制文本框不可输入英文单双引号的方法
2016/12/20 Javascript
在ABP框架中使用BootstrapTable组件的方法
2017/07/31 Javascript
Windows下使用Nodejs运行js的方法
2017/09/02 NodeJs
浅谈vue方法内的方法使用this的问题
2018/09/15 Javascript
详解使用WebPack搭建React开发环境
2019/08/06 Javascript
基于javascript原生判断DOM是否加载完毕
2020/10/14 Javascript
NodeJS模块Buffer原理及使用方法解析
2020/11/11 NodeJs
超详细小程序定位地图模块全系列开发教学
2020/11/24 Javascript
原生JS实现京东查看商品点击放大
2020/12/21 Javascript
Python合并两个字典的常用方法与效率比较
2015/06/17 Python
Python命令行解析模块详解
2018/02/01 Python
Python中 map()函数的用法详解
2018/07/10 Python
Python3基础教程之递归函数简单示例
2019/06/07 Python
Python 线程池用法简单示例
2019/10/02 Python
python自动化测试之异常及日志操作实例分析
2019/11/09 Python
python3光学字符识别模块tesserocr与pytesseract的使用详解
2020/02/26 Python
Python文件时间操作步骤代码详解
2020/04/13 Python
可爱的童装和鞋子:Fabkids
2019/08/16 全球购物
构造方法和其他方法的区别?怎么调用父类的构造方法
2013/09/22 面试题
为什么需要版本控制?
2013/08/08 面试题
《美丽的丹顶鹤》教学反思
2014/04/22 职场文书
小学竞选班干部演讲稿
2014/08/20 职场文书
万能检讨书2000字
2014/10/17 职场文书
合伙开公司协议书范本
2014/10/28 职场文书
世界文化遗产导游词
2015/02/13 职场文书
时尚女魔头观后感
2015/06/04 职场文书
毛主席纪念堂观后感
2015/06/17 职场文书
百年孤独读书笔记
2015/06/29 职场文书
sqlserver2017共享功能目录路径不可改的解决方法
2021/04/16 SQL Server
js 实现验证码输入框示例详解
2022/09/23 Javascript