{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"from keras.preprocessing import image\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Activation\nfrom keras.applications.vgg16 import VGG16, preprocess_input\nfrom xgboost import XGBClassifier\nfrom sklearn.model_selection import cross_val_score, learning_curve, train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import make_scorer\nfrom sklearn.metrics import log_loss, accuracy_score\nimport numpy as np\nimport math\nfrom tqdm import tqdm\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\n# %matplotlib inline\nimport time\nimport os\n\nVGG16_PATH = '../input/vgg16/'\nDOG_PATH = '../input/dog-breed-identification/'\nBATCH_SIZE = 50","execution_count":2,"outputs":[]},{"metadata":{"_cell_guid":"bf696484-0dd9-49ce-82c9-99c4f463a229","_uuid":"dee3e9555e9ced9ccff04de60021e08855c3c454","collapsed":true,"trusted":true},"cell_type":"code","source":"def path_by_id(id, test=False):\n    return DOG_PATH + ('train/' if not test else 'test/') + id + '.jpg'\n\ndef get_image_array(id, test=False):\n    img = image.load_img(path_by_id(id, test), target_size=(224, 224))\n    return image.img_to_array(img)","execution_count":3,"outputs":[]},{"metadata":{"_cell_guid":"7cea8c16-34a1-432d-9aad-565fc1602571","_uuid":"02c2d491211707d86e5ea00dfe58873782dfef7c","collapsed":true,"trusted":true},"cell_type":"code","source":"labels_data = pd.read_csv(DOG_PATH + 'labels.csv')\nlabels = labels_data.set_index('id')['breed']\ny = pd.get_dummies(labels, sparse = True)\ntrain_ids = y.index\ntest_ids = pd.read_csv(DOG_PATH + 'sample_submission.csv')['id']","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"# Instantiate the model with the pre-trained weights (no top)\nbase_model= VGG16(weights=(VGG16_PATH+'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'),\n                  include_top=False, pooling='avg')","execution_count":8,"outputs":[]},{"metadata":{"_cell_guid":"860d7bc5-e42d-4f69-aa23-ffcc5c38aefe","_uuid":"3449a4ef5a3c9a8e0c6deb6c2da0a09d97378e0a","collapsed":true,"trusted":true},"cell_type":"code","source":"def process_image(id, test=False):\n    return base_model.predict(preprocess_input(np.expand_dims(get_image_array(id, test), axis=0)))","execution_count":9,"outputs":[]},{"metadata":{"_cell_guid":"09ba0fbc-6295-4502-8e09-58871f8722e4","_uuid":"89265109ee3fc30636d57614b444952375aefcac","trusted":true},"cell_type":"code","source":"X_arr = []\nfor id in tqdm(train_ids):\n    X_arr.append(process_image(id)[0])\nX = pd.DataFrame(X_arr, index=train_ids)","execution_count":10,"outputs":[]},{"metadata":{"_cell_guid":"6298f4f4-2bcd-442c-8228-7e7a7317b385","_uuid":"eb5be2186df89fbe783405cb4222dbec42d8f473","trusted":true,"collapsed":true},"cell_type":"code","source":"# model = XGBClassifier(objective='multi:softmax', num_class=120, n_jobs=4, verbose=True)\n# model = LogisticRegression(n_jobs=4, verbose=True)\nmodel = Sequential([\n    Dense(1024, input_shape=(512,)),\n    Activation('relu'),\n    Dense(256, input_shape=(512,)),\n    Activation('relu'),\n    Dense(120),\n    Activation('softmax'),\n])\n\nmodel.compile(optimizer='rmsprop',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","execution_count":22,"outputs":[]},{"metadata":{"_cell_guid":"7762187b-774c-437e-a75b-8675bd9356af","_uuid":"ca53aa6ac99f75e94f3c178d7ea9936e1e095114","collapsed":true,"trusted":true},"cell_type":"code","source":"# X_train, X_cv, y_train, y_cv = train_test_split(X, y, test_size=0.20, random_state=42)\nX_train = X\ny_train = y","execution_count":16,"outputs":[]},{"metadata":{"_cell_guid":"e6559544-c2cd-4f50-aec9-5a229878f976","_uuid":"25d1f0237b46fe4201797c5131d2a9cac5e145a2","trusted":true},"cell_type":"code","source":"model.fit(X_train, np.asarray(y_train), epochs=100, batch_size=100, verbose=False)\n# model.fit(batch_iterator(range(0,100)),\n#         samples_per_epoch=1, nb_epoch=100)","execution_count":23,"outputs":[]},{"metadata":{"_cell_guid":"df045342-30d2-4592-9f0d-db44857bfd00","_uuid":"094046e5519320653add18b8a6fba3ccf52a9f2d","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ea2a968d-7817-4500-85c8-4124df8925e9","_uuid":"124a3d0e444ffde9266c9d11ccd528f3d3987080","trusted":true},"cell_type":"code","source":"X_test = []\nfor id in tqdm(test_ids):\n    X_test.append(process_image(id, test=True)[0])\nX_test = np.array(X_test)","execution_count":13,"outputs":[]},{"metadata":{"_cell_guid":"9cde3f5e-ef3f-4d77-bb14-72be0ba745b7","_uuid":"2be0003bcb6b9367e16a115311528bfa7e1a2f54","collapsed":true,"trusted":true},"cell_type":"code","source":"# y_pred_cv = pd.DataFrame(model.predict(X_cv))","execution_count":24,"outputs":[]},{"metadata":{"_cell_guid":"fa019f66-08ac-42d9-aea1-deef85e734cd","_uuid":"9e3790fc7460e884f8fecb3c1642d805481bcd9e","collapsed":true,"trusted":true},"cell_type":"code","source":"# y_cv = (np.asarray(y_cv) * range(120)).sum(axis=1)","execution_count":19,"outputs":[]},{"metadata":{"_cell_guid":"bb85bfb8-d3fb-4d26-8959-d1e380f57e9e","_uuid":"45010b4fad70719f398b1a514f9b89b717e28343","collapsed":true,"trusted":true},"cell_type":"code","source":"# y_pred_cv = y_pred_cv.idxmax(axis=1)","execution_count":25,"outputs":[]},{"metadata":{"_cell_guid":"93d18aac-3265-4b94-913f-ea4e5be13db9","_uuid":"c0686f7b88517d225cc0c542876c4ef894204b1c","trusted":true},"cell_type":"code","source":"# (y_cv== np.asarray(y_pred_cv)).sum() / len(y_cv)","execution_count":26,"outputs":[]},{"metadata":{"_cell_guid":"20be573f-dc0f-47ea-8cce-3eab73958060","_uuid":"b07c0470e51a4930d9b97af62b275e3f5c0fe02d","collapsed":true,"trusted":true},"cell_type":"code","source":"y_pred = pd.DataFrame(model.predict(X_test), index=test_ids)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"292f10e8-87ac-4b29-808c-6bb4aa7ae3ac","_uuid":"f3c2ef677ce1812790c93a43f28e74ecbbe278dc","collapsed":true,"trusted":true},"cell_type":"code","source":"y_pred = y_pred.idxmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e4046921-383b-4aa1-9fdd-fb7932555719","_uuid":"38b05b5de65d7ea3d27af6bed21beed857c2bd04","collapsed":true,"trusted":true},"cell_type":"code","source":"y_pred = pd.get_dummies(y_pred, prefix='', prefix_sep='')\ny_pred.columns = y_pred.columns.astype('int')\nmissing_cols = {x for x in range(120)} - set(y_pred.columns)\nfor col in missing_cols:\n    y_pred[col] = 0\ny_pred = y_pred.reindex_axis(sorted(y_pred.columns), axis=1)\ny_pred.columns = y.columns","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"74801b27-8c3b-402e-bb37-b783f3c899df","_uuid":"d986dcf89789ee63b80933c2f6495fe909145ae3","collapsed":true,"trusted":true},"cell_type":"code","source":"y_pred","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0862c265-3334-4108-8406-315e0f0742ad","_uuid":"5b425f9bffa23543ba0371613b17856a46815708","collapsed":true,"trusted":true},"cell_type":"code","source":"y_pred.to_csv('submission.csv', index_label='id')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}