"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"
],
"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]."
]
},
{
"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))"
"# 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",
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)
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:
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
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:
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).
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",
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'
)
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)