{"cells":[{"metadata":{"id":"Yqab_ejNMF-M","trusted":true,"collapsed":true},"cell_type":"code","source":"import numpy as np\nimport keras\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Activation,MaxPooling2D,Dropout\nfrom keras.layers.core import Dense, Flatten\nfrom keras.optimizers import Adam\nfrom keras.optimizers import SGD\nfrom keras.metrics import categorical_crossentropy\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.layers.convolutional import Conv2D\nfrom keras.models import load_model\nfrom keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping,LearningRateScheduler\nfrom keras import regularizers\nimport itertools\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\nfrom keras.regularizers import l2\nimport seaborn as sns\nimport os\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"id":"5VdyaMClMF-P","outputId":"7b0d900a-65c4-4232-fabc-e9c9fcc01903","trusted":true},"cell_type":"code","source":"#os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"-1\"\ntrain_path = '../input/catdog10000train5000validate/train/train'\nvalid_path = '../input/catdog10000train5000validate/valid/valid'\ntest_path  = '../input/catdog10000train5000validate/test/test'\nsave_path = \".\"\ntrain_gen = ImageDataGenerator(\n    shear_range=0.2,\n    zoom_range=0.2,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True,\n    channel_shift_range=10.,\n    fill_mode=\"nearest\"\n)\n\ntrain_batches = train_gen.flow_from_directory(directory=train_path, target_size=(224,224),\n    classes=['dog', 'cat'], batch_size=32)\nvalid_batches = ImageDataGenerator().flow_from_directory(directory=valid_path, target_size=(224,224), \n    classes=['dog', 'cat'], batch_size=32)\ntest_batches = ImageDataGenerator().flow_from_directory(directory=test_path, target_size=(224,224),\n    classes=['dog', 'cat'], batch_size=32,shuffle=False)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"zyrrA5u_MF-S","trusted":true},"cell_type":"code","source":"# learning rate schedule\ndef step_decay(epoch):\n\tinitial_lrate = 0.001\n\tdrop = 0.5\n\tepochs_drop = 50.0\n\tlrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))\n\treturn lrate","execution_count":null,"outputs":[]},{"metadata":{"id":"7TlXxClOMF-V","outputId":"7f97e0a7-3c63-4af8-9ff7-e3b301a8f539","trusted":true},"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(224,224,3),kernel_initializer='glorot_uniform',\n           bias_initializer='zeros', kernel_regularizer=l2(0.0005)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(filters=64, kernel_size=(3, 3), activation='relu',kernel_initializer='glorot_uniform', \n           bias_initializer='zeros', kernel_regularizer=l2(0.0005)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(filters=128, kernel_size=(3, 3), activation='relu',kernel_initializer='glorot_uniform',\n           bias_initializer='zeros', kernel_regularizer=l2(0.0005)),\n    Dropout(0.2),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(filters=128, kernel_size=(3, 3), activation='relu',kernel_initializer='glorot_uniform', \n           bias_initializer='zeros', kernel_regularizer=l2(0.0005)),\n    Flatten(),\n    Dropout(0.5),\n    Dense(512, activation='relu'),\n    Dense(2, activation='sigmoid')\n    ])\n\n\nmodel.compile(loss='binary_crossentropy',optimizer=Adam(lr=0.0001),metrics=['accuracy'])\n#model.compile(loss='binary_crossentropy',optimizer=SGD(lr=0.00001, decay=1e-6, momentum=0.9, nesterov=True),metrics=['accuracy'])\n\n# Add all the call backs for Keras\nreduce_lr_loss = ReduceLROnPlateau(\n    monitor='val_loss', \n\tfactor=0.1, \n    patience=7, \n\tverbose=1, \n    min_delta=1e-4,\n\tmode='auto')\n\n\nmcp_save       = ModelCheckpoint(\n    'cat_dog_kaggle.h5', \n\tsave_best_only=True, \n\tmonitor='val_loss', \n\tmode='auto'\n)\n\n\nif os.path.isfile('cat_dog_kaggle.h5'):\n        #This code is implemented to load the partly trained model which was stopped due to some reason\n    print(\"Running the Partially Trained Model\")\n    partially_trained_model = load_model('cat_dog_kaggle.h5')\n    history = partially_trained_model.fit_generator(generator=train_batches,steps_per_epoch=len(train_batches), \n                              validation_data=valid_batches, validation_steps=len(valid_batches), \n                              epochs=150, callbacks=[mcp_save],verbose=1) \nelse:\n    print(\"Running the Training of Model from Scratch\")\n    history = model.fit_generator(generator=train_batches,steps_per_epoch=len(train_batches), \n                              validation_data=valid_batches, validation_steps=len(valid_batches), \n                              epochs=150, callbacks=[ mcp_save],verbose=1)\n\n\nmodel.save(os.path.join(save_path,\"cat_dog_kaggle_final.h5\"))\n","execution_count":null,"outputs":[]},{"metadata":{"id":"UsQTdaXRMF-X","outputId":"76598a64-9fd6-430b-9774-4be2fbead8e6","scrolled":false,"trusted":true},"cell_type":"code","source":"acc = history.history['accuracy'] \nval_acc = history.history['val_accuracy'] \nloss = history.history['loss'] \nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc') \nplt.title('Training and validation accuracy') \nplt.legend() \nplt.figure() \nplt.plot(epochs, loss, 'bo', label='Training loss') \nplt.plot(epochs, val_loss, 'b', label='Validation loss') \nplt.title('Training and validation loss') \nplt.savefig('accuracy.png')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"id":"0L-v6cJnMF-Z","scrolled":true,"trusted":true},"cell_type":"code","source":"# plots images with labels within jupyter notebook\ndef plots(ims, figsize=(12,6), rows=1, interp=False, titles=None):\n    if type(ims[0]) is np.ndarray:\n        ims = np.array(ims).astype(np.uint8)\n        if (ims.shape[-1] != 3):\n            ims = ims.transpose((0,2,3,1))\n    f = plt.figure(figsize=figsize)\n    cols = len(ims)/rows\n    #cols = 8\n    for i in range(len(ims)):\n        sp = f.add_subplot(rows, cols, i+1)\n        sp.axis('Off')\n        if titles is not None:\n            sp.set_title(titles[i], fontsize=16)\n        plt.imshow(ims[i], interpolation=None if interp else 'none')\n\n#test_imgs, labels = next(test_batches)\n#plots(test_batches, figsize=(100,80), rows=4)","execution_count":null,"outputs":[]},{"metadata":{"id":"4nEn0K8aMF-b","outputId":"4dcf5ee9-db91-4a52-caa4-9dacdb5b41de","trusted":true},"cell_type":"code","source":"predictions = model.predict_generator(generator=test_batches, steps=len(test_batches), verbose=0)\npred_label = np.argmax(predictions,axis=1)\nclasses = np.argmax(predictions, axis=1)\ncm = confusion_matrix(test_batches.labels,pred_label)\nf,ax = plt.subplots(figsize=(4, 4))\nsns.heatmap(cm, annot=True, linewidths=0.01,cmap=\"Greens\",linecolor=\"gray\", fmt= '.1f',ax=ax)\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()\nplt.savefig('confusion_matrix.png')","execution_count":null,"outputs":[]},{"metadata":{"id":"OtPI3P9_MF-d","trusted":true},"cell_type":"code","source":"from sklearn.metrics import precision_score\nfrom sklearn.metrics import recall_score\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import roc_curve\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn import metrics\ntn, fp, fn, tp = cm.ravel()\naccuracy = (tn + tp)/(tn + tp + fn +fp)\nprecision = precision_score(test_batches.labels, pred_label, average='binary')\nrecall = recall_score(test_batches.labels, pred_label,average='binary')\nf1_score = f1_score(test_batches.labels, pred_label, average='binary')\nscore = metrics.accuracy_score(test_batches.labels, pred_label)\nlog_score = metrics.log_loss(pred_label, predictions)","execution_count":null,"outputs":[]},{"metadata":{"id":"z2OrZqtMMF-f","outputId":"d5bb98ba-910c-4cc1-972c-89a6f14b3b55","scrolled":true,"trusted":true},"cell_type":"code","source":"print(\"Precision \",precision*100)\nprint(\"Recall \",recall*100)\nprint(\"F1 Score \",recall*100)\nprint(\"Accuracy of the model\",accuracy*100)\nprint(\"Accuracy score: {}\".format(score))\nprint(\"Log loss score: {}\".format(log_score))","execution_count":null,"outputs":[]},{"metadata":{"id":"B8ez1WmLMF-h","outputId":"0fc0fe11-a995-41ca-8ac8-9d07e5babae1","trusted":true},"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\ndef plot_roc(pred,y):\n    fpr, tpr, _ = roc_curve(y, pred)\n    roc_auc = auc(fpr, tpr)\n\n    plt.figure()\n    plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.xlim([0.0, 1.0])\n    plt.ylim([0.0, 1.05])\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title('Receiver Operating Characteristic (ROC)')\n    plt.legend(loc=\"lower right\")\n    plt.show()\nplot_roc(pred_label,test_batches.labels)","execution_count":null,"outputs":[]}],"metadata":{"accelerator":"TPU","colab":{"collapsed_sections":[],"name":"colab_kaggle.ipynb","provenance":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.4"}},"nbformat":4,"nbformat_minor":4}