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