{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":8263117,"sourceType":"datasetVersion","datasetId":4902966},{"sourceId":8275565,"sourceType":"datasetVersion","datasetId":4906464}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import math, re, os    \nimport tensorflow as tf   \nimport numpy as np \nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets \nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\nprint(\"Tensorflow version \" + tf.__version__)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"/kaggle/input/asv-spoof-2019-chroma/image_data_chroma.tfrecord\"\n\n# Create an ImageDataGenerator\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\n# Create a TensorFlow dataset using flow_from_directory\ndataset = datagen.flow_from_directory(\n    data_dir,\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try: # detect TPUs\n    resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='local')\n    tf.tpu.experimental.initialize_tpu_system(resolver)\n    strategy = tf.distribute.TPUStrategy(resolver)     \n    print(strategy)\nexcept ValueError: # detect GPUs\n    strategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines    \n    #strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU\n    #strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() # for clusters of multi-GPU machines\n\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NEW on TPU in TensorFlow 24: shorter cross-compatible TPU/GPU/multi-GPU/cluster-GPU detection code\n       \n        ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Competition data access\nTPUs read data directly from Google Cloud Storage (GCS). This Kaggle utility will copy the dataset to a GCS bucket co-located with the TPU. If you have multiple datasets attached to the notebook, you can pass the name of a specific dataset to the get_gcs_path function. The name of the dataset is the name of the directory it is mounted in. Use `!ls /kaggle/input/` to list attached datasets.","metadata":{}},{"cell_type":"code","source":"!pip list\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nimport os \nimport numpy as np\nimport pandas as pd\nimport cv2\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report, ConfusionMatrixDisplay\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras import layers\n\nfrom keras import  layers\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.preprocessing.image import img_to_array, load_img\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input\n\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization\nfrom tensorflow.keras.applications import VGG19\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import InceptionV3\n","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:41:58.658548Z","iopub.execute_input":"2024-05-05T11:41:58.658985Z","iopub.status.idle":"2024-05-05T11:41:58.717244Z","shell.execute_reply.started":"2024-05-05T11:41:58.658951Z","shell.execute_reply":"2024-05-05T11:41:58.7163Z"},"trusted":true},"execution_count":7,"outputs":[]},{"cell_type":"markdown","source":"# Configuration","metadata":{}},{"cell_type":"code","source":"# Define the parameters\nIMG_WIDTH, IMG_HEIGHT = 224, 224\nBATCH_SIZE = 32\nEPOCHES = 30                                                                                                                                   # 100 - 102","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:42:02.437583Z","iopub.execute_input":"2024-05-05T11:42:02.43834Z","iopub.status.idle":"2024-05-05T11:42:02.442777Z","shell.execute_reply.started":"2024-05-05T11:42:02.438301Z","shell.execute_reply":"2024-05-05T11:42:02.441833Z"},"trusted":true},"execution_count":8,"outputs":[]},{"cell_type":"code","source":"! pip install keras","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization utilities\ndata -> pixels, nothing of much interest for the machine learning practitioner in this section.","metadata":{}},{"cell_type":"code","source":"def serialize_example(image, label):\n    \"\"\"\n    Serialize image and label into a tf.train.Example.\n    \"\"\"\n    feature = {\n        'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[tf.io.serialize_tensor(image).numpy()])),\n        'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))\n    }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()\n\ndef saving_tensor_data(image_data, labels, filepath):\n    \"\"\"\n    Save image data and labels as TFRecord files.\n    \"\"\"\n    with tf.io.TFRecordWriter(filepath) as writer:\n        for image, label in zip(image_data, labels):\n            serialized_example = serialize_example(image, label)\n            writer.write(serialized_example)\n\ndef parse_tfrecord(serialized_example):\n    \"\"\"\n    Parse a single example from TFRecord.\n    \"\"\"\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    parsed_example = tf.io.parse_single_example(serialized_example, feature_description)\n    image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n    label = parsed_example['label']\n    return image, label\n\ndef loading_tensor_data(filepath):\n    \"\"\"\n    Load image data and labels from TFRecord files.\n    \"\"\"\n    image_data = []\n    labels = []\n    for serialized_example in tf.data.TFRecordDataset(filepath):\n        image, label = parse_tfrecord(serialized_example)\n        image_data.append(image)\n        labels.append(label)\n    return image_data, labels\n\n\n        \n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving_tensor_data(image_data, labels, '/kaggle/working/image_data_chroma.tfrecord')\n\n# Loading data \ninput_shape = (224, 224,  3) \nloaded_image_data, loaded_labels = loading_tensor_data('/kaggle/input/asvspoof-2019-melspectogram/image_data_mel.tfrecord')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(loaded_image_data)\ny = np.array(loaded_labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef create_cnn_model(input_shape):\n    model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),\n    MaxPooling2D((3, 3)),\n    BatchNormalization(),  # Add Batch Normalization after Conv2D\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((3, 3)),\n    BatchNormalization(),  # Add Batch Normalization after Conv2D\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((3, 3)),\n    Dropout(0.2),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    BatchNormalization(),  # Add Batch Normalization before Dense layer\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(2, activation='softmax')\n])\n\n    return model\n\n# Define input shape of your images\ninput_shape = (224, 224,  3)  # Replace with your image dimensions\n\n# Print model summary\n","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:57:06.915605Z","iopub.execute_input":"2024-05-05T11:57:06.916002Z","iopub.status.idle":"2024-05-05T11:57:06.923258Z","shell.execute_reply.started":"2024-05-05T11:57:06.915971Z","shell.execute_reply":"2024-05-05T11:57:06.92203Z"},"trusted":true},"execution_count":21,"outputs":[]},{"cell_type":"code","source":"\n\nwith strategy.scope():\n    model_cnn = create_cnn_model(input_shape)# define your model normally\n    model_cnn.summary()\n    model_cnn.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nhistory_cnn = model_cnn.fit( X_train, y_train , epochs=EPOCHES, batch_size=BATCH_SIZE, validation_data=(X_val, y_val))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.applications import VGG19\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom keras.layers import Dropout","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# Convert binary labels to one-hot encoded labels\ny_train = to_categorical(y_train, num_classes=2)\ny_val = to_categorical(y_val, num_classes=2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras.backend ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(keras.__version__)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = {0: 0.5558320709803073, 1: 4.977713178294573}\n \n# Define custom loss function for weighted binary cross-entropy (if needed)\ndef weighted_binary_crossentropy(y_true, y_pred):\n    weights = tf.constant([class_weights[1]], dtype=tf.float32)\n    loss = tf.keras.losses.binary_crossentropy(y_true, y_pred)\n    weighted_loss = loss * weights\n    return tf.reduce_mean(weighted_loss)\n\ndef create_vgg_model(input_shape):   # Load the pre-trained VGG19 model\n    vgg_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)\n\n    # Create your own model\n    model = Sequential()\n\n    # Add VGG19 model layers to your model\n    for layer in vgg_model.layers:\n        model.add(layer)\n\n    # Freeze the layers of the VGG19 model\n    for layer in model.layers:\n        layer.trainable = False\n\n    # Add your custom layers\n    model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    # Add more layers here if needed\n\n    # Flatten the output of the last convolutional layer\n    model.add(Flatten())\n\n    # Add a fully connected layer\n    model.add(Dense(units=128, activation='relu'))\n\n    # Add output layer for multi-class classification\n    model.add(Dense(units=1, activation='sigmoid'))  # Assuming 2 classes: fake or real\n\n    # Display model summary\n    model.summary()\n    return model\n\n\n# Define early stopping\nearly_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)\n\n\nwith strategy.scope():\n    input_shape = (224, 224,  3)\n    model_vgg = create_vgg_model(input_shape)# define your model normally\n#     model_vgg.summary()\n    model_vgg.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(learning_rate=0.0001), metrics=['accuracy'])\n#     vgg_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)\n\n#     # Create your own model\n#     model_vgg = Sequential()\n\n#     # Add VGG19 model layers to your model\n#     for layer in vgg_model.layers:\n#         model_vgg.add(layer)\n\n#     # Freeze the layers of the VGG19 model\n#     for layer in model_vgg.layers:\n#         layer.trainable = False\n\n#     # Add your custom layers\n#     model_vgg.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\n#     model_vgg.add(MaxPooling2D(pool_size=(2, 2)))\n#     model_vgg.add(Dropout(0.25))\n\n#     model_vgg.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same'))\n#     model_vgg.add(MaxPooling2D(pool_size=(2, 2)))\n#     model_vgg.add(Dropout(0.25))\n\n#     # Add more layers here if needed\n\n#     # Flatten the output of the last convolutional layer\n#     model_vgg.add(Flatten())\n\n#     # Add a fully connected layer\n#     model_vgg.add(Dense(units=128, activation='relu'))\n\n#     # Add output layer for multi-class classification\n#     model_vgg.add(Dense(units=2, activation='softmax'))  # Assuming 2 classes: fake or real\n\n#     # Display model summary\n#     model_vgg.summary()\n#     # define your model normally\n#     model_vgg.compile(loss=weighted_binary_crossentropy, optimizer=Adam(learning_rate=0.0005), metrics=['accuracy'])\n\n# # Train the model with early stopping\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_vgg_model(input_shape):\n    vgg_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)\n\n    model = Sequential()\n\n    for layer in vgg_model.layers:\n        model.add(layer)\n\n    for layer in model.layers:\n        layer.trainable = False\n\n    model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Flatten())\n    model.add(Dense(units=128, activation='relu'))\n\n    # Output layer for binary classification with 'sigmoid' activation\n    model.add(Dense(units=1, activation='sigmoid'))\n\n    model.summary()\n    return model\n\nwith strategy.scope():\n    input_shape = (224, 224, 3)\n    model_vgg = create_vgg_model(input_shape)\n    model_vgg.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0001), metrics=['accuracy'])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_val.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_vgg = model_vgg.fit(X_train, y_train, epochs=30, batch_size=BATCH_SIZE, validation_data=(X_val, y_val))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.save('kaggle/working/model_chroma/model_vgg.keras')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.evaluate(X_val, y_val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CATEGORIES =['0','1']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg.evaluate(X_val, y_val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\n\ndef plot_graphs(history,y_pred):\n  sns.set()\n  fig = plt.figure(0, (12, 4))\n\n  ax= plt.subplot(1, 3, 1)\n  sns.lineplot( history.history['accuracy'], label='train')\n  sns.lineplot( history.history['val_accuracy'], label='valid')\n  plt.title('Accuracy')\n  plt.xlabel('Number of Epoches', fontweight='semibold')\n  plt.ylabel('Accuracy', fontweight='semibold')\n\n  ax = plt.subplot(1, 3, 2)\n  sns.lineplot(history.history['loss'], label='train')\n  sns.lineplot(history.history['val_loss'], label='valid')\n  plt.title('Loss')\n  plt.xlabel('Number of Epoches',fontweight='semibold')\n  plt.ylabel('Loss', fontweight='semibold')\n\n  LABELS = [\"spoofed\",\"bonafied\"]\n  cm = confusion_matrix(y_test, y_pred)\n\n  # Plot confusion matrix\n  ax = plt.subplot(1, 3, 3)\n  sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',xticklabels=LABELS, yticklabels=LABELS, cbar=False)\n  plt.title(\"VGG19 MODEL\", fontsize=12)\n  plt.xlabel('Predicted label', fontweight='semibold')\n  plt.ylabel('True label', fontweight='semibold')\n\n  plt.tight_layout()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the shape of X_test\nprint(\"Shape of X_test:\", X_test.shape)\n\n# Reshape X_test if necessary\nif X_test.shape[1:] != model_vgg.input_shape[1:]:\n    # Reshape X_test to match the input shape expected by model_vgg\n    new_shape = (X_test.shape[0],) + model_vgg.input_shape[1:]\n    X_test = np.reshape(X_test, new_shape)\n\n    # Verify the new shape of X_test\n    print(\"New shape of X_test:\", X_test.shape)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"-","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Datasets","metadata":{}},{"cell_type":"markdown","source":"# Dataset visualizations","metadata":{}},{"cell_type":"code","source":"# run this cell again for next set of images\ndisplay_batch_of_images(next(train_batch))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    img_adjust_layer = tf.keras.layers.Lambda(lambda data: tf.keras.applications.imagenet_utils.preprocess_input(tf.cast(data, tf.float32), mode=\"torch\"), input_shape=[*IMAGE_SIZE, 3])\n    pretrained_model = SwinTransformer('swin_large_224', num_classes=len(CLASSES), include_top=False, pretrained=True, use_tpu=False)\n    \n    model = tf.keras.Sequential([\n        img_adjust_layer,\n        pretrained_model,\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n    # topk = tf.keras.metrics.TopKCategoricalAccuracy(3, name=\"top-3-accuracy\")\n        \nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5, epsilon=1e-8),\n    loss = 'sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"history = model.fit(get_training_dataset(), steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS,\n                    validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_training_curves(history.history['loss'], history.history['val_loss'], 'loss', 211)\ndisplay_training_curves(history.history['accuracy'], history.history['val_accuracy'], 'accuracy', 212)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion matrix","metadata":{}},{"cell_type":"code","source":"cmdataset = get_validation_dataset(ordered=True) # since we are splitting the dataset and iterating separately on images and labels, order matters.\nimages_ds = cmdataset.map(lambda image, label: image)\nlabels_ds = cmdataset.map(lambda image, label: label).unbatch()\ncm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES))).numpy() # get everything as one batch\ncm_probabilities = model.predict(images_ds, steps=VALIDATION_STEPS)\ncm_predictions = np.argmax(cm_probabilities, axis=-1)\nprint(\"Correct   labels: \", cm_correct_labels.shape, cm_correct_labels)\nprint(\"Predicted labels: \", cm_predictions.shape, cm_predictions)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))\nscore = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\nprecision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\nrecall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\ncmat = (cmat.T / cmat.sum(axis=1)).T # normalized\ndisplay_confusion_matrix(cmat, score, precision, recall)\nprint('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ndef serialize_example(image, label):\n    \"\"\"\n    Serialize image and label into a tf.train.Example.\n    \"\"\"\n    feature = {\n        'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[tf.io.serialize_tensor(image).numpy()])),\n        'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))\n    }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()\n\ndef saving_tensor_data(image_data, labels, filepath):\n    \"\"\"\n    Save image data and labels as TFRecord files.\n    \"\"\"\n    with tf.io.TFRecordWriter(filepath) as writer:\n        for image, label in zip(image_data, labels):\n            serialized_example = serialize_example(image, label)\n            writer.write(serialized_example)\n\ndef parse_tfrecord(serialized_example):\n    \"\"\"\n    Parse a single example from TFRecord.\n    \"\"\"\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    parsed_example = tf.io.parse_single_example(serialized_example, feature_description)\n    image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n    label = parsed_example['label']\n    return image, label\n\ndef loading_tensor_data(filepath):\n    \"\"\"\n    Load image data and labels from TFRecord files.\n    \"\"\"\n    image_data = []\n    labels = []\n    for serialized_example in tf.data.TFRecordDataset(filepath):\n        image, label = parse_tfrecord(serialized_example)\n        image_data.append(image)\n        labels.append(label)\n    return image_data, labels\n\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading data\ninput_shape = (224, 224, 3)\nloaded_image_data, loaded_labels = loading_tensor_data('/kaggle/input/asvspoof-2019-melspectogram/image_data_mel.tfrecord')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"NEWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW","metadata":{}},{"cell_type":"code","source":"IMAGE_FOLDER_PATH = '/kaggle/input/asvspoof-2019-melspectogram/audio-image_melSpectogram-20240429T072948Z-001/audio-image_melSpectogram'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image_path, target_size=(IMG_WIDTH,IMG_HEIGHT)):\n     # Load the image using TensorFlow\n    img = tf.io.read_file(image_path)\n    img = tf.image.decode_image(img, channels=3)  # Ensure RGB channels\n\n    # Resize the image\n    img = tf.image.resize(img, target_size)\n\n    # Normalize pixel values to range [0, 1]\n    img = img / 255.0\n\n    return img","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preprocessing image with label 0\n\nimage_dir = f'{IMAGE_FOLDER_PATH}/0'\nimage_filenames = os.listdir(image_dir)\nprint(len(image_filenames))\nimage_data=[]\nlabels=[]\ncount =0\nfor image_filename in image_filenames:\n    image_path = os.path.join(image_dir, image_filename)\n    preprocessed_image = preprocess_image(image_path)\n    image_data.append(preprocessed_image)\n    labels.append(0)\n    count+=1\n    if count%1000==0 :\n      print(count)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preprocessing image with label 1\n\nimage_dir = f'{IMAGE_FOLDER_PATH}/1'\nimage_filenames = os.listdir(image_dir)\nprint(len(image_filenames))\nfor image_filename in image_filenames:\n    image_path = os.path.join(image_dir, image_filename)\n    preprocessed_image = preprocess_image(image_path)\n    image_data.append(preprocessed_image)\n    labels.append(1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def datasetloader(filepath):\n    def parse_tfrecord(serialized_example):\n        feature_description = {\n            'image': tf.io.FixedLenFeature([], tf.string),\n            'label': tf.io.FixedLenFeature([], tf.int64)\n        }\n        parsed_example = tf.io.parse_single_example(serialized_example, feature_description)\n        image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n        label = parsed_example['label']\n        return image, label\n\n    def load_dataset(filepaths):\n        dataset = tf.data.TFRecordDataset(filepaths)\n        dataset = dataset.map(parse_tfrecord)\n        return dataset\n\n    return load_dataset(filepath)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Load your data using the dataset loader function\n# Adjust paths accordingly\nfilepaths = ['/kaggle/input/asv-spoof-2019-chroma/image_data_chroma.tfrecord']\ndataset = datasetloader(filepaths)\n\n# Split dataset\ndataset_size = sum(1 for _ in dataset)\n# dataset_size= 25000\ntrain_size = int(0.64 * dataset_size)\nval_size = int(0.16 * dataset_size)\ntest_size = int(0.2 * dataset_size)\ntrain_dataset = dataset.take(train_size)\ntest_dataset = dataset.skip(train_size)\nval_dataset = test_dataset.skip(val_size)\ntest_dataset = test_dataset.take(test_size)\n\n# Define batch size\nBATCH_SIZE = 32\n\n# Batch and shuffle datasets\ntrain_dataset = train_dataset.shuffle(buffer_size=1000).batch(BATCH_SIZE).repeat()\nval_dataset = val_dataset.batch(BATCH_SIZE)\ntest_dataset = test_dataset.batch(BATCH_SIZE)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math, re, os \nimport tensorflow as tf\nimport numpy as np\nfrom kaggle_datasets import KaggleDatasets\nfrom sklearn.model_selection import train_test_split\n\n# Assuming you have loaded your image data and labels into X and y arrays\n\ndef serialize_example(image, label):\n    \"\"\"\n    Serialize image and label into a tf.train.Example.\n    \"\"\"\n    feature = {\n        'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[tf.io.serialize_tensor(image).numpy()])),\n        'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))\n    }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()\n\ndef saving_tensor_data(image_data, labels, filepath):\n    \"\"\"\n    Save image data and labels as TFRecord files.\n    \"\"\"\n    with tf.io.TFRecordWriter(filepath) as writer:\n        for image, label in zip(image_data, labels):\n            serialized_example = serialize_example(image, label)\n            writer.write(serialized_example)\n\ndef parse_tfrecord(serialized_example):\n    \"\"\"\n    Parse a single example from TFRecord.\n    \"\"\"\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    parsed_example = tf.io.parse_single_example(serialized_example, feature_description)\n    image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n    label = parsed_example['label']\n    return image, label\n\ndef loading_tensor_data(filepath):\n    \"\"\"\n    Load image data and labels from TFRecord files.\n    \"\"\"\n    image_data = []\n    labels = []\n    for serialized_example in tf.data.TFRecordDataset(filepath):\n        image, label = parse_tfrecord(serialized_example)\n        image_data.append(image)\n        labels.append(label)\n    return image_data, labels\n\n# Save your data to TFRecords\n# saving_tensor_data(X_train, y_train, 'train.tfrecord')\n# saving_tensor_data(X_val, y_val, 'val.tfrecord')\n# saving_tensor_data(X_test, y_test, 'test.tfrecord')\n      \n# Load your data from TFRecords\nX_train, y_train = loading_tensor_data('train.tfrecord')\nX_val, y_val = loading_tensor_data('val.tfrecord')\nX_test, y_test = loading_tensor_data('test.tfrecord')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\n\n# Get the GCS path for your dataset\ngcs_path = '/kaggle/input/asv-spoof-2019-chroma/image_data_chroma.tfrecord'\n# KaggleDatasets().get_gcs_path('your_dataset_name')\n\n# Extract the bucket name from the GCS path\nbucket_name = gcs_path.split('/')[2]\nprint(\"Bucket name:\", bucket_name)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom kaggle_datasets import KaggleDatasets\n\nAUTO = tf.data.experimental.AUTOTUNE\n\n# Get the GCS path for your dataset\ngcs_path = '/kaggle/input/asv-spoof-2019-chroma/image_data_chroma.tfrecord'\n# gcs_path = KaggleDatasets().get_gcs_path('your_dataset_name')\n\n# Extract the bucket name from the GCS path\nbucket_name = gcs_path.split('/')[2]\n\ndef serialize_example(image, label):\n    \"\"\"\n    Serialize image and label into a tf.train.Example.\n    \"\"\"\n    feature = {\n        'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[tf.io.serialize_tensor(image).numpy()])),\n        'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))\n    }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()\n\ndef saving_tensor_data(image_data, labels, filepath):\n    \"\"\"\n    Save image data and labels as TFRecord files.\n    \"\"\"\n    with tf.io.TFRecordWriter(filepath) as writer:\n        for image, label in zip(image_data, labels):\n            serialized_example = serialize_example(image, label)\n            writer.write(serialized_example)\n\ndef parse_tfrecord(serialized_example):\n    \"\"\"\n    Parse a single example from TFRecord.\n    \"\"\"\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    parsed_example = tf.io.parse_single_example(serialized_example, feature_description)\n    image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n    label = parsed_example['label']\n    return image, label\n\ndef loading_tensor_data(filepath):\n    \"\"\"\n    Load image data and labels from TFRecord files.\n    \"\"\"\n    image_data = []\n    labels = []\n    for serialized_example in tf.data.TFRecordDataset(filepath):\n        image, label = parse_tfrecord(serialized_example)\n        image_data.append(image)\n        labels.append(label)\n    return image_data, labels\n\n# Assuming you have loaded your image data and labels into X and y arrays\n\n# Split the data into train, validation, and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n\n# Save your data to TFRecords\nsaving_tensor_data(X_train, y_train, f'gs://{bucket_name}/train.tfrecord')\nsaving_tensor_data(X_val, y_val, f'gs://{bucket_name}/val.tfrecord')\nsaving_tensor_data(X_test, y_test, f'gs://{bucket_name}/test.tfrecord')\n\n# Load your data from TFRecords\ntrain_filenames = tf.io.gfile.glob(f\"gs://{bucket_name}/train.tfrecord\")\nval_filenames = tf.io.gfile.glob(f\"gs://{bucket_name}/val.tfrecord\")\ntest_filenames = tf.io.gfile.glob(f\"gs://{bucket_name}/test.tfrecord\")\n\n# Create TensorFlow datasets\ntrain_dataset = tf.data.TFRecordDataset(train_filenames, num_parallel_reads=AUTO)\nval_dataset = tf.data.TFRecordDataset(val_filenames, num_parallel_reads=AUTO)\ntest_dataset = tf.data.TFRecordDataset(test_filenames, num_parallel_reads=AUTO)\n\n# Parse TFRecord examples\ndef parse_tfrecord_example(example):\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    parsed_example = tf.io.parse_single_example(example, feature_description)\n    image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n    label = parsed_example['label']\n    return image, label\n\n# Apply parsing function to datasets\ntrain_dataset = train_dataset.map(parse_tfrecord_example, num_parallel_calls=AUTO)\nval_dataset = val_dataset.map(parse_tfrecord_example, num_parallel_calls=AUTO)\ntest_dataset = test_dataset.map(parse_tfrecord_example, num_parallel_calls=AUTO)\n\n# Shuffle and batch datasets\nBATCH_SIZE = 32\ntrain_dataset = train_dataset.shuffle(1000).batch(BATCH_SIZE).prefetch(AUTO)\nval_dataset = val_dataset.batch(BATCH_SIZE).prefetch(AUTO)\ntest_dataset = test_dataset.batch(BATCH_SIZE).prefetch(AUTO)\n\n# Now you can use these datasets for training your model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf      \nfrom sklearn.model_selection import train_test_split\n\nAUTO = tf.data.experimental.AUTOTUNE\n\n# Load your data from TFRecords\ntrain_filenames = tf.io.gfile.glob(f\"gs://{bucket_name}/train.tfrecord\")\nval_filenames = tf.io.gfile.glob(f\"gs://{bucket_name}/val.tfrecord\")\ntest_filenames = tf.io.gfile.glob(f\"gs://{bucket_name}/test.tfrecord\")\n\n# Create TensorFlow datasets\ntrain_dataset = tf.data.TFRecordDataset(train_filenames, num_parallel_reads=AUTO)\nval_dataset = tf.data.TFRecordDataset(val_filenames, num_parallel_reads=AUTO)\ntest_dataset = tf.data.TFRecordDataset(test_filenames, num_parallel_reads=AUTO)\n\n# Parse TFRecord examples\ndef parse_tfrecord_example(example):\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    parsed_example = tf.io.parse_single_example(example, feature_description)\n    image = tf.io.parse_tensor(parsed_example['image'], out_type=tf.float32)\n    label = parsed_example['label']\n    return image, label\n\n# Apply parsing function to datasets\ntrain_dataset = train_dataset.map(parse_tfrecord_example, num_parallel_calls=AUTO)\nval_dataset = val_dataset.map(parse_tfrecord_example, num_parallel_calls=AUTO)\ntest_dataset = test_dataset.map(parse_tfrecord_example, num_parallel_calls=AUTO)\n\n# Shuffle and batch datasets\nBATCH_SIZE = 32\ntrain_dataset = train_dataset.shuffle(1000).batch(BATCH_SIZE).prefetch(AUTO)\nval_dataset = val_dataset.batch(BATCH_SIZE).prefetch(AUTO)\ntest_dataset = test_dataset.batch(BATCH_SIZE).prefetch(AUTO)\n\n# Now you can use these datasets for training your model\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"/kaggle/input/asv-spoof-2019-chroma/image_data_chroma.tfrecord\"","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:42:10.469104Z","iopub.execute_input":"2024-05-05T11:42:10.470145Z","iopub.status.idle":"2024-05-05T11:42:10.474043Z","shell.execute_reply.started":"2024-05-05T11:42:10.470092Z","shell.execute_reply":"2024-05-05T11:42:10.473164Z"},"trusted":true},"execution_count":9,"outputs":[]},{"cell_type":"code","source":"# Function to parse TFRecords\ndef parse_tfrecord_fn(example):\n    feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    example = tf.io.parse_single_example(example, feature_description)\n    image = tf.io.parse_tensor(example['image'], out_type=tf.float32)\n    label = example['label']\n    return image, label\n\n# Load TFRecord dataset\ndef load_dataset(filenames):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(parse_tfrecord_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:42:12.772828Z","iopub.execute_input":"2024-05-05T11:42:12.773642Z","iopub.status.idle":"2024-05-05T11:42:12.779221Z","shell.execute_reply.started":"2024-05-05T11:42:12.773588Z","shell.execute_reply":"2024-05-05T11:42:12.778285Z"},"trusted":true},"execution_count":10,"outputs":[]},{"cell_type":"code","source":"# Load all filenames\nfilenames = tf.io.gfile.glob(data_dir)\n\n# Split filenames into train, validation, and test sets\ntrain_size = int(0.6 * len(filenames))\nval_size = int(0.2 * len(filenames))\ntest_size = len(filenames) - train_size - val_size\ntrain_filenames = filenames[:train_size]\nval_filenames = filenames[train_size:train_size+val_size]\ntest_filenames = filenames[train_size+val_size:]\n\n# Create TensorFlow datasets\ntrain_dataset = load_dataset(train_filenames)\nval_dataset = load_dataset(val_filenames)\ntest_dataset = load_dataset(test_filenames)\n\n# Shuffle and batch datasets\nBATCH_SIZE = 32 * strategy.num_replicas_in_sync\ntrain_dataset = train_dataset.shuffle(1000).batch(BATCH_SIZE).prefetch(tf.data.experimental.AUTOTUNE)\nval_dataset = val_dataset.batch(BATCH_SIZE).prefetch(tf.data.experimental.AUTOTUNE)\ntest_dataset = test_dataset.batch(BATCH_SIZE).prefetch(tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:42:15.882508Z","iopub.execute_input":"2024-05-05T11:42:15.883323Z","iopub.status.idle":"2024-05-05T11:42:16.019387Z","shell.execute_reply.started":"2024-05-05T11:42:15.883283Z","shell.execute_reply":"2024-05-05T11:42:16.018412Z"},"trusted":true},"execution_count":11,"outputs":[]},{"cell_type":"code","source":"def create_vgg_model(input_shape):\n    vgg_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)\n\n    model = Sequential()\n\n    for layer in vgg_model.layers:\n        model.add(layer)\n\n    for layer in model.layers:\n        layer.trainable = False\n\n    model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    model.add(Flatten())\n    model.add(Dense(units=128, activation='relu'))\n\n    # Output layer for binary classification with 'sigmoid' activation\n    model.add(Dense(units=1, activation='sigmoid'))\n\n    model.summary()\n    return model\n\n# with strategy.scope():\n#     input_shape = (224, 224, 3)\n#     model_vgg = create_vgg_model(input_shape)\n#     model_vgg.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0001), metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:55:57.935777Z","iopub.execute_input":"2024-05-05T11:55:57.936152Z","iopub.status.idle":"2024-05-05T11:55:59.012352Z","shell.execute_reply.started":"2024-05-05T11:55:57.936122Z","shell.execute_reply":"2024-05-05T11:55:59.011169Z"},"trusted":true},"execution_count":19,"outputs":[{"output_type":"display_data","data":{"text/plain":"\u001b[1mModel: \"sequential_1\"\u001b[0m\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"sequential_1\"</span>\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n┃\u001b[1m \u001b[0m\u001b[1mLayer (type)                   \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape          \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m      Param #\u001b[0m\u001b[1m \u001b[0m┃\n┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n│ block1_conv1 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m224\u001b[0m, \u001b[38;5;34m224\u001b[0m, \u001b[38;5;34m64\u001b[0m)   │         \u001b[38;5;34m1,792\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block1_conv2 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m224\u001b[0m, \u001b[38;5;34m224\u001b[0m, \u001b[38;5;34m64\u001b[0m)   │        \u001b[38;5;34m36,928\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block1_pool (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m112\u001b[0m, \u001b[38;5;34m112\u001b[0m, \u001b[38;5;34m64\u001b[0m)   │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block2_conv1 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m112\u001b[0m, \u001b[38;5;34m112\u001b[0m, \u001b[38;5;34m128\u001b[0m)  │        \u001b[38;5;34m73,856\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block2_conv2 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m112\u001b[0m, \u001b[38;5;34m112\u001b[0m, \u001b[38;5;34m128\u001b[0m)  │       \u001b[38;5;34m147,584\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block2_pool (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m128\u001b[0m)    │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv1 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m256\u001b[0m)    │       \u001b[38;5;34m295,168\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv2 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m256\u001b[0m)    │       \u001b[38;5;34m590,080\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv3 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m256\u001b[0m)    │       \u001b[38;5;34m590,080\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv4 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m56\u001b[0m, \u001b[38;5;34m256\u001b[0m)    │       \u001b[38;5;34m590,080\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_pool (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m256\u001b[0m)    │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv1 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m1,180,160\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv2 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv3 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv4 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m28\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_pool (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv1 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv2 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv3 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv4 (\u001b[38;5;33mConv2D\u001b[0m)           │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m14\u001b[0m, \u001b[38;5;34m512\u001b[0m)    │     \u001b[38;5;34m2,359,808\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_pool (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m7\u001b[0m, \u001b[38;5;34m7\u001b[0m, \u001b[38;5;34m512\u001b[0m)      │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ conv2d_2 (\u001b[38;5;33mConv2D\u001b[0m)               │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m5\u001b[0m, \u001b[38;5;34m5\u001b[0m, \u001b[38;5;34m64\u001b[0m)       │       \u001b[38;5;34m294,976\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ max_pooling2d_2 (\u001b[38;5;33mMaxPooling2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m2\u001b[0m, \u001b[38;5;34m2\u001b[0m, \u001b[38;5;34m64\u001b[0m)       │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dropout_2 (\u001b[38;5;33mDropout\u001b[0m)             │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m2\u001b[0m, \u001b[38;5;34m2\u001b[0m, \u001b[38;5;34m64\u001b[0m)       │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ conv2d_3 (\u001b[38;5;33mConv2D\u001b[0m)               │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m2\u001b[0m, \u001b[38;5;34m2\u001b[0m, \u001b[38;5;34m128\u001b[0m)      │        \u001b[38;5;34m73,856\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ max_pooling2d_3 (\u001b[38;5;33mMaxPooling2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m128\u001b[0m)      │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dropout_3 (\u001b[38;5;33mDropout\u001b[0m)             │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m128\u001b[0m)      │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ flatten_1 (\u001b[38;5;33mFlatten\u001b[0m)             │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m128\u001b[0m)            │             \u001b[38;5;34m0\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dense_2 (\u001b[38;5;33mDense\u001b[0m)                 │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m128\u001b[0m)            │        \u001b[38;5;34m16,512\u001b[0m │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dense_3 (\u001b[38;5;33mDense\u001b[0m)                 │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m)              │           \u001b[38;5;34m129\u001b[0m │\n└─────────────────────────────────┴────────────────────────┴───────────────┘\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n┃<span style=\"font-weight: bold\"> Layer (type)                    </span>┃<span style=\"font-weight: bold\"> Output Shape           </span>┃<span style=\"font-weight: bold\">       Param # </span>┃\n┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n│ block1_conv1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">224</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">224</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)   │         <span style=\"color: #00af00; text-decoration-color: #00af00\">1,792</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block1_conv2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">224</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">224</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)   │        <span style=\"color: #00af00; text-decoration-color: #00af00\">36,928</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block1_pool (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">112</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">112</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)   │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block2_conv1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">112</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">112</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">73,856</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block2_conv2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">112</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">112</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)  │       <span style=\"color: #00af00; text-decoration-color: #00af00\">147,584</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block2_pool (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)    │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)    │       <span style=\"color: #00af00; text-decoration-color: #00af00\">295,168</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)    │       <span style=\"color: #00af00; text-decoration-color: #00af00\">590,080</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)    │       <span style=\"color: #00af00; text-decoration-color: #00af00\">590,080</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_conv4 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">56</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)    │       <span style=\"color: #00af00; text-decoration-color: #00af00\">590,080</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block3_pool (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)    │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">1,180,160</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_conv4 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">28</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block4_pool (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_conv4 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)           │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">14</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)    │     <span style=\"color: #00af00; text-decoration-color: #00af00\">2,359,808</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ block5_pool (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">7</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">7</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)      │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ conv2d_2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)               │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">5</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">5</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)       │       <span style=\"color: #00af00; text-decoration-color: #00af00\">294,976</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ max_pooling2d_2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">2</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">2</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)       │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dropout_2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dropout</span>)             │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">2</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">2</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)       │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ conv2d_3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)               │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">2</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">2</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)      │        <span style=\"color: #00af00; text-decoration-color: #00af00\">73,856</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ max_pooling2d_3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)      │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dropout_3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dropout</span>)             │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)      │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ flatten_1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Flatten</span>)             │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)            │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dense_2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                 │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)            │        <span style=\"color: #00af00; text-decoration-color: #00af00\">16,512</span> │\n├─────────────────────────────────┼────────────────────────┼───────────────┤\n│ dense_3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                 │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>)              │           <span style=\"color: #00af00; text-decoration-color: #00af00\">129</span> │\n└─────────────────────────────────┴────────────────────────┴───────────────┘\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"\u001b[1m Total params: \u001b[0m\u001b[38;5;34m20,409,857\u001b[0m (77.86 MB)\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">20,409,857</span> (77.86 MB)\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m385,473\u001b[0m (1.47 MB)\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">385,473</span> (1.47 MB)\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m20,024,384\u001b[0m (76.39 MB)\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">20,024,384</span> (76.39 MB)\n</pre>\n"},"metadata":{}}]},{"cell_type":"code","source":"with strategy.scope():\n    model = Sequential([\n        Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),\n        MaxPooling2D((3, 3)),\n        BatchNormalization(),\n        Conv2D(64, (3, 3), activation='relu'),\n        MaxPooling2D((3, 3)),\n        BatchNormalization(),\n        Conv2D(128, (3, 3), activation='relu'),\n        MaxPooling2D((3, 3)),\n        Dropout(0.2),\n        Flatten(),\n        Dense(128, activation='relu'),\n        Dropout(0.5),\n        BatchNormalization(),\n        Dense(64, activation='relu'),\n        Dropout(0.5),\n        Dense(2, activation='softmax')\n    ])\n    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(train_dataset.batch(64), epochs=10, validation_data=val_dataset.batch(64))\n\n# # Evaluate on test data\n# test_loss, test_accuracy = model.evaluate(test_dataset.batch(64))\n# print(\"Test accuracy:\" test_accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T12:00:34.546318Z","iopub.execute_input":"2024-05-05T12:00:34.547372Z","iopub.status.idle":"2024-05-05T12:00:35.402945Z","shell.execute_reply.started":"2024-05-05T12:00:34.547329Z","shell.execute_reply":"2024-05-05T12:00:35.40165Z"},"trusted":true},"execution_count":27,"outputs":[{"name":"stdout","text":"Epoch 1/10\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)","Cell \u001b[0;32mIn[27], line 23\u001b[0m\n\u001b[1;32m     20\u001b[0m     model\u001b[38;5;241m.\u001b[39mcompile(optimizer\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124madam\u001b[39m\u001b[38;5;124m'\u001b[39m, loss\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msparse_categorical_crossentropy\u001b[39m\u001b[38;5;124m'\u001b[39m, metrics\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maccuracy\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m     22\u001b[0m \u001b[38;5;66;03m# Train the model\u001b[39;00m\n\u001b[0;32m---> 23\u001b[0m history \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrain_dataset\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbatch\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m64\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_dataset\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbatch\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m64\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     25\u001b[0m \u001b[38;5;66;03m# # Evaluate on test data\u001b[39;00m\n\u001b[1;32m     26\u001b[0m \u001b[38;5;66;03m# test_loss, test_accuracy = model.evaluate(test_dataset.batch(64))\u001b[39;00m\n\u001b[1;32m     27\u001b[0m \u001b[38;5;66;03m# print(\"Test accuracy:\" test_accuracy)\u001b[39;00m\n","File \u001b[0;32m/usr/local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    119\u001b[0m     filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n\u001b[1;32m    120\u001b[0m     \u001b[38;5;66;03m# To get the full stack trace, call:\u001b[39;00m\n\u001b[1;32m    121\u001b[0m     \u001b[38;5;66;03m# `keras.config.disable_traceback_filtering()`\u001b[39;00m\n\u001b[0;32m--> 122\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(filtered_tb) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    123\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m    124\u001b[0m     \u001b[38;5;28;01mdel\u001b[39;00m filtered_tb\n","File \u001b[0;32m/usr/local/lib/python3.10/site-packages/keras/src/backend/tensorflow/trainer.py:118\u001b[0m, in \u001b[0;36mTensorFlowTrainer.make_train_function.<locals>.one_step_on_iterator\u001b[0;34m(iterator)\u001b[0m\n\u001b[1;32m    116\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Runs a single training step given a Dataset iterator.\"\"\"\u001b[39;00m\n\u001b[1;32m    117\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mnext\u001b[39m(iterator)\n\u001b[0;32m--> 118\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdistribute_strategy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    119\u001b[0m \u001b[43m    \u001b[49m\u001b[43mone_step_on_data\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    120\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    121\u001b[0m outputs \u001b[38;5;241m=\u001b[39m reduce_per_replica(\n\u001b[1;32m    122\u001b[0m     outputs,\n\u001b[1;32m    123\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdistribute_strategy,\n\u001b[1;32m    124\u001b[0m     reduction\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdistribute_reduction_method,\n\u001b[1;32m    125\u001b[0m )\n\u001b[1;32m    126\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m outputs\n","\u001b[0;31mValueError\u001b[0m: input tensor Tensor(\"IteratorGetNext:0\", dtype=float32) to TPUStrategy.run() has unknown rank, which is not allowed"],"ename":"ValueError","evalue":"input tensor Tensor(\"IteratorGetNext:0\", dtype=float32) to TPUStrategy.run() has unknown rank, which is not allowed","output_type":"error"}]},{"cell_type":"code","source":"# Function to parse TFRecords\ndef parse_tfrecord_fn(example):\n    feature_description = {  \n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64)\n    }\n    example = tf.io.parse_single_example(example, feature_description)\n    image = tf.io.parse_tensor(example['image'], out_type=tf.float32)\n    label = example['label']\n    return image, label\n\n# Load TFRecord dataset\ndef load_dataset(filenames):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(parse_tfrecord_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:50:40.141173Z","iopub.execute_input":"2024-05-05T11:50:40.141603Z","iopub.status.idle":"2024-05-05T11:50:40.148261Z","shell.execute_reply.started":"2024-05-05T11:50:40.141567Z","shell.execute_reply":"2024-05-05T11:50:40.147345Z"},"trusted":true},"execution_count":14,"outputs":[]},{"cell_type":"code","source":"# Load all filenames\nfilenames = tf.io.gfile.glob(data_dir)\n\n# Split filenames into train, validation, and test sets\ntrain_size = int(0.6 * len(filenames))\nval_size = int(0.2 * len(filenames))\ntest_size = len(filenames) - train_size - val_size\ntrain_filenames = filenames[:train_size]\nval_filenames = filenames[train_size:train_size+val_size]\ntest_filenames = filenames[train_size+val_size:]\n\n# Create TensorFlow datasets\ntrain_dataset = load_dataset(train_filenames)\nval_dataset = load_dataset(val_filenames)\ntest_dataset = load_dataset(test_filenames)\n\n# Shuffle and batch datasets  \nBATCH_SIZE = 32 * strategy.num_replicas_in_sync\ntrain_dataset = train_dataset.repeat().shuffle(1000).batch(BATCH_SIZE).prefetch(tf.data.experimental.AUTOTUNE)\nval_dataset = val_dataset.batch(BATCH_SIZE).prefetch(tf.data.experimental.AUTOTUNE)\ntest_dataset = test_dataset.batch(BATCH_SIZE).prefetch(tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T11:55:41.304937Z","iopub.execute_input":"2024-05-05T11:55:41.305944Z","iopub.status.idle":"2024-05-05T11:55:41.384895Z","shell.execute_reply.started":"2024-05-05T11:55:41.305906Z","shell.execute_reply":"2024-05-05T11:55:41.383771Z"},"trusted":true},"execution_count":18,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}