{"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 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-05-06T13:53:07.882818Z","iopub.execute_input":"2023-05-06T13:53:07.883318Z","iopub.status.idle":"2023-05-06T13:53:15.901345Z","shell.execute_reply.started":"2023-05-06T13:53:07.883282Z","shell.execute_reply":"2023-05-06T13:53:15.900171Z"},"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-05-06T13:53:18.603951Z","iopub.execute_input":"2023-05-06T13:53:18.604973Z","iopub.status.idle":"2023-05-06T13:53:18.611425Z","shell.execute_reply.started":"2023-05-06T13:53:18.604933Z","shell.execute_reply":"2023-05-06T13:53:18.610301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Unzipping training file**","metadata":{}},{"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-05-06T13:53:21.756917Z","iopub.execute_input":"2023-05-06T13:53:21.757804Z","iopub.status.idle":"2023-05-06T13:53:43.106767Z","shell.execute_reply.started":"2023-05-06T13:53:21.757765Z","shell.execute_reply":"2023-05-06T13:53:43.104203Z"},"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-05-06T13:53:43.108810Z","iopub.execute_input":"2023-05-06T13:53:43.110104Z","iopub.status.idle":"2023-05-06T13:53:43.123892Z","shell.execute_reply.started":"2023-05-06T13:53:43.110027Z","shell.execute_reply":"2023-05-06T13:53:43.119081Z"},"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-05-06T13:53:43.130397Z","iopub.execute_input":"2023-05-06T13:53:43.131990Z","iopub.status.idle":"2023-05-06T13:53:43.199495Z","shell.execute_reply.started":"2023-05-06T13:53:43.131944Z","shell.execute_reply":"2023-05-06T13:53:43.197219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test file","metadata":{}},{"cell_type":"code","source":"test_data = os.listdir(Test_Folder_Path)\ntest_data","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-05-06T13:53:43.202518Z","iopub.execute_input":"2023-05-06T13:53:43.202808Z","iopub.status.idle":"2023-05-06T13:53:43.278149Z","shell.execute_reply.started":"2023-05-06T13:53:43.202780Z","shell.execute_reply":"2023-05-06T13:53:43.273133Z"},"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-05-06T13:53:48.131282Z","iopub.execute_input":"2023-05-06T13:53:48.131945Z","iopub.status.idle":"2023-05-06T13:53:48.151746Z","shell.execute_reply.started":"2023-05-06T13:53:48.131905Z","shell.execute_reply":"2023-05-06T13:53:48.150667Z"},"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-05-06T13:53:50.962995Z","iopub.execute_input":"2023-05-06T13:53:50.963616Z","iopub.status.idle":"2023-05-06T13:53:51.038381Z","shell.execute_reply.started":"2023-05-06T13:53:50.963571Z","shell.execute_reply":"2023-05-06T13:53:51.037212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:53:54.533532Z","iopub.execute_input":"2023-05-06T13:53:54.534123Z","iopub.status.idle":"2023-05-06T13:53:54.551819Z","shell.execute_reply.started":"2023-05-06T13:53:54.534074Z","shell.execute_reply":"2023-05-06T13:53:54.550770Z"},"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-05-06T13:53:58.546377Z","iopub.execute_input":"2023-05-06T13:53:58.546758Z","iopub.status.idle":"2023-05-06T13:53:58.579716Z","shell.execute_reply.started":"2023-05-06T13:53:58.546726Z","shell.execute_reply":"2023-05-06T13:53:58.578400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:54:04.397013Z","iopub.execute_input":"2023-05-06T13:54:04.397558Z","iopub.status.idle":"2023-05-06T13:54:04.411580Z","shell.execute_reply.started":"2023-05-06T13:54:04.397507Z","shell.execute_reply":"2023-05-06T13:54:04.410381Z"},"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-05-06T13:54:09.990755Z","iopub.execute_input":"2023-05-06T13:54:09.991333Z","iopub.status.idle":"2023-05-06T13:54:10.006159Z","shell.execute_reply.started":"2023-05-06T13:54:09.991285Z","shell.execute_reply":"2023-05-06T13:54:10.005079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Showing Images**","metadata":{}},{"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-05-06T13:54:12.549899Z","iopub.execute_input":"2023-05-06T13:54:12.550665Z","iopub.status.idle":"2023-05-06T13:54:14.158515Z","shell.execute_reply.started":"2023-05-06T13:54:12.550622Z","shell.execute_reply":"2023-05-06T13:54:14.157585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# When there is little data to train, we have to use ImageDataGenerator to increase the number of data\n1. rescale = 1./255 : change the value between 0 and 1<br>rotation_range = 15 : Random rotation within 15 degrees<br>shear_range = 0.1 : shear range 10% <br>\n4. zoom_range = 0.18 : zoom range 18%\n\n5. horizontal_flip = True : Randomly flip horizontally.\n\n6. width_shift_range = 0.1 : Randomly move the original image horizontally within 10% of the width<br>height_shift_range=0.1 : Randomly move the original image vertically within 10% of the width</span>","metadata":{}},{"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-05-06T13:54:23.031092Z","iopub.execute_input":"2023-05-06T13:54:23.031782Z","iopub.status.idle":"2023-05-06T13:54:23.206719Z","shell.execute_reply.started":"2023-05-06T13:54:23.031743Z","shell.execute_reply":"2023-05-06T13:54:23.205773Z"},"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-05-06T13:54:25.719391Z","iopub.execute_input":"2023-05-06T13:54:25.720327Z","iopub.status.idle":"2023-05-06T13:54:25.787777Z","shell.execute_reply.started":"2023-05-06T13:54:25.720287Z","shell.execute_reply":"2023-05-06T13:54:25.786859Z"},"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-05-06T13:54:29.705915Z","iopub.execute_input":"2023-05-06T13:54:29.706627Z","iopub.status.idle":"2023-05-06T13:54:29.806330Z","shell.execute_reply.started":"2023-05-06T13:54:29.706583Z","shell.execute_reply":"2023-05-06T13:54:29.805408Z"},"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-05-06T13:54:32.387380Z","iopub.execute_input":"2023-05-06T13:54:32.387848Z","iopub.status.idle":"2023-05-06T13:54:35.094207Z","shell.execute_reply.started":"2023-05-06T13:54:32.387804Z","shell.execute_reply":"2023-05-06T13:54:35.093090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model,show_shapes=True,show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:54:35.901923Z","iopub.execute_input":"2023-05-06T13:54:35.902632Z","iopub.status.idle":"2023-05-06T13:54:36.165545Z","shell.execute_reply.started":"2023-05-06T13:54:35.902588Z","shell.execute_reply":"2023-05-06T13:54:36.164132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras\nimport visualkeras\nvisualkeras.layered_view(model,legend=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:54:43.905832Z","iopub.execute_input":"2023-05-06T13:54:43.906310Z","iopub.status.idle":"2023-05-06T13:54:55.104496Z","shell.execute_reply.started":"2023-05-06T13:54:43.906263Z","shell.execute_reply":"2023-05-06T13:54:55.103222Z"},"trusted":true},"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-05-06T13:54:55.106806Z","iopub.execute_input":"2023-05-06T13:54:55.107256Z","iopub.status.idle":"2023-05-06T13:54:55.153265Z","shell.execute_reply.started":"2023-05-06T13:54:55.107209Z","shell.execute_reply":"2023-05-06T13:54:55.152503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fitting the Model**","metadata":{}},{"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-05-06T13:55:00.492664Z","iopub.execute_input":"2023-05-06T13:55:00.493378Z","iopub.status.idle":"2023-05-06T14:26:57.076143Z","shell.execute_reply.started":"2023-05-06T13:55:00.493339Z","shell.execute_reply":"2023-05-06T14:26:57.074879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Performance**","metadata":{}},{"cell_type":"code","source":"score=model.evaluate(val_data_generator)\nprint(score[1])","metadata":{"execution":{"iopub.status.busy":"2023-05-06T14:27:05.762910Z","iopub.execute_input":"2023-05-06T14:27:05.763664Z","iopub.status.idle":"2023-05-06T14:27:25.249064Z","shell.execute_reply.started":"2023-05-06T14:27:05.763612Z","shell.execute_reply":"2023-05-06T14:27:25.247925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plotting Accuracy**","metadata":{}},{"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-05-06T14:27:33.321327Z","iopub.execute_input":"2023-05-06T14:27:33.321733Z","iopub.status.idle":"2023-05-06T14:27:33.801381Z","shell.execute_reply.started":"2023-05-06T14:27:33.321697Z","shell.execute_reply":"2023-05-06T14:27:33.800119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Feeding our model with test data to predict**","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_data_generator)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T14:27:50.264976Z","iopub.execute_input":"2023-05-06T14:27:50.265707Z","iopub.status.idle":"2023-05-06T14:28:29.206802Z","shell.execute_reply.started":"2023-05-06T14:27:50.265667Z","shell.execute_reply":"2023-05-06T14:28:29.205734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.round(predictions.flatten()).astype(int)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-05-06T14:28:29.208861Z","iopub.execute_input":"2023-05-06T14:28:29.209354Z","iopub.status.idle":"2023-05-06T14:28:29.219230Z","shell.execute_reply.started":"2023-05-06T14:28:29.209312Z","shell.execute_reply":"2023-05-06T14:28:29.218106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.size(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T14:28:34.557291Z","iopub.execute_input":"2023-05-06T14:28:34.558125Z","iopub.status.idle":"2023-05-06T14:28:34.566968Z","shell.execute_reply.started":"2023-05-06T14:28:34.558044Z","shell.execute_reply":"2023-05-06T14:28:34.565401Z"},"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-05-06T14:28:46.194742Z","iopub.execute_input":"2023-05-06T14:28:46.195440Z","iopub.status.idle":"2023-05-06T14:28:46.217425Z","shell.execute_reply.started":"2023-05-06T14:28:46.195400Z","shell.execute_reply":"2023-05-06T14:28:46.216459Z"},"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-05-06T14:28:54.793833Z","iopub.execute_input":"2023-05-06T14:28:54.794859Z","iopub.status.idle":"2023-05-06T14:28:54.810785Z","shell.execute_reply.started":"2023-05-06T14:28:54.794816Z","shell.execute_reply":"2023-05-06T14:28:54.809486Z"},"trusted":true},"execution_count":null,"outputs":[]}]}