Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing netspec to have empty top (ntop=0) #2741

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions python/caffe/net_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ def to_proto(self):
return to_proto(self)


class NoTop:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add two newlines for pep8itude.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm that pep8... done.

""" Special class for layers without a top """

def __init__(self, fn):
self.fn = fn

def to_proto(self):
return to_proto(self)


class Function(object):
"""A Function specifies a layer, its parameters, and its inputs (which
are Tops from other layers)."""
Expand All @@ -105,7 +115,10 @@ def __init__(self, type_name, inputs, params):
self.in_place = self.params.get('in_place', False)
if 'in_place' in self.params:
del self.params['in_place']
self.tops = tuple(Top(self, n) for n in range(self.ntop))
if self.ntop == 0:
self.tops = (NoTop(self),)
else:
self.tops = tuple(Top(self, n) for n in range(self.ntop))

def _get_name(self, top, names, autonames):
if top not in names:
Expand All @@ -129,7 +142,8 @@ def _to_proto(self, layers, names, autonames):
layer.top.extend(layer.bottom)
else:
for top in self.tops:
layer.top.append(self._get_name(top, names, autonames))
if not isinstance(top,NoTop):
layer.top.append(self._get_name(top, names, autonames))
layer.name = self._get_name(self.tops[0], names, autonames)

for k, v in six.iteritems(self.params):
Expand Down Expand Up @@ -180,7 +194,7 @@ class Layers(object):
def __getattr__(self, name):
def layer_fn(*args, **kwargs):
fn = Function(name, args, kwargs)
if fn.ntop == 1:
if fn.ntop <= 1:
return fn.tops[0]
else:
return fn.tops
Expand Down