{"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":30635,"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-01-19T10:56:21.015984Z","iopub.execute_input":"2024-01-19T10:56:21.016354Z","iopub.status.idle":"2024-01-19T10:56:34.182813Z","shell.execute_reply.started":"2024-01-19T10:56:21.016321Z","shell.execute_reply":"2024-01-19T10:56:34.181802Z"},"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-01-19T10:56:34.185215Z","iopub.execute_input":"2024-01-19T10:56:34.186035Z","iopub.status.idle":"2024-01-19T10:56:48.812621Z","shell.execute_reply.started":"2024-01-19T10:56:34.185991Z","shell.execute_reply":"2024-01-19T10:56:48.811770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(os.listdir(\"train\")) #just for checking","metadata":{"execution":{"iopub.status.busy":"2024-01-23T16:33:24.452612Z","iopub.execute_input":"2024-01-23T16:33:24.452890Z","iopub.status.idle":"2024-01-23T16:33:24.457604Z","shell.execute_reply.started":"2024-01-23T16:33:24.452865Z","shell.execute_reply":"2024-01-23T16:33:24.456795Z"},"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-01-19T10:56:48.846394Z","iopub.execute_input":"2024-01-19T10:56:48.846823Z","iopub.status.idle":"2024-01-19T10:56:48.889734Z","shell.execute_reply.started":"2024-01-19T10:56:48.846792Z","shell.execute_reply":"2024-01-19T10:56:48.888724Z"},"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-01-19T10:56:48.892150Z","iopub.execute_input":"2024-01-19T10:56:48.892449Z","iopub.status.idle":"2024-01-19T10:56:48.921361Z","shell.execute_reply.started":"2024-01-19T10:56:48.892413Z","shell.execute_reply":"2024-01-19T10:56:48.920406Z"},"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-01-19T10:56:48.922860Z","iopub.execute_input":"2024-01-19T10:56:48.923716Z","iopub.status.idle":"2024-01-19T10:56:50.104410Z","shell.execute_reply.started":"2024-01-19T10:56:48.923676Z","shell.execute_reply":"2024-01-19T10:56:50.103445Z"},"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-01-19T10:56:50.105636Z","iopub.execute_input":"2024-01-19T10:56:50.105954Z","iopub.status.idle":"2024-01-19T10:56:50.110266Z","shell.execute_reply.started":"2024-01-19T10:56:50.105928Z","shell.execute_reply":"2024-01-19T10:56:50.109388Z"},"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-01-19T10:56:50.111417Z","iopub.execute_input":"2024-01-19T10:56:50.111728Z","iopub.status.idle":"2024-01-19T10:56:51.118861Z","shell.execute_reply.started":"2024-01-19T10:56:50.111704Z","shell.execute_reply":"2024-01-19T10:56:51.118004Z"},"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-01-19T10:56:51.120095Z","iopub.execute_input":"2024-01-19T10:56:51.120404Z","iopub.status.idle":"2024-01-19T10:56:52.908585Z","shell.execute_reply.started":"2024-01-19T10:56:51.120378Z","shell.execute_reply":"2024-01-19T10:56:52.907737Z"},"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-01-19T10:56:52.909765Z","iopub.execute_input":"2024-01-19T10:56:52.910059Z","iopub.status.idle":"2024-01-19T10:56:52.955668Z","shell.execute_reply.started":"2024-01-19T10:56:52.910035Z","shell.execute_reply":"2024-01-19T10:56:52.954728Z"},"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-01-19T10:56:52.956864Z","iopub.execute_input":"2024-01-19T10:56:52.957156Z","iopub.status.idle":"2024-01-19T10:56:52.973066Z","shell.execute_reply.started":"2024-01-19T10:56:52.957130Z","shell.execute_reply":"2024-01-19T10:56:52.972301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T10:56:52.974226Z","iopub.execute_input":"2024-01-19T10:56:52.974999Z","iopub.status.idle":"2024-01-19T10:56:53.023576Z","shell.execute_reply.started":"2024-01-19T10:56:52.974964Z","shell.execute_reply":"2024-01-19T10:56:53.022687Z"},"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-01-19T10:56:53.024842Z","iopub.execute_input":"2024-01-19T10:56:53.025192Z","iopub.status.idle":"2024-01-19T11:01:31.950520Z","shell.execute_reply.started":"2024-01-19T10:56:53.025157Z","shell.execute_reply":"2024-01-19T11:01:31.949579Z"},"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-01-19T11:01:31.954330Z","iopub.execute_input":"2024-01-19T11:01:31.954668Z","iopub.status.idle":"2024-01-19T11:01:32.227561Z","shell.execute_reply.started":"2024-01-19T11:01:31.954641Z","shell.execute_reply":"2024-01-19T11:01:32.226585Z"},"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.165370Z"}}},{"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-01-19T11:01:32.228728Z","iopub.execute_input":"2024-01-19T11:01:32.229011Z","iopub.status.idle":"2024-01-19T11:01:32.233954Z","shell.execute_reply.started":"2024-01-19T11:01:32.228987Z","shell.execute_reply":"2024-01-19T11:01:32.233047Z"},"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-01-19T11:01:32.235003Z","iopub.execute_input":"2024-01-19T11:01:32.235274Z","iopub.status.idle":"2024-01-19T11:01:32.253520Z","shell.execute_reply.started":"2024-01-19T11:01:32.235249Z","shell.execute_reply":"2024-01-19T11:01:32.252476Z"},"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=5  # You can adjust the number of epochs\n)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T11:01:32.254910Z","iopub.execute_input":"2024-01-19T11:01:32.255216Z","iopub.status.idle":"2024-01-19T11:08:31.521794Z","shell.execute_reply.started":"2024-01-19T11:01:32.255188Z","shell.execute_reply":"2024-01-19T11:08:31.520955Z"},"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-01-19T11:08:31.523247Z","iopub.execute_input":"2024-01-19T11:08:31.523637Z","iopub.status.idle":"2024-01-19T11:08:31.785301Z","shell.execute_reply.started":"2024-01-19T11:08:31.523602Z","shell.execute_reply":"2024-01-19T11:08:31.784342Z"},"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-01-19T11:08:31.786472Z","iopub.execute_input":"2024-01-19T11:08:31.786766Z","iopub.status.idle":"2024-01-19T11:08:35.195798Z","shell.execute_reply.started":"2024-01-19T11:08:31.786733Z","shell.execute_reply":"2024-01-19T11:08:35.194972Z"},"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-01-19T11:08:35.196926Z","iopub.execute_input":"2024-01-19T11:08:35.197226Z","iopub.status.idle":"2024-01-19T11:08:35.215376Z","shell.execute_reply.started":"2024-01-19T11:08:35.197196Z","shell.execute_reply":"2024-01-19T11:08:35.214490Z"},"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-01-19T11:08:35.216368Z","iopub.execute_input":"2024-01-19T11:08:35.216697Z","iopub.status.idle":"2024-01-19T11:08:35.343878Z","shell.execute_reply.started":"2024-01-19T11:08:35.216671Z","shell.execute_reply":"2024-01-19T11:08:35.343017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_predict = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T11:08:35.345071Z","iopub.execute_input":"2024-01-19T11:08:35.345449Z","iopub.status.idle":"2024-01-19T11:09:02.602391Z","shell.execute_reply.started":"2024-01-19T11:08:35.345402Z","shell.execute_reply":"2024-01-19T11:09:02.601490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1_predict","metadata":{"execution":{"iopub.status.busy":"2024-01-19T11:09:02.603965Z","iopub.execute_input":"2024-01-19T11:09:02.604650Z","iopub.status.idle":"2024-01-19T11:09:02.613079Z","shell.execute_reply.started":"2024-01-19T11:09:02.604610Z","shell.execute_reply":"2024-01-19T11:09:02.611490Z"},"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-01-19T11:09:02.614325Z","iopub.execute_input":"2024-01-19T11:09:02.614765Z","iopub.status.idle":"2024-01-19T11:09:02.624510Z","shell.execute_reply.started":"2024-01-19T11:09:02.614729Z","shell.execute_reply":"2024-01-19T11:09:02.623487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.tail()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T11:09:28.109819Z","iopub.execute_input":"2024-01-19T11:09:28.110497Z","iopub.status.idle":"2024-01-19T11:09:28.119683Z","shell.execute_reply.started":"2024-01-19T11:09:28.110460Z","shell.execute_reply":"2024-01-19T11:09:28.118622Z"},"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-01-19T11:36:40.263125Z","iopub.execute_input":"2024-01-19T11:36:40.263883Z","iopub.status.idle":"2024-01-19T11:36:41.065472Z","shell.execute_reply.started":"2024-01-19T11:36:40.263850Z","shell.execute_reply":"2024-01-19T11:36:41.064460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}