{"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":"2024-02-14T21:42:40.542815Z","iopub.execute_input":"2024-02-14T21:42:40.543173Z","iopub.status.idle":"2024-02-14T21:42:45.025763Z","shell.execute_reply.started":"2024-02-14T21:42:40.543145Z","shell.execute_reply":"2024-02-14T21:42:45.024948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:45.027668Z","iopub.execute_input":"2024-02-14T21:42:45.028209Z","iopub.status.idle":"2024-02-14T21:42:56.163260Z","shell.execute_reply.started":"2024-02-14T21:42:45.028182Z","shell.execute_reply":"2024-02-14T21:42:56.162396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(os.listdir(\"train\")) #just for checking","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:42:56.164345Z","iopub.execute_input":"2024-02-14T21:42:56.164639Z","iopub.status.idle":"2024-02-14T21:42:56.168578Z","shell.execute_reply.started":"2024-02-14T21:42:56.164614Z","shell.execute_reply":"2024-02-14T21:42:56.167751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:56.169518Z","iopub.execute_input":"2024-02-14T21:42:56.169786Z","iopub.status.idle":"2024-02-14T21:42:56.767996Z","shell.execute_reply.started":"2024-02-14T21:42:56.169757Z","shell.execute_reply":"2024-02-14T21:42:56.767067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:56.770924Z","iopub.execute_input":"2024-02-14T21:42:56.771250Z","iopub.status.idle":"2024-02-14T21:42:56.787154Z","shell.execute_reply.started":"2024-02-14T21:42:56.771224Z","shell.execute_reply":"2024-02-14T21:42:56.786286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:56.788388Z","iopub.execute_input":"2024-02-14T21:42:56.788662Z","iopub.status.idle":"2024-02-14T21:42:57.920391Z","shell.execute_reply.started":"2024-02-14T21:42:56.788638Z","shell.execute_reply":"2024-02-14T21:42:57.919546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:57.921456Z","iopub.execute_input":"2024-02-14T21:42:57.921730Z","iopub.status.idle":"2024-02-14T21:42:57.925705Z","shell.execute_reply.started":"2024-02-14T21:42:57.921706Z","shell.execute_reply":"2024-02-14T21:42:57.924853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:57.926842Z","iopub.execute_input":"2024-02-14T21:42:57.927108Z","iopub.status.idle":"2024-02-14T21:42:58.424132Z","shell.execute_reply.started":"2024-02-14T21:42:57.927085Z","shell.execute_reply":"2024-02-14T21:42:58.423091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:58.425623Z","iopub.execute_input":"2024-02-14T21:42:58.425944Z","iopub.status.idle":"2024-02-14T21:42:59.299734Z","shell.execute_reply.started":"2024-02-14T21:42:58.425917Z","shell.execute_reply":"2024-02-14T21:42:59.298744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:59.300890Z","iopub.execute_input":"2024-02-14T21:42:59.301177Z","iopub.status.idle":"2024-02-14T21:42:59.342313Z","shell.execute_reply.started":"2024-02-14T21:42:59.301151Z","shell.execute_reply":"2024-02-14T21:42:59.341633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:42:59.343232Z","iopub.execute_input":"2024-02-14T21:42:59.343473Z","iopub.status.idle":"2024-02-14T21:42:59.358792Z","shell.execute_reply.started":"2024-02-14T21:42:59.343452Z","shell.execute_reply":"2024-02-14T21:42:59.357934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:42:59.360001Z","iopub.execute_input":"2024-02-14T21:42:59.360594Z","iopub.status.idle":"2024-02-14T21:42:59.408811Z","shell.execute_reply.started":"2024-02-14T21:42:59.360559Z","shell.execute_reply":"2024-02-14T21:42:59.407994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:42:59.410029Z","iopub.execute_input":"2024-02-14T21:42:59.410593Z","iopub.status.idle":"2024-02-14T21:47:32.246031Z","shell.execute_reply.started":"2024-02-14T21:42:59.410559Z","shell.execute_reply":"2024-02-14T21:47:32.245210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:47:32.249707Z","iopub.execute_input":"2024-02-14T21:47:32.249992Z","iopub.status.idle":"2024-02-14T21:47:32.498311Z","shell.execute_reply.started":"2024-02-14T21:47:32.249967Z","shell.execute_reply":"2024-02-14T21:47:32.497423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:47:32.499232Z","iopub.execute_input":"2024-02-14T21:47:32.499482Z","iopub.status.idle":"2024-02-14T21:47:32.504595Z","shell.execute_reply.started":"2024-02-14T21:47:32.499459Z","shell.execute_reply":"2024-02-14T21:47:32.503701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:47:32.505717Z","iopub.execute_input":"2024-02-14T21:47:32.505973Z","iopub.status.idle":"2024-02-14T21:47:32.521878Z","shell.execute_reply.started":"2024-02-14T21:47:32.505950Z","shell.execute_reply":"2024-02-14T21:47:32.521021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=3  # You can adjust the number of epochs\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:47:32.522964Z","iopub.execute_input":"2024-02-14T21:47:32.523297Z","iopub.status.idle":"2024-02-14T21:51:38.448875Z","shell.execute_reply.started":"2024-02-14T21:47:32.523262Z","shell.execute_reply":"2024-02-14T21:51:38.447940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:51:38.452087Z","iopub.execute_input":"2024-02-14T21:51:38.452378Z","iopub.status.idle":"2024-02-14T21:51:38.700264Z","shell.execute_reply.started":"2024-02-14T21:51:38.452352Z","shell.execute_reply":"2024-02-14T21:51:38.699350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:51:38.701416Z","iopub.execute_input":"2024-02-14T21:51:38.701687Z","iopub.status.idle":"2024-02-14T21:51:42.127632Z","shell.execute_reply.started":"2024-02-14T21:51:38.701662Z","shell.execute_reply":"2024-02-14T21:51:42.126645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:51:42.128928Z","iopub.execute_input":"2024-02-14T21:51:42.129197Z","iopub.status.idle":"2024-02-14T21:51:42.147973Z","shell.execute_reply.started":"2024-02-14T21:51:42.129174Z","shell.execute_reply":"2024-02-14T21:51:42.147095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:51:42.149187Z","iopub.execute_input":"2024-02-14T21:51:42.149943Z","iopub.status.idle":"2024-02-14T21:51:42.274434Z","shell.execute_reply.started":"2024-02-14T21:51:42.149909Z","shell.execute_reply":"2024-02-14T21:51:42.273729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_predict = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:51:42.275496Z","iopub.execute_input":"2024-02-14T21:51:42.275788Z","iopub.status.idle":"2024-02-14T21:52:09.598231Z","shell.execute_reply.started":"2024-02-14T21:51:42.275762Z","shell.execute_reply":"2024-02-14T21:52:09.597370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_predict","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:52:09.599497Z","iopub.execute_input":"2024-02-14T21:52:09.599830Z","iopub.status.idle":"2024-02-14T21:52:09.606875Z","shell.execute_reply.started":"2024-02-14T21:52:09.599794Z","shell.execute_reply":"2024-02-14T21:52:09.605727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.5\ntest_data['Label'] = np.where(test1_predict > threshold, 'dog', 'cat')","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:52:09.608047Z","iopub.execute_input":"2024-02-14T21:52:09.608337Z","iopub.status.idle":"2024-02-14T21:52:09.617914Z","shell.execute_reply.started":"2024-02-14T21:52:09.608306Z","shell.execute_reply":"2024-02-14T21:52:09.617081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['Label'] = test_data['Label'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:52:09.619113Z","iopub.execute_input":"2024-02-14T21:52:09.621199Z","iopub.status.idle":"2024-02-14T21:52:09.638432Z","shell.execute_reply.started":"2024-02-14T21:52:09.621153Z","shell.execute_reply":"2024-02-14T21:52:09.637580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.tail()","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:52:09.639539Z","iopub.execute_input":"2024-02-14T21:52:09.639825Z","iopub.status.idle":"2024-02-14T21:52:09.648788Z","shell.execute_reply.started":"2024-02-14T21:52:09.639801Z","shell.execute_reply":"2024-02-14T21:52:09.647874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-02-14T21:52:09.649896Z","iopub.execute_input":"2024-02-14T21:52:09.650224Z","iopub.status.idle":"2024-02-14T21:52:10.513151Z","shell.execute_reply.started":"2024-02-14T21:52:09.650191Z","shell.execute_reply":"2024-02-14T21:52:10.512037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Submission","metadata":{}},{"cell_type":"code","source":"submission_df = test_data.copy()\nsubmission_df['id'] = submission_df['Filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['Label']\nsubmission_df.drop(['Filename'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T21:52:10.514537Z","iopub.execute_input":"2024-02-14T21:52:10.514930Z","iopub.status.idle":"2024-02-14T21:52:10.584210Z","shell.execute_reply.started":"2024-02-14T21:52:10.514895Z","shell.execute_reply":"2024-02-14T21:52:10.582430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}