{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport math\nimport matplotlib.pyplot as plt\nimport cv2\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nfrom PIL import Image\nimport os,sys\nprint(os.listdir(\"../input\"))\n\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"label_df = pd.read_csv('../input/iwildcam-2019-fgvc6/train.csv')\nsubmission_df = pd.read_csv('../input/iwildcam-2019-fgvc6/sample_submission.csv')\nlabel_df.head()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def samples(df, columns=3, rows=3):\n    fig=plt.figure(figsize=(6*columns, 3*rows))\n\n    for i in range(columns*rows):\n        img_path = df.loc[i,'file_name']\n        img_id = df.loc[i,'category_id']\n        img = cv2.imread(f'../input/train_images/{img_path}')\n        fig.add_subplot(rows, columns, i+1)\n        plt.title(img_id)\n        plt.imshow(img)\n\nsamples(label_df)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pad_width(im, new_shape, is_rgb=True):\n    pad_diff = new_shape - im.shape[0], new_shape - im.shape[1]\n    t, b = math.floor(pad_diff[0]/2), math.ceil(pad_diff[0]/2)\n    l, r = math.floor(pad_diff[1]/2), math.ceil(pad_diff[1]/2)\n    if is_rgb:\n        width = ((t,b), (l,r), (0, 0))\n    else:\n        width = ((t,b), (l,r))\n    return pad_width\n\ndef pad_and_resize(img_path, dataset, pad=False, desired_size=32):\n    img = cv2.imread(f'../input/{dataset}_images/{img_path}.jpg')\n    \n    if pad:\n        width = pad_width(img, max(img.shape))\n        padded = np.pad(img, width=width, mode='constant', constant_values=0)\n    else:\n        padded = img\n    \n    resized = cv2.resize(padded, (desired_size,)*2).astype('uint8')\n    \n    return resized\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\ntrain_resized = []\ntest_resized = []\n\nfor image_id in label_df['id']:\n    train_resized.append(\n        pad_and_resize(image_id, 'train')\n    )\n\nfor image_id in submission_df['Id']:\n    test_resized.append(\n        pad_and_resize(image_id, 'test')\n    )\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = np.stack(train_resized)\nX_test = np.stack(test_resized)\n\ntarget_dummies = pd.get_dummies(label_df['category_id'])\ntrain_label = target_dummies.columns.values\ny_train = target_dummies.values\n\nprint(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.save('Resized_Xtrain.npy',X_train)\nnp.save('Resized_ytrain.npy',y_train)\nnp.save('Resized_Xtest.npy',X_test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir('../input/reduceddata/wildcam-reduced'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from zipfile import ZipFile\n#zf = ZipFile('../input/reduceddata/wildcam-reduced.zip','r')\n#zf.extractall('..input/')\n#zf.close()\ny_train = np.load('../input/reduceddata/wildcam-reduced/y_train.npy')\nX_train = np.load('../input/reduceddata/wildcam-reduced/X_train.npy')\nX_test = np.load('../input/reduceddata/wildcam-reduced/X_test.npy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('X_train shape is',X_train.shape)\nprint('X_test shape is', X_test.shape)\nprint('y_train.shape is',y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_train /= 255\nX_test /= 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications import DenseNet121\nfrom keras.layers import *\nfrom keras.models import Sequential\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dense_network = DenseNet121(input_shape = (32, 32, 3),include_top = False, classes = 1000)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(dense_network)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(14, activation='softmax'))\n\nmodel.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import Callback\nfrom sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Metrics(Callback):\n    def on_train_begin(self, logs={}):\n        self.val_f1s = []\n        self.val_recalls = []\n        self.val_precisions = []\n\n    def on_epoch_end(self, epoch, logs={}):\n        X_val, y_val = self.validation_data[:2]\n        y_pred = self.model.predict(X_val)\n\n        y_pred_cat = keras.utils.to_categorical(\n            y_pred.argmax(axis=1),\n            num_classes=14\n        )\n        _val_f1 = f1_score(y_val, y_pred_cat, average='macro')\n        _val_recall = recall_score(y_val, y_pred_cat, average='macro')\n        _val_precision = precision_score(y_val, y_pred_cat, average='macro')\n\n        self.val_f1s.append(_val_f1)\n        self.val_recalls.append(_val_recall)\n        self.val_precisions.append(_val_precision)\n\n        print((f\"val_f1: {_val_f1:.4f}\"\n               f\" — val_precision: {_val_precision:.4f}\"\n               f\" — val_recall: {_val_recall:.4f}\"))\n\n        return\n\nf1_metrics = Metrics()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.callbacks import  ModelCheckpoint\n\ncheckpoint = ModelCheckpoint(\n    'model.h5', \n    monitor='val_acc', \n    verbose=1, \n    save_best_only=True, \n    save_weights_only=False,\n    mode='auto'\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n    x=X_train,\n    y=y_train,\n    batch_size=64,\n    epochs=10,\n    callbacks=[f1_metrics],\n    validation_split=0.2\n)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.subplots(figsize=(8,8))\nplt.plot(history.history['loss'],color='g')\nplt.plot(history.history['val_loss'],color='r')\nplt.legend(['training','validation'])\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.subplots(figsize=(8,8))\nplt.plot(history.history['acc'],color='g')\nplt.plot(history.history['val_acc'],color='r')\nplt.legend(['training','validation'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = model.predict(X_test)\nsubmission_df['Predicted'] = result.argmax(axis=1)\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('final.csv',index=False)","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}