preprocess single inputs instead of lists
authorEvan Shelhamer <shelhamer@imaginarynumber.net>
Tue, 20 May 2014 18:41:42 +0000 (11:41 -0700)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Tue, 20 May 2014 18:42:41 +0000 (11:42 -0700)
For compositionality and expectations.

python/caffe/classifier.py
python/caffe/detector.py
python/caffe/pycaffe.py

index d1875c2..f347be4 100644 (file)
@@ -73,7 +73,8 @@ class Classifier(caffe.Net):
             inputs = inputs[:, crop[0]:crop[2], crop[1]:crop[3], :]
 
         # Classify
-        caffe_in = self.preprocess(self.inputs[0], inputs)
+        caffe_in = np.asarray([self.preprocess(self.inputs[0], in_)
+                    for in_ in inputs])
         out = self.forward_all(**{self.inputs[0]: caffe_in})
         predictions = out[self.outputs[0]].squeeze(axis=(2,3))
 
index db9b1aa..5a30dab 100644 (file)
@@ -72,7 +72,8 @@ class Detector(caffe.Net):
                                            window[1]:window[3]])
 
         # Run through the net (warping windows to input dimensions).
-        caffe_in = self.preprocess(self.inputs[0], window_inputs)
+        caffe_in = np.asarray([self.preprocess(self.inputs[0], window_in)
+                    for window_in in window_inputs])
         out = self.forward_all(**{self.inputs[0]: caffe_in})
         predictions = out[self.outputs[0]].squeeze(axis=(2,3))
 
index 8a9e1aa..537e737 100644 (file)
@@ -238,9 +238,9 @@ def _Net_set_channel_swap(self, input_, order):
     self.channel_swap[input_] = order
 
 
-def _Net_preprocess(self, input_name, inputs):
+def _Net_preprocess(self, input_name, input_):
     """
-    Format inputs for Caffe:
+    Format input for Caffe:
     - convert to single
     - resize to input dimensions (preserving number of channels)
     - scale feature
@@ -249,51 +249,45 @@ def _Net_preprocess(self, input_name, inputs):
     - transpose dimensions to K x H x W
 
     Take
-    input_name: name of input blob
-    inputs: list of (H' x W' x K) ndarray
+    input_name: name of input blob to preprocess for
+    input_: (H' x W' x K) ndarray
 
     Give
     caffe_inputs: (K x H x W) ndarray
     """
-    caffe_inputs = []
-    for in_ in inputs:
-        caffe_in = in_.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:
-            caffe_in *= input_scale
-        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
-        caffe_inputs.append(caffe_in)
-    return np.asarray(caffe_inputs)
-
-
-def _Net_deprocess(self, input_name, inputs):
+    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:
+        caffe_in *= input_scale
+    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
+    return caffe_in
+
+
+def _Net_deprocess(self, input_name, input_):
     """
     Invert Caffe formatting; see Net.preprocess().
     """
-    decaf_inputs = []
-    for in_ in inputs:
-        decaf_in = in_.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
-        decaf_in = decaf_in.transpose((1,2,0))
-        if channel_order:
-            decaf_in = decaf_in[:, :, channel_order[::-1]]
-        if input_scale:
-            decaf_in /= input_scale
-        decaf_inputs.append(decaf_in)
-    return np.asarray(decaf_inputs)
+    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
+    decaf_in = decaf_in.transpose((1,2,0))
+    if channel_order:
+        decaf_in = decaf_in[:, :, channel_order[::-1]]
+    if input_scale:
+        decaf_in /= input_scale
+    return decaf_in
 
 
 def _Net_set_input_arrays(self, data, labels):