{"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-04-03T17:24:52.599521Z","iopub.execute_input":"2023-04-03T17:24:52.600291Z","iopub.status.idle":"2023-04-03T17:24:52.613375Z","shell.execute_reply.started":"2023-04-03T17:24:52.600251Z","shell.execute_reply":"2023-04-03T17:24:52.612303Z"},"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-03T17:24:52.615766Z","iopub.execute_input":"2023-04-03T17:24:52.616223Z","iopub.status.idle":"2023-04-03T17:24:52.622430Z","shell.execute_reply.started":"2023-04-03T17:24:52.616182Z","shell.execute_reply":"2023-04-03T17:24:52.621330Z"},"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-04-03T17:24:52.624648Z","iopub.execute_input":"2023-04-03T17:24:52.625342Z","iopub.status.idle":"2023-04-03T17:25:05.762154Z","shell.execute_reply.started":"2023-04-03T17:24:52.625305Z","shell.execute_reply":"2023-04-03T17:25:05.760947Z"},"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-03T17:25:05.764353Z","iopub.execute_input":"2023-04-03T17:25:05.765065Z","iopub.status.idle":"2023-04-03T17:25:05.771503Z","shell.execute_reply.started":"2023-04-03T17:25:05.765024Z","shell.execute_reply":"2023-04-03T17:25:05.770122Z"},"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-03T17:25:05.773235Z","iopub.execute_input":"2023-04-03T17:25:05.773928Z","iopub.status.idle":"2023-04-03T17:25:05.803133Z","shell.execute_reply.started":"2023-04-03T17:25:05.773707Z","shell.execute_reply":"2023-04-03T17:25:05.802024Z"},"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":{"execution":{"iopub.status.busy":"2023-04-03T17:25:05.806817Z","iopub.execute_input":"2023-04-03T17:25:05.807621Z","iopub.status.idle":"2023-04-03T17:25:05.833429Z","shell.execute_reply.started":"2023-04-03T17:25:05.807590Z","shell.execute_reply":"2023-04-03T17:25:05.832311Z"},"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-03T17:25:05.835063Z","iopub.execute_input":"2023-04-03T17:25:05.835927Z","iopub.status.idle":"2023-04-03T17:25:05.857127Z","shell.execute_reply.started":"2023-04-03T17:25:05.835890Z","shell.execute_reply":"2023-04-03T17:25:05.856219Z"},"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-03T17:25:05.859557Z","iopub.execute_input":"2023-04-03T17:25:05.860190Z","iopub.status.idle":"2023-04-03T17:25:05.922352Z","shell.execute_reply.started":"2023-04-03T17:25:05.860153Z","shell.execute_reply":"2023-04-03T17:25:05.921385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T17:25:05.923970Z","iopub.execute_input":"2023-04-03T17:25:05.924337Z","iopub.status.idle":"2023-04-03T17:25:05.934180Z","shell.execute_reply.started":"2023-04-03T17:25:05.924301Z","shell.execute_reply":"2023-04-03T17:25:05.933029Z"},"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-03T17:25:05.935904Z","iopub.execute_input":"2023-04-03T17:25:05.936283Z","iopub.status.idle":"2023-04-03T17:25:05.970736Z","shell.execute_reply.started":"2023-04-03T17:25:05.936247Z","shell.execute_reply":"2023-04-03T17:25:05.969654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T17:25:05.972315Z","iopub.execute_input":"2023-04-03T17:25:05.972668Z","iopub.status.idle":"2023-04-03T17:25:05.983124Z","shell.execute_reply.started":"2023-04-03T17:25:05.972612Z","shell.execute_reply":"2023-04-03T17:25:05.982052Z"},"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-03T17:25:05.988018Z","iopub.execute_input":"2023-04-03T17:25:05.988290Z","iopub.status.idle":"2023-04-03T17:25:05.998727Z","shell.execute_reply.started":"2023-04-03T17:25:05.988264Z","shell.execute_reply":"2023-04-03T17:25:05.997678Z"},"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-04-03T17:25:06.002134Z","iopub.execute_input":"2023-04-03T17:25:06.002404Z","iopub.status.idle":"2023-04-03T17:25:07.650385Z","shell.execute_reply.started":"2023-04-03T17:25:06.002378Z","shell.execute_reply":"2023-04-03T17:25:07.648855Z"},"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-04-03T17:25:07.652078Z","iopub.execute_input":"2023-04-03T17:25:07.652663Z","iopub.status.idle":"2023-04-03T17:25:07.852086Z","shell.execute_reply.started":"2023-04-03T17:25:07.652607Z","shell.execute_reply":"2023-04-03T17:25:07.850858Z"},"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-03T17:25:07.854126Z","iopub.execute_input":"2023-04-03T17:25:07.854528Z","iopub.status.idle":"2023-04-03T17:25:07.921935Z","shell.execute_reply.started":"2023-04-03T17:25:07.854481Z","shell.execute_reply":"2023-04-03T17:25:07.920963Z"},"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-03T17:25:07.923205Z","iopub.execute_input":"2023-04-03T17:25:07.923555Z","iopub.status.idle":"2023-04-03T17:25:08.024887Z","shell.execute_reply.started":"2023-04-03T17:25:07.923518Z","shell.execute_reply":"2023-04-03T17:25:08.023876Z"},"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-03T17:25:08.026705Z","iopub.execute_input":"2023-04-03T17:25:08.027094Z","iopub.status.idle":"2023-04-03T17:25:08.091458Z","shell.execute_reply.started":"2023-04-03T17:25:08.027055Z","shell.execute_reply":"2023-04-03T17:25:08.090432Z"},"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-04-03T17:25:08.093028Z","iopub.execute_input":"2023-04-03T17:25:08.093893Z","iopub.status.idle":"2023-04-03T17:25:08.309347Z","shell.execute_reply.started":"2023-04-03T17:25:08.093854Z","shell.execute_reply":"2023-04-03T17:25:08.307819Z"},"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-04-03T17:25:08.311693Z","iopub.execute_input":"2023-04-03T17:25:08.312083Z","iopub.status.idle":"2023-04-03T17:25:18.066554Z","shell.execute_reply.started":"2023-04-03T17:25:08.312037Z","shell.execute_reply":"2023-04-03T17:25:18.065352Z"},"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-04-03T17:25:18.068424Z","iopub.execute_input":"2023-04-03T17:25:18.068737Z","iopub.status.idle":"2023-04-03T17:25:18.106546Z","shell.execute_reply.started":"2023-04-03T17:25:18.068706Z","shell.execute_reply":"2023-04-03T17:25:18.105757Z"},"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-04-03T17:25:18.107611Z","iopub.execute_input":"2023-04-03T17:25:18.108101Z","iopub.status.idle":"2023-04-03T17:58:24.538317Z","shell.execute_reply.started":"2023-04-03T17:25:18.108063Z","shell.execute_reply":"2023-04-03T17:58:24.537295Z"},"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-04-03T17:58:24.539987Z","iopub.execute_input":"2023-04-03T17:58:24.540705Z","iopub.status.idle":"2023-04-03T17:58:44.460145Z","shell.execute_reply.started":"2023-04-03T17:58:24.540657Z","shell.execute_reply":"2023-04-03T17:58:44.459179Z"},"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-04-03T17:58:44.463269Z","iopub.execute_input":"2023-04-03T17:58:44.463559Z","iopub.status.idle":"2023-04-03T17:58:44.874543Z","shell.execute_reply.started":"2023-04-03T17:58:44.463530Z","shell.execute_reply":"2023-04-03T17:58:44.873588Z"},"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-04-03T17:58:44.875891Z","iopub.execute_input":"2023-04-03T17:58:44.876358Z","iopub.status.idle":"2023-04-03T17:59:24.139237Z","shell.execute_reply.started":"2023-04-03T17:58:44.876318Z","shell.execute_reply":"2023-04-03T17:59:24.138213Z"},"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-03T17:59:24.141045Z","iopub.execute_input":"2023-04-03T17:59:24.141427Z","iopub.status.idle":"2023-04-03T17:59:24.150952Z","shell.execute_reply.started":"2023-04-03T17:59:24.141388Z","shell.execute_reply":"2023-04-03T17:59:24.149682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.size(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T17:59:24.153353Z","iopub.execute_input":"2023-04-03T17:59:24.154212Z","iopub.status.idle":"2023-04-03T17:59:24.161446Z","shell.execute_reply.started":"2023-04-03T17:59:24.154173Z","shell.execute_reply":"2023-04-03T17:59:24.159980Z"},"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-03T17:59:24.163469Z","iopub.execute_input":"2023-04-03T17:59:24.163748Z","iopub.status.idle":"2023-04-03T17:59:24.188858Z","shell.execute_reply.started":"2023-04-03T17:59:24.163722Z","shell.execute_reply":"2023-04-03T17:59:24.187739Z"},"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-03T17:59:24.190186Z","iopub.execute_input":"2023-04-03T17:59:24.190532Z","iopub.status.idle":"2023-04-03T17:59:24.206604Z","shell.execute_reply.started":"2023-04-03T17:59:24.190497Z","shell.execute_reply":"2023-04-03T17:59:24.205606Z"},"trusted":true},"execution_count":null,"outputs":[]}]}