take array in pycaffe `Net.set_mean()` instead of file path
authorEvan Shelhamer <shelhamer@imaginarynumber.net>
Sat, 2 Aug 2014 00:55:30 +0000 (17:55 -0700)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Wed, 6 Aug 2014 06:17:59 +0000 (23:17 -0700)
examples/filter_visualization.ipynb
examples/imagenet_classification.ipynb
examples/net_surgery.ipynb
python/caffe/classifier.py
python/caffe/detector.py
python/caffe/pycaffe.py
python/classify.py
python/detect.py

index ea99f06..bbc919b 100644 (file)
@@ -65,7 +65,7 @@
       "net.set_phase_test()\n",
       "net.set_mode_cpu()\n",
       "# input preprocessing: 'data' is the name of the input blob == net.inputs[0]\n",
-      "net.set_mean('data', caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')  # ImageNet mean\n",
+      "net.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'))  # ImageNet mean\n",
       "net.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]\n",
       "net.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB"
      ],
index 60e8bd0..fc08c74 100644 (file)
@@ -53,7 +53,7 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "Loading a network is easy. `caffe.Classifier` takes care of everything. Note the arguments for configuring input preprocessing: mean subtraction switched on by giving a mean file, input channel swapping takes care of mapping RGB into the reference ImageNet model's BGR order, and raw scaling multiplies the feature scale from the input [0,1] to the ImageNet model's [0,255]."
+      "Loading a network is easy. `caffe.Classifier` takes care of everything. Note the arguments for configuring input preprocessing: mean subtraction switched on by giving a mean array, input channel swapping takes care of mapping RGB into the reference ImageNet model's BGR order, and raw scaling multiplies the feature scale from the input [0,1] to the ImageNet model's [0,255]."
      ]
     },
     {
@@ -61,7 +61,7 @@
      "collapsed": false,
      "input": [
       "net = caffe.Classifier(MODEL_FILE, PRETRAINED,\n",
-      "                       mean_file=caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy',\n",
+      "                       mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'),\n",
       "                       channel_swap=(2,1,0),\n",
       "                       raw_scale=255,\n",
       "                       image_dims=(256, 256))"
index 31847b2..5f151e4 100644 (file)
       "# load input and configure preprocessing\n",
       "im = caffe.io.load_image('images/cat.jpg')\n",
       "plt.imshow(im)\n",
-      "net_full_conv.set_mean('data', '../python/caffe/imagenet/ilsvrc_2012_mean.npy')\n",
+      "net_full_conv.set_mean('data', np.load('../python/caffe/imagenet/ilsvrc_2012_mean.npy'))\n",
       "net_full_conv.set_channel_swap('data', (2,1,0))\n",
       "net_full_conv.set_raw_scale('data', 255.0)\n",
       "# make classification map by forward pass and show top prediction index per location\n",
index 48835ba..61e916c 100644 (file)
@@ -14,13 +14,13 @@ class Classifier(caffe.Net):
     by scaling, center cropping, or oversampling.
     """
     def __init__(self, model_file, pretrained_file, image_dims=None,
-                 gpu=False, mean_file=None, input_scale=None, raw_scale=None,
+                 gpu=False, mean=None, input_scale=None, raw_scale=None,
                  channel_swap=None):
         """
         Take
         image_dims: dimensions to scale input for cropping/sampling.
             Default is to scale to net input size for whole-image crop.
-        gpu, mean_file, input_scale, raw_scale, channel_swap: params for
+        gpu, mean, input_scale, raw_scale, channel_swap: params for
             preprocessing options.
         """
         caffe.Net.__init__(self, model_file, pretrained_file)
@@ -31,8 +31,8 @@ class Classifier(caffe.Net):
         else:
             self.set_mode_cpu()
 
-        if mean_file:
-            self.set_mean(self.inputs[0], mean_file)
+        if mean is not None:
+            self.set_mean(self.inputs[0], mean)
         if input_scale is not None:
             self.set_input_scale(self.inputs[0], input_scale)
         if raw_scale is not None:
index a9b06cd..ccb5a1f 100644 (file)
@@ -24,12 +24,12 @@ class Detector(caffe.Net):
     Detector extends Net for windowed detection by a list of crops or
     selective search proposals.
     """
-    def __init__(self, model_file, pretrained_file, gpu=False, mean_file=None,
+    def __init__(self, model_file, pretrained_file, gpu=False, mean=None,
                  input_scale=None, raw_scale=None, channel_swap=None,
                  context_pad=None):
         """
         Take
-        gpu, mean_file, input_scale, raw_scale, channel_swap: params for
+        gpu, mean, input_scale, raw_scale, channel_swap: params for
             preprocessing options.
         context_pad: amount of surrounding context to take s.t. a `context_pad`
             sized border of pixels in the network input image is context, as in
@@ -43,8 +43,8 @@ class Detector(caffe.Net):
         else:
             self.set_mode_cpu()
 
-        if mean_file:
-            self.set_mean(self.inputs[0], mean_file)
+        if mean is not None:
+            self.set_mean(self.inputs[0], mean)
         if input_scale is not None:
             self.set_input_scale(self.inputs[0], input_scale)
         if raw_scale is not None:
index 43648d0..9a230a7 100644 (file)
@@ -201,20 +201,19 @@ def _Net_forward_backward_all(self, blobs=None, diffs=None, **kwargs):
     return all_outs, all_diffs
 
 
-def _Net_set_mean(self, input_, mean_f, mode='elementwise'):
+def _Net_set_mean(self, input_, mean, mode='elementwise'):
     """
     Set the mean to subtract for data centering.
 
     Take
     input_: which input to assign this mean.
-    mean_f: path to mean .npy with ndarray (input dimensional or broadcastable)
+    mean: mean K x H x W ndarray (input dimensional or broadcastable)
     mode: elementwise = use the whole mean (and check dimensions)
           channel = channel constant (e.g. mean pixel instead of mean image)
     """
     if input_ not in self.inputs:
         raise Exception('Input not in {}'.format(self.inputs))
     in_shape = self.blobs[input_].data.shape
-    mean = np.load(mean_f)
     if mode == 'elementwise':
         if mean.shape[1:] != in_shape[2:]:
             # Resize mean (which requires H x W x K input).
index 417f8b5..d3d239c 100755 (executable)
@@ -78,7 +78,6 @@ def main(argv):
         default='2,1,0',
         help="Order to permute input channels. The default converts " +
              "RGB -> BGR since BGR is the Caffe default by way of OpenCV."
-
     )
     parser.add_argument(
         "--ext",
@@ -89,12 +88,18 @@ def main(argv):
     args = parser.parse_args()
 
     image_dims = [int(s) for s in args.images_dim.split(',')]
-    channel_swap = [int(s) for s in args.channel_swap.split(',')]
+
+    mean, channel_swap = None, None
+    if args.mean_file:
+        mean = np.load(args.mean_file)
+    if args.channel_swap:
+        channel_swap = [int(s) for s in args.channel_swap.split(',')]
 
     # Make classifier.
     classifier = caffe.Classifier(args.model_def, args.pretrained_model,
-            image_dims=image_dims, gpu=args.gpu, mean_file=args.mean_file,
-            input_scale=args.input_scale, channel_swap=channel_swap)
+            image_dims=image_dims, gpu=args.gpu, mean=mean,
+            input_scale=args.input_scale, raw_scale=args.raw_scale,
+            channel_swap=channel_swap)
 
     if args.gpu:
         print 'GPU mode'
index 4cfe082..77f4676 100755 (executable)
@@ -98,11 +98,15 @@ def main(argv):
     )
     args = parser.parse_args()
 
-    channel_swap = [int(s) for s in args.channel_swap.split(',')]
+    mean, channel_swap = None, None
+    if args.mean_file:
+        mean = np.load(args.mean_file)
+    if args.channel_swap:
+        channel_swap = [int(s) for s in args.channel_swap.split(',')]
 
     # Make detector.
     detector = caffe.Detector(args.model_def, args.pretrained_model,
-            gpu=args.gpu, mean_file=args.mean_file,
+            gpu=args.gpu, mean=mean,
             input_scale=args.input_scale, raw_scale=args.raw_scale,
             channel_swap=channel_swap,
             context_pad=args.context_pad)