{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras \nfrom keras import layers\nfrom keras import models \nimport seaborn as sns\nimport pandas as pd\nimport numpy as np\nimport warnings\nimport os\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-28T16:10:46.148409Z","iopub.execute_input":"2024-10-28T16:10:46.148738Z","iopub.status.idle":"2024-10-28T16:11:09.941331Z","shell.execute_reply.started":"2024-10-28T16:10:46.148698Z","shell.execute_reply":"2024-10-28T16:11:09.940350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unzip data files","metadata":{}},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/input/dogs-vs-cats\"))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:20.662103Z","iopub.execute_input":"2024-10-28T16:11:20.662997Z","iopub.status.idle":"2024-10-28T16:11:20.669136Z","shell.execute_reply.started":"2024-10-28T16:11:20.662931Z","shell.execute_reply":"2024-10-28T16:11:20.668105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nzip_files = ['test1','train']\nfor zip_file in zip_files:\n    with zipfile.ZipFile(f'/kaggle/input/dogs-vs-cats/{zip_file}.zip','r') as file:\n        file.extractall(\".\")\n        print(\"{} unzipped successfully\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:20.900795Z","iopub.execute_input":"2024-10-28T16:11:20.901686Z","iopub.status.idle":"2024-10-28T16:11:36.152852Z","shell.execute_reply.started":"2024-10-28T16:11:20.901637Z","shell.execute_reply":"2024-10-28T16:11:36.151881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\".\"))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.154520Z","iopub.execute_input":"2024-10-28T16:11:36.154809Z","iopub.status.idle":"2024-10-28T16:11:36.160082Z","shell.execute_reply.started":"2024-10-28T16:11:36.154779Z","shell.execute_reply":"2024-10-28T16:11:36.159185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path=\"./train\"\nfile_names=os.listdir(train_path)\nprint(\"train samples \",len(file_names))\nprint(file_names[:3])","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.161045Z","iopub.execute_input":"2024-10-28T16:11:36.161371Z","iopub.status.idle":"2024-10-28T16:11:36.186422Z","shell.execute_reply.started":"2024-10-28T16:11:36.161319Z","shell.execute_reply":"2024-10-28T16:11:36.185650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path=\"./test1\"\nfile_names=os.listdir(test_path)\nprint(\"test samples \",len(file_names))\nprint(file_names[:3])","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.188378Z","iopub.execute_input":"2024-10-28T16:11:36.188671Z","iopub.status.idle":"2024-10-28T16:11:36.201429Z","shell.execute_reply.started":"2024-10-28T16:11:36.188640Z","shell.execute_reply":"2024-10-28T16:11:36.200438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def path_to_frame(files):\n    paths,labels=[],[]\n    path=f\"./{files}\"\n    file_names=os.listdir(path)\n    for name in file_names:\n        paths.append(name)\n        labels.append(name[:3])\n    return pd.DataFrame({\"path\":paths,\"target\":labels})","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.202622Z","iopub.execute_input":"2024-10-28T16:11:36.203346Z","iopub.status.idle":"2024-10-28T16:11:36.208432Z","shell.execute_reply.started":"2024-10-28T16:11:36.203302Z","shell.execute_reply":"2024-10-28T16:11:36.207496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=path_to_frame(\"train\")\ntest_df=path_to_frame(\"test1\")","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.209440Z","iopub.execute_input":"2024-10-28T16:11:36.209734Z","iopub.status.idle":"2024-10-28T16:11:36.258459Z","shell.execute_reply.started":"2024-10-28T16:11:36.209688Z","shell.execute_reply":"2024-10-28T16:11:36.257774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train shape\",train_df.shape)\nprint(\"test shape\", test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.259580Z","iopub.execute_input":"2024-10-28T16:11:36.259927Z","iopub.status.idle":"2024-10-28T16:11:36.264436Z","shell.execute_reply.started":"2024-10-28T16:11:36.259887Z","shell.execute_reply":"2024-10-28T16:11:36.263618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train: \")\nprint(train_df.head(2))\nprint()\nprint(\"test: \")\nprint(test_df.head(2))","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.265587Z","iopub.execute_input":"2024-10-28T16:11:36.265866Z","iopub.status.idle":"2024-10-28T16:11:36.287837Z","shell.execute_reply.started":"2024-10-28T16:11:36.265836Z","shell.execute_reply":"2024-10-28T16:11:36.286989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring and splitting training data","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns \n\nsns.countplot(x=train_df[\"target\"],alpha=0.2,color=\"g\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.288884Z","iopub.execute_input":"2024-10-28T16:11:36.289207Z","iopub.status.idle":"2024-10-28T16:11:36.549204Z","shell.execute_reply.started":"2024-10-28T16:11:36.289173Z","shell.execute_reply":"2024-10-28T16:11:36.548291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"balanced training data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_df,valid_df=train_test_split(train_df,test_size=0.2,stratify=train_df[\"target\"],random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:36.551750Z","iopub.execute_input":"2024-10-28T16:11:36.552062Z","iopub.status.idle":"2024-10-28T16:11:37.011591Z","shell.execute_reply.started":"2024-10-28T16:11:36.552029Z","shell.execute_reply":"2024-10-28T16:11:37.010787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(nrows=1,ncols=2,figsize=(10,4))\nsns.countplot(x=train_df[\"target\"],alpha=0.2,color=\"g\",ax=ax[0])\nax[0].set_title(\"Train data\")\nsns.countplot(x=valid_df[\"target\"],alpha=0.2,color=\"g\",ax=ax[1])\nax[1].set_title(\"Validition data\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:37.012730Z","iopub.execute_input":"2024-10-28T16:11:37.013095Z","iopub.status.idle":"2024-10-28T16:11:37.325140Z","shell.execute_reply.started":"2024-10-28T16:11:37.013059Z","shell.execute_reply":"2024-10-28T16:11:37.324176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Generator","metadata":{}},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\n    rescale=1/255,\n    rotation_range=40,\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)\ntraining_data = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory='./train',\n    x_col = 'path',\n    y_col='target',\n    target_size=(150,150),\n    class_mode='binary',\n    batch_size=50) ","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:44.016121Z","iopub.execute_input":"2024-10-28T16:11:44.016746Z","iopub.status.idle":"2024-10-28T16:11:44.281189Z","shell.execute_reply.started":"2024-10-28T16:11:44.016705Z","shell.execute_reply":"2024-10-28T16:11:44.280414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_datagen=ImageDataGenerator(rescale=1/255)\n\nvalidation_data = valid_datagen.flow_from_dataframe(\n    dataframe=valid_df,\n    directory='./train',\n    x_col = 'path',\n    y_col='target',\n    target_size=(150,150),\n    class_mode='binary',\n    batch_size=50) ","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:44.282640Z","iopub.execute_input":"2024-10-28T16:11:44.282978Z","iopub.status.idle":"2024-10-28T16:11:44.351937Z","shell.execute_reply.started":"2024-10-28T16:11:44.282933Z","shell.execute_reply":"2024-10-28T16:11:44.351216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=test_df.drop(\"target\",axis=1)\ntest_datagen=ImageDataGenerator(rescale=1/255)\n\ntest_data = test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    directory='./test1',\n    x_col = 'path',\n    y_col=None,\n    target_size=(150,150),\n    class_mode=None,\n    batch_size=50) ","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:47.451636Z","iopub.execute_input":"2024-10-28T16:11:47.452035Z","iopub.status.idle":"2024-10-28T16:11:47.587180Z","shell.execute_reply.started":"2024-10-28T16:11:47.451993Z","shell.execute_reply":"2024-10-28T16:11:47.586420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts, idx = 5, 0\nprint(\"Training samples\",end=\"\\n\\n\")\nfig, ax = plt.subplots(1, counts, figsize=(15, 5))\nfor img_batch, _ in training_data:  # Unpack batch to separate images and labels\n    # Display one sample image from the batch\n    ax[idx].imshow(img_batch[0])\n    ax[idx].axis(\"off\") \n    idx += 1\n    if idx == counts:\n        break\n\nplt.show()\n\n\ncounts, idx = 5, 0\nprint(\"Validation samples\",end=\"\\n\\n\")\nfig, ax = plt.subplots(1, counts, figsize=(15, 5))\nfor img_batch, _ in validation_data:  # Unpack batch to separate images and labels\n    # Display one sample image from the batch\n    ax[idx].imshow(img_batch[0])\n    ax[idx].axis(\"off\") \n    idx += 1\n    if idx == counts:\n        break\n\nplt.show()\n\n\ncounts, idx = 5, 0\nprint(\"Test samples\",end=\"\\n\\n\")\nfig, ax = plt.subplots(1, counts, figsize=(15, 5))\nfor img_batch in test_data:  # Unpack batch to separate images and labels\n    # Display one sample image from the batch\n    ax[idx].imshow(img_batch[0])\n    ax[idx].axis(\"off\") \n    idx += 1\n    if idx == counts:\n        break\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:47.640646Z","iopub.execute_input":"2024-10-28T16:11:47.640957Z","iopub.status.idle":"2024-10-28T16:11:51.367642Z","shell.execute_reply.started":"2024-10-28T16:11:47.640925Z","shell.execute_reply":"2024-10-28T16:11:51.366705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNN Model","metadata":{}},{"cell_type":"code","source":"model=models.Sequential(\n[\n    layers.Conv2D(32,(3,3),input_shape=(150,150,3,),activation=\"relu\"),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Conv2D(64,(3,3),activation=\"relu\"),\n    layers.MaxPooling2D((2,2)),\n    layers.Dropout((0.5)),\n\n    layers.Conv2D(128,(3,3),activation=\"relu\"),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Conv2D(128,(3,3),activation=\"relu\"),\n    layers.MaxPooling2D((2,2)),\n    \n    layers.Flatten(),\n    layers.Dropout((0.5)),\n    layers.Dense(512,activation=\"relu\"),\n    layers.Dense(1,activation=\"sigmoid\")\n])\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:11:53.100109Z","iopub.execute_input":"2024-10-28T16:11:53.100509Z","iopub.status.idle":"2024-10-28T16:11:54.970783Z","shell.execute_reply.started":"2024-10-28T16:11:53.100468Z","shell.execute_reply":"2024-10-28T16:11:54.969458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss=\"binary_crossentropy\",\n    optimizer=\"adam\",\n    metrics=[\"accuracy\"]\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:13:18.489143Z","iopub.execute_input":"2024-10-28T16:13:18.489939Z","iopub.status.idle":"2024-10-28T16:13:18.504559Z","shell.execute_reply.started":"2024-10-28T16:13:18.489899Z","shell.execute_reply":"2024-10-28T16:13:18.503527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.utils.plot_model(model,show_layer_activations=True,show_layer_names=True,show_shapes=True,show_dtype=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:13:20.661046Z","iopub.execute_input":"2024-10-28T16:13:20.661775Z","iopub.status.idle":"2024-10-28T16:13:21.679764Z","shell.execute_reply.started":"2024-10-28T16:13:20.661727Z","shell.execute_reply":"2024-10-28T16:13:21.678876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(\n    training_data,\n    steps_per_epoch=train_df.shape[0]//32,\n    epochs=10,\n    validation_data= validation_data, \n    validation_steps= valid_df.shape[0]// 32,\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T16:14:01.127180Z","iopub.execute_input":"2024-10-28T16:14:01.127977Z","iopub.status.idle":"2024-10-28T16:38:44.521221Z","shell.execute_reply.started":"2024-10-28T16:14:01.127923Z","shell.execute_reply":"2024-10-28T16:38:44.520188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_model_parameters(history):\n    acc=history[\"accuracy\"]\n    val_acc=history[\"val_accuracy\"]\n    loss=history[\"loss\"]\n    val_loss=history[\"val_loss\"]\n    fig, ax = plt.subplots(2, 1, figsize = (8, 10))\n\n    epochs = range(1, len(acc) + 1)\n    ax[0].plot(epochs, acc, '--', label='Training acc')\n    ax[0].plot(epochs, val_acc, label='Validation acc')\n    ax[0].set_title('Training and validation accuracy')\n    ax[0].set_ylim([0.0, 1.0])\n    ax[0].legend()\n\n    ax[1].plot(epochs, loss, '--', label='Training loss')\n    ax[1].plot(epochs, val_loss, label='Validation loss')\n    ax[1].set_title('Training and validation loss')\n    ax[1].legend()\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T17:05:38.029917Z","iopub.execute_input":"2024-10-28T17:05:38.030344Z","iopub.status.idle":"2024-10-28T17:05:38.038668Z","shell.execute_reply.started":"2024-10-28T17:05:38.030303Z","shell.execute_reply":"2024-10-28T17:05:38.037751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model_parameters(history.history)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T17:06:17.803674Z","iopub.execute_input":"2024-10-28T17:06:17.804454Z","iopub.status.idle":"2024-10-28T17:06:18.281663Z","shell.execute_reply.started":"2024-10-28T17:06:17.804415Z","shell.execute_reply":"2024-10-28T17:06:18.280808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.applications.vgg16 import VGG16\n\nmodel2 = Sequential([\n    VGG16(include_top=False, pooling='max', input_shape=(150, 150, 3), weights='imagenet'),\n    layers.Flatten(),\n    layers.Dense(512, activation='relu'),\n    layers.Dense(512, activation='relu'),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(1, activation='sigmoid')\n])\n\n# Freeze the VGG16 model layers to prevent training\nmodel2.layers[0].trainable = False\n\nmodel2.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-28T17:38:09.898506Z","iopub.execute_input":"2024-10-28T17:38:09.898859Z","iopub.status.idle":"2024-10-28T17:38:10.169065Z","shell.execute_reply.started":"2024-10-28T17:38:09.898828Z","shell.execute_reply":"2024-10-28T17:38:10.168156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile(\n    loss=\"binary_crossentropy\",\n    optimizer=\"adam\",\n    metrics=[\"accuracy\"]\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T17:38:46.980767Z","iopub.execute_input":"2024-10-28T17:38:46.981141Z","iopub.status.idle":"2024-10-28T17:38:46.990117Z","shell.execute_reply.started":"2024-10-28T17:38:46.981106Z","shell.execute_reply":"2024-10-28T17:38:46.989267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model2.fit(\n    training_data,\n    steps_per_epoch=train_df.shape[0]//32,\n    epochs=10,\n    validation_data= validation_data, \n    validation_steps= valid_df.shape[0]// 32,\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T17:39:00.101520Z","iopub.execute_input":"2024-10-28T17:39:00.101896Z","iopub.status.idle":"2024-10-28T18:06:34.035233Z","shell.execute_reply.started":"2024-10-28T17:39:00.101860Z","shell.execute_reply":"2024-10-28T18:06:34.034363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model_parameters(history.history)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T18:06:40.154342Z","iopub.execute_input":"2024-10-28T18:06:40.154739Z","iopub.status.idle":"2024-10-28T18:06:40.636291Z","shell.execute_reply.started":"2024-10-28T18:06:40.154702Z","shell.execute_reply":"2024-10-28T18:06:40.635442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  <p align=\"center\">**Doaa Helal**</p>\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}