{"metadata": {"language_info": {"version": "3.6.4", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "name": "python", "file_extension": ".py", "nbconvert_exporter": "python"}, "kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}}, "cells": [{"metadata": {"_uuid": "a88636aadd35f3636742b0c6df397c791e539b33", "collapsed": true, "_cell_guid": "eeaf2fb3-c5c2-4966-ae80-4d77e0179141"}, "outputs": [], "execution_count": null, "source": ["import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt"], "cell_type": "code"}, {"metadata": {"_uuid": "a54fe7e39734ad7cc8c21a975101ac8ec4eb35c3", "_cell_guid": "bb242f92-6e23-42c7-a407-02e3c7b9ba01"}, "outputs": [], "execution_count": null, "source": ["labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\n", "labels.head()"], "cell_type": "code"}, {"metadata": {"_uuid": "e1dd0ce0d89f0e2907449d5e174da42f749477af", "collapsed": true, "_cell_guid": "550566e7-1875-4883-b51a-dceb7c49b81b"}, "outputs": [], "execution_count": null, "source": ["top_breeds = sorted(list(labels['breed'].value_counts().head(16).index))\n", "labels = labels[labels['breed'].isin(top_breeds)]"], "cell_type": "code"}, {"metadata": {"_uuid": "c7d39136683398372c770e43c1aa10409d277cf0", "_cell_guid": "3f41411f-05bf-4640-bb20-c6f1928769ed"}, "outputs": [], "execution_count": null, "source": ["len(labels.breed.value_counts())"], "cell_type": "code"}, {"metadata": {"_uuid": "e39192de49065a2fce498804bb4f5b9e3908efc9", "_cell_guid": "b284932d-663d-4845-9065-9f9273b9fd91"}, "outputs": [], "execution_count": null, "source": ["np.average(labels.breed.value_counts())"], "cell_type": "code"}, {"metadata": {"_uuid": "71c82735d04029d3651206c1576994fd6ab44f78", "_cell_guid": "862c795a-3a5c-4560-97cd-21f79cf94fe8"}, "outputs": [], "execution_count": null, "source": ["labels.breed.value_counts().plot(kind='bar')"], "cell_type": "code"}, {"metadata": {"_uuid": "0e6bb328a8fdd584a2b534c79ce590a6ea54c817", "_cell_guid": "de93d88b-684e-4589-a7d5-bf7e73f8e006"}, "outputs": [], "execution_count": null, "source": ["train = labels.copy()\n", "train['filename'] = train.apply(lambda x: ('../input/dog-breed-identification/train/' + x['id'] + '.jpg'), axis=1)\n", "train.head()"], "cell_type": "code"}, {"metadata": {"_uuid": "1561fc52830cded8fb891c288b91d769e9f216d0", "_cell_guid": "96ab3d20-b1ad-48e3-9cb3-47e7506e65a7"}, "outputs": [], "execution_count": null, "source": ["from keras.preprocessing.image import img_to_array\n", "from keras.preprocessing.image import load_img\n", "from sklearn.model_selection import train_test_split"], "cell_type": "code"}, {"metadata": {"_uuid": "7e07c94ae0e8ebc69db09a16a44a7793f620c7bd", "collapsed": true, "_cell_guid": "81f40fda-01cf-4db3-b404-ef52d0ec1abd"}, "outputs": [], "execution_count": null, "source": ["train_data = np.array([ img_to_array(load_img(img, target_size=(299, 299))) for img in train['filename'].values.tolist()]).astype('float32')"], "cell_type": "code"}, {"metadata": {"_uuid": "c4661d307fac44f2e33b2e7e4fdd7fb1a88f262d", "_cell_guid": "74abcab2-43ef-492a-8b05-292e47ea0ef4"}, "outputs": [], "execution_count": null, "source": ["train_data.shape"], "cell_type": "code"}, {"metadata": {"_uuid": "4be4b092b4ae252226dbe2a8cf7d1ae008e69dc0", "scrolled": true, "_cell_guid": "557115b0-c293-4c1b-9c9d-8c213500161d"}, "outputs": [], "execution_count": null, "source": ["labels = train.breed\n", "labels"], "cell_type": "code"}, {"metadata": {"_uuid": "b1a5e43a0cccf4a53dfbfb09e280b1bbd9a404da", "collapsed": true, "_cell_guid": "a98c3605-543b-42af-b327-c1a214225147"}, "outputs": [], "execution_count": null, "source": ["x_train, x_val, y_train, y_val = train_test_split(train_data, labels, test_size=0.2)"], "cell_type": "code"}, {"metadata": {"_uuid": "52de40c269679654601366f710abbc33405ad532", "_cell_guid": "d3db64d4-b820-454b-b5c2-0c6b35c9054a"}, "outputs": [], "execution_count": null, "source": ["x_train.shape, y_train.shape, x_val.shape, y_val.shape"], "cell_type": "code"}, {"metadata": {"_uuid": "9dd018514e44d5bbe616ef871b7ce585df100f4e", "collapsed": true, "_cell_guid": "25e1a0d1-b1ce-4d35-baa9-22e0925fdfd8"}, "outputs": [], "execution_count": null, "source": ["y_train = pd.get_dummies(y_train.reset_index()).values[:,1:]"], "cell_type": "code"}, {"metadata": {"_uuid": "f650047d21d52c57a03a1163f02932c78fa19b38", "collapsed": true, "_cell_guid": "7ebeb9a8-0e38-421a-9dd2-8569c0a97672"}, "outputs": [], "execution_count": null, "source": ["y_val = pd.get_dummies(y_val.reset_index()).values[:, 1:]"], "cell_type": "code"}, {"metadata": {"_uuid": "ca58b3a72c44f21c0eb2ecb88ed4e37cf0a3cede", "_cell_guid": "04652aa9-2367-4fd6-8376-6d4e658c271d"}, "outputs": [], "execution_count": null, "source": ["x_train.shape, y_train.shape, x_val.shape, y_val.shape"], "cell_type": "code"}, {"metadata": {"_uuid": "8812f21c62ef195ebd5830f6da98bb7a226b4dd5", "_cell_guid": "6924b2eb-da06-4f77-82e2-380e58c5ecde"}, "outputs": [], "execution_count": null, "source": ["from os import makedirs\n", "from os.path import expanduser, exists, join\n", "\n", "!ls ../input/keras-pretrained-models/\n", "\n", "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)\n", "    \n", "!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/"], "cell_type": "code"}, {"metadata": {"_uuid": "2269fc5bad8804f79ea7bf2cfa54cc036481e448", "collapsed": true, "_cell_guid": "78ea17a8-d600-4695-a860-9b25a8fe8a7c"}, "outputs": [], "execution_count": null, "source": ["import keras\n", "from keras.applications import Xception, InceptionV3\n", "from keras.applications.xception import preprocess_input as xception_preprocessor\n", "from keras.applications.inception_v3 import preprocess_input as inception_v3_preprocessor\n", "from keras.preprocessing.image import ImageDataGenerator"], "cell_type": "code"}, {"metadata": {"_uuid": "7d6fbedb444c375ba522eefabb53dca8dbb5edfc", "collapsed": true, "_cell_guid": "b9e67b03-5eb5-4799-a681-4af95924b06a"}, "outputs": [], "execution_count": null, "source": ["xception_model = Xception(include_top=False, input_shape=(299, 299, 3), pooling='avg')\n", "inception_model = InceptionV3(include_top=False, input_shape=(299, 299, 3), pooling='avg')"], "cell_type": "code"}, {"metadata": {"_uuid": "4d4b30b9d5db7b960e85b1c456012cc29ebc45bb", "collapsed": true, "_cell_guid": "8489b962-039a-4a81-89cc-3efc374ae019"}, "outputs": [], "execution_count": null, "source": ["train_generator = ImageDataGenerator(zoom_range = 0.3, width_shift_range=0.1, height_shift_range=0.1)\n", "val_generator = ImageDataGenerator()"], "cell_type": "code"}, {"metadata": {"_uuid": "9d42a7b40a4b2cb4a43b4c3ab70ef812f2ab37c4", "_cell_guid": "9eb45b83-ce6e-4dd4-adcb-954975cac613"}, "outputs": [], "execution_count": null, "source": ["train_generator.preprocessing_function = inception_v3_preprocessor\n", "val_generator.preprocessing_function = inception_v3_preprocessor\n", "\n", "generator = train_generator.flow(x_train, y_train, shuffle=False)\n", "inception_train_bottleneck = inception_model.predict_generator(generator, verbose=1)\n", "np.save('inception-train.npy', inception_train_bottleneck)\n", "\n", "generator = val_generator.flow(x_val, y_val, shuffle=False)\n", "inception_val_bottleneck = inception_model.predict_generator(generator, verbose=1)\n", "np.save('inception-val.npy', inception_val_bottleneck)"], "cell_type": "code"}, {"metadata": {"_uuid": "e9216988ac45fa562d016aba9437fe1166509541", "_cell_guid": "a61c331b-edda-4bea-9afc-50576e623d53"}, "outputs": [], "execution_count": null, "source": ["train_generator.preprocessing_function = xception_preprocessor\n", "val_generator.preprocessing_function = xception_preprocessor\n", "\n", "generator = train_generator.flow(x_train, y_train, shuffle=False)\n", "xception_train_bottleneck = xception_model.predict_generator(generator, verbose=1)\n", "np.save('xception-train.npy', xception_train_bottleneck)\n", "\n", "generator = val_generator.flow(x_val, y_val, shuffle=False)\n", "xception_val_bottleneck = xception_model.predict_generator(generator, verbose=1)\n", "np.save('xception-val.npy', xception_val_bottleneck)"], "cell_type": "code"}, {"metadata": {"_uuid": "11b378cb77601aef7b71880b949f9ab9154e9dc4", "collapsed": true, "_cell_guid": "9e48102d-86e6-44fa-90c9-490e95502ab1"}, "outputs": [], "execution_count": null, "source": ["xception_train_bottleneck_2 = np.load('../input/bottleneck-features-dog-breeds-identification/inception-train.npy')\n", "xception_val_bottleneck_2 = np.load('../input/bottleneck-features-dog-breeds-identification/inception-val.npy')\n", "\n", "inception_train_bottleneck_2 = np.load('../input/bottleneck-features-dog-breeds-identification/xception-train.npy')\n", "inception_val_bottleneck_2 = np.load('../input/bottleneck-features-dog-breeds-identification/xception-val.npy')\n", "\n", "xception_train_bottleneck_3 = np.load('../input/bottleneck-features-inceptionxception/Xception_features.npy')\n", "xception_val_bottleneck_3 = np.load('../input/bottleneck-features-inceptionxception/Xception_validfeatures.npy')\n", "\n", "inception_train_bottleneck_3 = np.load('../input/bottleneck-features-inceptionxception/InceptionV3_features.npy')\n", "inception_val_bottleneck_3 = np.load('../input/bottleneck-features-inceptionxception/InceptionV3_validfeatures.npy')\n", "\n", "xception_train_bottleneck_4 = np.load('./xception-train.npy')\n", "xception_val_bottleneck_4 = np.load('./xception-val.npy')\n", "\n", "inception_train_bottleneck_4 = np.load('./inception-train.npy')\n", "inception_val_bottleneck_4 = np.load('./inception-val.npy')"], "cell_type": "code"}, {"metadata": {}, "outputs": [], "execution_count": null, "source": ["xception_train_bottleneck.shape, xception_val_bottleneck.shape, inception_train_bottleneck.shape, inception_val_bottleneck.shape"], "cell_type": "code"}, {"metadata": {}, "outputs": [], "execution_count": null, "source": ["xception_train_bottleneck_2.shape, xception_val_bottleneck_2.shape, inception_train_bottleneck_2.shape, inception_val_bottleneck_2.shape"], "cell_type": "code"}, {"metadata": {}, "outputs": [], "execution_count": null, "source": ["xception_train_bottleneck.dtype, xception_val_bottleneck.dtype, inception_train_bottleneck.dtype, inception_val_bottleneck.dtype"], "cell_type": "code"}, {"metadata": {}, "outputs": [], "execution_count": null, "source": ["xception_train_bottleneck_2.dtype, xception_val_bottleneck_2.dtype, inception_train_bottleneck_2.dtype, inception_val_bottleneck_2.dtype"], "cell_type": "code"}, {"metadata": {"_uuid": "5056ed1c60384ed8fe7b5d1f4b9548b94c799e57", "_cell_guid": "141ba152-4714-433e-bb01-52bcd6e6cb75"}, "outputs": [], "execution_count": null, "source": ["from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import log_loss, accuracy_score\n", "\n", "logreg_inception = LogisticRegression(multi_class='multinomial', solver='lbfgs')\n", "logreg_inception.fit(inception_train_bottleneck_4, (y_train * range(16)).sum(axis=1))\n", "inception_preds_val = logreg_inception.predict(inception_val_bottleneck_4)\n", "inception_preds_train = logreg_inception.predict(inception_train_bottleneck_4)\n", "\n", "logreg_xception = LogisticRegression(multi_class='multinomial', solver='lbfgs')\n", "logreg_xception.fit(xception_train_bottleneck_4, (y_train * range(16)).sum(axis=1))\n", "xception_preds_val = logreg_xception.predict(xception_val_bottleneck_4)\n", "xception_preds_train = logreg_xception.predict(xception_train_bottleneck_4)"], "cell_type": "code"}, {"metadata": {"_uuid": "4b910bf10289063c0a599fa6049c1d3182ffb2d1", "_cell_guid": "a21dbeb0-0e2e-479c-aade-833505cdb6ec"}, "outputs": [], "execution_count": null, "source": ["avgpreds_val = np.average([inception_preds_val, xception_preds_val], axis=0, weights=[1,1])\n", "avgpreds_train = np.average([inception_preds_train, xception_preds_train], axis=0, weights=[1,1])\n", "avgpreds_val.shape, avgpreds_train.shape"], "cell_type": "code"}, {"metadata": {"_uuid": "e965cfb5ef3dcf43dd03fb6f8794f64d6294075c", "_cell_guid": "18b4cd8e-bd6e-4d24-aa56-43aeafc83a80"}, "outputs": [], "execution_count": null, "source": ["accuracy_score(np.round(avgpreds_val).astype('int'), np.argmax(y_val, axis=1))"], "cell_type": "code"}, {"metadata": {"_uuid": "39794993a1c9f8ff481d8d3e32ffe0120849c536", "_cell_guid": "f97a7503-3656-4733-a04d-3638869ee936"}, "outputs": [], "execution_count": null, "source": ["accuracy_score(np.round(avgpreds_train).astype('int'), np.argmax(y_train, axis=1))"], "cell_type": "code"}, {"metadata": {"_uuid": "8ccf5333c79f215a3589ceed0b756ee51b369d12", "_cell_guid": "9836d149-7dc1-48b9-8268-b21fb078de90"}, "outputs": [], "execution_count": null, "source": ["import tensorflow as tf\n", "\n", "with tf.Session() as sess:\n", "    result = sess.run(tf.one_hot(np.round(avgpreds_val), depth = 16))\n", "    print('ensemble validation accuracy : {}'.format(accuracy_score(y_val, result)))"], "cell_type": "code"}, {"metadata": {"_uuid": "567dae27185dcba9858af5f164832796a40befb7", "collapsed": true, "_cell_guid": "72af973f-ad2d-4758-9df3-ad802594e590"}, "source": ["\n", "## Credits\n", "\n", "Some code is used from https://www.kaggle.com/robhardwick/xception-inceptionv3-ensemble-methods?scriptVersionId=2089175\n", "\n", "\n", "\n", "\n"], "cell_type": "markdown"}], "nbformat": 4, "nbformat_minor": 1}