From eed766faf1c73bfcda15263ea4ef7a0b546a89a8 Mon Sep 17 00:00:00 2001 From: Jonathan L Long Date: Fri, 18 Jul 2014 16:31:16 -0700 Subject: [PATCH 1/3] pycaffe: allow unspecified mean. Fixes #671. --- python/caffe/pycaffe.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/python/caffe/pycaffe.py b/python/caffe/pycaffe.py index 5c1512cd8b9..a2fb16ba06f 100644 --- a/python/caffe/pycaffe.py +++ b/python/caffe/pycaffe.py @@ -259,7 +259,6 @@ def _Net_preprocess(self, input_name, input_): caffe_in = input_.astype(np.float32) input_scale = self.input_scale.get(input_name) channel_order = self.channel_swap.get(input_name) - mean = self.mean.get(input_name) in_size = self.blobs[input_name].data.shape[2:] if caffe_in.shape[:2] != in_size: caffe_in = caffe.io.resize_image(caffe_in, in_size) @@ -268,8 +267,8 @@ def _Net_preprocess(self, input_name, input_): if channel_order: caffe_in = caffe_in[:, :, channel_order] caffe_in = caffe_in.transpose((2, 0, 1)) - if mean is not None: - caffe_in -= mean + if hasattr(self, 'mean'): + caffe_in -= self.mean.get(input_name, 0) return caffe_in @@ -280,9 +279,8 @@ def _Net_deprocess(self, input_name, input_): decaf_in = input_.copy().squeeze() input_scale = self.input_scale.get(input_name) channel_order = self.channel_swap.get(input_name) - mean = self.mean.get(input_name) - if mean is not None: - decaf_in += mean + if hasattr(self, 'mean'): + decaf_in += self.mean.get(input_name, 0) decaf_in = decaf_in.transpose((1,2,0)) if channel_order: channel_order_inverse = [channel_order.index(i) From 44d6abdb2a041a0192696fc520408ade00254a37 Mon Sep 17 00:00:00 2001 From: Jonathan L Long Date: Fri, 18 Jul 2014 16:32:12 -0700 Subject: [PATCH 2/3] pycaffe: reorder exceptions It doesn't make sense to complain about input not being batch-sized if it isn't even 4-d in the first place. --- python/caffe/pycaffe.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/caffe/pycaffe.py b/python/caffe/pycaffe.py index a2fb16ba06f..dde838d6600 100644 --- a/python/caffe/pycaffe.py +++ b/python/caffe/pycaffe.py @@ -57,10 +57,10 @@ def _Net_forward(self, blobs=None, **kwargs): # Set input according to defined shapes and make arrays single and # C-contiguous as Caffe expects. for in_, blob in kwargs.iteritems(): - if blob.shape[0] != self.blobs[in_].num: - raise Exception('Input is not batch sized') if blob.ndim != 4: raise Exception('{} blob is not 4-d'.format(in_)) + if blob.shape[0] != self.blobs[in_].num: + raise Exception('Input is not batch sized') self.blobs[in_].data[...] = blob self._forward() @@ -91,10 +91,10 @@ def _Net_backward(self, diffs=None, **kwargs): # Set top diffs according to defined shapes and make arrays single and # C-contiguous as Caffe expects. for top, diff in kwargs.iteritems(): - if diff.shape[0] != self.blobs[top].num: - raise Exception('Diff is not batch sized') if diff.ndim != 4: raise Exception('{} diff is not 4-d'.format(top)) + if diff.shape[0] != self.blobs[top].num: + raise Exception('Diff is not batch sized') self.blobs[top].diff[...] = diff self._backward() From 066f298d8b3dcea58b85fa478f226a1c0b169fe1 Mon Sep 17 00:00:00 2001 From: Jonathan L Long Date: Fri, 18 Jul 2014 16:45:17 -0700 Subject: [PATCH 3/3] pycaffe: test channel_order and input_scale against None An input_scale of zero should produce warnings/errors/infinities rather than being the same as an input_scale of one. A channel_order of () or 0 should not be equivalent to the default ordering. --- python/caffe/pycaffe.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/caffe/pycaffe.py b/python/caffe/pycaffe.py index dde838d6600..5f04a95246b 100644 --- a/python/caffe/pycaffe.py +++ b/python/caffe/pycaffe.py @@ -262,9 +262,9 @@ def _Net_preprocess(self, input_name, input_): in_size = self.blobs[input_name].data.shape[2:] if caffe_in.shape[:2] != in_size: caffe_in = caffe.io.resize_image(caffe_in, in_size) - if input_scale: + if input_scale is not None: caffe_in *= input_scale - if channel_order: + if channel_order is not None: caffe_in = caffe_in[:, :, channel_order] caffe_in = caffe_in.transpose((2, 0, 1)) if hasattr(self, 'mean'): @@ -282,11 +282,11 @@ def _Net_deprocess(self, input_name, input_): if hasattr(self, 'mean'): decaf_in += self.mean.get(input_name, 0) decaf_in = decaf_in.transpose((1,2,0)) - if channel_order: + if channel_order is not None: channel_order_inverse = [channel_order.index(i) for i in range(decaf_in.shape[2])] decaf_in = decaf_in[:, :, channel_order_inverse] - if input_scale: + if input_scale is not None: decaf_in /= input_scale return decaf_in