{"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":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30636,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"import zipfile\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.applications import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom datasets import load_dataset\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.image as mpimg\nimport os\nimport random\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:45:43.622698Z","iopub.execute_input":"2025-02-05T20:45:43.622934Z","iopub.status.idle":"2025-02-05T20:45:56.054873Z","shell.execute_reply.started":"2025-02-05T20:45:43.622912Z","shell.execute_reply":"2025-02-05T20:45:56.053924Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Extract data","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:46:15.011089Z","iopub.execute_input":"2025-02-05T20:46:15.011445Z","iopub.status.idle":"2025-02-05T20:46:26.689727Z","shell.execute_reply.started":"2025-02-05T20:46:15.011416Z","shell.execute_reply":"2025-02-05T20:46:26.688677Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\"train\")) #just for checking","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:47:06.488984Z","iopub.execute_input":"2025-02-05T20:47:06.489648Z","iopub.status.idle":"2025-02-05T20:47:06.514624Z","shell.execute_reply.started":"2025-02-05T20:47:06.489614Z","shell.execute_reply":"2025-02-05T20:47:06.513822Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filenames = os.listdir(\"train\")\nlabels = []\nfor filename in filenames:\n    if \"cat\" in filename:\n        labels.append('cat')\n    elif \"dog\" in filename:\n        labels.append('dog')\n    \ndf = pd.DataFrame({\n    'Filename': filenames,\n    'Label': labels\n})\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:47:31.393260Z","iopub.execute_input":"2025-02-05T20:47:31.393592Z","iopub.status.idle":"2025-02-05T20:47:31.428712Z","shell.execute_reply.started":"2025-02-05T20:47:31.393567Z","shell.execute_reply":"2025-02-05T20:47:31.427872Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"It's good to see the data is shuffled","metadata":{}},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:47:44.931410Z","iopub.execute_input":"2025-02-05T20:47:44.932222Z","iopub.status.idle":"2025-02-05T20:47:44.955498Z","shell.execute_reply.started":"2025-02-05T20:47:44.932190Z","shell.execute_reply":"2025-02-05T20:47:44.954625Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"We have 25,000 cat and 25,000 dog images.","metadata":{}},{"cell_type":"markdown","source":"## Display the images","metadata":{}},{"cell_type":"code","source":"# Function to display images\ndef display_images(image_paths, category):\n    plt.figure(figsize=(24, 4))\n    for i, img_path in enumerate(image_paths):\n        plt.subplot(1, 3, i + 1)\n        img = mpimg.imread(os.path.join('train', img_path))\n        plt.imshow(img)\n        plt.title(category)\n        plt.axis('off')\n    plt.show()\n\n# Selecting three random images of cats\ncat_images = [filename for filename in filenames if 'cat' in filename]\nselected_cat_images = random.sample(cat_images, 3)\n\n# Selecting three random images of dogs\ndog_images = [filename for filename in filenames if 'dog' in filename]\nselected_dog_images = random.sample(dog_images, 3)\n\n# Display the images\ndisplay_images(selected_cat_images, 'Cat')\ndisplay_images(selected_dog_images, 'Dog')\n","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:47:54.067760Z","iopub.execute_input":"2025-02-05T20:47:54.068110Z","iopub.status.idle":"2025-02-05T20:47:55.035808Z","shell.execute_reply.started":"2025-02-05T20:47:54.068080Z","shell.execute_reply":"2025-02-05T20:47:55.034910Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Prepration","metadata":{}},{"cell_type":"code","source":"# Constants\nIMAGE_WIDTH, IMAGE_HEIGHT = 224, 224\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:48:16.162036Z","iopub.execute_input":"2025-02-05T20:48:16.162374Z","iopub.status.idle":"2025-02-05T20:48:16.166528Z","shell.execute_reply.started":"2025-02-05T20:48:16.162347Z","shell.execute_reply":"2025-02-05T20:48:16.165621Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n\ntrain_generator = datagen.flow_from_dataframe(\n    dataframe=df,\n    directory='train/',\n    x_col='Filename',\n    y_col='Label',\n    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),\n    batch_size=BATCH_SIZE,\n    class_mode='binary',\n    subset='training'\n)\n\nvalidation_generator = datagen.flow_from_dataframe(\n    dataframe=df,\n    directory='train/',\n    x_col='Filename',\n    y_col='Label',\n    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),\n    batch_size=BATCH_SIZE,\n    class_mode='binary',\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:48:32.623673Z","iopub.execute_input":"2025-02-05T20:48:32.624303Z","iopub.status.idle":"2025-02-05T20:48:32.959369Z","shell.execute_reply.started":"2025-02-05T20:48:32.624274Z","shell.execute_reply":"2025-02-05T20:48:32.958745Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load VGG Model","metadata":{}},{"cell_type":"code","source":"# Load the VGG16 model, pretrained on ImageNet\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, 3))\n\n# Freeze the layers of the base model\nfor layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:48:40.597833Z","iopub.execute_input":"2025-02-05T20:48:40.598562Z","iopub.status.idle":"2025-02-05T20:48:44.058208Z","shell.execute_reply.started":"2025-02-05T20:48:40.598532Z","shell.execute_reply":"2025-02-05T20:48:44.057525Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create the model\nx = Flatten()(base_model.output)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\npredictions = Dense(1, activation='sigmoid')(x)\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:49:00.998311Z","iopub.execute_input":"2025-02-05T20:49:00.999106Z","iopub.status.idle":"2025-02-05T20:49:01.031952Z","shell.execute_reply.started":"2025-02-05T20:49:00.999075Z","shell.execute_reply":"2025-02-05T20:49:01.031062Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:49:07.270941Z","iopub.execute_input":"2025-02-05T20:49:07.271521Z","iopub.status.idle":"2025-02-05T20:49:07.287308Z","shell.execute_reply.started":"2025-02-05T20:49:07.271489Z","shell.execute_reply":"2025-02-05T20:49:07.286480Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:49:12.232697Z","iopub.execute_input":"2025-02-05T20:49:12.233069Z","iopub.status.idle":"2025-02-05T20:49:12.282253Z","shell.execute_reply.started":"2025-02-05T20:49:12.233039Z","shell.execute_reply":"2025-02-05T20:49:12.281357Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch=train_generator.samples // BATCH_SIZE,\n    validation_data=validation_generator,\n    validation_steps=validation_generator.samples // BATCH_SIZE,\n    epochs=5\n)","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:49:25.893214Z","iopub.execute_input":"2025-02-05T20:49:25.893902Z","iopub.status.idle":"2025-02-05T20:54:04.975128Z","shell.execute_reply.started":"2025-02-05T20:49:25.893870Z","shell.execute_reply":"2025-02-05T20:54:04.974085Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.8, 1])\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:54:09.049379Z","iopub.execute_input":"2025-02-05T20:54:09.049737Z","iopub.status.idle":"2025-02-05T20:54:09.258167Z","shell.execute_reply.started":"2025-02-05T20:54:09.049710Z","shell.execute_reply":"2025-02-05T20:54:09.257359Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Validation accuracy started decreasing after a while. Let's try to train also some layers of VGG model.","metadata":{"execution":{"iopub.status.busy":"2024-01-18T14:59:00.159774Z","iopub.execute_input":"2024-01-18T14:59:00.160509Z","iopub.status.idle":"2024-01-18T14:59:00.166267Z","shell.execute_reply.started":"2024-01-18T14:59:00.160461Z","shell.execute_reply":"2024-01-18T14:59:00.16537Z"}}},{"cell_type":"code","source":"#Unfreeze the last 10 layers\nfor layer in base_model.layers[-10:]:\n    layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:54:24.728505Z","iopub.execute_input":"2025-02-05T20:54:24.728839Z","iopub.status.idle":"2025-02-05T20:54:24.733688Z","shell.execute_reply.started":"2025-02-05T20:54:24.728813Z","shell.execute_reply":"2025-02-05T20:54:24.732729Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:54:34.128685Z","iopub.execute_input":"2025-02-05T20:54:34.129503Z","iopub.status.idle":"2025-02-05T20:54:34.140889Z","shell.execute_reply.started":"2025-02-05T20:54:34.129458Z","shell.execute_reply":"2025-02-05T20:54:34.140164Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Continue training\nhistory_fine = model.fit(\n    train_generator,\n    steps_per_epoch=train_generator.samples // BATCH_SIZE,\n    validation_data=validation_generator,\n    validation_steps=validation_generator.samples // BATCH_SIZE,\n    epochs=5  # You can adjust the number of epochs\n)","metadata":{"execution":{"iopub.status.busy":"2025-02-05T20:54:46.012941Z","iopub.execute_input":"2025-02-05T20:54:46.013280Z","iopub.status.idle":"2025-02-05T21:01:49.001043Z","shell.execute_reply.started":"2025-02-05T20:54:46.013251Z","shell.execute_reply":"2025-02-05T21:01:49.000359Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model again\nplt.plot(history_fine.history['accuracy'], label='accuracy')\nplt.plot(history_fine.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.92, 1])\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:01:54.344901Z","iopub.execute_input":"2025-02-05T21:01:54.345586Z","iopub.status.idle":"2025-02-05T21:01:54.548328Z","shell.execute_reply.started":"2025-02-05T21:01:54.345555Z","shell.execute_reply":"2025-02-05T21:01:54.547537Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Make Predictions with Test Data","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as test1_zip:\n    test1_zip.extractall('.')  ","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:02:05.517052Z","iopub.execute_input":"2025-02-05T21:02:05.517770Z","iopub.status.idle":"2025-02-05T21:02:08.625340Z","shell.execute_reply.started":"2025-02-05T21:02:05.517741Z","shell.execute_reply":"2025-02-05T21:02:08.624421Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dir = \"../working/test1/\"\nfilenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"Filename\": filenames})\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:02:17.124797Z","iopub.execute_input":"2025-02-05T21:02:17.125121Z","iopub.status.idle":"2025-02-05T21:02:17.142267Z","shell.execute_reply.started":"2025-02-05T21:02:17.125094Z","shell.execute_reply":"2025-02-05T21:02:17.141422Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_gen.flow_from_dataframe(\n    dataframe=test_data,\n    directory='test1/',\n    x_col='Filename',\n    y_col=None,\n    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),\n    batch_size=BATCH_SIZE,\n    class_mode=None,\n)","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:02:27.723038Z","iopub.execute_input":"2025-02-05T21:02:27.723457Z","iopub.status.idle":"2025-02-05T21:02:27.808990Z","shell.execute_reply.started":"2025-02-05T21:02:27.723423Z","shell.execute_reply":"2025-02-05T21:02:27.808305Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test1_predict = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:02:34.712765Z","iopub.execute_input":"2025-02-05T21:02:34.713103Z","iopub.status.idle":"2025-02-05T21:03:01.913705Z","shell.execute_reply.started":"2025-02-05T21:02:34.713078Z","shell.execute_reply":"2025-02-05T21:03:01.912946Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test1_predict","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:03:10.640040Z","iopub.execute_input":"2025-02-05T21:03:10.640817Z","iopub.status.idle":"2025-02-05T21:03:10.646530Z","shell.execute_reply.started":"2025-02-05T21:03:10.640789Z","shell.execute_reply":"2025-02-05T21:03:10.645711Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"threshold = 0.5\ntest_data['Label'] = np.where(test1_predict > threshold, 'dog', 'cat')","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:03:16.916839Z","iopub.execute_input":"2025-02-05T21:03:16.917596Z","iopub.status.idle":"2025-02-05T21:03:16.924252Z","shell.execute_reply.started":"2025-02-05T21:03:16.917567Z","shell.execute_reply":"2025-02-05T21:03:16.923457Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.tail()","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:03:29.780874Z","iopub.execute_input":"2025-02-05T21:03:29.781565Z","iopub.status.idle":"2025-02-05T21:03:29.789890Z","shell.execute_reply.started":"2025-02-05T21:03:29.781527Z","shell.execute_reply":"2025-02-05T21:03:29.788974Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_test = test_data.sample(n=6)\nplt.figure(figsize=(24, 8)) \n\nsubplot_index = 1\nfor _, row in sample_test.iterrows():\n    image_path = os.path.join(test_dir, row['Filename'])\n    img = mpimg.imread(image_path)\n    plt.subplot(2, 3, subplot_index)\n    plt.imshow(img)\n    plt.title(row['Label'])\n    plt.axis('off')\n    subplot_index += 1\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-05T21:03:38.737513Z","iopub.execute_input":"2025-02-05T21:03:38.738160Z","iopub.status.idle":"2025-02-05T21:03:39.626167Z","shell.execute_reply.started":"2025-02-05T21:03:38.738130Z","shell.execute_reply":"2025-02-05T21:03:39.625139Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}