{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-16T10:34:49.657813Z","iopub.execute_input":"2024-05-16T10:34:49.658492Z","iopub.status.idle":"2024-05-16T10:34:49.684280Z","shell.execute_reply.started":"2024-05-16T10:34:49.658457Z","shell.execute_reply":"2024-05-16T10:34:49.683352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/dogs-vs-cats/test1.zip\"\ntest_path = \"../input/dogs-vs-cats/train.zip\"\n\nfiles = \"/kaggle/working/\"\n\n# zipfile - python module for extracting files from a zip file\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:34:51.039779Z","iopub.execute_input":"2024-05-16T10:34:51.040103Z","iopub.status.idle":"2024-05-16T10:35:02.481728Z","shell.execute_reply.started":"2024-05-16T10:34:51.040080Z","shell.execute_reply":"2024-05-16T10:35:02.480447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"../working/train/\"\n\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\n\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:35:02.483643Z","iopub.execute_input":"2024-05-16T10:35:02.484764Z","iopub.status.idle":"2024-05-16T10:35:02.534988Z","shell.execute_reply.started":"2024-05-16T10:35:02.484735Z","shell.execute_reply":"2024-05-16T10:35:02.532915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation sets\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_df, val_df = train_test_split(data, test_size=0.2, stratify=data[\"label\"], random_state=42)\n\n# Create ImageDataGenerators\ntrain_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True, rotation_range=20, zoom_range=0.15)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_dataframe(train_df, directory=image_dir, x_col='filename', y_col='label', target_size=(150, 150), class_mode='binary', batch_size=32)\nval_generator = val_datagen.flow_from_dataframe(val_df, directory=image_dir, x_col='filename', y_col='label', target_size=(150, 150), class_mode='binary', batch_size=32)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:35:15.471175Z","iopub.execute_input":"2024-05-16T10:35:15.471973Z","iopub.status.idle":"2024-05-16T10:35:15.831762Z","shell.execute_reply.started":"2024-05-16T10:35:15.471942Z","shell.execute_reply":"2024-05-16T10:35:15.830779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_modified_vgg16(num_layers_block2, num_layers_block3, num_layers_block4, num_neurons_block2, num_neurons_block3, num_neurons_block4):\n    input_tensor = Input(shape=(150, 150, 3))\n    base_model = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)\n    \n    # Modify the number of layers and neurons\n    x = base_model.get_layer('block1_pool').output\n    \n    # Block 2\n    for i in range(num_layers_block2):\n        x = Conv2D(num_neurons_block2, (3, 3), activation='relu', padding='same', name=f'block2_conv{i+1}')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n    \n    # Block 3\n    for i in range(num_layers_block3):\n        x = Conv2D(num_neurons_block3, (3, 3), activation='relu', padding='same', name=f'block3_conv{i+1}')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n    \n    # Block 4\n    for i in range(num_layers_block4):\n        x = Conv2D(num_neurons_block4, (3, 3), activation='relu', padding='same', name=f'block4_conv{i+1}')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n    \n    x = base_model.get_layer('block5_pool').output\n    \n    x = Flatten(name='flatten')(x)\n    x = Dense(256, activation='relu', name='fc1')(x)\n    x = Dense(1, activation='sigmoid', name='predictions')(x)\n    \n    model = Model(inputs=base_model.input, outputs=x)\n    \n    for layer in base_model.layers:\n        layer.trainable = False\n    \n    return model\n","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:35:57.131946Z","iopub.execute_input":"2024-05-16T10:35:57.132812Z","iopub.status.idle":"2024-05-16T10:35:57.143645Z","shell.execute_reply.started":"2024-05-16T10:35:57.132780Z","shell.execute_reply":"2024-05-16T10:35:57.142642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport zipfile\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Dense, Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import VGG16\nimport matplotlib.pyplot as plt\n\ndef create_modified_vgg16(num_layers_block2, num_layers_block3, num_layers_block4, num_neurons_block2, num_neurons_block3, num_neurons_block4):\n    base_model = VGG16(include_top=False, weights='imagenet', input_tensor=Input(shape=(150, 150, 3)))\n    \n    x = base_model.output\n    x = Flatten()(x)\n    \n    for _ in range(num_layers_block2):\n        x = Dense(num_neurons_block2, activation='relu')(x)\n        \n    for _ in range(num_layers_block3):\n        x = Dense(num_neurons_block3, activation='relu')(x)\n        \n    for _ in range(num_layers_block4):\n        x = Dense(num_neurons_block4, activation='relu')(x)\n        \n    x = Dense(256, activation='relu', name='fc1')(x)\n    x = Dense(1, activation='sigmoid', name='predictions')(x)\n    \n    model = Model(inputs=base_model.input, outputs=x)\n    \n    for layer in base_model.layers:\n        layer.trainable = False\n    \n    return model\n\ndef train_and_evaluate_model(model, train_generator, val_generator, epochs=5):\n    model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])\n    \n    history = model.fit(train_generator, validation_data=val_generator, epochs=epochs, verbose=1)\n    \n    return history\n\n# File paths for training and test data\ntrain_path = \"../input/dogs-vs-cats/train.zip\"\ntest_path = \"../input/dogs-vs-cats/test1.zip\"\nfiles = \"/kaggle/working/\"\n\n# Unzip the dataset\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)\n\n# Set the image directory\nimage_dir = \"/kaggle/working/train/\"\n\n# Prepare the data\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\n\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\n\n# Split the data into training and validation sets\ntrain_df, val_df = train_test_split(data, test_size=0.2, stratify=data[\"label\"], random_state=42)\n\n# Image data generators for data augmentation\ntrain_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True, rotation_range=20, zoom_range=0.15)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_dataframe(train_df, directory=image_dir, x_col='filename', y_col='label', target_size=(150, 150), class_mode='binary', batch_size=32)\nval_generator = val_datagen.flow_from_dataframe(val_df, directory=image_dir, x_col='filename', y_col='label', target_size=(150, 150), class_mode='binary', batch_size=32)\n\n# Different configurations to experiment with\nexperiments = [\n    (2, 3, 3, 64, 128, 256),\n    (1, 2, 2, 32, 64, 128),\n    (3, 4, 4, 128, 256, 512),\n    (4, 5, 5, 256, 512, 1024),\n    (5, 6, 6, 512, 1024, 2048)\n    # Add more configurations as needed\n]\n\nhistories = []\n\n# Run experiments\nfor exp in experiments:\n    model = create_modified_vgg16(*exp)\n    history = train_and_evaluate_model(model, train_generator, val_generator)\n    histories.append(history)\n\n# Plot the results\nplt.figure(figsize=(14, 7))\n\nfor i, history in enumerate(histories):\n    plt.plot(history.history['val_accuracy'], label=f'Experiment {i+1}')\n\nplt.title('Validation Accuracy Comparison')\nplt.xlabel('Epochs')\nplt.ylabel('Validation Accuracy')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-16T10:42:22.254497Z","iopub.execute_input":"2024-05-16T10:42:22.254831Z","iopub.status.idle":"2024-05-16T11:34:57.876260Z","shell.execute_reply.started":"2024-05-16T10:42:22.254807Z","shell.execute_reply":"2024-05-16T11:34:57.875159Z"},"trusted":true},"execution_count":null,"outputs":[]}]}