{"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:01:12.422539Z","iopub.execute_input":"2024-12-17T19:01:12.422961Z","iopub.status.idle":"2024-12-17T19:01:29.818213Z","shell.execute_reply.started":"2024-12-17T19:01:12.422917Z","shell.execute_reply":"2024-12-17T19:01:29.816914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_CHANNELS=3\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:01:54.376731Z","iopub.execute_input":"2024-12-17T19:01:54.377162Z","iopub.status.idle":"2024-12-17T19:01:54.383126Z","shell.execute_reply.started":"2024-12-17T19:01:54.377126Z","shell.execute_reply":"2024-12-17T19:01:54.381548Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:02:42.044932Z","iopub.execute_input":"2024-12-17T19:02:42.045370Z","iopub.status.idle":"2024-12-17T19:02:42.050834Z","shell.execute_reply.started":"2024-12-17T19:02:42.045334Z","shell.execute_reply":"2024-12-17T19:02:42.049559Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:02:53.138075Z","iopub.execute_input":"2024-12-17T19:02:53.138502Z","iopub.status.idle":"2024-12-17T19:03:08.506074Z","shell.execute_reply.started":"2024-12-17T19:02:53.138464Z","shell.execute_reply":"2024-12-17T19:03:08.504690Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:03:18.444767Z","iopub.execute_input":"2024-12-17T19:03:18.446103Z","iopub.status.idle":"2024-12-17T19:03:18.483751Z","shell.execute_reply.started":"2024-12-17T19:03:18.446056Z","shell.execute_reply":"2024-12-17T19:03:18.482379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:03:32.333313Z","iopub.execute_input":"2024-12-17T19:03:32.333727Z","iopub.status.idle":"2024-12-17T19:03:32.353524Z","shell.execute_reply.started":"2024-12-17T19:03:32.333685Z","shell.execute_reply":"2024-12-17T19:03:32.352209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.tail()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:03:54.704961Z","iopub.execute_input":"2024-12-17T19:03:54.705352Z","iopub.status.idle":"2024-12-17T19:03:54.716644Z","shell.execute_reply.started":"2024-12-17T19:03:54.705320Z","shell.execute_reply":"2024-12-17T19:03:54.715233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:04:03.159332Z","iopub.execute_input":"2024-12-17T19:04:03.159732Z","iopub.status.idle":"2024-12-17T19:04:03.182479Z","shell.execute_reply.started":"2024-12-17T19:04:03.159695Z","shell.execute_reply":"2024-12-17T19:04:03.181212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:04:11.756284Z","iopub.execute_input":"2024-12-17T19:04:11.756700Z","iopub.status.idle":"2024-12-17T19:04:11.771841Z","shell.execute_reply.started":"2024-12-17T19:04:11.756663Z","shell.execute_reply":"2024-12-17T19:04:11.770508Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:05:20.515220Z","iopub.execute_input":"2024-12-17T19:05:20.516401Z","iopub.status.idle":"2024-12-17T19:05:20.776478Z","shell.execute_reply.started":"2024-12-17T19:05:20.516359Z","shell.execute_reply":"2024-12-17T19:05:20.775345Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:05:40.579241Z","iopub.execute_input":"2024-12-17T19:05:40.579629Z","iopub.status.idle":"2024-12-17T19:06:17.884904Z","shell.execute_reply.started":"2024-12-17T19:05:40.579593Z","shell.execute_reply":"2024-12-17T19:06:17.883684Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:06:36.143201Z","iopub.execute_input":"2024-12-17T19:06:36.143615Z","iopub.status.idle":"2024-12-17T19:06:36.372528Z","shell.execute_reply.started":"2024-12-17T19:06:36.143579Z","shell.execute_reply":"2024-12-17T19:06:36.371324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"CATEGORIES\"] = df[\"CATEGORIES\"].replace({0: 'cat', 1: 'dog'})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:07:14.143041Z","iopub.execute_input":"2024-12-17T19:07:14.143447Z","iopub.status.idle":"2024-12-17T19:07:14.154600Z","shell.execute_reply.started":"2024-12-17T19:07:14.143414Z","shell.execute_reply":"2024-12-17T19:07:14.153259Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:07:34.049655Z","iopub.execute_input":"2024-12-17T19:07:34.050992Z","iopub.status.idle":"2024-12-17T19:07:34.068883Z","shell.execute_reply.started":"2024-12-17T19:07:34.050946Z","shell.execute_reply":"2024-12-17T19:07:34.067757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:07:45.517632Z","iopub.execute_input":"2024-12-17T19:07:45.518059Z","iopub.status.idle":"2024-12-17T19:07:45.528763Z","shell.execute_reply.started":"2024-12-17T19:07:45.518021Z","shell.execute_reply":"2024-12-17T19:07:45.527372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:08:03.435034Z","iopub.execute_input":"2024-12-17T19:08:03.435415Z","iopub.status.idle":"2024-12-17T19:08:03.643356Z","shell.execute_reply.started":"2024-12-17T19:08:03.435384Z","shell.execute_reply":"2024-12-17T19:08:03.642195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:08:15.541935Z","iopub.execute_input":"2024-12-17T19:08:15.542433Z","iopub.status.idle":"2024-12-17T19:08:15.553233Z","shell.execute_reply.started":"2024-12-17T19:08:15.542391Z","shell.execute_reply":"2024-12-17T19:08:15.551647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:08:25.220330Z","iopub.execute_input":"2024-12-17T19:08:25.220744Z","iopub.status.idle":"2024-12-17T19:08:25.382123Z","shell.execute_reply.started":"2024-12-17T19:08:25.220708Z","shell.execute_reply":"2024-12-17T19:08:25.380866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = valid_df.shape[0]\nbatch_size=15","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:08:34.991859Z","iopub.execute_input":"2024-12-17T19:08:34.992294Z","iopub.status.idle":"2024-12-17T19:08:34.997606Z","shell.execute_reply.started":"2024-12-17T19:08:34.992257Z","shell.execute_reply":"2024-12-17T19:08:34.996409Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:08:44.701944Z","iopub.execute_input":"2024-12-17T19:08:44.702376Z","iopub.status.idle":"2024-12-17T19:08:44.713269Z","shell.execute_reply.started":"2024-12-17T19:08:44.702340Z","shell.execute_reply":"2024-12-17T19:08:44.711948Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:08:54.086874Z","iopub.execute_input":"2024-12-17T19:08:54.087263Z","iopub.status.idle":"2024-12-17T19:08:54.306706Z","shell.execute_reply.started":"2024-12-17T19:08:54.087228Z","shell.execute_reply":"2024-12-17T19:08:54.305550Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:09:07.703604Z","iopub.execute_input":"2024-12-17T19:09:07.704015Z","iopub.status.idle":"2024-12-17T19:09:07.764108Z","shell.execute_reply.started":"2024-12-17T19:09:07.703978Z","shell.execute_reply":"2024-12-17T19:09:07.762913Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:09:17.117599Z","iopub.execute_input":"2024-12-17T19:09:17.118015Z","iopub.status.idle":"2024-12-17T19:09:17.137543Z","shell.execute_reply.started":"2024-12-17T19:09:17.117977Z","shell.execute_reply":"2024-12-17T19:09:17.135957Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:09:26.179228Z","iopub.execute_input":"2024-12-17T19:09:26.179596Z","iopub.status.idle":"2024-12-17T19:09:27.286185Z","shell.execute_reply.started":"2024-12-17T19:09:26.179565Z","shell.execute_reply":"2024-12-17T19:09:27.284804Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:09:58.604584Z","iopub.execute_input":"2024-12-17T19:09:58.605043Z","iopub.status.idle":"2024-12-17T19:09:58.786795Z","shell.execute_reply.started":"2024-12-17T19:09:58.605004Z","shell.execute_reply":"2024-12-17T19:09:58.785899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:10:29.399093Z","iopub.execute_input":"2024-12-17T19:10:29.399501Z","iopub.status.idle":"2024-12-17T19:10:29.405993Z","shell.execute_reply.started":"2024-12-17T19:10:29.399467Z","shell.execute_reply":"2024-12-17T19:10:29.404805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:10:38.039897Z","iopub.execute_input":"2024-12-17T19:10:38.040309Z","iopub.status.idle":"2024-12-17T19:10:38.045935Z","shell.execute_reply.started":"2024-12-17T19:10:38.040272Z","shell.execute_reply":"2024-12-17T19:10:38.044529Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:10:47.827453Z","iopub.execute_input":"2024-12-17T19:10:47.828326Z","iopub.status.idle":"2024-12-17T19:10:47.833473Z","shell.execute_reply.started":"2024-12-17T19:10:47.828283Z","shell.execute_reply":"2024-12-17T19:10:47.832154Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:10:58.268589Z","iopub.execute_input":"2024-12-17T19:10:58.269800Z","iopub.status.idle":"2024-12-17T19:42:57.043358Z","shell.execute_reply.started":"2024-12-17T19:10:58.269692Z","shell.execute_reply":"2024-12-17T19:42:57.042080Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:44:21.188616Z","iopub.execute_input":"2024-12-17T19:44:21.189013Z","iopub.status.idle":"2024-12-17T19:44:21.581077Z","shell.execute_reply.started":"2024-12-17T19:44:21.188980Z","shell.execute_reply":"2024-12-17T19:44:21.579787Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:45:02.269409Z","iopub.execute_input":"2024-12-17T19:45:02.269840Z","iopub.status.idle":"2024-12-17T19:45:02.287277Z","shell.execute_reply.started":"2024-12-17T19:45:02.269780Z","shell.execute_reply":"2024-12-17T19:45:02.285944Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:45:13.883387Z","iopub.execute_input":"2024-12-17T19:45:13.883756Z","iopub.status.idle":"2024-12-17T19:45:14.005469Z","shell.execute_reply.started":"2024-12-17T19:45:13.883725Z","shell.execute_reply":"2024-12-17T19:45:14.004309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:45:25.044981Z","iopub.execute_input":"2024-12-17T19:45:25.045922Z","iopub.status.idle":"2024-12-17T19:45:56.185801Z","shell.execute_reply.started":"2024-12-17T19:45:25.045873Z","shell.execute_reply":"2024-12-17T19:45:56.184644Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[\"CATEGORIES\"] = np.argmax(predictions, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:47:15.993917Z","iopub.execute_input":"2024-12-17T19:47:15.994310Z","iopub.status.idle":"2024-12-17T19:47:16.000191Z","shell.execute_reply.started":"2024-12-17T19:47:15.994273Z","shell.execute_reply":"2024-12-17T19:47:15.998981Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:47:30.257361Z","iopub.execute_input":"2024-12-17T19:47:30.257737Z","iopub.status.idle":"2024-12-17T19:47:30.266004Z","shell.execute_reply.started":"2024-12-17T19:47:30.257703Z","shell.execute_reply":"2024-12-17T19:47:30.264620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['CATEGORIES'] = test_df['CATEGORIES'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:47:43.261607Z","iopub.execute_input":"2024-12-17T19:47:43.262033Z","iopub.status.idle":"2024-12-17T19:47:43.275297Z","shell.execute_reply.started":"2024-12-17T19:47:43.261987Z","shell.execute_reply":"2024-12-17T19:47:43.274081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[\"CATEGORIES\"].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:47:55.215198Z","iopub.execute_input":"2024-12-17T19:47:55.215983Z","iopub.status.idle":"2024-12-17T19:47:55.430990Z","shell.execute_reply.started":"2024-12-17T19:47:55.215935Z","shell.execute_reply":"2024-12-17T19:47:55.429787Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:48:07.061098Z","iopub.execute_input":"2024-12-17T19:48:07.061588Z","iopub.status.idle":"2024-12-17T19:48:10.987440Z","shell.execute_reply.started":"2024-12-17T19:48:07.061550Z","shell.execute_reply":"2024-12-17T19:48:10.986353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"CNN.h5\")\nmodel.save_weights(\"CNN.weights.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:48:28.961860Z","iopub.execute_input":"2024-12-17T19:48:28.962326Z","iopub.status.idle":"2024-12-17T19:48:29.078563Z","shell.execute_reply.started":"2024-12-17T19:48:28.962289Z","shell.execute_reply":"2024-12-17T19:48:29.077197Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:48:39.120176Z","iopub.execute_input":"2024-12-17T19:48:39.120556Z","iopub.status.idle":"2024-12-17T19:48:39.162284Z","shell.execute_reply.started":"2024-12-17T19:48:39.120522Z","shell.execute_reply":"2024-12-17T19:48:39.161233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!wget https://wl-genial.cf.tsp.li/resize/728x/jpg/27e/3ac/d457dd54cdbe4443445792ccb8.jpg\nimagen = load_img('d457dd54cdbe4443445792ccb8.jpg', target_size=IMAGE_SIZE)\nimg = np.expand_dims(imagen, 0) # make 'batch' of 1\npred = model.predict(img)\n#pred = labels[\"label_names\"][np.argmax(pred)]\ncategory = np.argmax(pred)\nplt.imshow(imagen)\nplt.title(f\"esa imagen es \\n ==> {'cat' if category==0 else 'dog'}\")\nplt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:48:48.068799Z","iopub.execute_input":"2024-12-17T19:48:48.069199Z","iopub.status.idle":"2024-12-17T19:48:49.861611Z","shell.execute_reply.started":"2024-12-17T19:48:48.069167Z","shell.execute_reply":"2024-12-17T19:48:49.860116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!wget https://oglebay.com/wp-content/uploads/2022/06/IMG_5670_M-e1654806092422.jpg\nimagen = load_img('IMG_5670_M-e1654806092422.jpg', target_size=IMAGE_SIZE)\nimg = np.expand_dims(imagen, 0) # make 'batch' of 1\npred = model.predict(img)\n#pred = labels[\"label_names\"][np.argmax(pred)]\ncategory = np.argmax(pred)\nplt.imshow(imagen)\nplt.title(f\"esa imagen es \\n ==> {'cat' if category==0 else 'dog'}\")\nplt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-17T19:49:08.294668Z","iopub.execute_input":"2024-12-17T19:49:08.295164Z","iopub.status.idle":"2024-12-17T19:49:10.445345Z","shell.execute_reply.started":"2024-12-17T19:49:08.295124Z","shell.execute_reply":"2024-12-17T19:49:10.443770Z"}},"outputs":[],"execution_count":null}]}