Python: NameError: name 'top' is not defined

629 views
Skip to first unread message

tom...@gmx.de

unread,
Apr 21, 2016, 12:44:01 PM4/21/16
to Caffe Users
Hi,

I was using an own python layer. I used this simple code:

import caffe
import numpy as np

class DataLabelLoader(caffe.Layer):


   
def setup(self, bottom, top):
       
print self
       
print top
       
print bottom
   
       
# two tops: data and label
       
if len(top) != 2:     #the evil line 16
           
raise Exception("Need to define two tops: data and label.")

   
def reshape(self, bottom, top):
       
       
pass

   
def forward(self, bottom, top):
       
pass

   
def backward(self, top, propagate_down, bottom):
       
pass


and I got the following error:

Traceback (most recent call last):
 
File "/home/me/python/train/layers.py", line 7, in <module>
   
class DataLabelLoader(caffe.Layer):
 
File "/home/me/python/train/layers.py", line 16, in DataLabelLoader
   
if len(top) != 2:
NameError: name 'top' is not defined


When I uncomment the line I can see the variables are existing!

<layers.DataLabelLoader object at 0x7ff489d31db8>
<caffe._caffe.RawBlobVec object at 0x7ff489f59cd0>
<caffe._caffe.RawBlobVec object at 0x7ff489eb8250>


How is this possible??

Cheers,
Thomy

Manish Sapkota

unread,
Apr 21, 2016, 12:51:14 PM4/21/16
to Caffe Users
You might be looking for two bottoms?

Manish Sapkota

unread,
Apr 21, 2016, 12:57:14 PM4/21/16
to Caffe Users
Well I tried your code, it is working fine at my end.

tom...@gmx.de

unread,
Apr 22, 2016, 3:17:13 AM4/22/16
to Caffe Users
Thank you for feedback!
Any idea how to solve it? Is it possible my python interface is not installed properly? What caffe installation guide did you follow? I found several guides and each one is telling you different installation steps...

tom...@gmx.de

unread,
Apr 22, 2016, 8:36:42 AM4/22/16
to Caffe Users
I found the problem: wrong indentation. Python assumed the line was not a member of the method because of spaces instead of tabulators...
Reply all
Reply to author
Forward
0 new messages