|
58 | 58 | "# The caffe module needs to be on the Python path;\n", |
59 | 59 | "# we'll add it here explicitly.\n", |
60 | 60 | "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", |
62 | 62 | "sys.path.insert(0, caffe_root + 'python')\n", |
63 | 63 | "\n", |
64 | 64 | "import caffe\n", |
|
88 | 88 | } |
89 | 89 | ], |
90 | 90 | "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 | + |
97 | 96 | ] |
98 | 97 | }, |
99 | 98 | { |
|
116 | 115 | "caffe.set_mode_cpu()\n", |
117 | 116 | "\n", |
118 | 117 | "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 | + |
120 | 119 | "\n", |
121 | 120 | "net = caffe.Net(model_def, # defines the structure of the model\n", |
122 | 121 | " model_weights, # contains the trained weights\n", |
|
153 | 152 | "# load the mean ImageNet image (as distributed with Caffe) for subtraction\n", |
154 | 153 | "mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')\n", |
155 | 154 | "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)", |
157 | 156 | "\n", |
158 | 157 | "# create transformer for the input called 'data'\n", |
159 | 158 | "transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})\n", |
|
260 | 259 | "\n", |
261 | 260 | "output_prob = output['prob'][0] # the output probability vector for the first image in the batch\n", |
262 | 261 | "\n", |
263 | | - "print 'predicted class is:', output_prob.argmax()" |
| 262 | + "print ('predicted class is:', output_prob.argmax())" |
264 | 263 | ] |
265 | 264 | }, |
266 | 265 | { |
|
287 | 286 | ], |
288 | 287 | "source": [ |
289 | 288 | "# 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", |
291 | 291 | "if not os.path.exists(labels_file):\n", |
292 | 292 | " !../data/ilsvrc12/get_ilsvrc_aux.sh\n", |
293 | 293 | " \n", |
294 | 294 | "labels = np.loadtxt(labels_file, str, delimiter='\\t')\n", |
295 | 295 | "\n", |
296 | | - "print 'output label:', labels[output_prob.argmax()]" |
| 296 | + "print ('output label:', labels[output_prob.argmax()])" |
297 | 297 | ] |
298 | 298 | }, |
299 | 299 | { |
|
336 | 336 | "# sort top five predictions from softmax output\n", |
337 | 337 | "top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items\n", |
338 | 338 | "\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])))" |
341 | 341 | ] |
342 | 342 | }, |
343 | 343 | { |
|
372 | 372 | } |
373 | 373 | ], |
374 | 374 | "source": [ |
375 | | - "%timeit net.forward()" |
| 375 | + "# %timeit net.forward()" |
376 | 376 | ] |
377 | 377 | }, |
378 | 378 | { |
|
398 | 398 | } |
399 | 399 | ], |
400 | 400 | "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()" |
405 | 405 | ] |
406 | 406 | }, |
407 | 407 | { |
|
457 | 457 | ], |
458 | 458 | "source": [ |
459 | 459 | "# 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))" |
462 | 462 | ] |
463 | 463 | }, |
464 | 464 | { |
|
493 | 493 | } |
494 | 494 | ], |
495 | 495 | "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))" |
498 | 498 | ] |
499 | 499 | }, |
500 | 500 | { |
|
727 | 727 | "outputs": [], |
728 | 728 | "source": [ |
729 | 729 | "# download an image\n", |
730 | | - "my_image_url = \"...\" # paste your URL here\n", |
| 730 | + "# my_image_url = \"...\" # paste your URL here\n", |
731 | 731 | "# 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", |
733 | 733 | "!wget -O image.jpg $my_image_url\n", |
734 | 734 | "\n", |
735 | 735 | "# transform it and copy it into the net\n", |
|
747 | 747 | "\n", |
748 | 748 | "plt.imshow(image)\n", |
749 | 749 | "\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])))" |
752 | 752 | ] |
753 | 753 | } |
754 | 754 | ], |
|
0 commit comments