Read the modified Caffemodel file network model Parameters _ neural network-Getting Started

Source: Internet
Author: User

Background
The teacher asked me and seniors to achieve a multiresolution detection network. The idea is on the basis of pvanet, from conv2-3 born a branch network, branch network RPN and FC and classifier are copied pvanet backbone network. Using shallow features to detect small targets, that is, the small target Training branch network alone, and finally the branch network and Backbone Pvanet merged. So use the Caffemodel to read and modify.

Reading of parameters in 1.caffemodel files

Caffemodel is a binary file, Caffe has a Python interface for reading. Network structure files such as test.prototxt and model files, such as Pvanet_frcnn_384_iter_5000.caffemodel, are required for reading.
#-*-Coding:utf-8-*-
import numpy as NP
import _init_paths
Import collections from
collections Import O Rdereddict
caffe.set_mode_cpu
net0 = caffe.net (' test23-4-24.prototxt ', \
    ' pvanet_frcnn_384_iter_5000. Caffemodel ', Caffe. TEST) #TEST/train
    #我的python脚本, prototxt,caffemodel files are placed under the sibling directory, and your file path is modified as needed
Conv1_w = net0.params[' conv1_1/ Conv '][0].data
#模型参数都存在了net. Params in this orderly dictionary, this is the dictionary in Python, so the manipulation of model parameters is the same as for Python dictionary operations. [' Conv1_1/conv '] is the key name, [0] is the dimension of the weight

keys0 = Net0.params.keys ()
print Net0.params.keys () for
key0 in KEYS0:   # Output all layer names, parameter
    print key0
    try:
        print net1.params[key1][0].data
    except Indexerror:
        Continue
    try:
        print net1.params[key1][1].data
    except Indexerror:
        continue
    try:
        Print Net1.params[key1][2].data
    except Indexerror:
        continue
    finally:
        print ' \ n '

2. Modify the parameters and save them as a new model file

The value of the general change parameter is not likely to be used, it is to modify the layer, copy some layer parameters to the new layer may be used. Here is the training of the branch network parameters and into the pvanet backbone network model.
[Note]pvanet training, when you specify a model file, if there are layers in the network that are not in the model, the initialization of these parameters is random, but the caffemodel of the training is matched to the network structure.
Net1 = caffe.net (' test_branch1.prototxt ', ' Branch1.caffemodel ', Caffe. TEST)
keys1 = Net1.params.keys () for
key1 in keys1:
    net0.params.setdefault (Key1, Newparams[key1])
    # Net0 net1 Common layer parameters with Net0, net1 unique new layer, layer name and parameter join Net0    

 #net0的参数另存为新caffemodel
 net0.save (' Test_tree.caffemodel ')

Related Reference Blog:
http://blog.csdn.net/jiongjiongxia123/article/details/60965743

Here's a blog to change the FC layer to conv layer is very easy to use: HTTP://NBVIEWER.JUPYTER.ORG/GITHUB/BVLC/CAFFE/BLOB/MASTER/EXAMPLES/NET_SURGERY.IPYNB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.