{"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 zipfile\nimport os\nimport numpy as np\nimport pandas as pd \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nfrom PIL import Image\nimport keras\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:24.622785Z","iopub.execute_input":"2022-12-19T19:07:24.623696Z","iopub.status.idle":"2022-12-19T19:07:31.415998Z","shell.execute_reply.started":"2022-12-19T19:07:24.623583Z","shell.execute_reply":"2022-12-19T19:07:31.414984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_path='/kaggle/input/dogs-vs-cats/train.zip'\nsave_path = '/kaggle/working/'\nfile=zipfile.ZipFile(zip_path)\nfile.extractall(save_path)\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:31.417933Z","iopub.execute_input":"2022-12-19T19:07:31.418888Z","iopub.status.idle":"2022-12-19T19:07:43.824363Z","shell.execute_reply.started":"2022-12-19T19:07:31.418845Z","shell.execute_reply":"2022-12-19T19:07:43.823231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_path='/kaggle/input/dogs-vs-cats/test1.zip'\nsave_path = '/kaggle/working/'\nfile=zipfile.ZipFile(zip_path)\nfile.extractall(save_path)\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:43.825958Z","iopub.execute_input":"2022-12-19T19:07:43.826333Z","iopub.status.idle":"2022-12-19T19:07:49.251462Z","shell.execute_reply.started":"2022-12-19T19:07:43.826294Z","shell.execute_reply":"2022-12-19T19:07:49.250387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"./train\")\ncategories = []\nfor image in filenames :\n    category = image.split('.')[0]\n    if category == 'dog' :\n        categories.append(1)\n    else :    \n        categories.append(0)\n\nimg_height, img_width = 128, 128\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:49.253877Z","iopub.execute_input":"2022-12-19T19:07:49.254232Z","iopub.status.idle":"2022-12-19T19:07:49.284055Z","shell.execute_reply.started":"2022-12-19T19:07:49.254196Z","shell.execute_reply":"2022-12-19T19:07:49.283046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'filename' : filenames, 'category' : categories})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:49.287575Z","iopub.execute_input":"2022-12-19T19:07:49.287880Z","iopub.status.idle":"2022-12-19T19:07:49.320838Z","shell.execute_reply.started":"2022-12-19T19:07:49.287851Z","shell.execute_reply":"2022-12-19T19:07:49.319842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:49.322334Z","iopub.execute_input":"2022-12-19T19:07:49.322744Z","iopub.status.idle":"2022-12-19T19:07:49.335835Z","shell.execute_reply.started":"2022-12-19T19:07:49.322658Z","shell.execute_reply":"2022-12-19T19:07:49.334617Z"},"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\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', \n                 input_shape=(img_width, img_height, 3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\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\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\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation='softmax'))\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:49.337960Z","iopub.execute_input":"2022-12-19T19:07:49.338743Z","iopub.status.idle":"2022-12-19T19:07:53.238555Z","shell.execute_reply.started":"2022-12-19T19:07:49.338704Z","shell.execute_reply":"2022-12-19T19:07:53.237478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = keras.callbacks.EarlyStopping(patience=10)\nlearning_rate_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:53.240109Z","iopub.execute_input":"2022-12-19T19:07:53.240750Z","iopub.status.idle":"2022-12-19T19:07:53.250697Z","shell.execute_reply.started":"2022-12-19T19:07:53.240711Z","shell.execute_reply":"2022-12-19T19:07:53.249637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'] = df['category'].map({0 : 'cat', 1 : 'dog'})","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:53.254777Z","iopub.execute_input":"2022-12-19T19:07:53.256187Z","iopub.status.idle":"2022-12-19T19:07:54.250098Z","shell.execute_reply.started":"2022-12-19T19:07:53.256034Z","shell.execute_reply":"2022-12-19T19:07:54.248889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(df, test_size=0.2, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalid_df = valid_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:54.254915Z","iopub.execute_input":"2022-12-19T19:07:54.255277Z","iopub.status.idle":"2022-12-19T19:07:54.268737Z","shell.execute_reply.started":"2022-12-19T19:07:54.255246Z","shell.execute_reply":"2022-12-19T19:07:54.267434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_valid = valid_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:54.270159Z","iopub.execute_input":"2022-12-19T19:07:54.270992Z","iopub.status.idle":"2022-12-19T19:07:54.276778Z","shell.execute_reply.started":"2022-12-19T19:07:54.270937Z","shell.execute_reply":"2022-12-19T19:07:54.275557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = keras.preprocessing.image.ImageDataGenerator(rotation_range=20,\n                                                         rescale = 1./255,\n                                                         horizontal_flip=True)\ntrain_generator = train_data.flow_from_dataframe(train_df,\n                                                './train/',\n                                                x_col = 'filename',\n                                                y_col = 'category',\n                                                target_size=[128,128],\n                                                class_mode='categorical',\n                                                batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:54.278702Z","iopub.execute_input":"2022-12-19T19:07:54.279125Z","iopub.status.idle":"2022-12-19T19:07:54.467283Z","shell.execute_reply.started":"2022-12-19T19:07:54.279085Z","shell.execute_reply":"2022-12-19T19:07:54.466247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data = keras.preprocessing.image.ImageDataGenerator(rescale = 1./255)\n                                                         \n                                                         \nvalid_generator = valid_data.flow_from_dataframe(valid_df,\n                                                './train/',\n                                                x_col = 'filename',\n                                                y_col = 'category',\n                                                target_size=[128,128],\n                                                class_mode='categorical',\n                                                batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:54.468717Z","iopub.execute_input":"2022-12-19T19:07:54.469746Z","iopub.status.idle":"2022-12-19T19:07:54.526535Z","shell.execute_reply.started":"2022-12-19T19:07:54.469706Z","shell.execute_reply":"2022-12-19T19:07:54.525388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=valid_generator,\n    validation_steps=total_valid//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:07:54.527967Z","iopub.execute_input":"2022-12-19T19:07:54.528900Z","iopub.status.idle":"2022-12-19T19:30:53.239735Z","shell.execute_reply.started":"2022-12-19T19:07:54.528868Z","shell.execute_reply":"2022-12-19T19:30:53.238711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:30:53.243567Z","iopub.execute_input":"2022-12-19T19:30:53.243889Z","iopub.status.idle":"2022-12-19T19:30:53.378889Z","shell.execute_reply.started":"2022-12-19T19:30:53.243861Z","shell.execute_reply":"2022-12-19T19:30:53.377872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_loss(history):\n    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))\n    ax1.plot(history.history['loss'], color='b', label=\"Training loss\")\n    ax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\n    ax1.set_xticks(np.arange(1, epochs, 1))\n\n    ax2.plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\n    ax2.plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\n    ax2.set_xticks(np.arange(1, epochs, 1))\n\n    legend = plt.legend(loc='best', shadow=True)\n    plt.tight_layout()\n    plt.show()\nplot_loss(history)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:30:53.380195Z","iopub.execute_input":"2022-12-19T19:30:53.380586Z","iopub.status.idle":"2022-12-19T19:30:53.748365Z","shell.execute_reply.started":"2022-12-19T19:30:53.380550Z","shell.execute_reply":"2022-12-19T19:30:53.747376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"./test1\")\ntest_df = pd.DataFrame({'filename' : filenames})    \nsamples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:30:53.749978Z","iopub.execute_input":"2022-12-19T19:30:53.750380Z","iopub.status.idle":"2022-12-19T19:30:53.765296Z","shell.execute_reply.started":"2022-12-19T19:30:53.750305Z","shell.execute_reply":"2022-12-19T19:30:53.764075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\ntest_generator = test_data.flow_from_dataframe(\n    test_df, \n    \"./test1/\", \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)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:30:53.766714Z","iopub.execute_input":"2022-12-19T19:30:53.767193Z","iopub.status.idle":"2022-12-19T19:30:53.868135Z","shell.execute_reply.started":"2022-12-19T19:30:53.767154Z","shell.execute_reply":"2022-12-19T19:30:53.867029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(samples/batch_size))\ntest_df['category'] = np.argmax(predict, axis=-1)\ntest_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2022-12-19T19:30:53.869780Z","iopub.execute_input":"2022-12-19T19:30:53.870176Z","iopub.status.idle":"2022-12-19T19:31:26.414558Z","shell.execute_reply.started":"2022-12-19T19:30:53.870137Z","shell.execute_reply":"2022-12-19T19:31:26.413563Z"},"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":"2022-12-19T19:31:26.416344Z","iopub.execute_input":"2022-12-19T19:31:26.417101Z","iopub.status.idle":"2022-12-19T19:31:26.643715Z","shell.execute_reply.started":"2022-12-19T19:31:26.417063Z","shell.execute_reply":"2022-12-19T19:31:26.642628Z"},"trusted":true},"execution_count":null,"outputs":[]}]}