parameters are different each time when extracting data from caffemodel file

30 views
Skip to first unread message

Wei Huang

unread,
Apr 11, 2018, 5:19:37 AM4/11/18
to Caffe Users
Hi guys,

I extract the weights and parameters from .caffemodel file, and every time  I ran the python to extract the data, the weights of the same layer are different. 

I wonder does anyone met this probelm before?

The code I used as follows:

import numpy as np
import time
from array import array

import sys

caffe_root = '/home/caffer/'

sys.path.insert(0,caffe_root+'python')
import caffe

import struct
import re
import os
model_file = "/home/caffer/models/squeezenet/"
net_file = model_file+"deploy.prototxt"
caffe_model = model_file+"snapshot_iter_199003.caffemodel"
net = caffe.Net(net_file,caffe_model,caffe.TEST)
net.forward()
for k, v in net.params.items():
  print k, " w : ", np.shape(v[0].data), " b : ", np.shape(v[1].data)
  w = np.reshape(v[0].data, [-1, v[0].data.shape[-1]])
  np.savetxt('w_'+k, w)    
  b = np.reshape(v[1].data, [-1, v[1].data.shape[-1]])
  np.savetxt('b_'+k, b)
        
for k, v in net.blobs.items():
    print k, " shape : ", np.shape(v.data)
    if np.shape(np.shape(v.data))[0] >= 3:    
        d = np.reshape(v.data, [-1, v.data.shape[-1]])
    elif np.shape(v.data) == ():
        d = np.reshape(v.data, [1, 1])
    else:
        d = v.data
    np.savetxt('d_'+k, d)    
print "Finish!"

And I also extract data using another code:
#!/usr/bin/env python
import sys
caffe_root = '/home/caffer/'
sys.path.insert(0,caffe_root+'python')
import caffe

import numpy as np

import os
np.set_printoptions(threshold='nan')

net_file = "deploy.prototxt"
MODEL_FILE = 'solver.prototxt'
PRETRAIN_FILE = 'snapshot_iter_199003.caffemodel'

params_txt = 'params.txt'
pf = open(params_txt, 'w')

net = caffe.Net(net_file, PRETRAIN_FILE, caffe.TEST)

for param_name in net.params.keys():
    weight = net.params[param_name][0].data
    bias = net.params[param_name][1].data

    pf.write(param_name)
    pf.write('\n')

    pf.write('\n' + param_name + '_weight:\n\n')
    weight.shape = (-1, 1)

    for w in weight:
        pf.write('%ff, ' % w)

    pf.write('\n\n' + param_name + '_bias:\n\n')
    bias.shape = (-1, 1)
    for b in bias:
        pf.write('%ff, ' % b)
    pf.write('\n\n')

pf.close


The weights of  the same layer are always different and it seems it does not read the data from .caffemodel, it looks like it reads data from training processing. I am not sure what's wrong with it.

Thank you all!

Best wishes,
Wei

Przemek D

unread,
Apr 12, 2018, 2:44:13 AM4/12/18
to Caffe Users
Is it the case for all layers or just one? I would suspect that a layer actually is not copied from the caffemodel, but initialized using its filler - this would be consistent with the behavior you observe. Hard to say without knowing more though. Please run caffe test on this network and save the log. This will contain some information on what happens with the layers, please attach it to your reply (attach, please don't just paste it into the post).

Wei Huang

unread,
Apr 16, 2018, 4:01:35 AM4/16/18
to Caffe Users
Hi,

Thanks for the quick response!

 I found that if I used deploy.prototxt then the weights could be extracted and could be always the same no matter how many times you ran the python file. And the feature map and input data are all zero. However, if I used the test.prototxt file then the weights could be different each time. I wonder does this affet?

And, sometimes the layer name has "/" in it. When I run the python file to extract data the "/" could be recognized as a directory so I change it into "_". For example, like "fire2/conv", I train this layer using layer name "fire2/conv", and change the name into "fire2_conv" when extracting the data.  Does this affet?
Thank you very very much!!!

Best wishes,
Wei

在 2018年4月12日星期四 UTC+8下午2:44:13,Przemek D写道:

Przemek D

unread,
Apr 17, 2018, 6:44:54 AM4/17/18
to Caffe Users
However, if I used the test.prototxt file then the weights could be different each time.
Impossible to say without seeing the two files.

the "/" could be recognized as a directory so I change it into "_"
I can't see how a layer name could be confused with a directory, but I'm pretty sure this is the reason behind a behavior you observe. Caffe encounters a layer "fire2_conv" and looks for such a layer in the caffemodel, but since "fire2/conv" is something else, it skips it and doesn't load anything. Instead a layer is initialized with its default filler. We could make sure by taking a look at the test log.
Reply all
Reply to author
Forward
0 new messages