From b29d271b8cd679588618d502add8a4eae2beb853 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Tue, 21 Jun 2016 16:22:20 -0700 Subject: [PATCH 1/2] add layer_dict to the python interface --- python/caffe/pycaffe.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/python/caffe/pycaffe.py b/python/caffe/pycaffe.py index ca6d050e2bd..4f84605ba00 100644 --- a/python/caffe/pycaffe.py +++ b/python/caffe/pycaffe.py @@ -43,6 +43,16 @@ def _Net_blob_loss_weights(self): self._blob_loss_weights)) return self._blob_loss_weights_dict +@property +def _Net_layer_dict(self): + """ + An OrderedDict (bottom to top, i.e., input to output) of network + layers indexed by name + """ + if not hasattr(self, '_layer_dict'): + self._layer_dict = OrderedDict(zip(self._layer_names, self.layers)) + return self._layer_dict + @property def _Net_params(self): @@ -311,6 +321,7 @@ def __getitem__(self, name): # Attach methods to Net. Net.blobs = _Net_blobs Net.blob_loss_weights = _Net_blob_loss_weights +Net.layer_dict = _Net_layer_dict Net.params = _Net_params Net.forward = _Net_forward Net.backward = _Net_backward From 5417f106c14c782865e2a5484020b8e45a8b2b80 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Tue, 21 Jun 2016 16:39:30 -0700 Subject: [PATCH 2/2] add tests for pycaffe's layer_dict --- python/caffe/test/test_net.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/caffe/test/test_net.py b/python/caffe/test/test_net.py index 4cacfcd05bb..546bd5faa0b 100644 --- a/python/caffe/test/test_net.py +++ b/python/caffe/test/test_net.py @@ -59,6 +59,13 @@ def test_memory(self): for bl in blobs: total += bl.data.sum() + bl.diff.sum() + def test_layer_dict(self): + layer_dict = self.net.layer_dict + self.assertEqual(list(layer_dict.keys()), list(self.net._layer_names)) + for i, name in enumerate(self.net._layer_names): + self.assertEqual(layer_dict[name].type, + self.net.layers[i].type) + def test_forward_backward(self): self.net.forward() self.net.backward()