{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":1003830,"sourceType":"datasetVersion","datasetId":550917}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Basic\nimport 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\nimport matplotlib.image as img\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-03T13:31:41.854644Z","iopub.execute_input":"2024-08-03T13:31:41.855105Z","iopub.status.idle":"2024-08-03T13:31:41.863116Z","shell.execute_reply.started":"2024-08-03T13:31:41.855072Z","shell.execute_reply":"2024-08-03T13:31:41.861954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/dogs-vs-cats/train.zip\"\ntest_path = \"../input/dogs-vs-cats/test1.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)\n    \nimage_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()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:31:41.866862Z","iopub.execute_input":"2024-08-03T13:31:41.867963Z","iopub.status.idle":"2024-08-03T13:32:02.086050Z","shell.execute_reply.started":"2024-08-03T13:31:41.867929Z","shell.execute_reply":"2024-08-03T13:32:02.084760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:02.088511Z","iopub.execute_input":"2024-08-03T13:32:02.089024Z","iopub.status.idle":"2024-08-03T13:32:02.107939Z","shell.execute_reply.started":"2024-08-03T13:32:02.088976Z","shell.execute_reply":"2024-08-03T13:32:02.106583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare the doc and cat seprate","metadata":{}},{"cell_type":"code","source":"data.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:02.109513Z","iopub.execute_input":"2024-08-03T13:32:02.110399Z","iopub.status.idle":"2024-08-03T13:32:02.126284Z","shell.execute_reply.started":"2024-08-03T13:32:02.110327Z","shell.execute_reply":"2024-08-03T13:32:02.125118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Print Random Images to review images are properly align with lable and checking shape to develope understanding about image ","metadata":{}},{"cell_type":"code","source":"data.iloc[5]['label']","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:02.128855Z","iopub.execute_input":"2024-08-03T13:32:02.129259Z","iopub.status.idle":"2024-08-03T13:32:02.138134Z","shell.execute_reply.started":"2024-08-03T13:32:02.129226Z","shell.execute_reply":"2024-08-03T13:32:02.137041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\n# Select 16 random indices from the training set\nrandom_indices = random.sample(range(12500), 16)\n# random_indices\n\n# Plot the images and their corresponding labels\nfig, axes = plt.subplots(4, 4, figsize=(15, 8))\nfor i, index in enumerate(random_indices):\n    fig.subplots_adjust(hspace=0.5)  # Add space between subplots\n#     print(index)\n    ax = axes[i // 4, i % 4]\n#     image_path = \"./train/\"+data.iloc[index]['label']+\"/\"+data.iloc[index]['filename']\n   \n    image_data = img.imread(f\"./train/{data.iloc[index]['filename']}\")\n    ax.imshow(image_data)\n    ax.set_title(f\"Label: {data.iloc[index]['label']}\\nShape: {image_data.shape}\")\n    ax.axis(\"off\")\n\nplt.suptitle(\"16 Random Images from Dataset with Image and Labels and shapes\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:02.139322Z","iopub.execute_input":"2024-08-03T13:32:02.139697Z","iopub.status.idle":"2024-08-03T13:32:03.756455Z","shell.execute_reply.started":"2024-08-03T13:32:02.139664Z","shell.execute_reply":"2024-08-03T13:32:03.755109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"photos are appering with different color and different shapes","metadata":{}},{"cell_type":"markdown","source":">**Spliting data into test and traning sets**","metadata":{}},{"cell_type":"code","source":"train_set, test_set = train_test_split(data, test_size=0.2,random_state=40)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:03.757856Z","iopub.execute_input":"2024-08-03T13:32:03.758215Z","iopub.status.idle":"2024-08-03T13:32:03.771005Z","shell.execute_reply.started":"2024-08-03T13:32:03.758184Z","shell.execute_reply":"2024-08-03T13:32:03.769480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"spleting of data is done , let's check the distribution dog vs cat","metadata":{}},{"cell_type":"code","source":"print('Train set:\\n',train_set['label'].value_counts())\nplt.figure(figsize=(10,5))\nplt.subplot(1,3,1)\nsns.countplot(x=train_set['label'], order=['dog','cat'])\nplt.title('Train set label distribution')\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:03.772599Z","iopub.execute_input":"2024-08-03T13:32:03.773055Z","iopub.status.idle":"2024-08-03T13:32:03.994276Z","shell.execute_reply.started":"2024-08-03T13:32:03.773001Z","shell.execute_reply":"2024-08-03T13:32:03.992828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprint('\\nTest set:\\n',test_set['label'].value_counts())\nplt.subplot(1,3,3)\nsns.countplot(x=test_set['label'], order=['dog','cat'])\nplt.title('Test set label distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:03.996382Z","iopub.execute_input":"2024-08-03T13:32:03.996881Z","iopub.status.idle":"2024-08-03T13:32:04.163474Z","shell.execute_reply.started":"2024-08-03T13:32:03.996836Z","shell.execute_reply":"2024-08-03T13:32:04.162333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For dog and cat , tanin and test set generated with balanced data","metadata":{}},{"cell_type":"markdown","source":"Generating image data with ","metadata":{}},{"cell_type":"code","source":"\nWIDTH, HEIGHT,BATCH_SIZE = 150, 150 ,25 # Define hyperparameters","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:04.165061Z","iopub.execute_input":"2024-08-03T13:32:04.165492Z","iopub.status.idle":"2024-08-03T13:32:04.171779Z","shell.execute_reply.started":"2024-08-03T13:32:04.165453Z","shell.execute_reply":"2024-08-03T13:32:04.170241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_gen_setting = ImageDataGenerator(rescale=1./255,\n      rotation_range=30,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      fill_mode='nearest')\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:04.176259Z","iopub.execute_input":"2024-08-03T13:32:04.176663Z","iopub.status.idle":"2024-08-03T13:32:04.188795Z","shell.execute_reply.started":"2024-08-03T13:32:04.176630Z","shell.execute_reply":"2024-08-03T13:32:04.187269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_gen_setting","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:04.190403Z","iopub.execute_input":"2024-08-03T13:32:04.191086Z","iopub.status.idle":"2024-08-03T13:32:04.203905Z","shell.execute_reply.started":"2024-08-03T13:32:04.191049Z","shell.execute_reply":"2024-08-03T13:32:04.202712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Note that the validation data should not be augmented!\ntest_data_gen_setting = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:04.206006Z","iopub.execute_input":"2024-08-03T13:32:04.206469Z","iopub.status.idle":"2024-08-03T13:32:04.215260Z","shell.execute_reply.started":"2024-08-03T13:32:04.206426Z","shell.execute_reply":"2024-08-03T13:32:04.214018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data = train_data_gen_setting.flow_from_dataframe(\n                                                       dataframe=train_set, \n                                                       directory='./train',\n                                                       x_col='filename',\n                                                       y_col='label',\n                                                       target_size=(WIDTH, HEIGHT),\n                                                       class_mode='categorical',\n                                                       batch_size=BATCH_SIZE)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:32:04.217086Z","iopub.execute_input":"2024-08-03T13:32:04.217539Z","iopub.status.idle":"2024-08-03T13:32:04.438344Z","shell.execute_reply.started":"2024-08-03T13:32:04.217496Z","shell.execute_reply":"2024-08-03T13:32:04.437165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_set.columns)\nprint(test_set.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:18.916500Z","iopub.execute_input":"2024-08-03T13:46:18.917443Z","iopub.status.idle":"2024-08-03T13:46:18.923472Z","shell.execute_reply.started":"2024-08-03T13:46:18.917401Z","shell.execute_reply":"2024-08-03T13:46:18.922153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data_gen_setting.flow_from_dataframe(\n                                                     dataframe=test_set,\n                                                         directory='./train',\n                                                         x_col='filename',\n                                                         y_col='label',\n                                                         target_size=(WIDTH,HEIGHT),\n                                                         class_mode='categorical',\n                                                         batch_size=BATCH_SIZE\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:45:38.818161Z","iopub.execute_input":"2024-08-03T13:45:38.818605Z","iopub.status.idle":"2024-08-03T13:45:38.887493Z","shell.execute_reply.started":"2024-08-03T13:45:38.818563Z","shell.execute_reply":"2024-08-03T13:45:38.886303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_indices = random.sample(range(5000), 16)\n# random_indices\n\n# Plot the images and their corresponding labels\nfig, axes = plt.subplots(4, 4, figsize=(15, 8))\nfor i, index in enumerate(random_indices):\n    fig.subplots_adjust(hspace=0.5)  # Add space between subplots\n#     print(index)\n    ax = axes[i // 4, i % 4]\n    image_data = img.imread(f\"./train/{test_data.filenames[index]}\")\n    ax.imshow(image_data)\n    ax.set_title(f\"Filename: {test_data.filenames[index]}\")\n    ax.axis(\"off\")\n\nplt.suptitle(\"16 Random Images from test Dataset\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:46:00.285567Z","iopub.execute_input":"2024-08-03T13:46:00.285976Z","iopub.status.idle":"2024-08-03T13:46:01.930091Z","shell.execute_reply.started":"2024-08-03T13:46:00.285945Z","shell.execute_reply":"2024-08-03T13:46:01.928916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select 16 random indices from the test set\nrandom_indices = random.sample(range(5000), 16)\n# random_indices\n\n# Plot the images and their corresponding labels\nfig, axes = plt.subplots(4, 4, figsize=(15, 8))\nfor i, index in enumerate(random_indices):\n    fig.subplots_adjust(hspace=0.5)  # Add space between subplots\n#     print(index)\n    ax = axes[i // 4, i % 4]\n    image_data = img.imread(f\"./train/{training_data.filenames[index]}\")\n    ax.imshow(image_data)\n    ax.set_title(f\"Filename: {training_data.filenames[index]}\")\n    ax.axis(\"off\")\n\nplt.suptitle(\"16 Random Images from traning Dataset\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:38:32.153762Z","iopub.execute_input":"2024-08-03T13:38:32.154242Z","iopub.status.idle":"2024-08-03T13:38:34.949197Z","shell.execute_reply.started":"2024-08-03T13:38:32.154203Z","shell.execute_reply":"2024-08-03T13:38:34.948123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making CNN model with 16 layers, including 13 convolutional layers and 3 fully connected layers like VGG16","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(16, (3, 3), activation='relu', padding='same', input_shape=(WIDTH, HEIGHT, 3)))\nmodel.add(Conv2D(16, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D((2, 2), strides=(2, 2)))\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D((2, 2), strides=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D((2, 2), strides=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D((2, 2), strides=(2, 2)))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D((2, 2), strides=(2, 2)))\n\n# Fully connected layers\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation='softmax')) \n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T15:44:05.111118Z","iopub.execute_input":"2024-08-03T15:44:05.112106Z","iopub.status.idle":"2024-08-03T15:44:05.572241Z","shell.execute_reply.started":"2024-08-03T15:44:05.112063Z","shell.execute_reply":"2024-08-03T15:44:05.570797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-08-03T15:44:37.912214Z","iopub.execute_input":"2024-08-03T15:44:37.913166Z","iopub.status.idle":"2024-08-03T15:44:37.923572Z","shell.execute_reply.started":"2024-08-03T15:44:37.913124Z","shell.execute_reply":"2024-08-03T15:44:37.922398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the model's architecture\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-03T15:44:40.348207Z","iopub.execute_input":"2024-08-03T15:44:40.348611Z","iopub.status.idle":"2024-08-03T15:44:40.396027Z","shell.execute_reply.started":"2024-08-03T15:44:40.348579Z","shell.execute_reply":"2024-08-03T15:44:40.394910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n\n# Initialize callbacks\nearlystop_cb = EarlyStopping(patience=10, restore_best_weights=True)\nreduce_lr_cb = ReduceLROnPlateau(factor=0.5, patience=5, monitor='val_loss', min_lr=0.00001)\ncheckpoint_cb = ModelCheckpoint('model.keras', save_best_only=True, monitor='val_loss')\n\n# Fit the model with callbacks\nhistory = model.fit(\n    training_data,\n    epochs=30,\n    validation_data=test_data,\n    validation_steps=30,\n    steps_per_epoch=30 ,\n    callbacks=[earlystop_cb, reduce_lr_cb, checkpoint_cb]\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T15:51:16.519800Z","iopub.execute_input":"2024-08-03T15:51:16.520270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Plot training & validation accuracy values\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-08-03T14:24:10.275428Z","iopub.status.idle":"2024-08-03T14:24:10.275865Z","shell.execute_reply.started":"2024-08-03T14:24:10.275661Z","shell.execute_reply":"2024-08-03T14:24:10.275680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}