{"metadata": {"language_info": {"version": "3.6.3", "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "name": "python", "mimetype": "text/x-python"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}, "nbformat_minor": 1, "cells": [{"outputs": [], "execution_count": null, "metadata": {"_uuid": "d383f29bf5e4b2a0bce7be3053f0c0a0f8c7939b", "_cell_guid": "efd6ca21-6b77-4fb1-8084-229440ae4925", "collapsed": true}, "cell_type": "code", "source": []}, {"outputs": [], "execution_count": null, "metadata": {"_uuid": "7025f7e95c54503253774eeec624b63a0c581331", "_cell_guid": "4aaa7e81-43f7-4273-b440-ca4564b8afcd", "scrolled": true}, "cell_type": "code", "source": ["%matplotlib inline\n", "import numpy as np\n", "import pandas as pd\n", "import datetime as dt\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.axes_grid1 import ImageGrid\n", "from os import listdir, makedirs\n", "from os.path import join, exists, expanduser\n", "from tqdm import tqdm\n", "from sklearn.metrics import log_loss, accuracy_score\n", "from keras.preprocessing import image\n", "from keras.applications.vgg16 import VGG16\n", "from keras.applications.resnet50 import ResNet50\n", "from keras.applications import xception\n", "from keras.applications import inception_v3\n", "from keras.applications.vgg16 import preprocess_input, decode_predictions\n", "from sklearn.linear_model import LogisticRegression"]}, {"outputs": [], "execution_count": null, "metadata": {"_uuid": "77e3bcc853c8479e2f5fecd0cbcb2ade13cacf3d", "_cell_guid": "01d03852-37c7-4ad8-8ee3-c8f372513828", "collapsed": true}, "cell_type": "code", "source": ["start = dt.datetime.now()"]}, {"outputs": [], "execution_count": null, "metadata": {"_uuid": "721283390964a33ce59833789e717aa3abb195bd", "_cell_guid": "8fc12733-5d40-4aaf-a65c-171ea68306ee"}, "cell_type": "code", "source": ["ls ../input/keras-pretrained-models/"]}, {"outputs": [], "execution_count": null, "metadata": {}, "cell_type": "code", "source": ["cache_dir = expanduser(join('~', '.keras'))\n", "if not exists(cache_dir):\n", "    makedirs(cache_dir)\n", "models_dir = join(cache_dir, 'models')\n", "if not exists(models_dir):\n", "    makedirs(models_dir)"]}, {"outputs": [], "execution_count": null, "metadata": {}, "cell_type": "code", "source": ["!cp ../input/keras-pretrained-models/*notop* ~/.keras/models/\n", "!cp ../input/keras-pretrained-models/imagenet_class_index.json ~/.keras/models/\n", "!cp ../input/keras-pretrained-models/resnet50* ~/.keras/models/"]}, {"outputs": [], "execution_count": null, "metadata": {}, "cell_type": "code", "source": ["!ls ~/.keras/models"]}, {"outputs": [], "execution_count": null, "metadata": {}, "cell_type": "code", "source": ["\n", "\n", "!ls ../input/dog-breed-identification\n", "\n"]}, {"outputs": [], "execution_count": null, "metadata": {}, "cell_type": "code", "source": ["INPUT_SIZE = 224\n", "NUM_CLASSES = 16\n", "SEED = 1987\n", "data_dir = '../input/dog-breed-identification'\n", "labels = pd.read_csv(join(data_dir, 'labels.csv'))\n", "sample_submission = pd.read_csv(join(data_dir, 'sample_submission.csv'))\n", "print(len(listdir(join(data_dir, 'train'))), len(labels))\n", "print(len(listdir(join(data_dir, 'test'))), len(sample_submission))"]}, {"outputs": [], "execution_count": null, "metadata": {"collapsed": true}, "cell_type": "code", "source": ["def read_img(img_id, train_or_test, size):\n", "    \"\"\"Read and resize image.\n", "    # Arguments\n", "        img_id: string\n", "        train_or_test: string 'train' or 'test'.\n", "        size: resize the original image.\n", "    # Returns\n", "        Image as numpy array.\n", "    \"\"\"\n", "    img = image.load_img(join(data_dir, train_or_test, '%s.jpg' % img_id), target_size=size)\n", "    img = image.img_to_array(img)\n", "    return img"]}, {"outputs": [], "execution_count": null, "metadata": {}, "cell_type": "code", "source": ["model = ResNet50(weights='imagenet')\n", "j = int(np.sqrt(NUM_CLASSES))\n", "i = int(np.ceil(1. * NUM_CLASSES / j))\n", "fig = plt.figure(1, figsize=(16, 16))\n", "grid = ImageGrid(fig, 111, nrows_ncols=(i, j), axes_pad=0.05)\n", "for i, (img_id, breed) in enumerate(labels.loc[labels['rank'] == 1, ['id', 'breed']].values):\n", "    ax = grid[i]\n", "    img = read_img(img_id, 'train', (224, 224))\n", "    ax.imshow(img / 255.)\n", "    x = preprocess_input(np.expand_dims(img.copy(), axis=0))\n", "    preds = model.predict(x)\n", "    _, imagenet_class_name, prob = decode_predictions(preds, top=1)[0][0]\n", "   # ax.text(10, 180, ': %s (%.2f)' % (imagenet_class_name , prob), color='w', backgroundcolor='k', alpha=0.8)\n", "    ax.text(10, 200, 'Name: %s' % breed, color='k', backgroundcolor='w', alpha=0.8)\n", "    ax.axis('off')\n", "plt.show()"]}], "nbformat": 4}