{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\n\n# keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom keras.optimizers import Adam\nfrom keras.losses import SparseCategoricalCrossentropy\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# sklearn library\nfrom sklearn.preprocessing import StandardScaler \nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import classification_report ","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:04.806607Z","iopub.execute_input":"2022-05-16T05:05:04.806991Z","iopub.status.idle":"2022-05-16T05:05:04.813663Z","shell.execute_reply.started":"2022-05-16T05:05:04.806953Z","shell.execute_reply":"2022-05-16T05:05:04.812625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# unzipping data files\n\n#! unzip \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\" -d train\n#! unzip \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\" -d test","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-16T05:05:04.978082Z","iopub.execute_input":"2022-05-16T05:05:04.978452Z","iopub.status.idle":"2022-05-16T05:05:04.982672Z","shell.execute_reply.started":"2022-05-16T05:05:04.978418Z","shell.execute_reply":"2022-05-16T05:05:04.981702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-processig Data","metadata":{}},{"cell_type":"code","source":"# creating dataframe\n\n# labels\nfile = os.listdir(\"./train/train\")\nLabels = list(map(lambda x: x.split('.')[0], file))\n\n# for filenames\nf = Path(\"./train/train\")\nFile_Path = list(f.glob(r\"**/*.jpg\"))\n\n# dataframe\nFile_Path = pd.Series(File_Path).astype(str)\nLabels = pd.Series(Labels)\ndf = pd.concat([File_Path,Labels],axis=1)\ndf.columns = ['filename', 'category']","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:05.154414Z","iopub.execute_input":"2022-05-16T05:05:05.154777Z","iopub.status.idle":"2022-05-16T05:05:05.437430Z","shell.execute_reply.started":"2022-05-16T05:05:05.154745Z","shell.execute_reply":"2022-05-16T05:05:05.436453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:05.439138Z","iopub.execute_input":"2022-05-16T05:05:05.439545Z","iopub.status.idle":"2022-05-16T05:05:05.450667Z","shell.execute_reply.started":"2022-05-16T05:05:05.439504Z","shell.execute_reply":"2022-05-16T05:05:05.449436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ensuring data isn't biased\n\ndf['category'].value_counts().plot.bar(color=['green', 'orange']);","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:05.453073Z","iopub.execute_input":"2022-05-16T05:05:05.453489Z","iopub.status.idle":"2022-05-16T05:05:05.590652Z","shell.execute_reply.started":"2022-05-16T05:05:05.453448Z","shell.execute_reply":"2022-05-16T05:05:05.589567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Viewing dataframe images\n\nfig, axes = plt.subplots(nrows=3,ncols=3, figsize=(7, 7),\n                        subplot_kw={'xticks': [], 'yticks': []})\n\n# iterating over images\nfor name, ax, in enumerate(axes.flatten()):\n    ax.imshow(plt.imread(df.filename[name]))   # reading images\n    ax.set_title(df.category[name])            #labelling images\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:05.592669Z","iopub.execute_input":"2022-05-16T05:05:05.593070Z","iopub.status.idle":"2022-05-16T05:05:06.646534Z","shell.execute_reply.started":"2022-05-16T05:05:05.593030Z","shell.execute_reply":"2022-05-16T05:05:06.633771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting the dataset\n# specified random state for split to give same output\n\ntrain_set, test_data = train_test_split(df, test_size= 0.2, random_state = 42)\ntrain_data, val_data = train_test_split(train_set, test_size= 0.2, random_state = 42)\n\n\nprint(train_data.shape)\nprint(test_data.shape)\nprint(val_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:06.647921Z","iopub.execute_input":"2022-05-16T05:05:06.648319Z","iopub.status.idle":"2022-05-16T05:05:06.691212Z","shell.execute_reply.started":"2022-05-16T05:05:06.648279Z","shell.execute_reply":"2022-05-16T05:05:06.689911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resetting index\n\ntrain_data = train_data.reset_index(drop=True)\ntest_data = test_data.reset_index(drop=True)\nval_data = val_data.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:06.696591Z","iopub.execute_input":"2022-05-16T05:05:06.699474Z","iopub.status.idle":"2022-05-16T05:05:06.710668Z","shell.execute_reply.started":"2022-05-16T05:05:06.699429Z","shell.execute_reply":"2022-05-16T05:05:06.709168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation and Generators","metadata":{}},{"cell_type":"code","source":"img_size = (128, 128)\ninput_shape = (128, 128, 3)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:06.717071Z","iopub.execute_input":"2022-05-16T05:05:06.720073Z","iopub.status.idle":"2022-05-16T05:05:06.728413Z","shell.execute_reply.started":"2022-05-16T05:05:06.720025Z","shell.execute_reply":"2022-05-16T05:05:06.726978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image Generator\n\nimg_gen = ImageDataGenerator(    \n    rotation_range=10,\n    rescale=1./255,\n    horizontal_flip=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:06.736811Z","iopub.execute_input":"2022-05-16T05:05:06.739634Z","iopub.status.idle":"2022-05-16T05:05:06.748235Z","shell.execute_reply.started":"2022-05-16T05:05:06.739588Z","shell.execute_reply":"2022-05-16T05:05:06.746989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example generator\n\nexample_data = train_data.sample(n=1).reset_index(drop=True)\n\nexample_gen = img_gen.flow_from_dataframe(\n    dataframe = example_data,\n    x_col = 'filename', #name of the column containing the image in the test set\n    y_col ='category', #name of column containing the target in the test set\n    target_size = img_size,\n    class_mode ='categorical',\n    batch_size = 32,\n    shuffle = False # not to shuffle the given data\n)\n\nfor i in range(1, 10):\n    plt.subplot(3, 3, i)\n    for X_batch, Y_batch in example_gen:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.axis('Off')\n        break\n\nplt.tight_layout()  # for auto padding \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:06.755240Z","iopub.execute_input":"2022-05-16T05:05:06.758110Z","iopub.status.idle":"2022-05-16T05:05:07.681808Z","shell.execute_reply.started":"2022-05-16T05:05:06.758069Z","shell.execute_reply":"2022-05-16T05:05:07.680841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training generator\ntrain_gen = img_gen.flow_from_dataframe(\n    train_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='categorical',\n    batch_size=32,\n    shuffle = False\n)\n\n# validation generator\nvalidation_gen = img_gen.flow_from_dataframe(\n    val_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='categorical',\n    batch_size=32,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:07.683205Z","iopub.execute_input":"2022-05-16T05:05:07.683697Z","iopub.status.idle":"2022-05-16T05:05:07.931714Z","shell.execute_reply.started":"2022-05-16T05:05:07.683654Z","shell.execute_reply":"2022-05-16T05:05:07.930725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN Model","metadata":{}},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.utils.vis_utils import plot_model\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom keras.applications.vgg19 import VGG19\nfrom keras.applications.inception_v3 import InceptionV3\nfrom keras.layers import Dense, Input, concatenate, Dropout\nminput = Input(shape=input_shape)\nx1 = ResNet50(weights='imagenet', include_top=False, pooling='avg')(minput)\nx2 = VGG19(weights='imagenet', include_top=False, pooling='avg',)(minput)\nx3 = InceptionV3(weights='imagenet', include_top=False, pooling= 'avg')(minput)\nmerge = concatenate([x3,x2,x1])\nx = Dense(units=1024, activation='relu')(merge)\nx = Dropout(0.3)(x)\noutput = Dense(2, activation='softmax')(x)\nmodel = Model(inputs=minput, outputs=output)\nfreeze = 3\nfor layer in model.layers[:freeze]:\n    layer.trainable = False\nfor layer in model.layers[freeze:]:\n    layer.trainable = True\nmodel.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:07.933159Z","iopub.execute_input":"2022-05-16T05:05:07.933522Z","iopub.status.idle":"2022-05-16T05:05:13.391445Z","shell.execute_reply.started":"2022-05-16T05:05:07.933482Z","shell.execute_reply":"2022-05-16T05:05:13.390532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fitting model\n\nhistory = model.fit(train_gen,\n                    validation_data = validation_gen,\n                    epochs = 5,\n                    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T05:05:13.392935Z","iopub.execute_input":"2022-05-16T05:05:13.393312Z","iopub.status.idle":"2022-05-16T06:24:50.978913Z","shell.execute_reply.started":"2022-05-16T05:05:13.393276Z","shell.execute_reply":"2022-05-16T06:24:50.978027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"# Loss vs Accuracy\n\n#accuracy\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\n#loss\nloss = history.history['loss'] \nval_loss = history.history['val_loss']","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:24:50.980507Z","iopub.execute_input":"2022-05-16T06:24:50.980845Z","iopub.status.idle":"2022-05-16T06:24:50.986691Z","shell.execute_reply.started":"2022-05-16T06:24:50.980809Z","shell.execute_reply":"2022-05-16T06:24:50.985751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\n\n# visualising Accuracy \nplt.subplot(2, 1, 1)\n\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\n\nplt.ylabel('Accuracy') \nplt.title('Training and Validation Accuracy') \n\n# Visualising Loss\nplt.subplot(2, 1, 2)\n\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\n\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\n\nlegend = plt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:24:50.988945Z","iopub.execute_input":"2022-05-16T06:24:50.989675Z","iopub.status.idle":"2022-05-16T06:24:51.264143Z","shell.execute_reply.started":"2022-05-16T06:24:50.989635Z","shell.execute_reply":"2022-05-16T06:24:51.263338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking accuracy on the test data we left aside\n\ntest_gen = img_gen.flow_from_dataframe(\n    test_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='categorical',\n    batch_size=32,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:24:51.265389Z","iopub.execute_input":"2022-05-16T06:24:51.265899Z","iopub.status.idle":"2022-05-16T06:24:51.323173Z","shell.execute_reply.started":"2022-05-16T06:24:51.265845Z","shell.execute_reply":"2022-05-16T06:24:51.322064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the label of the test_images\npred = model.predict(test_gen)\npred = np.argmax(pred,axis = 1) # pick the class with highest probability\n\n# labelling data\nlabels = (train_gen.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npred2 = [labels[k] for k in pred]\n\n# expected output\ny_test = test_data.category\n\n# model accuracy\nprint(classification_report(y_test, pred2))","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:24:51.324606Z","iopub.execute_input":"2022-05-16T06:24:51.324965Z","iopub.status.idle":"2022-05-16T06:28:45.068866Z","shell.execute_reply.started":"2022-05-16T06:24:51.324930Z","shell.execute_reply":"2022-05-16T06:28:45.067829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display 15 picture of the dataset with their labels --> EXTRAS\n\nfig, axes = plt.subplots(nrows=3, ncols=5, figsize=(15, 10),\n                        subplot_kw={'xticks': [], 'yticks': []})\n\ncolor = \"blue\" if pred2[i] == test_data.category.iloc[i] else \"red\"\nfor i, ax ,in enumerate(axes.flat):\n    ax.imshow(plt.imread(test_data.filename.iloc[i]))\n    ax.set_title(f\"True: {test_data.category.iloc[i]}\\nPredicted: {pred2[i]}\",color=color)\n    \nplt.subplots_adjust(hspace = 0.3)\nplt.suptitle(\"Model predictions (blue: correct, red: incorrect)\",y=0.98)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:28:45.070390Z","iopub.execute_input":"2022-05-16T06:28:45.071035Z","iopub.status.idle":"2022-05-16T06:28:46.531987Z","shell.execute_reply.started":"2022-05-16T06:28:45.070991Z","shell.execute_reply":"2022-05-16T06:28:46.531096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"test_filenames = os.listdir(\"./test/test\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:28:46.533218Z","iopub.execute_input":"2022-05-16T06:28:46.533737Z","iopub.status.idle":"2022-05-16T06:28:46.555130Z","shell.execute_reply.started":"2022-05-16T06:28:46.533702Z","shell.execute_reply":"2022-05-16T06:28:46.553827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking accuracy on the test data we left aside\n\ntest_gen = img_gen.flow_from_dataframe(\n    test_df, \n    \"./test/test\",\n    x_col='filename',\n    y_col=None,\n    target_size=img_size,\n    class_mode=None,\n    batch_size=32,\n    shuffle = False # not to shuffle the given data\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:28:46.556985Z","iopub.execute_input":"2022-05-16T06:28:46.557375Z","iopub.status.idle":"2022-05-16T06:28:46.683592Z","shell.execute_reply.started":"2022-05-16T06:28:46.557338Z","shell.execute_reply":"2022-05-16T06:28:46.682376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the label of the test_images\npred = model.predict(test_gen)\npred = np.argmax(pred,axis = 1) # pick the class with highest probability\n\n# labelling data\nlabels = (train_gen.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npred2 = [labels[k] for k in pred]","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:28:46.685412Z","iopub.execute_input":"2022-05-16T06:28:46.685814Z","iopub.status.idle":"2022-05-16T06:38:26.480146Z","shell.execute_reply.started":"2022-05-16T06:28:46.685771Z","shell.execute_reply":"2022-05-16T06:38:26.479175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = np.zeros(len(pred2), dtype='int')\n\npred2 = np.array(pred2)\npred2=='cat'\n\nlabel[pred2=='cat']= 0\nlabel[pred2=='dog']= 1","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:38:26.481666Z","iopub.execute_input":"2022-05-16T06:38:26.482037Z","iopub.status.idle":"2022-05-16T06:38:26.490725Z","shell.execute_reply.started":"2022-05-16T06:38:26.482000Z","shell.execute_reply":"2022-05-16T06:38:26.489709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = pd.Series(label)\nind = np.arange(1, len(pred2)+1)\nmy_submission = pd.DataFrame({'id':ind, 'label':label})\nmy_submission","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:38:26.492379Z","iopub.execute_input":"2022-05-16T06:38:26.493281Z","iopub.status.idle":"2022-05-16T06:38:26.511297Z","shell.execute_reply.started":"2022-05-16T06:38:26.493152Z","shell.execute_reply":"2022-05-16T06:38:26.510276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T06:38:26.516161Z","iopub.execute_input":"2022-05-16T06:38:26.516458Z","iopub.status.idle":"2022-05-16T06:38:26.547835Z","shell.execute_reply.started":"2022-05-16T06:38:26.516427Z","shell.execute_reply":"2022-05-16T06:38:26.546944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Refresh the contents of the output directory '/kaggle/working' to find the csv file******","metadata":{}}]}