{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "import os.path\n", "import pickle\n", "\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import classification_report\n", "\n", "from jupyter_progressbar import ProgressBar\n", "import numpy as np\n", "\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Flatten, GlobalAveragePooling2D\n", "from keras.optimizers import SGD\n", "from keras import backend as K\n", "from keras.applications.inception_v3 import preprocess_input as preprocess, InceptionV3\n", "\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "matplotlib.rcParams['figure.figsize'] = (20, 10)\n", "matplotlib.rcParams['font.size'] = 24\n", "matplotlib.rcParams['lines.linewidth'] = 5\n", "matplotlib.rcParams['lines.markersize'] = 20\n", "\n", "import tensorflow as tf\n", "from tensorflow.python.client import device_lib\n", "\n", "import time\n", "from matplotlib import pyplot\n", "from collections import defaultdict\n", "from ipy_table import make_table, set_row_style, set_column_style\n", "from jupyter_progressbar import ProgressBar\n", "\n", "# Disable progressbar VGG-batches\n", "ProgressBar = lambda x: x" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "
nametype
/device:CPU:0CPU
/device:GPU:0GPU
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "devices = device_lib.list_local_devices()\n", "\n", "make_table([[\"name\", \"type\"]] + [\n", " [device.name, device.device_type]\n", " for device in devices\n", "])\n", "set_row_style(0, bold=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "with open('creepycrawly_and_cats.p3', 'rb') as f:\n", " X, y = pickle.load(f)\n", "classes = ['creepy crawly', 'cat']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "import numpy\n", "from skimage.transform import resize\n", "\n", "def resize_for_model(X, model):\n", " target = tuple([x.value for x in model.input.get_shape()][1:3])\n", " if target[0] is None or target[1] is None:\n", " return X\n", " result = numpy.zeros((X.shape[0], target[0], target[1], X.shape[3]), dtype=X.dtype)\n", " \n", " for i in range(X.shape[0]):\n", " result[i] = resize(X[i], target + (X.shape[3], ))\n", " return result" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "results = defaultdict(dict)\n", "for device in devices:\n", " from keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions\n", " \n", " with tf.device(device.name):\n", " model = InceptionV3(weights='imagenet')\n", " X_ = preprocess_input(resize_for_model(X, model))\n", " t0 = time.time()\n", " preds = model.predict(X_, verbose=0)\n", " t1 = time.time()\n", " \n", " results[device.name]['inceptionv3'] = t1 - t0\n", " K.clear_session()\n", " \n", " from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions\n", " with tf.device(device.name):\n", " model = ResNet50(weights='imagenet')\n", " X_ = preprocess_input(resize_for_model(X, model))\n", " t0 = time.time()\n", " preds = model.predict(X_, verbose=0)\n", " t1 = time.time()\n", " \n", " results[device.name]['resnet50'] = t1 - t0\n", " K.clear_session()\n", "\n", " vgg_batch_size = 2\n", " \n", " from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions\n", " with tf.device(device.name):\n", " model = VGG16(weights='imagenet', include_top=True)\n", " X_ = preprocess_input(resize_for_model(X, model))\n", " t0 = time.time()\n", " for start, end in zip(\n", " range(0, len(X), vgg_batch_size),\n", " ProgressBar(range(vgg_batch_size, len(X), vgg_batch_size))\n", " ):\n", " model.predict(X_[start:end], verbose=0)\n", " t1 = time.time()\n", " \n", " results[device.name]['vgg16'] = t1 - t0\n", " K.clear_session()\n", " \n", " from keras.applications.vgg19 import VGG19, preprocess_input, decode_predictions\n", " \n", " with tf.device(device.name):\n", " model = VGG19(weights='imagenet', include_top=True)\n", " X_ = preprocess_input(resize_for_model(X, model))\n", " t0 = time.time()\n", " for start, end in zip(\n", " range(0, len(X), vgg_batch_size),\n", " ProgressBar(range(vgg_batch_size, len(X), vgg_batch_size))\n", " ):\n", " model.predict(X_[start:end], verbose=0)\n", " t1 = time.time()\n", " \n", " results[device.name]['vgg19'] = t1 - t0\n", " K.clear_session()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "
deviceresnet50vgg19inceptionv3vgg16
/device:GPU:059.2201156.987515.0459129.3934
/device:CPU:0605.16311857.012689.15971445.7997
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model_names = list(next(iter(results.values())).keys())\n", "\n", "make_table([[\"device\"] + model_names] + [\n", " [device] + [result[model_name] for model_name in model_names]\n", " for device, result in results.items()\n", "])\n", "set_column_style(0, bold=True, align='right')\n", "set_row_style(0, bold=True, align='right')" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }