Search code examples
caffepre-trained-model

How a subset of pretrained caffe model can be saved?


I am working on a pretrained caffe model (in python) which has 3 layers. I want to decompose this caffe model and create a new model the same as first layer of this model. For example:

Original Caffe model data -> conv1_1 -> conv1_2 -> conv2_1 -> conv2_2 -> conv3_1 -> conv3_2

New Caffe model data -> conv1_1 -> conv1_2

Can anybody help me?


Solution

  • Python exposes the data inside the .caffemodel file. It can be accessed as an array. For example,

    net = caffe.Net('path/to/conv.prototxt', 'path/to/conv.caffemodel', caffe.TEST)
    W = net.params['con_1'][0].data[...]
    b = net.params['con_1'][1].data[...]
    

    You can copy this data into a new file and save it as a .caffemodel file. Have a look at this and this.