{"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":"\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport zipfile\nimport os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:02.202781Z","iopub.execute_input":"2021-11-27T23:41:02.203772Z","iopub.status.idle":"2021-11-27T23:41:04.673156Z","shell.execute_reply.started":"2021-11-27T23:41:02.203649Z","shell.execute_reply":"2021-11-27T23:41:04.671963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:04.675245Z","iopub.execute_input":"2021-11-27T23:41:04.675525Z","iopub.status.idle":"2021-11-27T23:41:04.679814Z","shell.execute_reply.started":"2021-11-27T23:41:04.675494Z","shell.execute_reply":"2021-11-27T23:41:04.67857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",'r') as zip_:\n    zip_.extractall(\"/kaggle/files/\")\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",'r') as zip_:\n    zip_.extractall(\"/kaggle/files/\")","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:04.681594Z","iopub.execute_input":"2021-11-27T23:41:04.68199Z","iopub.status.idle":"2021-11-27T23:41:22.129279Z","shell.execute_reply.started":"2021-11-27T23:41:04.68183Z","shell.execute_reply":"2021-11-27T23:41:22.12858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"/kaggle/files/train/\")  \ncategories = [] \nfor filename in filenames:                                \n    category = filename.split('.')[0]   \n    if category == 'dog':\n        categories.append(1) \n    else:\n        categories.append(0) \n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n}) ","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:22.13066Z","iopub.execute_input":"2021-11-27T23:41:22.13104Z","iopub.status.idle":"2021-11-27T23:41:22.182275Z","shell.execute_reply.started":"2021-11-27T23:41:22.131007Z","shell.execute_reply":"2021-11-27T23:41:22.181023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head() ","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:22.185481Z","iopub.execute_input":"2021-11-27T23:41:22.18582Z","iopub.status.idle":"2021-11-27T23:41:22.473291Z","shell.execute_reply.started":"2021-11-27T23:41:22.185782Z","shell.execute_reply":"2021-11-27T23:41:22.472561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail() ","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:22.474828Z","iopub.execute_input":"2021-11-27T23:41:22.47583Z","iopub.status.idle":"2021-11-27T23:41:22.486923Z","shell.execute_reply.started":"2021-11-27T23:41:22.475789Z","shell.execute_reply":"2021-11-27T23:41:22.485977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:22.488226Z","iopub.execute_input":"2021-11-27T23:41:22.488507Z","iopub.status.idle":"2021-11-27T23:41:22.708821Z","shell.execute_reply.started":"2021-11-27T23:41:22.488464Z","shell.execute_reply":"2021-11-27T23:41:22.708125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:22.710368Z","iopub.execute_input":"2021-11-27T23:41:22.710904Z","iopub.status.idle":"2021-11-27T23:41:22.715074Z","shell.execute_reply.started":"2021-11-27T23:41:22.710859Z","shell.execute_reply":"2021-11-27T23:41:22.714102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential \nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\n\n\nmodel = Sequential() \n\n\n\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape = [128, 128, 3]))\n\nmodel.add(BatchNormalization()) \nmodel.add(MaxPooling2D(pool_size=(2, 2))) \nmodel.add(Dropout(0.25)) \n\n\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\n\n\nmodel.add(Conv2D(128, (3, 3), activation='relu')) \nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\n\n\nmodel.add(Flatten())\n\nmodel.add(keras.layers.Dense(512, activation = 'relu'))\nmodel.add(keras.layers.Dropout(0.3))\n\nmodel.add(keras.layers.Dense(2, activation = 'sigmoid'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) \n\n\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:22.716766Z","iopub.execute_input":"2021-11-27T23:41:22.717253Z","iopub.status.idle":"2021-11-27T23:41:23.023715Z","shell.execute_reply.started":"2021-11-27T23:41:22.717208Z","shell.execute_reply":"2021-11-27T23:41:23.022411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'})","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.025654Z","iopub.execute_input":"2021-11-27T23:41:23.026008Z","iopub.status.idle":"2021-11-27T23:41:23.036992Z","shell.execute_reply.started":"2021-11-27T23:41:23.02596Z","shell.execute_reply":"2021-11-27T23:41:23.036032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.038282Z","iopub.execute_input":"2021-11-27T23:41:23.038616Z","iopub.status.idle":"2021-11-27T23:41:23.05283Z","shell.execute_reply.started":"2021-11-27T23:41:23.038575Z","shell.execute_reply":"2021-11-27T23:41:23.051854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.054975Z","iopub.execute_input":"2021-11-27T23:41:23.055274Z","iopub.status.idle":"2021-11-27T23:41:23.066408Z","shell.execute_reply.started":"2021-11-27T23:41:23.055241Z","shell.execute_reply":"2021-11-27T23:41:23.065365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.reset_index(drop=True) ","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.067936Z","iopub.execute_input":"2021-11-27T23:41:23.068185Z","iopub.status.idle":"2021-11-27T23:41:23.076249Z","shell.execute_reply.started":"2021-11-27T23:41:23.068154Z","shell.execute_reply":"2021-11-27T23:41:23.075092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.081385Z","iopub.execute_input":"2021-11-27T23:41:23.083099Z","iopub.status.idle":"2021-11-27T23:41:23.096349Z","shell.execute_reply.started":"2021-11-27T23:41:23.08302Z","shell.execute_reply":"2021-11-27T23:41:23.094992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.09828Z","iopub.execute_input":"2021-11-27T23:41:23.09857Z","iopub.status.idle":"2021-11-27T23:41:23.294357Z","shell.execute_reply.started":"2021-11-27T23:41:23.098536Z","shell.execute_reply":"2021-11-27T23:41:23.293698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.295982Z","iopub.execute_input":"2021-11-27T23:41:23.297077Z","iopub.status.idle":"2021-11-27T23:41:23.309881Z","shell.execute_reply.started":"2021-11-27T23:41:23.297022Z","shell.execute_reply":"2021-11-27T23:41:23.308856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.310961Z","iopub.execute_input":"2021-11-27T23:41:23.311558Z","iopub.status.idle":"2021-11-27T23:41:23.503924Z","shell.execute_reply.started":"2021-11-27T23:41:23.311523Z","shell.execute_reply":"2021-11-27T23:41:23.503102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.505299Z","iopub.execute_input":"2021-11-27T23:41:23.505557Z","iopub.status.idle":"2021-11-27T23:41:23.510354Z","shell.execute_reply.started":"2021-11-27T23:41:23.505525Z","shell.execute_reply":"2021-11-27T23:41:23.509049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape) \nprint(validate_df.shape) ","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.511781Z","iopub.execute_input":"2021-11-27T23:41:23.512005Z","iopub.status.idle":"2021-11-27T23:41:23.527669Z","shell.execute_reply.started":"2021-11-27T23:41:23.511977Z","shell.execute_reply":"2021-11-27T23:41:23.526834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=20,\n    rescale=1./255,\n    zoom_range=0.2,\n    horizontal_flip=True,\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.529296Z","iopub.execute_input":"2021-11-27T23:41:23.529739Z","iopub.status.idle":"2021-11-27T23:41:23.538739Z","shell.execute_reply.started":"2021-11-27T23:41:23.529686Z","shell.execute_reply":"2021-11-27T23:41:23.537548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe( \n    train_df, \n    \"/kaggle/files/train/\",\n    x_col='filename',\n    y_col='category', \n    target_size=[128, 128], \n    class_mode='categorical', \n    batch_size=batch_size \n)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.540831Z","iopub.execute_input":"2021-11-27T23:41:23.541308Z","iopub.status.idle":"2021-11-27T23:41:23.81137Z","shell.execute_reply.started":"2021-11-27T23:41:23.541272Z","shell.execute_reply":"2021-11-27T23:41:23.810387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\n\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/files/train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=[128,128],\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.812794Z","iopub.execute_input":"2021-11-27T23:41:23.813043Z","iopub.status.idle":"2021-11-27T23:41:23.888829Z","shell.execute_reply.started":"2021-11-27T23:41:23.81301Z","shell.execute_reply":"2021-11-27T23:41:23.88794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=5 \n                            \nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,   \n    validation_data=validation_generator, \n    validation_steps=total_validate//batch_size, \n                                                \n    \n    steps_per_epoch=total_train//batch_size,                           \n)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T23:41:23.962563Z","iopub.execute_input":"2021-11-27T23:41:23.963004Z","iopub.status.idle":"2021-11-28T00:23:15.244831Z","shell.execute_reply.started":"2021-11-27T23:41:23.962968Z","shell.execute_reply":"2021-11-28T00:23:15.243402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12)) \nax1.plot(history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:28:30.760978Z","iopub.execute_input":"2021-11-28T00:28:30.761701Z","iopub.status.idle":"2021-11-28T00:28:31.195718Z","shell.execute_reply.started":"2021-11-28T00:28:30.761622Z","shell.execute_reply":"2021-11-28T00:28:31.194469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/files/train/\") \ntest_df = pd.DataFrame({                                       \n    'filename': test_filenames                                 \n})\nnb_samples = test_df.shape[0] ","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:28:56.962736Z","iopub.execute_input":"2021-11-28T00:28:56.963033Z","iopub.status.idle":"2021-11-28T00:28:56.992574Z","shell.execute_reply.started":"2021-11-28T00:28:56.963001Z","shell.execute_reply":"2021-11-28T00:28:56.991499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/files/train/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=[128,128],\n    batch_size=batch_size,\n    shuffle=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:29:03.143901Z","iopub.execute_input":"2021-11-28T00:29:03.144257Z","iopub.status.idle":"2021-11-28T00:29:03.4215Z","shell.execute_reply.started":"2021-11-28T00:29:03.144222Z","shell.execute_reply":"2021-11-28T00:29:03.420331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:29:08.096221Z","iopub.execute_input":"2021-11-28T00:29:08.097269Z","iopub.status.idle":"2021-11-28T00:32:30.687639Z","shell.execute_reply.started":"2021-11-28T00:29:08.097216Z","shell.execute_reply":"2021-11-28T00:32:30.686243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:32:58.829448Z","iopub.execute_input":"2021-11-28T00:32:58.829829Z","iopub.status.idle":"2021-11-28T00:32:58.837182Z","shell.execute_reply.started":"2021-11-28T00:32:58.82979Z","shell.execute_reply":"2021-11-28T00:32:58.836517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:33:01.943929Z","iopub.execute_input":"2021-11-28T00:33:01.944576Z","iopub.status.idle":"2021-11-28T00:33:01.95197Z","shell.execute_reply.started":"2021-11-28T00:33:01.944528Z","shell.execute_reply":"2021-11-28T00:33:01.951118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category']","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:33:57.527003Z","iopub.execute_input":"2021-11-28T00:33:57.527932Z","iopub.status.idle":"2021-11-28T00:33:57.5382Z","shell.execute_reply.started":"2021-11-28T00:33:57.527878Z","shell.execute_reply":"2021-11-28T00:33:57.536911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict((v,k) for k,v in train_generator.class_indices.items())","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:34:03.601404Z","iopub.execute_input":"2021-11-28T00:34:03.602513Z","iopub.status.idle":"2021-11-28T00:34:03.609148Z","shell.execute_reply.started":"2021-11-28T00:34:03.602404Z","shell.execute_reply":"2021-11-28T00:34:03.608494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:36:52.304755Z","iopub.execute_input":"2021-11-28T00:36:52.306012Z","iopub.status.idle":"2021-11-28T00:36:52.322538Z","shell.execute_reply.started":"2021-11-28T00:36:52.305956Z","shell.execute_reply":"2021-11-28T00:36:52.321607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:35:59.363989Z","iopub.execute_input":"2021-11-28T00:35:59.365589Z","iopub.status.idle":"2021-11-28T00:35:59.381771Z","shell.execute_reply.started":"2021-11-28T00:35:59.365522Z","shell.execute_reply":"2021-11-28T00:35:59.380562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:36:08.092771Z","iopub.execute_input":"2021-11-28T00:36:08.093138Z","iopub.status.idle":"2021-11-28T00:36:08.100579Z","shell.execute_reply.started":"2021-11-28T00:36:08.093099Z","shell.execute_reply":"2021-11-28T00:36:08.099266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:23:15.981164Z","iopub.status.idle":"2021-11-28T00:23:15.981526Z","shell.execute_reply.started":"2021-11-28T00:23:15.981319Z","shell.execute_reply":"2021-11-28T00:23:15.981341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(18)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:23:15.982741Z","iopub.status.idle":"2021-11-28T00:23:15.983072Z","shell.execute_reply.started":"2021-11-28T00:23:15.982898Z","shell.execute_reply":"2021-11-28T00:23:15.982918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T00:23:15.984741Z","iopub.status.idle":"2021-11-28T00:23:15.985078Z","shell.execute_reply.started":"2021-11-28T00:23:15.984905Z","shell.execute_reply":"2021-11-28T00:23:15.984926Z"},"trusted":true},"execution_count":null,"outputs":[]}]}