{"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","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-16T13:39:43.259605Z","iopub.execute_input":"2024-05-16T13:39:43.260068Z","iopub.status.idle":"2024-05-16T13:40:02.694868Z","shell.execute_reply.started":"2024-05-16T13:39:43.260029Z","shell.execute_reply":"2024-05-16T13:40:02.693798Z"},"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-16T13:40:02.696573Z","iopub.execute_input":"2024-05-16T13:40:02.69692Z","iopub.status.idle":"2024-05-16T13:40:02.999442Z","shell.execute_reply.started":"2024-05-16T13:40:02.696886Z","shell.execute_reply":"2024-05-16T13:40:02.997671Z"},"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\n\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)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T13:46:22.378511Z","iopub.execute_input":"2024-05-16T13:46:22.378997Z","iopub.status.idle":"2024-05-16T13:46:23.191104Z","shell.execute_reply.started":"2024-05-16T13:46:22.378932Z","shell.execute_reply":"2024-05-16T13:46:23.189296Z"},"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     # 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","metadata":{"execution":{"iopub.status.busy":"2024-05-16T13:46:35.703533Z","iopub.status.idle":"2024-05-16T13:46:35.70399Z","shell.execute_reply.started":"2024-05-16T13:46:35.703769Z","shell.execute_reply":"2024-05-16T13:46:35.70379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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\ntrain_path = \"../input/dogs-vs-cats/train.zip\"\ntest_path = \"../input/dogs-vs-cats/test1.zip\"\nfiles = \"/kaggle/working/\"\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)\nimage_dir = \"/kaggle/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\ntrain_df, val_df = train_test_split(data, test_size=0.2, stratify=data[\"label\"], random_state=42)\n\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\nexperiments = [\n    (2, 3, 3, 64, 128, 256),\n    (1, 2, 2, 32, 64, 128),\n    (3, 4, 4, 128, 256, 512),\n    # Add more configurations as needed\n]\nhistories = []\n\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# Plotting 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()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T13:46:36.182068Z","iopub.execute_input":"2024-05-16T13:46:36.182935Z","iopub.status.idle":"2024-05-16T13:46:47.38179Z","shell.execute_reply.started":"2024-05-16T13:46:36.182894Z","shell.execute_reply":"2024-05-16T13:46:47.380317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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\ntrain_path = \"../input/dogs-vs-cats/train.zip\"\ntest_path = \"../input/dogs-vs-cats/test1.zip\"\nfiles = \"/kaggle/working/\"\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)\nimage_dir = \"/kaggle/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\ntrain_df, val_df = train_test_split(data, test_size=0.2, stratify=data[\"label\"], random_state=42)\n\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)\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\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# Plotting the results\nplt.figure(figsize=(14, 7))\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()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T13:46:23.372723Z","iopub.execute_input":"2024-05-16T13:46:23.373284Z","iopub.status.idle":"2024-05-16T13:46:35.702537Z","shell.execute_reply.started":"2024-05-16T13:46:23.373232Z","shell.execute_reply":"2024-05-16T13:46:35.700798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}