{"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":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1- Import Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport random as rnd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\n\nimport keras\nfrom keras.models import Sequential\nfrom keras.utils import to_categorical\nfrom keras.preprocessing.image import load_img\nfrom keras.layers import Flatten, Dense, Dropout\nfrom keras.layers import Conv2D, MaxPool2D, BatchNormalization, Activation\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nprint(os.listdir(\"/kaggle/input/dogs-vs-cats\"))","metadata":{"execution":{"iopub.status.busy":"2024-09-25T09:55:25.336409Z","iopub.execute_input":"2024-09-25T09:55:25.336913Z","iopub.status.idle":"2024-09-25T09:55:38.838412Z","shell.execute_reply.started":"2024-09-25T09:55:25.336877Z","shell.execute_reply":"2024-09-25T09:55:38.837404Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"['test1.zip', 'train.zip', 'sampleSubmission.csv']\n","output_type":"stream"}]},{"cell_type":"markdown","source":"# 2- Data-Constants","metadata":{}},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_CHANNELS=3\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:01.735622Z","iopub.execute_input":"2024-08-27T13:38:01.735911Z","iopub.status.idle":"2024-08-27T13:38:01.744372Z","shell.execute_reply.started":"2024-08-27T13:38:01.73588Z","shell.execute_reply":"2024-08-27T13:38:01.743497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3- Load DataSet","metadata":{}},{"cell_type":"code","source":"# import zipfile\n\n# zip_files = ['test1', 'train']\n\n# for zip_file in zip_files:\n#     with zipfile.ZipFile(f\"../input/dogs-vs-cats/{zip_file}.zip\", \"r\") as z:\n#         z.extractall(\".\")\n#         print(f\"{zip_file} unzipped\")","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:01.764104Z","iopub.execute_input":"2024-08-27T13:38:01.764643Z","iopub.status.idle":"2024-08-27T13:38:01.768163Z","shell.execute_reply.started":"2024-08-27T13:38:01.76461Z","shell.execute_reply":"2024-08-27T13:38:01.767281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Extract Compressed Files","metadata":{}},{"cell_type":"code","source":"import zipfile\n\npath = \"/kaggle/input/dogs-vs-cats\"\nfor zip_file in os.listdir(path)[:2]:\n    zipfile.ZipFile(f\"{path}/{zip_file}\", 'r').extractall(\".\")\n#     print(f\"{path}/{zip_file}\")\n    print(f\"{zip_file} unzipped\")","metadata":{"execution":{"iopub.status.busy":"2024-09-25T09:55:55.793858Z","iopub.execute_input":"2024-09-25T09:55:55.795019Z","iopub.status.idle":"2024-09-25T09:56:13.022817Z","shell.execute_reply.started":"2024-09-25T09:55:55.794948Z","shell.execute_reply":"2024-09-25T09:56:13.021899Z"},"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"test1.zip unzipped\ntrain.zip unzipped\n","output_type":"stream"}]},{"cell_type":"markdown","source":"# 4- Data Preprocessing","metadata":{}},{"cell_type":"markdown","source":"> ## Label Encoding","metadata":{}},{"cell_type":"code","source":"train_images = os.listdir(\"/kaggle/working/train\")\nCATEGORIES = []\n\nfor image_name in train_images:\n    class_name = image_name.split('.')[0]  # cat.588.jpg\n    CATEGORIES.append(0 if class_name=='cat' else 1)\n\ndf = pd.DataFrame({\"train_images\": train_images, \"CATEGORIES\": CATEGORIES})","metadata":{"execution":{"iopub.status.busy":"2024-09-25T09:58:02.16249Z","iopub.execute_input":"2024-09-25T09:58:02.163326Z","iopub.status.idle":"2024-09-25T09:58:02.211037Z","shell.execute_reply.started":"2024-09-25T09:58:02.163286Z","shell.execute_reply":"2024-09-25T09:58:02.210296Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"markdown","source":"> ## Show DataFrame","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-25T09:58:05.310053Z","iopub.execute_input":"2024-09-25T09:58:05.310404Z","iopub.status.idle":"2024-09-25T09:58:05.330796Z","shell.execute_reply.started":"2024-09-25T09:58:05.310373Z","shell.execute_reply":"2024-09-25T09:58:05.329928Z"},"trusted":true},"execution_count":4,"outputs":[{"execution_count":4,"output_type":"execute_result","data":{"text/plain":"    train_images  CATEGORIES\n0   dog.4044.jpg           1\n1   dog.9450.jpg           1\n2  dog.12443.jpg           1\n3   dog.8831.jpg           1\n4  cat.10155.jpg           0","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>train_images</th>\n      <th>CATEGORIES</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>dog.4044.jpg</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>dog.9450.jpg</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>dog.12443.jpg</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>dog.8831.jpg</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>cat.10155.jpg</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-09-25T09:58:11.149811Z","iopub.execute_input":"2024-09-25T09:58:11.150197Z","iopub.status.idle":"2024-09-25T09:58:11.169464Z","shell.execute_reply.started":"2024-09-25T09:58:11.150159Z","shell.execute_reply":"2024-09-25T09:58:11.168647Z"},"trusted":true},"execution_count":5,"outputs":[{"execution_count":5,"output_type":"execute_result","data":{"text/plain":"        CATEGORIES\ncount  25000.00000\nmean       0.50000\nstd        0.50001\nmin        0.00000\n25%        0.00000\n50%        0.50000\n75%        1.00000\nmax        1.00000","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CATEGORIES</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>25000.00000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>0.50000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>0.50001</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>0.00000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>0.00000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>0.50000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>1.00000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>1.00000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-25T09:58:14.578672Z","iopub.execute_input":"2024-09-25T09:58:14.579064Z","iopub.status.idle":"2024-09-25T09:58:14.591898Z","shell.execute_reply.started":"2024-09-25T09:58:14.579026Z","shell.execute_reply":"2024-09-25T09:58:14.590851Z"},"trusted":true},"execution_count":6,"outputs":[{"execution_count":6,"output_type":"execute_result","data":{"text/plain":"CATEGORIES\n1    12500\n0    12500\nName: count, dtype: int64"},"metadata":{}}]},{"cell_type":"markdown","source":"Dataset is balanced","metadata":{}},{"cell_type":"markdown","source":"> ## Visualize The Data","metadata":{}},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:13.323225Z","iopub.execute_input":"2024-08-27T13:38:13.323523Z","iopub.status.idle":"2024-08-27T13:38:13.53712Z","shell.execute_reply.started":"2024-08-27T13:38:13.323491Z","shell.execute_reply":"2024-08-27T13:38:13.536279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## *From our data we have 12500 cats and 12500 dogs*","metadata":{}},{"cell_type":"markdown","source":"> ## Show Dimensions Of Images","metadata":{}},{"cell_type":"code","source":"shapes = []\n\nfor image in df[\"train_images\"]:\n    path = os.path.join(\"/kaggle/working/train\",image)\n    image_array = plt.imread(path)\n    shapes.append(image_array.shape)\n    \nprint(pd.Series(shapes).value_counts())\n\nindex = np.argmin(shapes)\nprint(f\"\\nThe Minimum Dimension is ==> {shapes[index]}\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:13.538384Z","iopub.execute_input":"2024-08-27T13:38:13.538756Z","iopub.status.idle":"2024-08-27T13:38:42.573915Z","shell.execute_reply.started":"2024-08-27T13:38:13.538713Z","shell.execute_reply":"2024-08-27T13:38:42.572916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## **See sample image**\n","metadata":{}},{"cell_type":"code","source":"sample = rnd.choice(train_images)\nimage = load_img(os.path.join(\"/kaggle/working/train\", sample))\nprint(type(image))\nplt.imshow(image)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:42.575094Z","iopub.execute_input":"2024-08-27T13:38:42.575419Z","iopub.status.idle":"2024-08-27T13:38:42.813658Z","shell.execute_reply.started":"2024-08-27T13:38:42.575387Z","shell.execute_reply":"2024-08-27T13:38:42.812724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Prepare data\n> Because we will use image genaretor with class_mode=\"categorical\". We need to convert column category into string. Then imagenerator will convert it one-hot encoding which is good for our classification.\n> So we will convert 1 to dog and 0 to cat","metadata":{}},{"cell_type":"code","source":"df[\"CATEGORIES\"] = df[\"CATEGORIES\"].replace({0: 'cat', 1: 'dog'})","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:42.817762Z","iopub.execute_input":"2024-08-27T13:38:42.818407Z","iopub.status.idle":"2024-08-27T13:38:42.825904Z","shell.execute_reply.started":"2024-08-27T13:38:42.818371Z","shell.execute_reply":"2024-08-27T13:38:42.824875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Split Data","metadata":{}},{"cell_type":"code","source":"train_df, valid_df = train_test_split(df, test_size=0.20, random_state=42)\n\ntrain_df = train_df.reset_index(drop=True)\nvalid_df = valid_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:42.827151Z","iopub.execute_input":"2024-08-27T13:38:42.827489Z","iopub.status.idle":"2024-08-27T13:38:42.843Z","shell.execute_reply.started":"2024-08-27T13:38:42.827458Z","shell.execute_reply":"2024-08-27T13:38:42.842142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show tarin DataFrame","metadata":{}},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:42.844062Z","iopub.execute_input":"2024-08-27T13:38:42.84439Z","iopub.status.idle":"2024-08-27T13:38:42.85496Z","shell.execute_reply.started":"2024-08-27T13:38:42.844357Z","shell.execute_reply":"2024-08-27T13:38:42.85402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:42.855936Z","iopub.execute_input":"2024-08-27T13:38:42.856193Z","iopub.status.idle":"2024-08-27T13:38:43.076601Z","shell.execute_reply.started":"2024-08-27T13:38:42.856165Z","shell.execute_reply":"2024-08-27T13:38:43.075732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show valid DataFrame","metadata":{}},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.077852Z","iopub.execute_input":"2024-08-27T13:38:43.078152Z","iopub.status.idle":"2024-08-27T13:38:43.086458Z","shell.execute_reply.started":"2024-08-27T13:38:43.078119Z","shell.execute_reply":"2024-08-27T13:38:43.085565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.087543Z","iopub.execute_input":"2024-08-27T13:38:43.08783Z","iopub.status.idle":"2024-08-27T13:38:43.242035Z","shell.execute_reply.started":"2024-08-27T13:38:43.0878Z","shell.execute_reply":"2024-08-27T13:38:43.241112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Data Augmentation","metadata":{}},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = valid_df.shape[0]\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.243333Z","iopub.execute_input":"2024-08-27T13:38:43.243767Z","iopub.status.idle":"2024-08-27T13:38:43.249872Z","shell.execute_reply.started":"2024-08-27T13:38:43.243725Z","shell.execute_reply":"2024-08-27T13:38:43.24777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Train Generator","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.251377Z","iopub.execute_input":"2024-08-27T13:38:43.25184Z","iopub.status.idle":"2024-08-27T13:38:43.26273Z","shell.execute_reply.started":"2024-08-27T13:38:43.251798Z","shell.execute_reply":"2024-08-27T13:38:43.261801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/working/train\", \n    x_col='train_images',\n    y_col='CATEGORIES',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.263858Z","iopub.execute_input":"2024-08-27T13:38:43.264727Z","iopub.status.idle":"2024-08-27T13:38:43.505737Z","shell.execute_reply.started":"2024-08-27T13:38:43.264683Z","shell.execute_reply":"2024-08-27T13:38:43.504937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Valid Generator","metadata":{}},{"cell_type":"code","source":"valid_datagen = ImageDataGenerator(rescale=1./255)\n\nvalid_generator = valid_datagen.flow_from_dataframe(\n    valid_df, \n    \"/kaggle/working/train\", \n    x_col='train_images',\n    y_col='CATEGORIES',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.50692Z","iopub.execute_input":"2024-08-27T13:38:43.507317Z","iopub.status.idle":"2024-08-27T13:38:43.577058Z","shell.execute_reply.started":"2024-08-27T13:38:43.507272Z","shell.execute_reply":"2024-08-27T13:38:43.576338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # See how our generator work","metadata":{}},{"cell_type":"code","source":"example_df = train_df.sample(n=1) # take 1 image-sample\nprint(example_df, \"\\n\")\n\nexample_df= example_df.reset_index(drop=True)\nprint(example_df, \"\\n\")\n\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"/kaggle/working/train\", \n    x_col='train_images',\n    y_col='CATEGORIES',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.578087Z","iopub.execute_input":"2024-08-27T13:38:43.578386Z","iopub.status.idle":"2024-08-27T13:38:43.591932Z","shell.execute_reply.started":"2024-08-27T13:38:43.578355Z","shell.execute_reply":"2024-08-27T13:38:43.591034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show Generated Images from One-Image","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.axis('off')\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T13:38:43.593242Z","iopub.execute_input":"2024-08-27T13:38:43.593637Z","iopub.status.idle":"2024-08-27T13:38:45.0828Z","shell.execute_reply.started":"2024-08-27T13:38:43.593596Z","shell.execute_reply":"2024-08-27T13:38:45.081901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5- Build The Model","metadata":{}},{"cell_type":"markdown","source":"![image](https://i.imgur.com/ebkMGGu.jpg)","metadata":{}},{"cell_type":"markdown","source":"> * **Input Layer**: It represent input image data. It will reshape image into single diminsion array. Example your image is 64x64 = 4096, it will convert to (4096,1) array.\n> * **Conv Layer**: This layer will extract features from image.\n> * **Pooling Layer**: This layerreduce the spatial volume of input image after convolution.\n> * **Fully Connected Layer**: It connect the network from a layer to another layer\n> * **Output Layer**: It is the predicted values layer.","metadata":{}},{"cell_type":"markdown","source":"> # Fit Model","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)),\n#     BatchNormalization(),\n    MaxPool2D(pool_size=(4,4)),\n    Dropout(rate=0.25),\n    \n    Conv2D(64, (3, 3), activation='relu'),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(4,4)),\n    Dropout(rate=0.25),\n    \n    Conv2D(128, (3, 3), activation='relu'),\n    BatchNormalization(),\n    MaxPool2D(pool_size=(4,4)),\n    Dropout(rate=0.25),\n    \n    Flatten(),\n    \n    Dense(512, activation='relu'),\n    Dropout(rate=0.5),\n\n    Dense(2, activation='softmax') # 2 because we have cat and dog classes\n\n])\n\n# Compile\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# Summary of Model\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:19:06.516027Z","iopub.execute_input":"2024-08-27T14:19:06.516888Z","iopub.status.idle":"2024-08-27T14:19:06.624734Z","shell.execute_reply.started":"2024-08-27T14:19:06.516846Z","shell.execute_reply":"2024-08-27T14:19:06.623785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Callbacks","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:19:19.519038Z","iopub.execute_input":"2024-08-27T14:19:19.519443Z","iopub.status.idle":"2024-08-27T14:19:19.524119Z","shell.execute_reply.started":"2024-08-27T14:19:19.519404Z","shell.execute_reply":"2024-08-27T14:19:19.523191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ### Early Stop\n> To prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased","metadata":{}},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=5)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:19:20.74886Z","iopub.execute_input":"2024-08-27T14:19:20.749568Z","iopub.status.idle":"2024-08-27T14:19:20.753785Z","shell.execute_reply.started":"2024-08-27T14:19:20.749527Z","shell.execute_reply":"2024-08-27T14:19:20.752767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ### Learning Rate Reduction\n> We will reduce the learning rate when then accuracy not increase for 2 steps","metadata":{}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:19:22.37726Z","iopub.execute_input":"2024-08-27T14:19:22.377661Z","iopub.status.idle":"2024-08-27T14:19:22.382786Z","shell.execute_reply.started":"2024-08-27T14:19:22.377624Z","shell.execute_reply":"2024-08-27T14:19:22.381875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 3 if FAST_RUN else 10\n\nhistory = model.fit(train_generator,\n                    epochs=epochs,\n                    validation_data=valid_generator,\n#                     validation_steps=total_validate//batch_size, # 5000//15  = 333\n#                     steps_per_epoch=total_train//batch_size,     # 20000//15 = 1333\n                    callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:19:24.797186Z","iopub.execute_input":"2024-08-27T14:19:24.797617Z","iopub.status.idle":"2024-08-27T14:37:14.755571Z","shell.execute_reply.started":"2024-08-27T14:19:24.797581Z","shell.execute_reply":"2024-08-27T14:37:14.754669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Visualize The Performance","metadata":{}},{"cell_type":"code","source":"accuracy = history.history[\"accuracy\"]\nval_accuracy = history.history[\"val_accuracy\"]\n\nloss = history.history[\"loss\"]\nval_loss = history.history[\"val_loss\"]\n\nepochs = range(len(accuracy))\n\nplt.figure(figsize=(12,6))\n\nplt.subplot(1,2,1)\nplt.plot(epochs, accuracy, 'bo', label=\"Train_Acc\")\nplt.plot(epochs, val_accuracy, 'r', label=\"val_Acc\")\nplt.legend(loc='best', shadow=True)\n\nplt.subplot(1,2,2)\nplt.plot(epochs, loss, 'bo', label=\"Train_Loss\")\nplt.plot(epochs, val_loss, 'r', label=\"val_Loss\")\nplt.legend(loc='best', shadow=True)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:14.757849Z","iopub.execute_input":"2024-08-27T14:37:14.758657Z","iopub.status.idle":"2024-08-27T14:37:15.114129Z","shell.execute_reply.started":"2024-08-27T14:37:14.758604Z","shell.execute_reply":"2024-08-27T14:37:15.113234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # The Epochs = 10 Only. We Can Increase The number of Epochs to Reach Suitable Accuracy.","metadata":{}},{"cell_type":"markdown","source":"> # Prepare Testing Data","metadata":{}},{"cell_type":"code","source":"test_file_names = os.listdir(\"/kaggle/working/test1\")\ntest_df = pd.DataFrame({'file_name': test_file_names})\n\ntotal_testing = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:15.11552Z","iopub.execute_input":"2024-08-27T14:37:15.116168Z","iopub.status.idle":"2024-08-27T14:37:15.130492Z","shell.execute_reply.started":"2024-08-27T14:37:15.11612Z","shell.execute_reply":"2024-08-27T14:37:15.129562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Test The Model","metadata":{}},{"cell_type":"markdown","source":"> # Create Testing Generator","metadata":{}},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test1\", \n    x_col='file_name',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:15.132311Z","iopub.execute_input":"2024-08-27T14:37:15.132608Z","iopub.status.idle":"2024-08-27T14:37:15.265521Z","shell.execute_reply.started":"2024-08-27T14:37:15.132576Z","shell.execute_reply":"2024-08-27T14:37:15.264761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Predict","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:15.266591Z","iopub.execute_input":"2024-08-27T14:37:15.266895Z","iopub.status.idle":"2024-08-27T14:37:33.959644Z","shell.execute_reply.started":"2024-08-27T14:37:15.266862Z","shell.execute_reply":"2024-08-27T14:37:33.958687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> For categoral classication the prediction will come with probability of each category. So we will pick the category that have the highest probability with numpy average max","metadata":{}},{"cell_type":"code","source":"test_df[\"CATEGORIES\"] = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:33.961144Z","iopub.execute_input":"2024-08-27T14:37:33.961695Z","iopub.status.idle":"2024-08-27T14:37:33.967565Z","shell.execute_reply.started":"2024-08-27T14:37:33.961646Z","shell.execute_reply":"2024-08-27T14:37:33.966582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> We will convert the predict category back into our generator classes by using train_generator.class_indices. It is the classes that image generator map while converting data into computer vision","metadata":{}},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['CATEGORIES'] = test_df['CATEGORIES'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:33.96883Z","iopub.execute_input":"2024-08-27T14:37:33.969148Z","iopub.status.idle":"2024-08-27T14:37:33.981097Z","shell.execute_reply.started":"2024-08-27T14:37:33.969115Z","shell.execute_reply":"2024-08-27T14:37:33.9802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> From our prepare data part. We map data with {1: 'dog', 0: 'cat'}. Now we will map the result back to dog is 1 and cat is 0\n\n","metadata":{}},{"cell_type":"code","source":"test_df['CATEGORIES'] = test_df['CATEGORIES'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:33.982876Z","iopub.execute_input":"2024-08-27T14:37:33.983747Z","iopub.status.idle":"2024-08-27T14:37:34.000917Z","shell.execute_reply.started":"2024-08-27T14:37:33.983695Z","shell.execute_reply":"2024-08-27T14:37:33.999961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6- Visualize The Result","metadata":{}},{"cell_type":"code","source":"test_df[\"CATEGORIES\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:34.004022Z","iopub.execute_input":"2024-08-27T14:37:34.004414Z","iopub.status.idle":"2024-08-27T14:37:34.242071Z","shell.execute_reply.started":"2024-08-27T14:37:34.004379Z","shell.execute_reply":"2024-08-27T14:37:34.241103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Show Predicted Images","metadata":{}},{"cell_type":"code","source":"sample_test = test_df.head(36)\n\nplt.figure(figsize=(15,15))\n\nfor index, row in sample_test.iterrows():\n    file_name = row['file_name']\n    category = row['CATEGORIES']\n    path = os.path.join(\"/kaggle/working/test1\", file_name)\n    img = load_img(path, target_size=IMAGE_SIZE)\n    plt.subplot(6,6,index+1)\n    plt.imshow(img)\n    plt.title(f\"{file_name}\\n is ==> {'cat' if category==0 else 'dog'}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:34.243249Z","iopub.execute_input":"2024-08-27T14:37:34.243542Z","iopub.status.idle":"2024-08-27T14:37:38.515088Z","shell.execute_reply.started":"2024-08-27T14:37:34.243511Z","shell.execute_reply":"2024-08-27T14:37:38.513613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7- Save The Model","metadata":{}},{"cell_type":"code","source":"model.save(\"CNN.h5\")\nmodel.save_weights(\"CNN.weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:38.516617Z","iopub.execute_input":"2024-08-27T14:37:38.516936Z","iopub.status.idle":"2024-08-27T14:37:38.653181Z","shell.execute_reply.started":"2024-08-27T14:37:38.516903Z","shell.execute_reply":"2024-08-27T14:37:38.652369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8- Submission","metadata":{}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['file_name'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['CATEGORIES']\nsubmission_df.drop(['file_name', 'CATEGORIES'], axis=1, inplace=True)\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-27T14:37:38.654382Z","iopub.execute_input":"2024-08-27T14:37:38.654711Z","iopub.status.idle":"2024-08-27T14:37:38.69991Z","shell.execute_reply.started":"2024-08-27T14:37:38.654679Z","shell.execute_reply":"2024-08-27T14:37:38.699001Z"},"trusted":true},"execution_count":null,"outputs":[]}]}