| { |
| "cells": [ |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "import numpy as np\n", |
| "import matplotlib.pyplot as plt\n", |
| "%matplotlib inline\n", |
| "\n", |
| "# Make sure that caffe is on the python path:\n", |
| "caffe_root = './' # this file is expected to be in {caffe_root}/examples\n", |
| "import sys\n", |
| "sys.path.insert(0, caffe_root + 'python')\n", |
| "\n", |
| "import caffe\n", |
| "\n", |
| "# configure plotting\n", |
| "plt.rcParams['figure.figsize'] = (10, 10)\n", |
| "plt.rcParams['image.interpolation'] = 'nearest'\n", |
| "plt.rcParams['image.cmap'] = 'gray'" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# Load the net, list its data and params, and filter an example image.\n", |
| "caffe.set_mode_gpu()\n", |
| "net = caffe.Net('test_templates/conv.prototxt', caffe.TEST)\n", |
| "print(\"blobs {}\\nparams {}\".format(net.blobs.keys(), net.params.keys()))\n", |
| "\n", |
| "# load image and prepare as a single input batch for Caffe\n", |
| "im = np.array(caffe.io.load_image('test_templates/cat_gray.jpg', color=False)).squeeze()\n", |
| "plt.title(\"original image\")\n", |
| "plt.imshow(im)\n", |
| "plt.axis('off')\n", |
| "\n", |
| "im_input = im[np.newaxis, np.newaxis, :, :]\n", |
| "net.blobs['data'].reshape(*im_input.shape)\n", |
| "net.blobs['data'].data[...] = im_input" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# helper show filter outputs\n", |
| "def show_filters(net):\n", |
| " net.forward()\n", |
| " plt.figure()\n", |
| " filt_min, filt_max = net.blobs['conv'].data.min(), net.blobs['conv'].data.max()\n", |
| " for i in range(3):\n", |
| " plt.subplot(1,4,i+2)\n", |
| " plt.title(\"filter #{} output\".format(i))\n", |
| " plt.imshow(net.blobs['conv'].data[0, i], vmin=filt_min, vmax=filt_max)\n", |
| " plt.tight_layout()\n", |
| " plt.axis('off')\n", |
| "\n", |
| "# filter the image with initial \n", |
| "show_filters(net)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "metadata": {}, |
| "outputs": [], |
| "source": [] |
| } |
| ], |
| "metadata": { |
| "kernelspec": { |
| "display_name": "Python 2", |
| "language": "python", |
| "name": "KERNEL_NAME" |
| }, |
| "language_info": { |
| "codemirror_mode": { |
| "name": "ipython", |
| "version": 2 |
| }, |
| "file_extension": ".py", |
| "mimetype": "text/x-python", |
| "name": "python", |
| "nbconvert_exporter": "python", |
| "pygments_lexer": "ipython2", |
| "version": "2.7.13" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 1 |
| } |