{"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":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import os\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random\nimport numpy as np\nimport pandas as pd\n\n# visuals\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom PIL import Image\n\n# Scikit-learn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,confusion_matrix,ConfusionMatrixDisplay\n\n# Tensorflow\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,MaxPooling2D,Dropout,Flatten,BatchNormalization,Conv2D\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:36.033784Z","iopub.execute_input":"2023-11-01T12:01:36.034716Z","iopub.status.idle":"2023-11-01T12:01:36.044365Z","shell.execute_reply.started":"2023-11-01T12:01:36.03467Z","shell.execute_reply":"2023-11-01T12:01:36.042836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/dogs-vs-cats/test1.zip\"\ntest_path = \"../input/dogs-vs-cats/train.zip\"\n\nfiles = \"/kaggle/working/\"\n\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:36.047292Z","iopub.execute_input":"2023-11-01T12:01:36.047744Z","iopub.status.idle":"2023-11-01T12:01:46.498021Z","shell.execute_reply.started":"2023-11-01T12:01:36.047711Z","shell.execute_reply":"2023-11-01T12:01:46.496671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"../working/train/\"\n\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\n\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:46.499973Z","iopub.execute_input":"2023-11-01T12:01:46.500361Z","iopub.status.idle":"2023-11-01T12:01:46.533765Z","shell.execute_reply.started":"2023-11-01T12:01:46.500332Z","shell.execute_reply":"2023-11-01T12:01:46.532357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20)) # specifying the overall grid size\nplt.subplots_adjust(hspace=0.4)\n\n\nfor i in range(10):\n    \n    plt.subplot(1,10,i+1)    # the number of images in the grid is 10*10 (100)\n    filename = 'train/' + 'dog.' + str(i) + '.jpg'\n    image = imread(filename)\n    plt.imshow(image)\n    plt.title('Dog',fontsize=12)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:46.535764Z","iopub.execute_input":"2023-11-01T12:01:46.536142Z","iopub.status.idle":"2023-11-01T12:01:49.008241Z","shell.execute_reply.started":"2023-11-01T12:01:46.536114Z","shell.execute_reply":"2023-11-01T12:01:49.006874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20)) # specifying the overall grid size\nplt.subplots_adjust(hspace=0.4)\n\n\nfor i in range(10):\n    \n    plt.subplot(1,10,i+1)    # the number of images in the grid is 10*10 (100)\n    filename = 'train/' + 'cat.' + str(i) + '.jpg'\n    image = imread(filename)\n    plt.imshow(image)\n    plt.title('Cat',fontsize=12)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:49.011799Z","iopub.execute_input":"2023-11-01T12:01:49.012161Z","iopub.status.idle":"2023-11-01T12:01:50.280939Z","shell.execute_reply.started":"2023-11-01T12:01:49.012136Z","shell.execute_reply":"2023-11-01T12:01:50.279595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train test split using dataframe\n\nlabels = data['label']\n\nX_train, X_temp = train_test_split(data, test_size=0.2, stratify=labels, random_state = 42)\n\nlabel_test_val = X_temp['label']\n\nX_test, X_val = train_test_split(X_temp, test_size=0.5, stratify=label_test_val, random_state = 42)\n\nprint('The shape of train data',X_train.shape)\nprint('The shape of test data',X_test.shape)\nprint('The shape of validation data',X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:50.282551Z","iopub.execute_input":"2023-11-01T12:01:50.282926Z","iopub.status.idle":"2023-11-01T12:01:50.330821Z","shell.execute_reply.started":"2023-11-01T12:01:50.282895Z","shell.execute_reply":"2023-11-01T12:01:50.329904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['Cat','Dog']\n\nlabel1,count1 = np.unique(X_train.label,return_counts=True)\nlabel2,count2 = np.unique(X_val.label,return_counts=True)\nlabel3,count3 = np.unique(X_test.label,return_counts=True)\n\nuni1 = pd.DataFrame(data=count1,index=labels,columns=['Count1'])\nuni2 = pd.DataFrame(data=count2,index=labels,columns=['Count2'])\nuni3 = pd.DataFrame(data=count3,index=labels,columns=['Count3'])\n\n\nplt.figure(figsize=(20,6),dpi=200)\nsns.set_style('darkgrid')\n\nplt.subplot(131)\nsns.barplot(data=uni1,x=uni1.index,y='Count1',palette='icefire',width=0.2).set_title('Class distribution in Training set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\nplt.subplot(132)\nsns.barplot(data=uni2,x=uni2.index,y='Count2',palette='icefire',width=0.2).set_title('Class distribution in validation set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\n\nplt.subplot(133)\nsns.barplot(data=uni3,x=uni3.index,y='Count3',palette='icefire',width=0.2).set_title('Class distribution in Testing set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:50.332645Z","iopub.execute_input":"2023-11-01T12:01:50.333222Z","iopub.status.idle":"2023-11-01T12:01:51.030345Z","shell.execute_reply.started":"2023-11-01T12:01:50.33318Z","shell.execute_reply":"2023-11-01T12:01:51.029268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parameter\nimage_size = 128\nimage_channel = 3\nbat_size = 32","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:51.031898Z","iopub.execute_input":"2023-11-01T12:01:51.03314Z","iopub.status.idle":"2023-11-01T12:01:51.038935Z","shell.execute_reply.started":"2023-11-01T12:01:51.033095Z","shell.execute_reply":"2023-11-01T12:01:51.037692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Membuat image data generator\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n                                    rotation_range = 15,\n                                    horizontal_flip = True,\n                                    zoom_range = 0.2,\n                                    shear_range = 0.1,\n                                    fill_mode = 'reflect',\n                                    width_shift_range = 0.1,\n                                    height_shift_range = 0.1)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:51.039935Z","iopub.execute_input":"2023-11-01T12:01:51.040477Z","iopub.status.idle":"2023-11-01T12:01:51.053932Z","shell.execute_reply.started":"2023-11-01T12:01:51.040453Z","shell.execute_reply":"2023-11-01T12:01:51.052945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Applying image data generator ke train dan test data\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    X_train,\n    directory='train/',\n    x_col='filename',\n    y_col='label',\n    batch_size=bat_size,\n    target_size=(image_size, image_size)\n)\n\nval_generator = test_datagen.flow_from_dataframe(\n    X_val,\n    directory='train/',\n    x_col='filename',\n    y_col='label',\n    batch_size=bat_size,\n    target_size=(image_size, image_size),\n    shuffle=False\n)\n\ntest_generator = test_datagen.flow_from_dataframe(\n    X_test,\n    directory='train/',\n    x_col='filename',\n    y_col='label',\n    batch_size=bat_size,\n    target_size=(image_size, image_size),\n    shuffle=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:51.055037Z","iopub.execute_input":"2023-11-01T12:01:51.055423Z","iopub.status.idle":"2023-11-01T12:01:51.280878Z","shell.execute_reply.started":"2023-11-01T12:01:51.055397Z","shell.execute_reply":"2023-11-01T12:01:51.279838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate data generators untuk training dan validasi\ntrain_gen = train_datagen.flow_from_directory(\n    '/kaggle/working/dataset_dogs_vs_cats/train/',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=bat_size\n)\n\nval_gen = test_datagen.flow_from_directory(\n    '/kaggle/working/dataset_dogs_vs_cats/test/',\n    class_mode='binary',\n    batch_size=bat_size,\n    target_size=(image_size, image_size),\n    shuffle=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:01:51.28214Z","iopub.execute_input":"2023-11-01T12:01:51.282464Z","iopub.status.idle":"2023-11-01T12:01:51.348961Z","shell.execute_reply.started":"2023-11-01T12:01:51.282436Z","shell.execute_reply":"2023-11-01T12:01:51.346883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\n# Input Layer\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape = (image_size,image_size,image_channel))) \nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n\n# Bloack 1 \nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n# Block 2\nmodel.add(Conv2D(128,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n# Block 3\nmodel.add(Conv2D(256,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n\n# Fully Connected layers \nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\n# Output layer\nmodel.add(Dense(2,activation='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:02:00.501445Z","iopub.execute_input":"2023-11-01T12:02:00.50193Z","iopub.status.idle":"2023-11-01T12:02:00.815Z","shell.execute_reply.started":"2023-11-01T12:02:00.501902Z","shell.execute_reply":"2023-11-01T12:02:00.813649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy',\n                                            patience=2,\n                                            factor=0.5,\n                                            min_lr = 0.00001,\n                                            verbose = 1)\n\nearly_stoping = EarlyStopping(monitor='val_loss',patience= 3,restore_best_weights=True,verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:02:08.301803Z","iopub.execute_input":"2023-11-01T12:02:08.30236Z","iopub.status.idle":"2023-11-01T12:02:08.310416Z","shell.execute_reply.started":"2023-11-01T12:02:08.302331Z","shell.execute_reply":"2023-11-01T12:02:08.307815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:02:10.173345Z","iopub.execute_input":"2023-11-01T12:02:10.173834Z","iopub.status.idle":"2023-11-01T12:02:10.212426Z","shell.execute_reply.started":"2023-11-01T12:02:10.173796Z","shell.execute_reply":"2023-11-01T12:02:10.209904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_dog = model.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[early_stoping,learning_rate_reduction],\n                    epochs = 30,\n                    # steps_per_epoch = len(train_generator),\n                    # validation_steps = len(val_generaotor),\n                   )","metadata":{"execution":{"iopub.status.busy":"2023-11-01T12:02:12.012635Z","iopub.execute_input":"2023-11-01T12:02:12.013952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plots for accuracy and Loss with epochs\n\nerror = pd.DataFrame(cat_dog.history)\n\nplt.figure(figsize=(18,5),dpi=200)\nsns.set_style('darkgrid')\n\nplt.subplot(121)\nplt.title('Cross Entropy Loss',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Loss',fontsize=12)\nplt.plot(error['loss'])\nplt.plot(error['val_loss'])\n\nplt.subplot(122)\nplt.title('Classification Accuracy',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Accuracy',fontsize=12)\nplt.plot(error['accuracy'])\nplt.plot(error['val_accuracy'])\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluvate for train generator\nloss,acc = model.evaluate(train_generator,batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for training data is:',acc*100)\nprint('The Loss of the model for training data is:',loss)\n\n# Evaluvate for validation generator\nloss,acc = model.evaluate(val_generator,batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for validation data is:',acc*100)\nprint('The Loss of the model for validation data is:',loss)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the Model\nmodel.save(\"model.h5\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prediction\nresult = model.predict(test_generator,batch_size = bat_size,verbose = 0)\n\ny_pred = np.argmax(result, axis = 1)\n\ny_true = test_generator.labels\n\n# Evaluvate\nloss,acc = model.evaluate(test_generator, batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for testing data is:',acc*100)\nprint('The Loss of the model for testing data is:',loss)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_mtx = confusion_matrix(y_true,y_pred) \n\nf,ax = plt.subplots(figsize = (8,4),dpi=200)\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.1, cmap = \"gist_yarg_r\", linecolor=\"black\", fmt='.0f', ax=ax,cbar=False, xticklabels=labels, yticklabels=labels)\n\nplt.xlabel(\"Predicted Label\",fontsize=10)\nplt.ylabel(\"True Label\",fontsize=10)\nplt.title(\"Confusion Matrix\",fontsize=13)\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size =(128,128)\n\n# loading into dataframe\ntest_dir = \"../working/test1/\"\nfilenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"filename\": filenames})\ntest_data['label'] = 'unknown'\n\n# Create data genenerator for test data\ntest1_idg =  test_datagen.flow_from_dataframe(test_data, \n                                     \"test1/\", \n                                     x_col= \"filename\",\n                                     y_col = 'label',\n                                     batch_size = bat_size,\n                                     target_size=size, \n                                     shuffle = False)\n\n# Test Prediction\ntest1_predict = model.predict(test1_idg,verbose = 0)\n\ntest1_predict_argmax = np.argmax(test1_predict, axis=1)\n\ny_test_pred = test1_predict_argmax\n\ntest_data['label'] = y_test_pred\n\n# mapping\nlabel_mapping = {0: 'cat', 1: 'dog'}\ntest_data['label'] = test_data['label'].map(label_mapping)\ntest_data.head()\n\n# csv file output for submission\nsub = pd.read_csv('/kaggle/input/dogs-vs-cats/sampleSubmission.csv',index_col='id')\n\nsub['label'] = y_test_pred\n\nsub.to_csv('submission.csv',index=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 10, figsize=(20, 4))\nfor idx in range(10):\n    image_path = os.path.join(test_dir, test_data.iloc[idx]['filename'])\n    image = Image.open(image_path)\n    axes[idx].imshow(image)\n    axes[idx].set_title(\"Label: \" + test_data.iloc[idx]['label'])\n    axes[idx].axis('off')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}