how to define caffe layers in python ?

5,285 views
Skip to first unread message

zhenlin tsai

unread,
Jul 3, 2015, 4:19:58 AM7/3/15
to caffe...@googlegroups.com
I read a lenet example on https://github.com/BVLC/caffe/blob/master/examples/01-learning-lenet.ipynb ; The author define caffe layers like this:

from caffe import layers as L
from caffe import params as P

def lenet(lmdb, batch_size):
    # our version of LeNet: a series of linear and simple nonlinear transformations
    n = caffe.NetSpec()
    n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdb,
                             transform_param=dict(scale=1./255), ntop=2)
    n.conv1 = L.Convolution(n.data, kernel_size=5, num_output=20, weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.conv2 = L.Convolution(n.pool1, kernel_size=5, num_output=50, weight_filler=dict(type='xavier'))
    n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.ip1 = L.InnerProduct(n.pool2, num_output=500, weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.ip1, in_place=True)
    n.ip2 = L.InnerProduct(n.relu1, num_output=10, weight_filler=dict(type='xavier'))
    n.loss = L.SoftmaxWithLoss(n.ip2, n.label)
    return n.to_proto()
    
with open('examples/mnist/lenet_auto_train.prototxt', 'w') as f:
    f.write(str(lenet('examples/mnist/mnist_train_lmdb', 64)))
    
with open('examples/mnist/lenet_auto_test.prototxt', 'w') as f:
    f.write(str(lenet('examples/mnist/mnist_test_lmdb', 100)))

While i try it, error occurs that  "cannot import name layers"  . Please tell me  why this happened. THanks

zhenlin tsai

unread,
Jul 3, 2015, 4:21:32 AM7/3/15
to caffe...@googlegroups.com
I import layers and ImportError occurs that:

>>> from caffe import layers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name layers


在 2015年7月3日星期五 UTC+8下午4:19:58,zhenlin tsai写道:

Ricardo Cortez

unread,
Jul 3, 2015, 12:59:51 PM7/3/15
to caffe...@googlegroups.com

zhenlin tsai,

Have you solved this issue?  I have the same problem. 

Philip H

unread,
Jul 3, 2015, 4:16:34 PM7/3/15
to caffe...@googlegroups.com
Are you using the most recent version of caffe? Did you make pycaffe?

Cheers,
Phil

Ricardo Cortez

unread,
Jul 3, 2015, 4:21:56 PM7/3/15
to caffe...@googlegroups.com

Hey Phil,

I'm using the last version, I did the make pycaffe and when running the tests everything is OK. 
Now, I tried to run the learning-lenet and the brewing-logreg examples but in both cases I got this error.

Cheers,

Ricardo

Philip H

unread,
Jul 3, 2015, 4:37:44 PM7/3/15
to caffe...@googlegroups.com
I don't think the tests actually test the Python wrappers. So it's probably a Python issue. If you open a console, run python and then import caffe, does that produce an error? You probably just need to tell Python where to find the caffe wrappers and a simple thing to try is to make sure that sys.path.insert(0, './python') actually adds the right path. You could replace './python' with '/the/full/path/to/caffe/python'. Does that help maybe?

Ricardo Cortez

unread,
Jul 3, 2015, 5:00:47 PM7/3/15
to caffe...@googlegroups.com
 It allows me to import caffe without any problem. I've checked and used the path in both ways and the error is still there. Do you have any other idea? I've been looking for further info but seems it isn't a common problem.


Philip H

unread,
Jul 3, 2015, 5:12:50 PM7/3/15
to caffe...@googlegroups.com
Do you have any other old version of caffe to which your PYTHONPATH might point to? 
Try this to find out where Python thinks caffe lives:
import caffe
print caffe.__file__

and make sure that export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH is in your ~/.bashrc

Ricardo Cortez

unread,
Jul 3, 2015, 6:16:00 PM7/3/15
to caffe...@googlegroups.com
Maybe it's pointing to a different path, I'm not sure though 

Using:  >> import sys

           >> print sys.path

I got:


['', '/usr/local/bin', '/Library/Python/2.7/site-packages/pip-7.0.3-py2.7.egg', '/usr/local/lib/wxPython-3.0.2.0/lib/python2.7/site-packages', '/usr/local/lib/wxPython-3.0.2.0/lib/python2.7/site-packages/wx-3.0-osx_carbon', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Users/RiccoCez/Library/Python/2.7/lib/python/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages', '/usr/local/lib/wxPython-3.0.2.0/lib/python2.7', '/Library/Python/2.7/site-packages/IPython/extensions', '/Users/RiccoCez/.ipython']


and including what you said >> import caffe

                                              >>  print caffe.__file__

I got :    

                      caffe/__init__.pyc

Philip H

unread,
Jul 4, 2015, 2:41:39 AM7/4/15
to caffe...@googlegroups.com
And what do you get when you go to the console and enter: echo $PYTHONPATH

Ricardo Cortez

unread,
Jul 4, 2015, 5:14:28 AM7/4/15
to caffe...@googlegroups.com

I got nothing, just got blank.

Every time I try setting  export PYTHONPATH=/usr/lib/python2.7 and reboot, it's reset to blank

zhenlin tsai

unread,
Jul 4, 2015, 9:22:51 AM7/4/15
to caffe...@googlegroups.com
I make pycaffe already, import caffe is ok  while there is no module named layers 

在 2015年7月4日星期六 UTC+8上午4:16:34,Philip H写道:

zhenlin tsai

unread,
Jul 4, 2015, 9:33:27 AM7/4/15
to caffe...@googlegroups.com
Hi, Phi. I try the input you suggested, result was:
>>> import caffe
>>> print caffe.__file__
**/caffe-master/python/caffe/__init__.pyc

what puzzles me is that I didn't find any modules or functions named 'layers' or 'params' in ./caffe-master/python/.... , 



在 2015年7月4日星期六 UTC+8上午5:12:50,Philip H写道:

zhenlin tsai

unread,
Jul 4, 2015, 9:40:47 AM7/4/15
to caffe...@googlegroups.com
NO , have you made some progress? Although i don't think it's essential for us to run caffe , I run test and other examples successfully. 
And  i didn't find functions named layers and params in path ./caffe-mater/python/caffe/...   , it's strange

在 2015年7月4日星期六 UTC+8上午12:59:51,Ricardo Cortez写道:

Philip H

unread,
Jul 5, 2015, 5:24:20 AM7/5/15
to caffe...@googlegroups.com
@Ricardo:
Did you do the following?
open ~/.bashrc with a reasonable text editor (for example vim)
put at the end export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
save and exit
source ~/.bashrc

That should permanently add the right path.

@Zhenlin:
Bear in mind that these are just Python wrappers. 

zhenlin tsai

unread,
Jul 5, 2015, 9:31:08 AM7/5/15
to caffe...@googlegroups.com
 yeah, I complete it by updating the pycaffe version. 

 really thank you!  

在 2015年7月5日星期日 UTC+8下午5:24:20,Philip H写道:

Ricardo Cortez

unread,
Jul 5, 2015, 10:08:58 AM7/5/15
to caffe...@googlegroups.com
Thank you Phil!! Everything is fixed now. 

Philip H

unread,
Jul 5, 2015, 10:13:59 AM7/5/15
to caffe...@googlegroups.com
Fantastic. Have fun guys!


Am Freitag, 3. Juli 2015 10:19:58 UTC+2 schrieb zhenlin tsai:

Jack Hunt

unread,
Jul 8, 2015, 10:05:24 AM7/8/15
to caffe...@googlegroups.com
Hi All,
I am experiencing an extremely similar issue after following the advice in this thread. So, I pulled the latest revision of caffe this morning, compiled and added to my PYTHONINCLUDE. However, I am receiving:-
Traceback (most recent call last):
  File "./test.py", line 12, in <module>
    define_network('', '')
  File "./test.py", line 9, in define_network
    net = caffe.NetSpec()
AttributeError: 'module' object has no attribute 'NetSpec'


However, including caffe in the shell works just fine. I have the same issue on Ubuntu and Mac OSX. If anybody has any further insight it would be wonderful.

Best,
Jack H

waspinator

unread,
Dec 8, 2015, 4:43:20 PM12/8/15
to Caffe Users
Is there a way to specify the learning rate?

layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  # learning rate and decay multipliers for the filters
  param { lr_mult: 1 decay_mult: 1 }
  # learning rate and decay multipliers for the biases
  param { lr_mult: 2 decay_mult: 0 }

and multiple bottom and top connections?

Nelson Enrique Yalta Soplin

unread,
Jan 11, 2016, 12:29:47 AM1/11/16
to Caffe Users

for adding some additional parameters, you can copy this code:
def conv_norm_act(bottom, act, nout, kh, kw=1,  stride=1, pad=0, lr_mult1=1,lr_mult2=2, sparse=15):
L
.Convolution(bottom, param=[dict(lr_mult=lr_mult1),dict(lr_mult=lr_mult2)],
        kernel_h
=kh, kernel_w=kw, stride=stride, num_output=nout, pad=pad,
        weight_filler
=dict(type='gaussian', std=0.1, sparse=sparse),
        bias_filler
=dict(type='constant', value=0))

BTW:
someone tried to deploy the network in python and input the name and the input data size. I dont know how to do it just by modifying the txt. but cannot add a name or dim layers for deploying.


Regards

Etienne Perot

unread,
Feb 18, 2016, 6:39:46 AM2/18/16
to Caffe Users
Hello there!

I'm trying the same thing here. I manage to do some small trainings with cifar10. I have just realized (sorry if it's obvious) that for Activations, BN or Dropout layer you can specify the top to be bottom by adding "in_place=True", I think that can save some memory. (by the way does anybody know if BN has to be followed by Scale Layer ? that seems redundant with the code inside the Batch_norm.cpp)
The only thing I'm having troubles with is :

- How would you specify a train_test model  with train & validation datasets defined with in the python net_spec? 
Reply all
Reply to author
Forward
0 new messages