{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nimport tensorflow.keras as keras\nfrom keras import regularizers\nfrom keras.applications.resnet50 import ResNet50, preprocess_input\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.metrics import categorical_accuracy, top_k_categorical_accuracy, categorical_crossentropy\nimport os\nfrom tqdm import tqdm\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom keras.preprocessing.image import ImageDataGenerator\n\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9918f2c8d3556e75cc94cc41fc4f3323bea3c8b3"},"cell_type":"code","source":"df_train = pd.read_csv('../input/dog-breed-identification/labels.csv') \ndf_test = pd.read_csv('../input/dog-breed-identification/sample_submission.csv') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26ca1f0a72c44ec28803b111778bfee69bc02998"},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"063c723146ad83b0f5b0ea8e376b89129c95ffc4"},"cell_type":"code","source":"df_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0293ffd5ee7ca8717f134bfc654623ae940b1bc"},"cell_type":"code","source":"labels = df_train['breed']\none_hot = pd.get_dummies(labels, sparse = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2621ce3a02d13f4e651222233648bd8a814059c"},"cell_type":"code","source":"one_hot_labels = np.asarray(one_hot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb65dd8cd12273701922e56f4d0e249cad650cfc"},"cell_type":"code","source":"im_resize = 64\n#visualize a dogger\ndogger1 = df_train['id'][0]\ndogger2 = df_train['id'][1]\ndogger3 = df_train['id'][2]\ndogger4 = df_train['id'][3]\npupper1 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger1))\npupper2 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger2))\npupper3 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger3), cv2.IMREAD_GRAYSCALE)\npupper4 = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(dogger4), cv2.IMREAD_GRAYSCALE)\npupper4 = cv2.resize(pupper4, (im_resize, im_resize))\nf, axarr = plt.subplots(2,2)\naxarr[0,0].imshow(pupper1)\naxarr[0,1].imshow(pupper2)\naxarr[1,0].imshow(pupper3,cmap=\"gray\", vmin=0, vmax=255)\naxarr[1,1].imshow(pupper4,cmap=\"gray\", vmin=0, vmax=255)\nplt.xticks([])\nplt.yticks([])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"139b0e9c9856c5158b974555008ddf152b13cbbe"},"cell_type":"code","source":"im_size = pupper1.shape\nprint(im_size)\nprint(cv2.resize(pupper1, (im_resize, im_resize)).shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50c66c7abdeb4478ac52e1013b7b5867ba243fa3"},"cell_type":"code","source":"x_train = []\ny_train = []\nx_test = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3be7e20410a2b0374be73e38a81c8aee672dbaa9"},"cell_type":"code","source":"i = 0 \nfor f, breed in tqdm(df_train.values):\n    img = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(f))\n    img_resized = cv2.resize(img, (im_resize, im_resize))\n    x_train.append(img_resized)\n    label = one_hot_labels[i]\n    y_train.append(label)\n    i += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d0b4ac7910368aa1806e7544f235da733e85ebc"},"cell_type":"code","source":"del df_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8372e0ee527f043df2ec267845329cbf538d37ce"},"cell_type":"code","source":"for f in tqdm(df_test['id'].values):\n    img = cv2.imread('../input/dog-breed-identification/test/{}.jpg'.format(f))\n    img_resized = cv2.resize(img, (im_resize, im_resize))\n    x_test.append(img_resized)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9f86326b40aa50c5d4d196e6fac15f8eb94c890"},"cell_type":"code","source":"num_class = 120 #static ftw","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e9078701b87ee09b7b3a235bc9262dc3ab167df"},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(x_train, y_train, shuffle=True,  test_size=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f1464f0497cbf34d7a756d3133d59e2d339af19"},"cell_type":"code","source":"del x_train, y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ff03bce73be33ab8d4697989e843da9e9135089"},"cell_type":"code","source":"def top_3_accuracy(y_true, y_pred):\n    return top_k_categorical_accuracy(y_true, y_pred, k=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1045af204c54069db458aa163255659010882fb"},"cell_type":"code","source":"datagen = ImageDataGenerator(width_shift_range=0.2,\n                            height_shift_range=0.2,\n                            zoom_range=0.2,\n                            rotation_range=30,\n                            vertical_flip=False,\n                            horizontal_flip=True)\n\n\ndatagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41f5dfae4bbfd648400f89d878c5c37752f0136e"},"cell_type":"code","source":"\nbase_model = ResNet50(weights=\"../input/keras-pretrained-models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\",include_top=False, input_shape=(im_resize, im_resize, 3))\n\nx = base_model.output\nx = Flatten()(x)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.25)(x)\n\nlogits = Dense(num_class, activation='softmax')(x)\n\nmodel = Model(base_model.input, logits)\n\nmodel.compile(optimizer='Adam',\n          loss='categorical_crossentropy', \n           metrics=[categorical_crossentropy, categorical_accuracy])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bff918d1418425c5a3f04e87d1406f3558883600"},"cell_type":"code","source":"def gen_graph(history, title):\n    plt.plot(history.history['categorical_accuracy'])\n    plt.plot(history.history['val_categorical_accuracy'])\n    plt.title('Accuracy ' + title)\n    plt.ylabel('Accuracy')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()\n    plt.plot(history.history['categorical_crossentropy'])\n    plt.plot(history.history['val_categorical_crossentropy'])\n    plt.title('Loss ' + title)\n    plt.ylabel('MLogLoss')\n    plt.xlabel('Epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0153f85d4e7d3a3e45925fddf963e6f9463d6f8a"},"cell_type":"code","source":"train_generator = datagen.flow(np.array(X_train), np.array(Y_train), \n                               batch_size=32) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b814e1c53da62f7fca5bf9c62b056aee8a6c2677","scrolled":true},"cell_type":"code","source":"batch_size = 512\n\nhistory_rmsprop = model.fit_generator(\n    train_generator,\n    epochs=30, steps_per_epoch=len(X_train) / batch_size,\n    validation_data=(np.array(X_train), np.array(Y_train)), validation_steps=len(X_valid) / batch_size)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d700a487b7fe4fdbb73a5896c69c004bc73913d3"},"cell_type":"code","source":"preds = model.predict(np.array(x_test), verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50e3c0ad0fbdfca1336ff1bd51eaf5f69aa341e1"},"cell_type":"code","source":"#plot the accuracy\ngen_graph(history_rmsprop, \n              \"ResNet50 RMSprop\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06af78277b24dde2a87b179ed5f14caac9f64c85"},"cell_type":"code","source":"\n\nsub = pd.DataFrame(preds)\ncol_names = one_hot.columns.values\nsub.columns = col_names\nsub.insert(0, 'id', df_test['id'])\nsub.head(5)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"672869459127950bb35c7dce0ebb976c830589a2"},"cell_type":"code","source":"sub.to_csv(\"output_rmsprop_aug.csv\", index=False)\n\nmodel.save('rmsprop_v2_augmentation.h5')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}