{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-06T15:25:03.725972Z","iopub.execute_input":"2023-04-06T15:25:03.726366Z","iopub.status.idle":"2023-04-06T15:25:03.761544Z","shell.execute_reply.started":"2023-04-06T15:25:03.726332Z","shell.execute_reply":"2023-04-06T15:25:03.760184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nos.listdir('../input/dogs-vs-cats-redux-kernels-edition')","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:03.763441Z","iopub.execute_input":"2023-04-06T15:25:03.764004Z","iopub.status.idle":"2023-04-06T15:25:15.417615Z","shell.execute_reply.started":"2023-04-06T15:25:03.763967Z","shell.execute_reply":"2023-04-06T15:25:15.416475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../working'))  #current working folder or directory","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:15.418883Z","iopub.execute_input":"2023-04-06T15:25:15.419495Z","iopub.status.idle":"2023-04-06T15:25:15.425889Z","shell.execute_reply.started":"2023-04-06T15:25:15.419462Z","shell.execute_reply":"2023-04-06T15:25:15.424597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))\n        ","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:15.428372Z","iopub.execute_input":"2023-04-06T15:25:15.429203Z","iopub.status.idle":"2023-04-06T15:25:32.957195Z","shell.execute_reply.started":"2023-04-06T15:25:15.429170Z","shell.execute_reply":"2023-04-06T15:25:32.956407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../working'))  #current working folder or directory","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:32.958401Z","iopub.execute_input":"2023-04-06T15:25:32.958699Z","iopub.status.idle":"2023-04-06T15:25:32.966332Z","shell.execute_reply.started":"2023-04-06T15:25:32.958669Z","shell.execute_reply":"2023-04-06T15:25:32.964424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_Folder_Path = \"../working/train\"\nTest_Folder_Path = \"../working/test\"\ntrain_file_names = os.listdir(Train_Folder_Path)\ntest_file_names = os.listdir(Test_Folder_Path)\ntrain_file_names[0:7]","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:32.968727Z","iopub.execute_input":"2023-04-06T15:25:32.970915Z","iopub.status.idle":"2023-04-06T15:25:32.996446Z","shell.execute_reply.started":"2023-04-06T15:25:32.970841Z","shell.execute_reply":"2023-04-06T15:25:32.994355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = os.listdir(Test_Folder_Path)\ntest_data[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:32.999817Z","iopub.execute_input":"2023-04-06T15:25:33.000187Z","iopub.status.idle":"2023-04-06T15:25:33.013068Z","shell.execute_reply.started":"2023-04-06T15:25:33.000156Z","shell.execute_reply":"2023-04-06T15:25:33.011037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label = []\na = os.listdir(\"../working/test\")\nfor i in range(len(a)):\n    test_label.append(a[i].split(\".\")[0])\ntest_label[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.015177Z","iopub.execute_input":"2023-04-06T15:25:33.015540Z","iopub.status.idle":"2023-04-06T15:25:33.035519Z","shell.execute_reply.started":"2023-04-06T15:25:33.015508Z","shell.execute_reply":"2023-04-06T15:25:33.033165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = []\nfull_paths = []\nfor file_name in train_file_names:\n    target = file_name.split(\".\")[0]\n    full_path = os.path.join(Train_Folder_Path, file_name)\n    full_paths.append(full_path)\n    targets.append(target)\n\ntrain_df = pd.DataFrame()\ntrain_df['image_path'] = full_paths\ntrain_df['target'] = targets","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.037496Z","iopub.execute_input":"2023-04-06T15:25:33.037938Z","iopub.status.idle":"2023-04-06T15:25:33.119018Z","shell.execute_reply.started":"2023-04-06T15:25:33.037893Z","shell.execute_reply":"2023-04-06T15:25:33.117769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.123688Z","iopub.execute_input":"2023-04-06T15:25:33.124057Z","iopub.status.idle":"2023-04-06T15:25:33.148713Z","shell.execute_reply.started":"2023-04-06T15:25:33.124022Z","shell.execute_reply":"2023-04-06T15:25:33.146819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_paths = []\nfor file_name in test_file_names:\n    target = file_name.split(\".\")[0]\n    full_path = os.path.join(Test_Folder_Path, file_name)\n    full_paths.append(full_path)\n\ntest_df = pd.DataFrame()\ntest_df['image_path'] = full_paths","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.152886Z","iopub.execute_input":"2023-04-06T15:25:33.153255Z","iopub.status.idle":"2023-04-06T15:25:33.189166Z","shell.execute_reply.started":"2023-04-06T15:25:33.153220Z","shell.execute_reply":"2023-04-06T15:25:33.187895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.190438Z","iopub.execute_input":"2023-04-06T15:25:33.190730Z","iopub.status.idle":"2023-04-06T15:25:33.201483Z","shell.execute_reply.started":"2023-04-06T15:25:33.190697Z","shell.execute_reply":"2023-04-06T15:25:33.199807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data,val_data = train_test_split(train_df,random_state=234)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.202914Z","iopub.execute_input":"2023-04-06T15:25:33.203305Z","iopub.status.idle":"2023-04-06T15:25:33.221889Z","shell.execute_reply.started":"2023-04-06T15:25:33.203238Z","shell.execute_reply":"2023-04-06T15:25:33.220904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axes=plt.subplots(5,5,figsize=(10,10))\naxes = axes.ravel()\nfor i in range(25):\n    axes[i].imshow(img.imread(train_df.iloc[i,0]))\n    axes[i].axis('off')\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:33.223308Z","iopub.execute_input":"2023-04-06T15:25:33.223618Z","iopub.status.idle":"2023-04-06T15:25:34.905778Z","shell.execute_reply.started":"2023-04-06T15:25:33.223578Z","shell.execute_reply":"2023-04-06T15:25:34.904479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(zoom_range=.18,rotation_range=10,rescale=1./255,shear_range=0.1,\n                            horizontal_flip=True,width_shift_range=0.1,height_shift_range=0.1)\ntrain_data_generator = train_datagen.flow_from_dataframe(dataframe=train_data,x_col='image_path',y_col='target',\n                                                  target_size=(150,150),class_mode='binary',batch_size=150,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:34.907242Z","iopub.execute_input":"2023-04-06T15:25:34.907802Z","iopub.status.idle":"2023-04-06T15:25:35.084593Z","shell.execute_reply.started":"2023-04-06T15:25:34.907771Z","shell.execute_reply":"2023-04-06T15:25:35.083449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_datagen = ImageDataGenerator(rescale=1./255)\nval_data_generator = val_datagen.flow_from_dataframe(dataframe=val_data,x_col='image_path',y_col='target',\n                                                  target_size=(150,150),class_mode='binary',batch_size=150,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:35.085792Z","iopub.execute_input":"2023-04-06T15:25:35.086093Z","iopub.status.idle":"2023-04-06T15:25:35.154602Z","shell.execute_reply.started":"2023-04-06T15:25:35.086063Z","shell.execute_reply":"2023-04-06T15:25:35.152708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_data_generator = test_datagen.flow_from_dataframe(dataframe=test_df,x_col='image_path',y_col=None,\n                                                  target_size=(150,150),class_mode=None,batch_size=150,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:35.156418Z","iopub.execute_input":"2023-04-06T15:25:35.156914Z","iopub.status.idle":"2023-04-06T15:25:35.247901Z","shell.execute_reply.started":"2023-04-06T15:25:35.156869Z","shell.execute_reply":"2023-04-06T15:25:35.246549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32,kernel_size=(3,3),input_shape=(150,150,3),activation='relu'))\nmodel.add(Conv2D(64,kernel_size=(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:35.249406Z","iopub.execute_input":"2023-04-06T15:25:35.249763Z","iopub.status.idle":"2023-04-06T15:25:35.706307Z","shell.execute_reply.started":"2023-04-06T15:25:35.249709Z","shell.execute_reply":"2023-04-06T15:25:35.705226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", optimizer='adam', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:46.373306Z","iopub.execute_input":"2023-04-06T15:25:46.373726Z","iopub.status.idle":"2023-04-06T15:25:46.421416Z","shell.execute_reply.started":"2023-04-06T15:25:46.373687Z","shell.execute_reply":"2023-04-06T15:25:46.419321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(train_data_generator,epochs=12,validation_data=val_data_generator,\n                 validation_steps=val_data.shape[0]/150,\n                 steps_per_epoch=train_data.shape[0]/150,\n                 verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T15:25:46.423061Z","iopub.execute_input":"2023-04-06T15:25:46.423619Z","iopub.status.idle":"2023-04-06T18:29:25.783416Z","shell.execute_reply.started":"2023-04-06T15:25:46.423573Z","shell.execute_reply":"2023-04-06T18:29:25.778329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score=model.evaluate(val_data_generator)\nprint(score[1])","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:29:25.790872Z","iopub.execute_input":"2023-04-06T18:29:25.791794Z","iopub.status.idle":"2023-04-06T18:30:27.896800Z","shell.execute_reply.started":"2023-04-06T18:29:25.791715Z","shell.execute_reply":"2023-04-06T18:30:27.895671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = hist.history['accuracy']\nval_acc = hist.history['val_accuracy']\nloss = hist.history['loss']\nval_loss = hist.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'go', label='Training Loss')\nplt.plot(epochs, val_loss, 'g', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:30:27.899431Z","iopub.execute_input":"2023-04-06T18:30:27.899918Z","iopub.status.idle":"2023-04-06T18:30:28.319697Z","shell.execute_reply.started":"2023-04-06T18:30:27.899870Z","shell.execute_reply":"2023-04-06T18:30:28.318458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_data_generator)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:30:28.321447Z","iopub.execute_input":"2023-04-06T18:30:28.322653Z","iopub.status.idle":"2023-04-06T18:32:33.231523Z","shell.execute_reply.started":"2023-04-06T18:30:28.322612Z","shell.execute_reply":"2023-04-06T18:32:33.229705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.round(predictions.flatten()).astype(int)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:32:33.233590Z","iopub.execute_input":"2023-04-06T18:32:33.233953Z","iopub.status.idle":"2023-04-06T18:32:33.243375Z","shell.execute_reply.started":"2023-04-06T18:32:33.233915Z","shell.execute_reply":"2023-04-06T18:32:33.242355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.size(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:32:33.245209Z","iopub.execute_input":"2023-04-06T18:32:33.245684Z","iopub.status.idle":"2023-04-06T18:32:33.257439Z","shell.execute_reply.started":"2023-04-06T18:32:33.245641Z","shell.execute_reply":"2023-04-06T18:32:33.255739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':test_label,'label':predictions})\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:32:33.265199Z","iopub.execute_input":"2023-04-06T18:32:33.265709Z","iopub.status.idle":"2023-04-06T18:32:33.305486Z","shell.execute_reply.started":"2023-04-06T18:32:33.265672Z","shell.execute_reply":"2023-04-06T18:32:33.303618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/working/submission.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T18:32:33.308229Z","iopub.execute_input":"2023-04-06T18:32:33.308804Z","iopub.status.idle":"2023-04-06T18:32:33.334113Z","shell.execute_reply.started":"2023-04-06T18:32:33.308753Z","shell.execute_reply":"2023-04-06T18:32:33.332197Z"},"trusted":true},"execution_count":null,"outputs":[]}]}