Instantly share code, notes, and snippets.

What would you like to do?
Combining Pre-trained Left and Right nets into a single joint model
import numpy as np
import sys, os
# Edit the paths as needed:
caffe_root = '../caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe
# Path to your combined net prototxt files:
combined_model_root_path = './models/combined_net/'
l_model_root_path = './models/left/2015-07-20/'
r_model_root_path= './models/right/2015-07-20/'
# The pre-trained Caffemodel files:
lnet_file = '_iter_50000.caffemodel'
rnet_file = '_iter_60000.caffemodel'
# Their respective prototxt files:
lnet_proto = 'net.prototxt'
rnet_proto = 'net.prototxt'
# Chdir if your prototxt files specify your training and testing files
# in relative paths:
lnet = caffe.Net(lnet_proto, lnet_file, caffe.TRAIN)
rnet = caffe.Net(rnet_proto, rnet_file, caffe.TRAIN)
comb_net = caffe.Net('net.prototxt', caffe.TRAIN)
# The layers you want to combine into the new caffe net:
layer_names = ['ff1', 'ff2']
# The two nets we have already loaded:
nets = {
'l': lnet,
'r': rnet,
# For each of the pretrained net sides, copy the params to
# the corresponding layer of the combined net:
for side, net in nets.items():
for layer in layer_names:
W = net.params[layer][0].data[...] # Grab the pretrained weights
b = net.params[layer][1].data[...] # Grab the pretrained bias
comb_net.params['{}_{}'.format(side, layer)][0].data[...] = W # Insert into new combined net
comb_net.params['{}_{}'.format(side, layer)][1].data[...] = b
# Save the combined model with pretrained weights to a caffemodel file:'pretrained.caffemodel')

This comment has been minimized.

Engineering-Course commented Oct 8, 2016

I wonder the two pre-trained model are the same networks or different?


This comment has been minimized.

priyapaul commented Apr 21, 2017

I am not able to load two nets, my terminal becomes unresponsive. Is there anything to be taken care of? The following codes prints loaded rgb net and then tries to load the second net . After printing out the following, it hangs

I0421 18:26:05.947526 10280 net.cpp:408] data -> label
I0421 18:26:05.947546 10280 cpm_data_transformer.cpp:242] CPMDataTransformer constructor done.

net_rgb = caffe.Net(net_proto, net_file, caffe.TRAIN)
print("loaded rgb net")
comb_net = caffe.Net('pose_train_test_rgb.prototxt', caffe.TRAIN)
print("loaded comb net")

This comment has been minimized.

BalajiB197 commented May 23, 2018

W0523 12:27:00.884069 6835 _caffe.cpp:139] DEPRECATION WARNING - deprecated use of Python interface
I am facing this error, when running the script.
I have installed caffe from source and version is 1.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment