Skip to content

Commit 48def55

Browse files
authored
Update 00-classification.ipynb
1 parent 4a5ec9b commit 48def55

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

examples/00-classification.ipynb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"# The caffe module needs to be on the Python path;\n",
5959
"# we'll add it here explicitly.\n",
6060
"import sys\n",
61-
"caffe_root = '../' # this file should be run from {caffe_root}/examples (otherwise change this line)\n",
61+
"caffe_root = '/opt/caffe/' # this file should be run from {caffe_root}/examples (otherwise change this line)\n",
6262
"sys.path.insert(0, caffe_root + 'python')\n",
6363
"\n",
6464
"import caffe\n",
@@ -88,12 +88,11 @@
8888
}
8989
],
9090
"source": [
91-
"import os\n",
92-
"if os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):\n",
93-
" print 'CaffeNet found.'\n",
94-
"else:\n",
95-
" print 'Downloading pre-trained CaffeNet model...'\n",
96-
" !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet"
91+
"model_weights_url = 'http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel' \n",
92+
"!wget -O bvlc_reference_caffenet.caffemodel $model_weights_url \n",
93+
"model_weights = './bvlc_reference_caffenet.caffemodel' \n"
94+
95+
9796
]
9897
},
9998
{
@@ -116,7 +115,7 @@
116115
"caffe.set_mode_cpu()\n",
117116
"\n",
118117
"model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'\n",
119-
"model_weights = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'\n",
118+
120119
"\n",
121120
"net = caffe.Net(model_def, # defines the structure of the model\n",
122121
" model_weights, # contains the trained weights\n",
@@ -153,7 +152,7 @@
153152
"# load the mean ImageNet image (as distributed with Caffe) for subtraction\n",
154153
"mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')\n",
155154
"mu = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values\n",
156-
"print 'mean-subtracted values:', zip('BGR', mu)\n",
155+
"print ('mean-subtracted values:', list(zip('BGR', mu))\n)",
157156
"\n",
158157
"# create transformer for the input called 'data'\n",
159158
"transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})\n",
@@ -260,7 +259,7 @@
260259
"\n",
261260
"output_prob = output['prob'][0] # the output probability vector for the first image in the batch\n",
262261
"\n",
263-
"print 'predicted class is:', output_prob.argmax()"
262+
"print ('predicted class is:', output_prob.argmax())"
264263
]
265264
},
266265
{
@@ -287,13 +286,14 @@
287286
],
288287
"source": [
289288
"# load ImageNet labels\n",
290-
"labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'\n",
289+
"import os \n",
290+
"labels_file = '../' + 'data/ilsvrc12/synset_words.txt'\n",
291291
"if not os.path.exists(labels_file):\n",
292292
" !../data/ilsvrc12/get_ilsvrc_aux.sh\n",
293293
" \n",
294294
"labels = np.loadtxt(labels_file, str, delimiter='\\t')\n",
295295
"\n",
296-
"print 'output label:', labels[output_prob.argmax()]"
296+
"print ('output label:', labels[output_prob.argmax()])"
297297
]
298298
},
299299
{
@@ -336,8 +336,8 @@
336336
"# sort top five predictions from softmax output\n",
337337
"top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items\n",
338338
"\n",
339-
"print 'probabilities and labels:'\n",
340-
"zip(output_prob[top_inds], labels[top_inds])"
339+
"print ('probabilities and labels:') \n",
340+
"print( list(zip(output_prob[top_inds], labels[top_inds])))"
341341
]
342342
},
343343
{
@@ -372,7 +372,7 @@
372372
}
373373
],
374374
"source": [
375-
"%timeit net.forward()"
375+
"# %timeit net.forward()"
376376
]
377377
},
378378
{
@@ -398,10 +398,10 @@
398398
}
399399
],
400400
"source": [
401-
"caffe.set_device(0) # if we have multiple GPUs, pick the first one\n",
402-
"caffe.set_mode_gpu()\n",
403-
"net.forward() # run once before timing to set up memory\n",
404-
"%timeit net.forward()"
401+
"# caffe.set_device(0) # if we have multiple GPUs, pick the first one\n",
402+
"# caffe.set_mode_gpu()\n",
403+
"# net.forward() # run once before timing to set up memory\n",
404+
"# %timeit net.forward()"
405405
]
406406
},
407407
{
@@ -457,8 +457,8 @@
457457
],
458458
"source": [
459459
"# for each layer, show the output shape\n",
460-
"for layer_name, blob in net.blobs.iteritems():\n",
461-
" print layer_name + '\\t' + str(blob.data.shape)"
460+
"for layer_name, blob in net.blobs.items():\n",
461+
" print (layer_name + '\\t' + str(blob.data.shape))"
462462
]
463463
},
464464
{
@@ -493,8 +493,8 @@
493493
}
494494
],
495495
"source": [
496-
"for layer_name, param in net.params.iteritems():\n",
497-
" print layer_name + '\\t' + str(param[0].data.shape), str(param[1].data.shape)"
496+
"for layer_name, param in net.params.items():\n",
497+
" print (layer_name + '\\t' + str(param[0].data.shape), str(param[1].data.shape))"
498498
]
499499
},
500500
{
@@ -727,9 +727,9 @@
727727
"outputs": [],
728728
"source": [
729729
"# download an image\n",
730-
"my_image_url = \"...\" # paste your URL here\n",
730+
"# my_image_url = \"...\" # paste your URL here\n",
731731
"# for example:\n",
732-
"# my_image_url = \"https://upload.wikimedia.org/wikipedia/commons/b/be/Orang_Utan%2C_Semenggok_Forest_Reserve%2C_Sarawak%2C_Borneo%2C_Malaysia.JPG\"\n",
732+
"my_image_url = \"https://upload.wikimedia.org/wikipedia/commons/b/be/Orang_Utan%2C_Semenggok_Forest_Reserve%2C_Sarawak%2C_Borneo%2C_Malaysia.JPG\"\n",
733733
"!wget -O image.jpg $my_image_url\n",
734734
"\n",
735735
"# transform it and copy it into the net\n",
@@ -747,8 +747,8 @@
747747
"\n",
748748
"plt.imshow(image)\n",
749749
"\n",
750-
"print 'probabilities and labels:'\n",
751-
"zip(output_prob[top_inds], labels[top_inds])"
750+
"print ('probabilities and labels:') \n",
751+
"print (list(zip(output_prob[top_inds], labels[top_inds])))"
752752
]
753753
}
754754
],

0 commit comments

Comments
 (0)