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

Small improvements to pycaffe #733

Merged
merged 3 commits into from
Jul 28, 2014
Merged
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
26 changes: 12 additions & 14 deletions python/caffe/pycaffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -259,17 +259,16 @@ 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)
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 mean is not None:
caffe_in -= mean
if hasattr(self, 'mean'):
caffe_in -= self.mean.get(input_name, 0)
return caffe_in


Expand All @@ -280,15 +279,14 @@ 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:
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

Expand Down