{"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)\n\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\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[{"output_type":"stream","text":"['reducing-image-sizes-to-32x32', 'iwildcam-2019-fgvc6', 'densenet-keras']\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport json\n\nimport numpy as np\nimport pandas as pd\nimport keras\nfrom keras import layers\nfrom keras.applications import DenseNet121\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Dropout, Activation, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.models import Sequential\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score","execution_count":2,"outputs":[{"output_type":"stream","text":"Using TensorFlow backend.\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = np.load('../input/reducing-image-sizes-to-32x32/X_train.npy')\nx_test = np.load('../input/reducing-image-sizes-to-32x32/X_test.npy')\ny_train = np.load('../input/reducing-image-sizes-to-32x32/y_train.npy')\n\nprint('x_train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')","execution_count":3,"outputs":[{"output_type":"stream","text":"x_train shape: (196299, 32, 32, 3)\n196299 train samples\n153730 test samples\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Convert the images to float and scale it to a range of 0 to 1\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train /= 255.\nx_test /= 255.","execution_count":4,"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\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()","execution_count":5,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"densenet = DenseNet121(\n    weights='../input/densenet-keras/DenseNet-BC-121-32-no-top.h5',\n    include_top=False,\n    input_shape=(32,32,3)\n)","execution_count":6,"outputs":[{"output_type":"stream","text":"WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\nInstructions for updating:\nColocations handled automatically by placer.\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(densenet)\nmodel.add(layers.GlobalAveragePooling2D())\nmodel.add(layers.Dense(14, activation='softmax'))","execution_count":7,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='adamax',\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":8,"outputs":[{"output_type":"stream","text":"_________________________________________________________________\nLayer (type)                 Output Shape              Param #   \n=================================================================\ndensenet121 (Model)          (None, 1, 1, 1024)        7037504   \n_________________________________________________________________\nglobal_average_pooling2d_1 ( (None, 1024)              0         \n_________________________________________________________________\ndense_1 (Dense)              (None, 14)                14350     \n=================================================================\nTotal params: 7,051,854\nTrainable params: 6,968,206\nNon-trainable params: 83,648\n_________________________________________________________________\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"checkpoint = 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\nhistory = model.fit(\n    x=x_train,\n    y=y_train,\n    batch_size=64,\n    epochs=7,\n    callbacks=[checkpoint, f1_metrics],\n    validation_split=0.1\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('history.json', 'w') as f:\n    json.dump(history.history, f)\n\nhistory_df = pd.DataFrame(history.history)\nhistory_df['val_f1'] = f1_metrics.val_f1s\nhistory_df['val_precision'] = f1_metrics.val_precisions\nhistory_df['val_recall'] = f1_metrics.val_recalls\nhistory_df[['loss', 'val_loss']].plot()\nhistory_df[['acc', 'val_acc']].plot()\nhistory_df[['val_f1', 'val_precision', 'val_recall']].plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights('model.h5')\ny_test = model.predict(x_test)\n\nsubmission_df = pd.read_csv('../input/iwildcam-2019-fgvc6/sample_submission.csv')\nsubmission_df['Predicted'] = y_test.argmax(axis=1)\n\nsubmission_df['Species'] = 'default'\nspecies = []\nfor index, row in submission_df.iterrows():\n\tif row['Predicted'] == 0:\n\t\tspecies.append('empty')\n\telif row['Predicted'] == 1:\n\t\tspecies.append('deer')\n\telif row['Predicted'] == 2:\n\t\tspecies.append('moose')\n\telif row['Predicted'] == 3:\n\t\tspecies.append('squirrel')\n\telif row['Predicted'] == 4:\n\t\tspecies.append('rodent')\n\telif row['Predicted'] == 5:\n\t\tspecies.append('small_mammal')\n\telif row['Predicted'] == 6:\n\t\tspecies.append('elk')\n\telif row['Predicted'] == 7:\n\t\tspecies.append('pronghorn_antelope')\n\telif row['Predicted'] == 8:\n\t\tspecies.append('rabbit')\n\telif row['Predicted'] == 9:\n\t\tspecies.append('bighorn_sheep')\n\telif row['Predicted'] == 10:\n\t\tspecies.append('fox')\n\telif row['Predicted'] == 11:\n\t\tspecies.append('coyote')\n\telif row['Predicted'] == 12:\n\t\tspecies.append('black_bear')\n\telif row['Predicted'] == 13:\n\t\tspecies.append('raccoon')\n\telif row['Predicted'] == 14:\n\t\tspecies.append('skunk')\n\telif row['Predicted'] == 15:\n\t\tspecies.append('wolf')\n\telif row['Predicted'] == 16:\n\t\tspecies.append('bobcat')\n\telif row['Predicted'] == 17:\n\t\tspecies.append('cat')\n\telif row['Predicted'] == 18:\n\t\tspecies.append('dog')\n\telif row['Predicted'] == 19:\n\t\tspecies.append('oppossum')\n\telif row['Predicted'] == 20:\n\t\tspecies.append('bison')\n\telif row['Predicted'] == 21:\n\t\tspecies.append('mountain_goat')\n\telif row['Predicted'] == 22:\n\t\tspecies.append('mountain_lion')\n        \nsubmission_df['Species'] = species\n        \nprint(submission_df.shape)\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('submission.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}