{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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","editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"execution":{"iopub.status.busy":"2023-10-02T13:56:02.851853Z","iopub.execute_input":"2023-10-02T13:56:02.852578Z","iopub.status.idle":"2023-10-02T13:56:17.166494Z","shell.execute_reply.started":"2023-10-02T13:56:02.852541Z","shell.execute_reply":"2023-10-02T13:56:17.165542Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\nimport os\n\n# Define the paths to the .7z files\ntrain_7z_path = '/kaggle/input/cifar-10/train.7z'\ntest_7z_path = '/kaggle/input/cifar-10/test.7z'\n\n# Define the extraction paths\nextraction_path = '/kaggle/working/'\n\n# Unzip the train dataset\nwith py7zr.SevenZipFile(train_7z_path, mode='r') as z:\n    z.extractall(path=extraction_path)\n\n# Unzip the test dataset\nwith py7zr.SevenZipFile(test_7z_path, mode='r') as z:\n    z.extractall(path=extraction_path)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T13:58:21.302215Z","iopub.execute_input":"2023-10-02T13:58:21.302736Z","iopub.status.idle":"2023-10-02T14:16:11.852483Z","shell.execute_reply.started":"2023-10-02T13:58:21.302682Z","shell.execute_reply":"2023-10-02T14:16:11.851077Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(train_dir))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-03T17:52:11.015534Z","iopub.execute_input":"2023-10-03T17:52:11.015901Z","iopub.status.idle":"2023-10-03T17:52:11.235764Z","shell.execute_reply.started":"2023-10-03T17:52:11.015878Z","shell.execute_reply":"2023-10-03T17:52:11.234613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-05T08:44:19.617612Z","iopub.execute_input":"2023-10-05T08:44:19.617972Z","iopub.status.idle":"2023-10-05T08:44:33.340972Z","shell.execute_reply.started":"2023-10-05T08:44:19.61794Z","shell.execute_reply":"2023-10-05T08:44:33.339915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define paths to the train and test image folders\ntrain_dir = '/kaggle/working/train/'\ntest_dir = '/kaggle/working/test/'\n\n# Load the labels from trainLabels.csv\nlabels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n\n# Function to load and preprocess the images\ndef load_and_preprocess_image(image_path, label):\n    image = keras.preprocessing.image.load_img(image_path, target_size=(32, 32))\n    image = keras.preprocessing.image.img_to_array(image)\n    image /= 255.0  # Normalize pixel values to the range [0, 1]\n    return image, label\n\n# Load and preprocess images\nimages = []\nlabels = []\n\nfor img_name, label in labels_df.values:\n    img_path = os.path.join(train_dir, str(img_name) + '.png')\n    img, label = load_and_preprocess_image(img_path, label)\n    images.append(img)\n    labels.append(label)\n\n# Convert lists to numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)\n","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-05T08:44:37.462977Z","iopub.execute_input":"2023-10-05T08:44:37.463534Z","iopub.status.idle":"2023-10-05T08:44:51.512594Z","shell.execute_reply.started":"2023-10-05T08:44:37.463501Z","shell.execute_reply":"2023-10-05T08:44:51.511615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\n# Function to display random images with labels\ndef display_images(images, labels, num_images=5):\n    plt.figure(figsize=(15, 5))\n    for i in range(num_images):\n        index = random.randint(0, len(images) - 1)\n        plt.subplot(1, num_images, i + 1)\n        plt.imshow(images[index])\n        plt.title(f\"Label: {labels[index]}\")\n        plt.axis('off')\n    plt.show()\n\n# Display a random sample of images\ndisplay_images(X_train, y_train)\n","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-05T08:44:59.792048Z","iopub.execute_input":"2023-10-05T08:44:59.792467Z","iopub.status.idle":"2023-10-05T08:45:00.232766Z","shell.execute_reply.started":"2023-10-05T08:44:59.792428Z","shell.execute_reply":"2023-10-05T08:45:00.23193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Detect GPUs, if available\ngpus = tf.config.experimental.list_physical_devices('GPU')\nif gpus:\n    try:\n        # Currently, memory growth needs to be the same across GPUs\n        for gpu in gpus:\n            tf.config.experimental.set_memory_growth(gpu, True)\n        logical_gpus = tf.config.experimental.list_logical_devices('GPU')\n        print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPUs\")\n    except RuntimeError as e:\n        print(e)","metadata":{"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the MirroredStrategy for two GPUs\nstrategy = tf.distribute.MirroredStrategy(devices=['/GPU:0', '/GPU:1'])\n\nprint(\"Number of devices: {}\".format(strategy.num_replicas_in_sync))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-05T08:45:11.255369Z","iopub.execute_input":"2023-10-05T08:45:11.255716Z","iopub.status.idle":"2023-10-05T08:45:11.369447Z","shell.execute_reply.started":"2023-10-05T08:45:11.25567Z","shell.execute_reply":"2023-10-05T08:45:11.368486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data augmentation with ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rotation_range=20,  # Randomly rotate images by up to 20 degrees\n    width_shift_range=0.2,  # Randomly shift the width by up to 20% of the image width\n    height_shift_range=0.2,  # Randomly shift the height by up to 20% of the image height\n    shear_range=0.2,  # Randomly apply shear transformations\n    zoom_range=0.2,  # Randomly zoom in by up to 20%\n    horizontal_flip=True,  # Randomly flip images horizontally\n    fill_mode='nearest'  # Fill in missing pixels with the nearest available pixel\n)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-05T09:59:15.183022Z","iopub.execute_input":"2023-10-05T09:59:15.183431Z","iopub.status.idle":"2023-10-05T09:59:15.189428Z","shell.execute_reply.started":"2023-10-05T09:59:15.183397Z","shell.execute_reply":"2023-10-05T09:59:15.188112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Data augmentation with ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rotation_range=10,  # Randomly rotate images by up to 10 degrees\n    width_shift_range=0.1,  # Randomly shift the width by up to 10% of the image width\n    height_shift_range=0.1,  # Randomly shift the height by up to 10% of the image height\n    shear_range=0.1,  # Randomly apply shear transformations\n    zoom_range=0.1,  # Randomly zoom in by up to 10%\n    horizontal_flip=True,  # Randomly flip images horizontally\n    fill_mode='nearest'  # Fill in missing pixels with the nearest available pixel\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T23:49:30.927878Z","iopub.execute_input":"2023-10-04T23:49:30.928579Z","iopub.status.idle":"2023-10-04T23:49:30.933517Z","shell.execute_reply.started":"2023-10-04T23:49:30.928547Z","shell.execute_reply":"2023-10-04T23:49:30.932562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Define a function to apply Cutout augmentation to an image\ndef cutout(img, mask_size):\n    \"\"\"Apply cutout to the input image.\"\"\"\n    h, w, _ = img.shape\n    mask = np.ones((h, w, 3), np.float32)\n\n    # Determine the coordinates of the cutout box\n    y = np.random.randint(0, h)\n    x = np.random.randint(0, w)\n\n    y1 = np.clip(y - mask_size // 2, 0, h)\n    y2 = np.clip(y + mask_size // 2, 0, h)\n    x1 = np.clip(x - mask_size // 2, 0, w)\n    x2 = np.clip(x + mask_size // 2, 0, w)\n\n    # Apply the cutout\n    mask[y1:y2, x1:x2, :] = 0\n\n    # Apply the mask to the image\n    img = img * mask\n\n    return img\n\n# Example usage with ImageDataGenerator\ndatagen = ImageDataGenerator(\n    # Other data augmentation settings (e.g., rotation, flip, etc.)\n    preprocessing_function=lambda img: cutout(img, mask_size=7)  # Adjust mask_size as needed\n)\n\n# Use datagen.flow for training\ntrain_generator = datagen.flow(X_train, y_train, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T09:49:58.07087Z","iopub.execute_input":"2023-10-05T09:49:58.071246Z","iopub.status.idle":"2023-10-05T09:49:58.078483Z","shell.execute_reply.started":"2023-10-05T09:49:58.071217Z","shell.execute_reply":"2023-10-05T09:49:58.077324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply data augmentation to the training data\ndatagen.fit(X_train)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-05T09:59:19.136251Z","iopub.execute_input":"2023-10-05T09:59:19.136768Z","iopub.status.idle":"2023-10-05T09:59:19.45445Z","shell.execute_reply.started":"2023-10-05T09:59:19.136728Z","shell.execute_reply":"2023-10-05T09:59:19.453515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Define a function to display augmented images\ndef display_augmented_images(generator, num_images=5):\n    # Generate augmented images\n    augmented_images, _ = next(generator)\n\n    # Create a figure and axis to display images\n    fig, axes = plt.subplots(1, num_images, figsize=(15, 5))\n\n    # Display the augmented images\n    for i in range(num_images):\n        axes[i].imshow(augmented_images[i])\n        axes[i].axis('off')\n\n    plt.show()\n\n# Call the function to display augmented images\ndisplay_augmented_images(train_generator, num_images=5)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T09:50:05.955746Z","iopub.execute_input":"2023-10-05T09:50:05.956122Z","iopub.status.idle":"2023-10-05T09:50:06.135286Z","shell.execute_reply.started":"2023-10-05T09:50:05.956089Z","shell.execute_reply":"2023-10-05T09:50:06.133967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom tensorflow.keras import layers, models\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_val = label_encoder.transform(y_val)\nwith strategy.scope():\n    # Define the CNN model\n    \n    model = models.Sequential()\n\n    model.add(layers.Conv2D(64, (5,5), padding='same', activation='relu', input_shape=(32,32,3)))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(128, activation='relu'))\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(10, activation='softmax'))\n    \n\n\n    # Compile the model\n    adam = Adam(lr=0.0001)\n    model.compile(optimizer=adam,\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the model with data augmentation\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64), validation_data=(X_val, y_val), epochs=70)\nmodel.save('/kaggle/working/my_model_with_data_augmentation.h5')","metadata":{"execution":{"iopub.status.busy":"2023-10-03T09:45:01.339012Z","iopub.execute_input":"2023-10-03T09:45:01.339351Z","iopub.status.idle":"2023-10-03T10:21:22.255012Z","shell.execute_reply.started":"2023-10-03T09:45:01.339326Z","shell.execute_reply":"2023-10-03T10:21:22.253989Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow as tf\n\n# Define the load_and_preprocess_image function\ndef load_and_preprocess_image(image_path):\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, (32, 32))\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    image = image / 255.0  # Normalize pixel values to the range [0, 1]\n    return image\nlabel_to_string = {\n    0: \"airplane\",\n    1: \"automobile\",\n    2: \"bird\",\n    3: \"cat\",\n    4: \"deer\",\n    5: \"dog\",\n    6: \"frog\",\n    7: \"horse\",\n    8: \"ship\",\n    9: \"truck\"\n}\n\n# List all files in the Test folder\ntest_image_files = os.listdir(\"/kaggle/working/test/\")\n\n# Create a list of image IDs (without the file extension)\nimage_ids = [os.path.splitext(image_file)[0] for image_file in test_image_files]\n\n# Create a dataset for the test images\ntest_image_paths = [os.path.join(\"/kaggle/working/test/\", image_file) for image_file in test_image_files]\ntest_dataset = tf.data.Dataset.from_tensor_slices(test_image_paths)\ntest_dataset = test_dataset.map(load_and_preprocess_image)  # Assuming you have defined load_and_preprocess_image\n\n# Make predictions for the test dataset\npredictions = model.predict(test_dataset.batch(60))  # Use batch processing for efficiency\n\n# Convert predicted class indices to class labels\npredicted_labels = np.argmax(predictions, axis=1)\npredicted_labels_strings = [label_to_string[label] for label in predicted_labels]\n\n# Create a DataFrame for the predictions with \"id\" as the image names\nsubmission_df = pd.DataFrame({'id': image_ids, 'label': predicted_labels_strings})\n\n# Save the submission DataFrame to a CSV file\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n","metadata":{"editable":false,"execution":{"iopub.status.busy":"2023-10-04T22:47:53.497172Z","iopub.execute_input":"2023-10-04T22:47:53.497602Z","iopub.status.idle":"2023-10-04T22:48:20.032726Z","shell.execute_reply.started":"2023-10-04T22:47:53.497571Z","shell.execute_reply":"2023-10-04T22:48:20.031174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import optimizers\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom tensorflow.keras import layers, models\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_val = label_encoder.transform(y_val)\nwith strategy.scope():\n    # Define the CNN model\n    \n    model = models.Sequential()\n\n    model.add(layers.Conv2D(64, (5,5), padding='same', activation='relu', input_shape=(32,32,3)))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n    \n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(128, activation='relu'))\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(10, activation='softmax'))\n    \n\n\n    # Compile the model\n    adam = Adam(lr=0.00001)\n    model.compile(optimizer=adam,\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the model with data augmentation\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64), validation_data=(X_val, y_val), epochs=200)\nmodel.save('/kaggle/working/my_model_with_data_augmentation1.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T20:35:31.848019Z","iopub.execute_input":"2023-10-03T20:35:31.848344Z","iopub.status.idle":"2023-10-03T22:23:50.158027Z","shell.execute_reply.started":"2023-10-03T20:35:31.848318Z","shell.execute_reply":"2023-10-03T22:23:50.156834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow as tf\n\n# Define the load_and_preprocess_image function\ndef load_and_preprocess_image(image_path):\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, (32, 32))\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    image = image / 255.0  # Normalize pixel values to the range [0, 1]\n    return image\nlabel_to_string = {\n    0: \"airplane\",\n    1: \"automobile\",\n    2: \"bird\",\n    3: \"cat\",\n    4: \"deer\",\n    5: \"dog\",\n    6: \"frog\",\n    7: \"horse\",\n    8: \"ship\",\n    9: \"truck\"\n}\n\n# List all files in the Test folder\ntest_image_files = os.listdir(\"/kaggle/working/test/\")\n\n# Create a list of image IDs (without the file extension)\nimage_ids = [os.path.splitext(image_file)[0] for image_file in test_image_files]\n\n# Create a dataset for the test images\ntest_image_paths = [os.path.join(\"/kaggle/working/test/\", image_file) for image_file in test_image_files]\ntest_dataset = tf.data.Dataset.from_tensor_slices(test_image_paths)\ntest_dataset = test_dataset.map(load_and_preprocess_image)  # Assuming you have defined load_and_preprocess_image\n\n# Make predictions for the test dataset\npredictions = model.predict(test_dataset.batch(60))  # Use batch processing for efficiency\n\n# Convert predicted class indices to class labels\npredicted_labels = np.argmax(predictions, axis=1)\npredicted_labels_strings = [label_to_string[label] for label in predicted_labels]\n\n# Create a DataFrame for the predictions with \"id\" as the image names\nsubmission_df = pd.DataFrame({'id': image_ids, 'label': predicted_labels_strings})\n\n# Save the submission DataFrame to a CSV file\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T10:09:19.290202Z","iopub.execute_input":"2023-10-04T10:09:19.290556Z","iopub.status.idle":"2023-10-04T10:10:34.327784Z","shell.execute_reply.started":"2023-10-04T10:09:19.290525Z","shell.execute_reply":"2023-10-04T10:10:34.326503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import Callback\n\n# Define a custom callback to stop training when accuracy exceeds the threshold\nclass CustomEarlyStopping(Callback):\n    def __init__(self, monitor='accuracy', value=0.91, verbose=1):\n        super(CustomEarlyStopping, self).__init__()\n        self.monitor = monitor\n        self.value = value\n        self.verbose = verbose\n\n    def on_epoch_end(self, epoch, logs=None):\n        current_accuracy = logs.get(self.monitor)\n        if current_accuracy is None:\n            raise ValueError(f\"Metric '{self.monitor}' is not available. Available metrics: {','.join(logs.keys())}\")\n\n        if current_accuracy >= self.value:\n            if self.verbose > 0:\n                print(f\"\\nReached {self.monitor} >= {self.value}, stopping training.\")\n            self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2023-10-05T08:45:54.828297Z","iopub.execute_input":"2023-10-05T08:45:54.828954Z","iopub.status.idle":"2023-10-05T08:45:54.834965Z","shell.execute_reply.started":"2023-10-05T08:45:54.828915Z","shell.execute_reply":"2023-10-05T08:45:54.834066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom tensorflow.keras import layers, models\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_val = label_encoder.transform(y_val)\nwith strategy.scope():\n    # Define the CNN model\n    \n    model = models.Sequential()\n\n    model.add(layers.Conv2D(64, (5,5), padding='same', activation='relu', input_shape=(32,32,3)))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n    \n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(128, activation='relu'))\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(10, activation='softmax'))\n    # Create an instance of the custom early stopping callback\n    custom_early_stopping = CustomEarlyStopping(monitor='val_accuracy', value=0.88)\n\n    \n\n    # Define the EarlyStopping callback\n    early_stopping = EarlyStopping(\n        monitor='val_loss',  # Metric to monitor (e.g., validation loss)\n        patience=10,          # Number of epochs with no improvement after which training will be stopped\n        restore_best_weights=True  # Restore model weights to the best epoch\n    )\n\n    # Compile the model\n    adam = Adam(lr=0.0001)\n    model.compile(optimizer=adam,\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the model with data augmentation\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64), validation_data=(X_val, y_val), epochs=100, callbacks=[custom_early_stopping] )\nmodel.save('/kaggle/working/my_model_with_data_augmentation1.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-03T23:10:38.911826Z","iopub.execute_input":"2023-10-03T23:10:38.912163Z","iopub.status.idle":"2023-10-03T23:50:42.569912Z","shell.execute_reply.started":"2023-10-03T23:10:38.912139Z","shell.execute_reply":"2023-10-03T23:50:42.568893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom tensorflow.keras import layers, models\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_val = label_encoder.transform(y_val)\nwith strategy.scope():\n    # Define the CNN model\n    \n    model = models.Sequential()\n\n    model.add(layers.Conv2D(64, (5,5), padding='same', activation='relu', input_shape=(32,32,3)))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n    \n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(128, activation='relu'))\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(10, activation='softmax'))\n    # Create an instance of the custom early stopping callback\n    custom_early_stopping = CustomEarlyStopping(monitor='val_accuracy', value=0.91)\n\n    \n\n    # Define the EarlyStopping callback\n    early_stopping = EarlyStopping(\n        monitor='val_loss',  # Metric to monitor (e.g., validation loss)\n        patience=10,          # Number of epochs with no improvement after which training will be stopped\n        restore_best_weights=True  # Restore model weights to the best epoch\n    )\n\n    # Compile the model\n    adam = Adam(lr=0.0001)\n    model.compile(optimizer=adam,\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the model with data augmentation\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64), validation_data=(X_val, y_val), epochs=300, callbacks=[custom_early_stopping] )\nmodel.save('/kaggle/working/my_model_with_data_augmentation2.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T07:17:42.140094Z","iopub.execute_input":"2023-10-04T07:17:42.140442Z","iopub.status.idle":"2023-10-04T10:09:01.742908Z","shell.execute_reply.started":"2023-10-04T07:17:42.140396Z","shell.execute_reply":"2023-10-04T10:09:01.741986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom tensorflow.keras import layers, models\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_val = label_encoder.transform(y_val)\n'''\nwith strategy.scope():\n    # Define the CNN model\n    \n    model = models.Sequential()\n\n    model.add(layers.Conv2D(128, (5,5), padding='same', activation='relu', input_shape=(32,32,3)))\n    model.add(BatchNormalization())\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\n    \n    \n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.Conv2D(68, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(layers.MaxPooling2D(pool_size=(2,2)))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(128, activation='relu'))\n    model.add(layers.Dropout(0.2))\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dropout(0.2))\n    model.add(layers.Dense(10, activation='softmax'))\n    '''\nfrom tensorflow.keras.layers import Add\n\n# Define a function to create a residual block\nfrom tensorflow.keras.layers import Add\n\n# Define a function to create a residual block\ndef residual_block(x, filters, kernel_size=3, strides=1, activation='relu'):\n    shortcut = x\n    \n    # Apply the first convolutional layer\n    x = layers.Conv2D(filters, kernel_size, strides=strides, padding='same')(x)\n    x = BatchNormalization()(x)\n    x = layers.Activation(activation)(x)\n    \n    # Apply the second convolutional layer\n    x = layers.Conv2D(filters, kernel_size, padding='same')(x)\n    x = BatchNormalization()(x)\n    \n    # If the dimensions of the shortcut and x do not match, apply a 1x1 convolution to match the dimensions\n    if shortcut.shape[-1] != x.shape[-1]:\n        shortcut = layers.Conv2D(filters, (1, 1), strides=strides, padding='same')(shortcut)\n    \n    # Add the shortcut to x\n    x = layers.Add()([x, shortcut])\n    x = layers.Activation(activation)(x)\n    \n    return x\n\n\nwith strategy.scope():\n    # Define the CNN model with residual connections\n    input_layer = layers.Input(shape=(32, 32, 3))\n    x = layers.Conv2D(128, (5, 5), padding='same', activation='relu',)(input_layer)\n    x = BatchNormalization()(x)\n\n    # First residual block\n    x = residual_block(x, filters=128)\n\n    x = layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)\n\n    # Second residual block\n    x = residual_block(x, filters=68)\n\n    x = layers.MaxPooling2D(pool_size=(2, 2))(x)\n\n    # Third residual block\n    x = residual_block(x, filters=128)\n\n    x = layers.MaxPooling2D(pool_size=(2, 2))(x)\n\n    x = layers.Flatten()(x)\n    x = layers.Dense(128, activation='relu')(x)\n    x = layers.Dropout(0.4)(x)\n    x = layers.Dense(64, activation='relu')(x)\n    x = layers.Dropout(0.4)(x)\n    output_layer = layers.Dense(10, activation='softmax')(x)\n\n    model = models.Model(inputs=input_layer, outputs=output_layer)\n\n    # Create an instance of the custom early stopping callback\n    custom_early_stopping = CustomEarlyStopping(monitor='val_accuracy', value=0.91)\n\n    \n\n    # Define the EarlyStopping callback\n    early_stopping = EarlyStopping(\n        monitor='val_loss',  # Metric to monitor (e.g., validation loss)\n        patience=10,          # Number of epochs with no improvement after which training will be stopped\n        restore_best_weights=True  # Restore model weights to the best epoch\n    )\n\n    # Compile the model\n    adam = Adam(lr=0.0001)\n    model.compile(optimizer=adam,\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the model with data augmentation\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64), validation_data=(X_val, y_val), epochs=300, callbacks=[custom_early_stopping] )\nmodel.save('/kaggle/working/my_model_with_data_augmentation3.h5')\nimport os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow as tf\n\n# Define the load_and_preprocess_image function\ndef load_and_preprocess_image(image_path):\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, (32, 32))\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    image = image / 255.0  # Normalize pixel values to the range [0, 1]\n    return image\nlabel_to_string = {\n    0: \"airplane\",\n    1: \"automobile\",\n    2: \"bird\",\n    3: \"cat\",\n    4: \"deer\",\n    5: \"dog\",\n    6: \"frog\",\n    7: \"horse\",\n    8: \"ship\",\n    9: \"truck\"\n}\n\n# List all files in the Test folder\ntest_image_files = os.listdir(\"/kaggle/working/test/\")\n\n# Create a list of image IDs (without the file extension)\nimage_ids = [os.path.splitext(image_file)[0] for image_file in test_image_files]\n\n# Create a dataset for the test images\ntest_image_paths = [os.path.join(\"/kaggle/working/test/\", image_file) for image_file in test_image_files]\ntest_dataset = tf.data.Dataset.from_tensor_slices(test_image_paths)\ntest_dataset = test_dataset.map(load_and_preprocess_image)  # Assuming you have defined load_and_preprocess_image\n\n# Make predictions for the test dataset\npredictions = model.predict(test_dataset.batch(60))  # Use batch processing for efficiency\n\n# Convert predicted class indices to class labels\npredicted_labels = np.argmax(predictions, axis=1)\npredicted_labels_strings = [label_to_string[label] for label in predicted_labels]\n\n# Create a DataFrame for the predictions with \"id\" as the image names\nsubmission_df = pd.DataFrame({'id': image_ids, 'label': predicted_labels_strings})\n\n# Save the submission DataFrame to a CSV file\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T09:59:30.983Z","iopub.execute_input":"2023-10-05T09:59:30.98333Z","iopub.status.idle":"2023-10-05T12:00:17.34235Z","shell.execute_reply.started":"2023-10-05T09:59:30.9833Z","shell.execute_reply":"2023-10-05T12:00:17.338334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define paths to the train and test image folders\ntrain_dir = '/kaggle/working/train/'\ntest_dir = '/kaggle/working/test/'\n\n# Load the labels from trainLabels.csv\nlabels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:00:45.092742Z","iopub.execute_input":"2023-10-05T12:00:45.094186Z","iopub.status.idle":"2023-10-05T12:00:45.121117Z","shell.execute_reply.started":"2023-10-05T12:00:45.094129Z","shell.execute_reply":"2023-10-05T12:00:45.120122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess_image(image_path, label):\n    image = tf.keras.preprocessing.image.load_img(image_path, target_size=(32, 32))\n    image = tf.keras.preprocessing.image.img_to_array(image)\n    image /= 255.0  # Normalize pixel values to the range [0, 1]\n    return image, label\n\n# Load and preprocess images\nimages = []\nlabels = []\n\nfor img_name, label in labels_df.values:\n    img_path = os.path.join(train_dir, str(img_name) + '.png')\n    img, label = load_and_preprocess_image(img_path, label)\n    images.append(img)\n    labels.append(label)\n\n# Convert lists to numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:01:13.176737Z","iopub.execute_input":"2023-10-05T12:01:13.177463Z","iopub.status.idle":"2023-10-05T12:01:24.597019Z","shell.execute_reply.started":"2023-10-05T12:01:13.17743Z","shell.execute_reply":"2023-10-05T12:01:24.595806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)\n\n# One-hot encode the labels\nlabel_encoder = LabelEncoder()\ny_train_encoded = label_encoder.fit_transform(y_train)\ny_val_encoded = label_encoder.transform(y_val)\ny_train_one_hot = to_categorical(y_train_encoded, num_classes=10)\ny_val_one_hot = to_categorical(y_val_encoded, num_classes=10)\n\n# Data augmentation with ImageDataGenerator\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:01:28.391538Z","iopub.execute_input":"2023-10-05T12:01:28.391865Z","iopub.status.idle":"2023-10-05T12:01:28.548284Z","shell.execute_reply.started":"2023-10-05T12:01:28.391837Z","shell.execute_reply":"2023-10-05T12:01:28.547347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load pre-trained ResNet-50 model\nresnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(32, 32, 3))\n\n# Add custom classification head for ResNet-50\nx_resnet = resnet_model.output\nx_resnet = GlobalAveragePooling2D()(x_resnet)\nx_resnet = Dense(128, activation='relu')(x_resnet)\npredictions_resnet = Dense(10, activation='softmax')(x_resnet)\n\n# Create the final ResNet-50 model\nresnet_model_final = Model(inputs=resnet_model.input, outputs=predictions_resnet)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:01:45.869595Z","iopub.execute_input":"2023-10-05T12:01:45.870681Z","iopub.status.idle":"2023-10-05T12:01:50.230297Z","shell.execute_reply.started":"2023-10-05T12:01:45.870638Z","shell.execute_reply":"2023-10-05T12:01:50.229215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50, VGG16\n# Load pre-trained VGG16 model\nvgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3))\n\n# Add custom classification head for VGG16\nx_vgg = vgg_model.output\nx_vgg = GlobalAveragePooling2D()(x_vgg)\nx_vgg = Dense(128, activation='relu')(x_vgg)\npredictions_vgg = Dense(10, activation='softmax')(x_vgg)\n\n# Create the final VGG16 model\nvgg_model_final = Model(inputs=vgg_model.input, outputs=predictions_vgg)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:02:27.125603Z","iopub.execute_input":"2023-10-05T12:02:27.125958Z","iopub.status.idle":"2023-10-05T12:02:28.65715Z","shell.execute_reply.started":"2023-10-05T12:02:27.125929Z","shell.execute_reply":"2023-10-05T12:02:28.656096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, concatenate\n# Combine the models into an ensemble\ncombined_model_input = concatenate([resnet_model_final.output, vgg_model_final.output])\nensemble_output = Dense(10, activation='softmax')(combined_model_input)\nensemble_model = Model(inputs=[resnet_model_final.input, vgg_model_final.input], outputs=ensemble_output)\n\n# Compile the ensemble model\nensemble_model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:03:21.155358Z","iopub.execute_input":"2023-10-05T12:03:21.155701Z","iopub.status.idle":"2023-10-05T12:03:21.197211Z","shell.execute_reply.started":"2023-10-05T12:03:21.155671Z","shell.execute_reply":"2023-10-05T12:03:21.196342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the ensemble model with data augmentation\nbatch_size = 32\nepochs = 10\n\nhistory = ensemble_model.fit(\n    [datagen.flow(X_train, y_train_one_hot, batch_size=batch_size), datagen.flow(X_train, y_train_one_hot, batch_size=batch_size)],\n    steps_per_epoch=len(X_train) / batch_size,\n    epochs=epochs,\n    validation_data=([X_val, X_val], y_val_one_hot)\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T12:04:00.399751Z","iopub.execute_input":"2023-10-05T12:04:00.400096Z","iopub.status.idle":"2023-10-05T12:04:00.447436Z","shell.execute_reply.started":"2023-10-05T12:04:00.400068Z","shell.execute_reply":"2023-10-05T12:04:00.446213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.applications import ResNet50, VGG16\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, concatenate\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Define paths to the train and test image folders\ntrain_dir = '/kaggle/working/train/'\ntest_dir = '/kaggle/working/test/'\n\n# Load the labels from trainLabels.csv\nlabels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n\n# Function to load and preprocess the images\ndef load_and_preprocess_image(image_path, label):\n    image = tf.keras.preprocessing.image.load_img(image_path, target_size=(32, 32))\n    image = tf.keras.preprocessing.image.img_to_array(image)\n    image /= 255.0  # Normalize pixel values to the range [0, 1]\n    return image, label\n\n# Load and preprocess images\nimages = []\nlabels = []\n\nfor img_name, label in labels_df.values:\n    img_path = os.path.join(train_dir, str(img_name) + '.png')\n    img, label = load_and_preprocess_image(img_path, label)\n    images.append(img)\n    labels.append(label)\n\n# Convert lists to numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)\n\n# One-hot encode the labels\nlabel_encoder = LabelEncoder()\ny_train_encoded = label_encoder.fit_transform(y_train)\ny_val_encoded = label_encoder.transform(y_val)\ny_train_one_hot = to_categorical(y_train_encoded, num_classes=10)\ny_val_one_hot = to_categorical(y_val_encoded, num_classes=10)\n\n# Data augmentation with ImageDataGenerator for ResNet-50\ndatagen_resnet = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\n# Data augmentation with ImageDataGenerator for VGG16\ndatagen_vgg = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\n# Load pre-trained ResNet-50 model\nresnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(32, 32, 3))\n\n# Add custom classification head for ResNet-50\nx_resnet = resnet_model.output\nx_resnet = GlobalAveragePooling2D()(x_resnet)\nx_resnet = Dense(128, activation='relu')(x_resnet)\npredictions_resnet = Dense(10, activation='softmax')(x_resnet)\n\n# Create the final ResNet-50 model\nresnet_model_final = Model(inputs=resnet_model.input, outputs=predictions_resnet)\n\n# Load pre-trained VGG16 model\nvgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3))\n\n# Add custom classification head for VGG16\nx_vgg = vgg_model.output\nx_vgg = GlobalAveragePooling2D()(x_vgg)\nx_vgg = Dense(128, activation='relu')(x_vgg)\npredictions_vgg = Dense(10, activation='softmax')(x_vgg)\n\n# Create the final VGG16 model\nvgg_model_final = Model(inputs=vgg_model.input, outputs=predictions_vgg)\n\n# Compile the models\nresnet_model_final.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\nvgg_model_final.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the models with data augmentation\nbatch_size = 32\nepochs = 30\n\n# For ResNet-50\nhistory_resnet = resnet_model_final.fit(\n    datagen_resnet.flow(X_train, y_train_one_hot, batch_size=batch_size),\n    steps_per_epoch=len(X_train) / batch_size,\n    epochs=epochs,\n    validation_data=(X_val, y_val_one_hot)\n)\n\n# For VGG16\nhistory_vgg = vgg_model_final.fit(\n    datagen_vgg.flow(X_train, y_train_one_hot, batch_size=batch_size),\n    steps_per_epoch=len(X_train) / batch_size,\n    epochs=epochs,\n    validation_data=(X_val, y_val_one_hot)\n)\n\n# Combine predictions from both models\ncombined_predictions = np.concatenate([resnet_model_final.predict(X_val), vgg_model_final.predict(X_val)], axis=1)\n\n# Create an ensemble model for the combined predictions\nensemble_input = Input(shape=(20,))  # Change the shape if needed based on the number of classes\nensemble_output = Dense(10, activation='softmax')(ensemble_input)  # 10 classes in CIFAR-10\nensemble_model = Model(inputs=ensemble_input, outputs=ensemble_output)\n\n# Compile the ensemble model\nensemble_model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the ensemble model\nensemble_history = ensemble_model.fit(\n    combined_predictions,\n    y_val_one_hot,\n    batch_size=batch_size,\n    epochs=epochs,\n    validation_data=(combined_predictions, y_val_one_hot)\n)\n\n# Evaluate the ensemble model\ntest_loss, test_accuracy = ensemble_model.evaluate(combined_predictions, y_val_one_hot)\nprint(\"Test accuracy (ensemble):\", test_accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T13:19:59.119888Z","iopub.execute_input":"2023-10-05T13:19:59.121395Z"},"trusted":true},"execution_count":null,"outputs":[]}]}