tensorflow-keras-cpu-gpu/03. Popular image classification models.ipynb

257 lines
10 KiB
Plaintext

{
"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": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:black; border-collapse:collapse;\"><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\"><b>name</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\"><b>type</b></td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">/device:CPU:0</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">CPU</td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">/device:GPU:0</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">GPU</td></tr></table>"
],
"text/plain": [
"<ipy_table.ipy_table.IpyTable at 0x7fc8a18d95f8>"
]
},
"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": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:black; border-collapse:collapse;\"><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>device</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>resnet50</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>vgg19</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>inceptionv3</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>vgg16</b></td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>/device:GPU:0</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">59.2201</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">156.9875</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">15.0459</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">129.3934</td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>/device:CPU:0</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">605.1631</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">1857.0126</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">89.1597</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">1445.7997</td></tr></table>"
],
"text/plain": [
"<ipy_table.ipy_table.IpyTable at 0x7fc88813c860>"
]
},
"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
}