zl程序教程

您现在的位置是:首页 >  其它

当前栏目

人脸识别0-11:insightFace - mxnet 转 caffe模型

模型 11 人脸识别 Caffe MXNet
2023-09-14 09:13:07 时间

以下链接是个人关于insightFace所有见解,如有错误欢迎大家指出,我会第一时间纠正,如有兴趣可以加微信:17575010159 相互讨论技术。
人脸识别0-00:insightFace目录
这是本人项目的源码:https://github.com/944284742/1.FaceRecognition
其中script目录下的文件为本人编写,主要用于适应自己的项目,可以查看该目录下的redeme文件。

前言

本人在实际项目落地的过程中,需要把mxnet训练好的模型,转化为caffe模型,并且在华为设备上跑起来,所以写了这篇博客当作笔记。

资源下载

转换工具: https://github.com/Laulian/MxNet2Caffe-mobilefacenet
下载之后,本人如下:
在这里插入图片描述

转换模型:https://github.com/deepinsight/insightface/wiki/Model-Zoo

下载了上面的资源之后,还需要搭建winds caffe(本人在winds10下进行转换)环境:
wind10 + cuda9.0 + cudnn7.1 + caffe(c++与python接口) + anconda2 环境搭建:https://blog.csdn.net/weixin_43013761/article/details/105094390

模型转换

第一步: 下载好上面提供的资源,把任意一个想要转换的 insightFace 模型解压之后放置如下:
在这里插入图片描述
第二步: 执行指令如下:

如果没有安装mxnet,先执行下面指令安装:

pip install --pre mxnet-cu90 -i https://pypi.douban.com/simple

下面再执行指令生成模型:

python json2prototxt.py --mx-json mxnet-model/model-symbol.json --cf-prototxt caffe-model/model.prototxt

python mxnet2caffe.py --mx-model mxnet-model/model --mx-epoch 0 --cf-prototxt caffe-model/model.prototxt --cf-model caffe-model/model.caffemodel

执行完成可以可以看到生成的 caffe 模型如下:
在这里插入图片描述

模型对比

生成模型之后,我们总需要对比 mxnet 模型和 caffe 模型他们提取的特征是否一致,所以编写代码如下:

#coding=utf-8
import sys, argparse
import numpy as np
import mxnet as mx
import cv2
import caffe
import copy


def get_model(image_size, model_path, epoch, layer):
    ctx = mx.gpu(0)
    sym, arg_params, aux_params = mx.model.load_checkpoint(model_path, epoch)
    all_layers = sym.get_internals()
    sym = all_layers[layer + '_output']
    model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
    model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))])
    model.set_params(arg_params, aux_params)
    return model



def get_feature(model, img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # color transform:BGR---RGB
    img = np.transpose(img, (2, 0, 1))
    input_blob = np.expand_dims(img, axis=0)
    data = mx.nd.array(input_blob)
    db = mx.io.DataBatch(data=(data,))
    model.forward(db, is_train=False)
    embedding = model.get_outputs()[0].asnumpy()[0]
    l2_norm = cv2.norm(embedding, cv2.NORM_L2)
    return embedding / l2_norm

def get_caffe_net(caffemodel, prototxt):
    facenet = caffe.Net(prototxt, caffemodel, caffe.TEST)
    return facenet

def caffe_get_feature(net, img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img - 127.5
    img = img * 0.0078125
    tempimg = np.zeros((1, 112, 112, 3))
    tempimg[0, :, :, :] = img
    tempimg = tempimg.transpose(0, 3, 1, 2)
    net.blobs['data'].data[...] = tempimg
    net.forward()
    features = copy.deepcopy(net.blobs['fc1'].data[...])
    feature = np.array(features[0])
    l2_norm = cv2.norm(feature, cv2.NORM_L2)
    return feature / l2_norm


def cos(v1, v2):
    lenth = len(v1)
    sum = 0
    for i in range(lenth):
        sum += v1[i] * v2[i]
    return sum


if __name__ == '__main__':


    # mxnet init
    model_path = './mxnet-model/model'
    epoch = 0
    image_size = (112,112)
    layer = 'fc1'
    model = get_model(image_size, model_path, epoch, layer)



    #caffe net init
    caffemodel = './caffe-model/model.caffemodel'
    prototxt = './caffe-model/model.prototxt'
    net = get_caffe_net(caffemodel, prototxt)


    image_path = './Aaron_Eckhart_0001.jpg'

    img = cv2.imread(image_path)
    feature_mxnet = get_feature(model, img)
    feature_caffe = caffe_get_feature(net, img)

    print 'sim: ', cos(feature_mxnet, feature_caffe)

该代码的原理很简单:

  1. 加载 mxnet 模型,提取特征
  2. 加载 caffe 模型,提取特征
  3. 对比两个特征是否一致(获得他们的余弦相似度)