简体   繁体   English

从.caffemodel中提取权重而不在Python中安装caffe

[英]Extracting weights from .caffemodel without caffe installed in Python

Is there a relatively simple way to extract weights in Python from one of the many pretrained models in Caffe Zoo WITHOUT CAFFE (nor pyCaffe)? 是否有一种相对简单的方法从Caffe Zoo中的许多预训练模型之一中提取Python中的权重而没有CAFFE (也不是pyCaffe)? ie parsing .caffemodel to hdf5/numpy or whatever format that can be read by Python? .caffemodel .caffemodel解析为hdf5 / numpy或Python可以读取的任何格式?

All the answers I found use C++ code with caffe classes or Pycaffe. 我找到的所有答案都使用带有caffe类或Pycaffe的C ++代码。 I have looked at pycaffe's code it looks like you really need caffe to make sense of the binary is that the only solution? 我看过pycaffe的代码看起来你真的需要caffe来理解二进制是唯一的解决方案吗?

I had to resolve that exact issue just now. 我刚才必须解决这个问题。 Assuming you have a .caffemodel (binary proto format), it turns out to be quite simple. 假设你有一个.caffemodel(二进制原型格式),结果很简单。

  1. Download the latest caffe.proto 下载最新的caffe.proto

  2. Compile into python library: protoc --python_out=. 编译成python库:protoc --python_out =。 caffe.proto caffe.proto

  3. Import and parse 导入和解析

The sample code below 下面的示例代码

import numpy as np
import sys, os
import argparse
import caffe_pb2 as cq

f = open('VGG_ILSVRC_16_layers.caffemodel', 'r')
cq2 = cq.NetParameter()
cq2.ParseFromString(f.read())
f.close()
print "name 1st layer: " + cq2.layers[0].name 

produces for me: 为我生产:

name 1st layer: conv1_1

Obviously you can extract anything else you want from your object. 显然,你可以从你的对象中提取你想要的任何东西。 I just printed the name of my first layer as an example. 我刚刚打印了第一层的名称作为示例。 Also, your model may be expressing the layers in either the layers array (deprecated) or the layer (no 's') array, but you get the gist. 此外,您的模型可能在图层数组(已弃用)或图层(无's')数组中表示图层,但您可以获得要点。

Nowadays, caffe can save the weights in two formats: BINARYPROTO, or HDF5. 如今,caffe可以将权重保存为两种格式:BINARYPROTO或HDF5。 Binary weights files with extension .caffemodel are in BINARYPROTO format, while extension .caffemodel.h5 are in HDF5 format. 扩展名为.caffemodel二进制加权文件采用BINARYPROTO格式,扩展名.caffemodel.h5采用HDF5格式。 Since the HDF5 format was introduced to caffe recently, I expect most models you currently encounter in the "model zoo" to be in the more "traditional" BINARYPROTO format. 由于最近将HDF5格式引入了caffe,我预计您目前在“模型动物园”中遇到的大多数模型都采用更“传统”的BINARYPROTO格式。

If the weights are in stored in HDF5 format, you might be able to pick through them using h5py package. 如果权重以HDF5格式存储,您可以使用h5py包来选择它们。

However, the BINARYPROTO format is based on a binary serialization of google protocol buffer format that is defined by caffe.proto . 但是,BINARYPROTO格式基于由caffe.proto定义的谷歌协议缓冲区格式的二进制序列化。 I am no expert in protocol buffers, but I suspect you will have a really hard time deciphering the binary file without explicitly "compiling" the caffe.proto protobuf definition files (which is part of caffe build). 我在协议缓冲区不是专家,但我怀疑你将有一个艰难的时期解密二进制文件没有明确“编译”的caffe.proto protobuf的定义文件(这是朱古力构建的一部分)。

I suppose the easiest way to pick into the weights is by installing caffe and using its python/C++ interface. 我认为选择权重的最简单方法是安装caffe并使用其python / C ++接口。 Why don't you just do that? 你为什么不这样做?

As it so happens, ethereon made a wonderful library called caffe-tensorflow to convert caffe models to Tensorflow code, but that is not all! 正如它所发生的那样,ethereon创建了一个名为caffe-tensorflow的精彩库来将caffe模型转换为Tensorflow代码,但这并非全部! It also allows the user to convert .caffemodel files to .npy files without having to build pycaffe! 它还允许用户将.caffemodel文件转换为.npy文件,而无需构建pycaffe! It tests if caffe was built and if not it falls back to a pure google protobuf implementation. 它测试是否构建了caffe,如果没有,它会回归纯粹的google protobuf实现。

I don't understand why you want to do that without caffe/pycaffe, perhaps you are tired of deploying caffe on new machine ? 我不明白你为什么要在没有caffe / pycaffe的情况下这样做,也许你已经厌倦了在新机器上部署caffe? But since caffemodel is special binary data type of caffe, using others' tool doesn't make life easier. 但由于caffemodel是特殊的二元数据类型的caffe,使用他人的工具不会让生活更轻松。

If you do insist to do this, there is another framework : Mocha on Julia , which provides a method to extracting caffemodel to hdf5. 如果你坚持这样做,还有另一个框架: Julia上的Mocha ,它提供了一种将caffemodel提取到hdf5的方法。 I hope this could help you. 我希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM