{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30822,"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-19T21:08:17.479730Z","iopub.execute_input":"2024-12-19T21:08:17.480108Z","iopub.status.idle":"2024-12-19T21:08:31.301228Z","shell.execute_reply.started":"2024-12-19T21:08:17.480065Z","shell.execute_reply":"2024-12-19T21:08:31.300057Z"}},"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-19T21:08:56.129090Z","iopub.execute_input":"2024-12-19T21:08:56.129447Z","iopub.status.idle":"2024-12-19T21:08:56.133927Z","shell.execute_reply.started":"2024-12-19T21:08:56.129396Z","shell.execute_reply":"2024-12-19T21:08:56.132927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1', 'train']\n\nfor 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-19T21:09:25.245162Z","iopub.execute_input":"2024-12-19T21:09:25.245560Z","iopub.status.idle":"2024-12-19T21:09:39.335724Z","shell.execute_reply.started":"2024-12-19T21:09:25.245527Z","shell.execute_reply":"2024-12-19T21:09:39.334621Z"}},"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-19T21:10:23.841479Z","iopub.execute_input":"2024-12-19T21:10:23.841869Z","iopub.status.idle":"2024-12-19T21:10:35.545709Z","shell.execute_reply.started":"2024-12-19T21:10:23.841836Z","shell.execute_reply":"2024-12-19T21:10:35.544563Z"}},"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-19T21:10:42.340567Z","iopub.execute_input":"2024-12-19T21:10:42.340909Z","iopub.status.idle":"2024-12-19T21:10:42.380764Z","shell.execute_reply.started":"2024-12-19T21:10:42.340880Z","shell.execute_reply":"2024-12-19T21:10:42.379871Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:10:50.906949Z","iopub.execute_input":"2024-12-19T21:10:50.907262Z","iopub.status.idle":"2024-12-19T21:10:50.928274Z","shell.execute_reply.started":"2024-12-19T21:10:50.907238Z","shell.execute_reply":"2024-12-19T21:10:50.926794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.tail()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:11:11.591240Z","iopub.execute_input":"2024-12-19T21:11:11.591619Z","iopub.status.idle":"2024-12-19T21:11:11.600296Z","shell.execute_reply.started":"2024-12-19T21:11:11.591585Z","shell.execute_reply":"2024-12-19T21:11:11.599221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:11:24.949416Z","iopub.execute_input":"2024-12-19T21:11:24.949837Z","iopub.status.idle":"2024-12-19T21:11:24.973703Z","shell.execute_reply.started":"2024-12-19T21:11:24.949808Z","shell.execute_reply":"2024-12-19T21:11:24.972551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:11:33.021189Z","iopub.execute_input":"2024-12-19T21:11:33.021606Z","iopub.status.idle":"2024-12-19T21:11:33.039454Z","shell.execute_reply.started":"2024-12-19T21:11:33.021574Z","shell.execute_reply":"2024-12-19T21:11:33.038544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"CATEGORIES\"].value_counts().plot.bar()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:11:40.900279Z","iopub.execute_input":"2024-12-19T21:11:40.900686Z","iopub.status.idle":"2024-12-19T21:11:41.174619Z","shell.execute_reply.started":"2024-12-19T21:11:40.900654Z","shell.execute_reply":"2024-12-19T21:11:41.173559Z"}},"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\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:11:53.210255Z","iopub.execute_input":"2024-12-19T21:11:53.210640Z","iopub.status.idle":"2024-12-19T21:12:27.779489Z","shell.execute_reply.started":"2024-12-19T21:11:53.210608Z","shell.execute_reply":"2024-12-19T21:12:27.778508Z"}},"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()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:12:41.080929Z","iopub.execute_input":"2024-12-19T21:12:41.081305Z","iopub.status.idle":"2024-12-19T21:12:41.245027Z","shell.execute_reply.started":"2024-12-19T21:12:41.081272Z","shell.execute_reply":"2024-12-19T21:12:41.243579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"CATEGORIES\"] = df[\"CATEGORIES\"].replace({0: 'cat', 1: 'dog'})\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:13:00.012452Z","iopub.execute_input":"2024-12-19T21:13:00.012794Z","iopub.status.idle":"2024-12-19T21:13:00.020716Z","shell.execute_reply.started":"2024-12-19T21:13:00.012765Z","shell.execute_reply":"2024-12-19T21:13:00.019544Z"}},"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-19T21:13:20.441806Z","iopub.execute_input":"2024-12-19T21:13:20.442131Z","iopub.status.idle":"2024-12-19T21:13:20.453381Z","shell.execute_reply.started":"2024-12-19T21:13:20.442107Z","shell.execute_reply":"2024-12-19T21:13:20.452533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:13:24.110178Z","iopub.execute_input":"2024-12-19T21:13:24.110526Z","iopub.status.idle":"2024-12-19T21:13:24.119466Z","shell.execute_reply.started":"2024-12-19T21:13:24.110500Z","shell.execute_reply":"2024-12-19T21:13:24.118395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['CATEGORIES'].value_counts().plot.bar()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:13:31.726894Z","iopub.execute_input":"2024-12-19T21:13:31.727270Z","iopub.status.idle":"2024-12-19T21:13:31.938285Z","shell.execute_reply.started":"2024-12-19T21:13:31.727239Z","shell.execute_reply":"2024-12-19T21:13:31.937286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:13:39.538865Z","iopub.execute_input":"2024-12-19T21:13:39.539199Z","iopub.status.idle":"2024-12-19T21:13:39.547118Z","shell.execute_reply.started":"2024-12-19T21:13:39.539172Z","shell.execute_reply":"2024-12-19T21:13:39.546036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_df['CATEGORIES'].value_counts().plot.bar()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:13:47.129649Z","iopub.execute_input":"2024-12-19T21:13:47.130029Z","iopub.status.idle":"2024-12-19T21:13:47.281269Z","shell.execute_reply.started":"2024-12-19T21:13:47.130001Z","shell.execute_reply":"2024-12-19T21:13:47.280032Z"}},"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-19T21:13:57.491082Z","iopub.execute_input":"2024-12-19T21:13:57.491401Z","iopub.status.idle":"2024-12-19T21:13:57.496101Z","shell.execute_reply.started":"2024-12-19T21:13:57.491378Z","shell.execute_reply":"2024-12-19T21:13:57.495035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:14:04.660721Z","iopub.execute_input":"2024-12-19T21:14:04.661053Z","iopub.status.idle":"2024-12-19T21:14:04.670383Z","shell.execute_reply.started":"2024-12-19T21:14:04.661025Z","shell.execute_reply":"2024-12-19T21:14:04.669279Z"}},"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-19T21:14:11.969814Z","iopub.execute_input":"2024-12-19T21:14:11.970274Z","iopub.status.idle":"2024-12-19T21:14:12.180652Z","shell.execute_reply.started":"2024-12-19T21:14:11.970243Z","shell.execute_reply":"2024-12-19T21:14:12.179678Z"}},"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-19T21:14:19.339300Z","iopub.execute_input":"2024-12-19T21:14:19.339741Z","iopub.status.idle":"2024-12-19T21:14:19.399294Z","shell.execute_reply.started":"2024-12-19T21:14:19.339707Z","shell.execute_reply":"2024-12-19T21:14:19.398458Z"}},"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-19T21:14:30.418108Z","iopub.execute_input":"2024-12-19T21:14:30.418489Z","iopub.status.idle":"2024-12-19T21:14:30.432263Z","shell.execute_reply.started":"2024-12-19T21:14:30.418417Z","shell.execute_reply":"2024-12-19T21:14:30.431349Z"}},"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-19T21:14:40.247311Z","iopub.execute_input":"2024-12-19T21:14:40.247740Z","iopub.status.idle":"2024-12-19T21:14:41.282607Z","shell.execute_reply.started":"2024-12-19T21:14:40.247707Z","shell.execute_reply":"2024-12-19T21:14:41.281295Z"}},"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-19T21:14:56.847638Z","iopub.execute_input":"2024-12-19T21:14:56.847991Z","iopub.status.idle":"2024-12-19T21:14:57.070852Z","shell.execute_reply.started":"2024-12-19T21:14:56.847963Z","shell.execute_reply":"2024-12-19T21:14:57.067151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:15:27.204603Z","iopub.execute_input":"2024-12-19T21:15:27.204978Z","iopub.status.idle":"2024-12-19T21:15:27.209189Z","shell.execute_reply.started":"2024-12-19T21:15:27.204945Z","shell.execute_reply":"2024-12-19T21:15:27.208125Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=5)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:15:33.920983Z","iopub.execute_input":"2024-12-19T21:15:33.921337Z","iopub.status.idle":"2024-12-19T21:15:33.925902Z","shell.execute_reply.started":"2024-12-19T21:15:33.921309Z","shell.execute_reply":"2024-12-19T21:15:33.924685Z"}},"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-19T21:15:42.713315Z","iopub.execute_input":"2024-12-19T21:15:42.713686Z","iopub.status.idle":"2024-12-19T21:15:42.718367Z","shell.execute_reply.started":"2024-12-19T21:15:42.713656Z","shell.execute_reply":"2024-12-19T21:15:42.717214Z"}},"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-19T21:15:59.713797Z","iopub.execute_input":"2024-12-19T21:15:59.714117Z","iopub.status.idle":"2024-12-19T21:31:48.658213Z","shell.execute_reply.started":"2024-12-19T21:15:59.714090Z","shell.execute_reply":"2024-12-19T21:31:48.656908Z"}},"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()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:54:59.618468Z","iopub.execute_input":"2024-12-19T21:54:59.618837Z","iopub.status.idle":"2024-12-19T21:55:00.014572Z","shell.execute_reply.started":"2024-12-19T21:54:59.618785Z","shell.execute_reply":"2024-12-19T21:55:00.013513Z"}},"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]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:56:16.670203Z","iopub.execute_input":"2024-12-19T21:56:16.670586Z","iopub.status.idle":"2024-12-19T21:56:16.684676Z","shell.execute_reply.started":"2024-12-19T21:56:16.670554Z","shell.execute_reply":"2024-12-19T21:56:16.683706Z"}},"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)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:56:20.839580Z","iopub.execute_input":"2024-12-19T21:56:20.839952Z","iopub.status.idle":"2024-12-19T21:56:20.948563Z","shell.execute_reply.started":"2024-12-19T21:56:20.839922Z","shell.execute_reply":"2024-12-19T21:56:20.947632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(test_generator)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T21:59:20.423150Z","iopub.execute_input":"2024-12-19T21:59:20.423517Z","iopub.status.idle":"2024-12-19T21:59:49.806500Z","shell.execute_reply.started":"2024-12-19T21:59:20.423490Z","shell.execute_reply":"2024-12-19T21:59:49.805581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[\"CATEGORIES\"] = np.argmax(predictions, axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T22:00:02.038854Z","iopub.execute_input":"2024-12-19T22:00:02.039227Z","iopub.status.idle":"2024-12-19T22:00:02.044561Z","shell.execute_reply.started":"2024-12-19T22:00:02.039193Z","shell.execute_reply":"2024-12-19T22:00:02.043265Z"}},"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)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T22:00:06.092395Z","iopub.execute_input":"2024-12-19T22:00:06.092832Z","iopub.status.idle":"2024-12-19T22:00:06.101406Z","shell.execute_reply.started":"2024-12-19T22:00:06.092798Z","shell.execute_reply":"2024-12-19T22:00:06.100442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['CATEGORIES'] = test_df['CATEGORIES'].replace({ 'dog': 1, 'cat': 0 })\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T22:00:11.677808Z","iopub.execute_input":"2024-12-19T22:00:11.678191Z","iopub.status.idle":"2024-12-19T22:00:11.689154Z","shell.execute_reply.started":"2024-12-19T22:00:11.678158Z","shell.execute_reply":"2024-12-19T22:00:11.688081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[\"CATEGORIES\"].value_counts().plot.bar()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T22:00:17.631090Z","iopub.execute_input":"2024-12-19T22:00:17.631413Z","iopub.status.idle":"2024-12-19T22:00:17.843966Z","shell.execute_reply.started":"2024-12-19T22:00:17.631387Z","shell.execute_reply":"2024-12-19T22:00:17.842917Z"}},"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-19T22:00:20.274657Z","iopub.execute_input":"2024-12-19T22:00:20.275105Z","iopub.status.idle":"2024-12-19T22:00:24.115919Z","shell.execute_reply.started":"2024-12-19T22:00:20.275073Z","shell.execute_reply":"2024-12-19T22:00:24.114375Z"}},"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-19T22:00:38.762708Z","iopub.execute_input":"2024-12-19T22:00:38.763134Z","iopub.status.idle":"2024-12-19T22:00:38.841472Z","shell.execute_reply.started":"2024-12-19T22:00:38.763101Z","shell.execute_reply":"2024-12-19T22:00:38.840314Z"}},"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-19T22:00:47.522306Z","iopub.execute_input":"2024-12-19T22:00:47.522725Z","iopub.status.idle":"2024-12-19T22:00:47.567453Z","shell.execute_reply.started":"2024-12-19T22:00:47.522693Z","shell.execute_reply":"2024-12-19T22:00:47.566298Z"}},"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-19T22:01:00.636095Z","iopub.execute_input":"2024-12-19T22:01:00.636452Z","iopub.status.idle":"2024-12-19T22:01:01.149626Z","shell.execute_reply.started":"2024-12-19T22:01:00.636408Z","shell.execute_reply":"2024-12-19T22:01:01.148138Z"}},"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-19T22:01:43.656362Z","iopub.execute_input":"2024-12-19T22:01:43.656751Z","iopub.status.idle":"2024-12-19T22:01:44.380469Z","shell.execute_reply.started":"2024-12-19T22:01:43.656721Z","shell.execute_reply":"2024-12-19T22:01:44.379228Z"}},"outputs":[],"execution_count":null}]}