{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8033468,"sourceType":"datasetVersion","datasetId":4735360}],"dockerImageVersionId":30683,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport glob\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom tensorflow.keras import layers, models\n\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import NASNetMobile , VGG19\nfrom tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D, concatenate, LSTM, Bidirectional, Reshape, Dropout\nfrom tensorflow.keras.models import Model\n\nfrom sklearn.preprocessing import LabelBinarizer\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import roc_auc_score, roc_curve, auc\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-22T07:20:44.799110Z","iopub.execute_input":"2024-04-22T07:20:44.799473Z","iopub.status.idle":"2024-04-22T07:21:05.101453Z","shell.execute_reply.started":"2024-04-22T07:20:44.799443Z","shell.execute_reply":"2024-04-22T07:21:05.100562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Base directory where the folders are stored\nimage_folder = \"/kaggle/input/birdclef-2024-mel-spectrograms/train_images/\"\n\n# Mapping of classes to numerical labels\n# Classes are alphabetically sorted - so we can easily restore class order when we load\nclass_labels = {class_name: i for i, class_name in enumerate(sorted(os.listdir(image_folder)))}\nnum_classes = len(class_labels)\n\n# Collect all file paths and their corresponding class labels\nfile_paths = []\nlabels = []\n\n# New structure to keep track of groups\nsamples = {}\n\nfor class_name in os.listdir(image_folder):\n    class_dir = os.path.join(image_folder, class_name)\n    for filename in os.listdir(class_dir):\n        # Extract base sample name from filename (files split at \"_\")\n        sample_base = filename.split('_')[0] \n        full_path = os.path.join(class_dir, filename)\n        \n        if sample_base not in samples:\n            samples[sample_base] = {'files': [], 'label': class_labels[class_name]}\n        samples[sample_base]['files'].append(full_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:31.135626Z","iopub.execute_input":"2024-04-22T07:21:31.136261Z","iopub.status.idle":"2024-04-22T07:21:36.694448Z","shell.execute_reply.started":"2024-04-22T07:21:31.136216Z","shell.execute_reply":"2024-04-22T07:21:36.693484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples[sample_base]['files'][:1]","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:36.696053Z","iopub.execute_input":"2024-04-22T07:21:36.696423Z","iopub.status.idle":"2024-04-22T07:21:36.703862Z","shell.execute_reply.started":"2024-04-22T07:21:36.696389Z","shell.execute_reply":"2024-04-22T07:21:36.703005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_size = 0.2\nbatch_size = 32\n\n# Convert the samples dictionary into a list for splitting\nsamples_list = list(samples.items())\n\n# Split the list of tuples into training and validation sets\ntrain_samples, val_samples = train_test_split(samples_list, test_size=test_size, random_state=42)\n\ndef preprocess_image(file_path, label):\n    img = tf.io.read_file(file_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.random_contrast(img, lower=0.2, upper=1.8)\n    img = tf.image.random_brightness(img, max_delta=0.2)\n\n    #scale 0-1\n    img = tf.cast(img, tf.float32)\n\n    #scale -1 to +1\n#     img = tf.cast(img, tf.float32) / 255.0  # Normalize to [0, 1]\n    img = (img / 127.5) - 1.0  # Scale to [-1, +1]\n    \n    label = tf.one_hot(label, depth=num_classes)\n    return img,label\n\n# Function to extract file paths and labels from the split samples\ndef extract_files_and_labels(sample_list):\n    file_paths = []\n    labels = []\n    for _, sample_info in sample_list:\n        file_paths.extend(sample_info['files'])\n        labels.extend([sample_info['label']] * len(sample_info['files']))\n    return file_paths, labels\n\ntrain_files, train_labels = extract_files_and_labels(train_samples)\nval_files, val_labels = extract_files_and_labels(val_samples)\n\nnum_classes = np.max(train_labels) + 1\n\n# Create a tf.data.Dataset from file paths and labels\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_files, train_labels))\ntrain_dataset = train_dataset.map(preprocess_image, num_parallel_calls=AUTOTUNE)\ntrain_dataset = train_dataset.shuffle(buffer_size=1000).batch(batch_size).prefetch(AUTOTUNE)\n\nval_dataset = tf.data.Dataset.from_tensor_slices((val_files, val_labels))\nval_dataset = val_dataset.map(preprocess_image, num_parallel_calls=AUTOTUNE)\nval_dataset = val_dataset.batch(batch_size).prefetch(AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:39.790657Z","iopub.execute_input":"2024-04-22T07:21:39.791342Z","iopub.status.idle":"2024-04-22T07:21:41.027889Z","shell.execute_reply.started":"2024-04-22T07:21:39.791308Z","shell.execute_reply":"2024-04-22T07:21:41.027051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# Define CRNN model with VGG-19 base\ndef create_crnn_model(input_shape, num_classes):\n    # Load pre-trained VGG-19 model without the fully connected layers\n    vgg_base = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)\n    \n    # Freeze the convolutional layers\n    for layer in vgg_base.layers:\n        layer.trainable = False\n    \n    # Extract features using VGG-19\n    vgg_output = vgg_base.output\n    \n    # Average Pooling\n    avg_pooling = layers.GlobalAveragePooling2D()(vgg_output)\n    # Convert features to sequence\n#     reshaped_output = layers.Reshape((-1, 512))(vgg_output)\n    \n#     # Recurrent layers\n#     lstm_layer1 = layers.LSTM(64, return_sequences=True)(reshaped_output)\n#     lstm_layer2 = layers.LSTM(64)(lstm_layer1)\n\n    # Dense layers\n    dense_layer1 = layers.Dense(512, activation='relu')(avg_pooling)\n    output_layer = layers.Dense(num_classes, activation='softmax')(dense_layer1)\n    \n    # Create model\n    model = models.Model(inputs=vgg_base.input, outputs=output_layer)\n    \n    return model\n\n# Define input shape and number of classes\ninput_shape = (244, 244, 3)  # Adjust according to your spectrogram shape\nnum_classes = 182  # Example number of classes\n\n# Create CRNN model with VGG-19 base\nmodel = create_crnn_model(input_shape, num_classes)\n\n# Compile model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Print model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:46.237146Z","iopub.execute_input":"2024-04-22T07:21:46.237502Z","iopub.status.idle":"2024-04-22T07:21:47.227859Z","shell.execute_reply.started":"2024-04-22T07:21:46.237472Z","shell.execute_reply":"2024-04-22T07:21:47.226923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import LearningRateScheduler\n\n# Define your custom learning rate scheduler function\ndef scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return float( lr * tf.math.exp(-0.1))\n\n# Create the LearningRateScheduler callback\nlr_scheduler = LearningRateScheduler(scheduler)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:52.070631Z","iopub.execute_input":"2024-04-22T07:21:52.071408Z","iopub.status.idle":"2024-04-22T07:21:52.076546Z","shell.execute_reply.started":"2024-04-22T07:21:52.071372Z","shell.execute_reply":"2024-04-22T07:21:52.075515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\n# Define input shape for NASNetMobile\ninput_shape_nasnet = (224, 224, 3)\n\n# Create input for NASNetMobile\ninput_data = Input(shape=input_shape_nasnet, name='input1')\n\n# # Flatten the convolutional layers\n# x2 = layers.Flatten()(input_data)\n\n# Convolutional layers\nx = layers.Conv2D(32, (3, 3), activation='relu')(input_data)\nx = layers.MaxPooling2D((2, 2))(x)\nx = layers.Conv2D(64, (3, 3), activation='relu')(x)\nx = layers.MaxPooling2D((2, 2))(x)\nx = layers.Conv2D(128, (3, 3), activation='relu')(x)\nx = layers.MaxPooling2D((2, 2))(x)\nx = layers.Conv2D(128, (4, 4), activation='relu')(x)\nx = layers.MaxPooling2D((2, 2))(x)\nx = layers.Conv2D(128, (3, 3), activation='relu')(x)\nx = layers.MaxPooling2D((2, 2))(x)\n\n# Reshape for recurrent layer\nx = layers.Reshape((-1, 128))(x)\n\n#     # Recurrent layers\n#     model.add(layers.LSTM(64, return_sequences=True))\n#     model.add(layers.LSTM(64))\n\n# Flatten the convolutional layers\nx = layers.Flatten()(x)\n\n# Dense layers\nx = layers.Dense(1024, activation='relu')(x)\nx = layers.Dropout(0.5)(x)\n# # Define the second dense layer\n# x2 = layers.Dense(num_classes, activation='sigmoid')(x)\n\n# Connect the two output layers\n# concatenated_output = layers.concatenate([x1, x2])\n\n# Define the second dense layer\nx = layers.Dense(num_classes, activation='sigmoid')(x)\n\n# Create model\nmodel = models.Model(inputs=input_data, outputs=x)\n\n# Define your custom learning rate\ncustom_learning_rate = 0.001  # Adjust this value according to your needs\n\n# Create an optimizer with the custom learning rate\noptimizer = Adam(learning_rate=custom_learning_rate)\n\n# Compile model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Print model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:54.223628Z","iopub.execute_input":"2024-04-22T07:21:54.224424Z","iopub.status.idle":"2024-04-22T07:21:54.335205Z","shell.execute_reply.started":"2024-04-22T07:21:54.224393Z","shell.execute_reply":"2024-04-22T07:21:54.334265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Define input shape\n# input_shape = (224, 224, 3)\n\n# # Create the model\n# model = models.Sequential()\n\n# # Add convolutional layers\n# model.add(layers.Conv2D(32, (3, 3), strides=(2, 2), padding='same', input_shape=input_shape))\n# model.add(layers.BatchNormalization())\n# model.add(layers.Activation('relu'))\n\n# model.add(layers.Conv2D(64, (3, 3), strides=(2, 2), padding='same'))\n# model.add(layers.BatchNormalization())\n# model.add(layers.Activation('relu'))\n\n# model.add(layers.Conv2D(128, (3, 3), strides=(2, 2), padding='same'))\n# model.add(layers.BatchNormalization())\n# model.add(layers.Activation('relu'))\n\n# # Add max-pooling and dropout layers\n# model.add(layers.MaxPooling2D((2, 2)))\n# model.add(layers.Dropout(0.2))\n\n# model.add(layers.MaxPooling2D((2, 2)))\n# model.add(layers.Dropout(0.2))\n\n# model.add(layers.MaxPooling2D((2, 2)))\n# model.add(layers.Dropout(0.2))\n\n# # Add global average pooling\n# model.add(layers.GlobalAveragePooling2D())\n\n# # Add dense layers\n# model.add(layers.Dense(512, activation='relu'))\n# model.add(layers.Dropout(0.2))\n\n# # Output layer\n# model.add(layers.Dense(182, activation='softmax'))\n\n# # Compile the model\n# model.compile(optimizer='adam',\n#               loss='categorical_crossentropy',\n#               metrics=['accuracy'])\n\n# # Print model summary\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:55.935653Z","iopub.execute_input":"2024-04-22T07:21:55.936003Z","iopub.status.idle":"2024-04-22T07:21:55.941509Z","shell.execute_reply.started":"2024-04-22T07:21:55.935975Z","shell.execute_reply":"2024-04-22T07:21:55.940383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turn off all warnings except for errors\ntf.get_logger().setLevel('ERROR')\n\n# Fit the model with callbacks\nhistory = model.fit(train_dataset, \n                                     epochs=30,\n                                     batch_size=1,\n                                     validation_data=val_dataset,\n                                     callbacks=[lr_scheduler]\n                                    )\n\n\n# Evaluate model\ntest_loss, test_acc = model.evaluate(val_dataset)\nprint('Test accuracy:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:21:56.870391Z","iopub.execute_input":"2024-04-22T07:21:56.870982Z","iopub.status.idle":"2024-04-22T07:33:25.637296Z","shell.execute_reply.started":"2024-04-22T07:21:56.870952Z","shell.execute_reply":"2024-04-22T07:33:25.636314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get predictions on validation data\nval_predictions = model.predict(val_dataset)\n\n# Convert tensors to numpy arrays\nval_labels_np = np.concatenate([y.numpy() for x, y in val_dataset], axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:33:25.639163Z","iopub.execute_input":"2024-04-22T07:33:25.639621Z","iopub.status.idle":"2024-04-22T07:33:32.395666Z","shell.execute_reply.started":"2024-04-22T07:33:25.639585Z","shell.execute_reply":"2024-04-22T07:33:32.394558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_labels_np , val_predictions","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:34:39.848183Z","iopub.execute_input":"2024-04-22T07:34:39.849011Z","iopub.status.idle":"2024-04-22T07:34:39.857284Z","shell.execute_reply.started":"2024-04-22T07:34:39.848977Z","shell.execute_reply":"2024-04-22T07:34:39.856276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\n# Assuming val_labels_np and val_predictions are your validation labels and predictions respectively\n# Convert the predicted probabilities to class labels by taking the argmax along axis 1\npredicted_classes = val_predictions.argmax(axis=1)\n\n# Convert one-hot encoded labels to class labels by taking the argmax along axis 1\ntrue_classes = val_labels_np.argmax(axis=1)\n\n# Compute accuracy\naccuracy = accuracy_score(true_classes, predicted_classes)\nprint(\"Accuracy:\", accuracy)\n\n# Compute precision\nprecision = precision_score(true_classes, predicted_classes, average='weighted')\nprint(\"Precision:\", precision)\n\n# Compute recall\nrecall = recall_score(true_classes, predicted_classes, average='weighted')\nprint(\"Recall:\", recall)\n\n# Compute F1-score\nf1 = f1_score(true_classes, predicted_classes, average='weighted')\nprint(\"F1-score:\", f1)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:34:46.628912Z","iopub.execute_input":"2024-04-22T07:34:46.629257Z","iopub.status.idle":"2024-04-22T07:34:46.672952Z","shell.execute_reply.started":"2024-04-22T07:34:46.629219Z","shell.execute_reply":"2024-04-22T07:34:46.672090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import roc_curve, auc\nfrom sklearn.preprocessing import label_binarize\n\n# Assuming val_labels_np and val_predictions are your validation labels and predictions respectively\n# Convert the one-hot encoded labels to binary format\ntrue_labels = np.argmax(val_labels_np, axis=1)\n# Binarize the true labels\ntrue_labels_binarized = label_binarize(true_labels, classes=np.arange(182))\n\n# Compute the ROC curve and ROC area for each class\nfpr = dict()\ntpr = dict()\nroc_auc = dict()\nfor i in range(182):\n    fpr[i], tpr[i], _ = roc_curve(true_labels_binarized[:, i], val_predictions[:, i])\n    if np.sum(true_labels_binarized[:, i]) > 0:  # Check if there are positive samples for the class\n        roc_auc[i] = auc(fpr[i], tpr[i])\n\n# Plot ROC curve for each class\nplt.figure(figsize=(8, 6))\nfor i in range(182):\n    if i in roc_auc:  # Plot only if ROC AUC score is available\n        plt.plot(fpr[i], tpr[i], label=f'Class {i} (AUC = {roc_auc[i]:.2f})')\n\nplt.plot([0, 1], [0, 1], 'k--', label='Random')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('ROC Curve for Multiclass Classification (One-vs-Rest)')\nplt.legend(loc='lower right')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:34:48.489187Z","iopub.execute_input":"2024-04-22T07:34:48.489801Z","iopub.status.idle":"2024-04-22T07:34:51.080753Z","shell.execute_reply.started":"2024-04-22T07:34:48.489769Z","shell.execute_reply":"2024-04-22T07:34:51.079860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate mean ROC AUC score\nmean_roc_auc = np.mean(list(roc_auc.values()))\nprint(\"Mean ROC AUC Score:\", mean_roc_auc)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:35:03.966098Z","iopub.execute_input":"2024-04-22T07:35:03.966463Z","iopub.status.idle":"2024-04-22T07:35:03.971556Z","shell.execute_reply.started":"2024-04-22T07:35:03.966436Z","shell.execute_reply":"2024-04-22T07:35:03.970635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Base directory where the folders are stored\nimage_folder = \"/kaggle/input/birdclef-2024/unlabeled_soundscapes\"\n\n# Mapping of classes to numerical labels\n# Classes are alphabetically sorted - so we can easily restore class order when we load\nclass_labels = {class_name: i for i, class_name in enumerate(sorted(os.listdir(image_folder)))}\nnum_classes = len(class_labels)\n\n# Collect all file paths and their corresponding class labels\nfile_paths = []\nlabels = []\n\nfor class_name in os.listdir(image_folder):\n    class_dir = os.path.join(image_folder, class_name)\n   \n    # Extract base sample name from filename (files split at \"_\")\n    sample_base = filename.split('_')[0] \n    file_paths.append(class_dir)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:35:08.200111Z","iopub.execute_input":"2024-04-22T07:35:08.200719Z","iopub.status.idle":"2024-04-22T07:35:08.432573Z","shell.execute_reply.started":"2024-04-22T07:35:08.200684Z","shell.execute_reply":"2024-04-22T07:35:08.431635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\nimport io\nfrom PIL import Image\n\n# Function to preprocess OGG audio file into an image\n@tf.autograph.experimental.do_not_convert\ndef preprocess_audio_to_image(file_path):\n    # Ensure file_path is a Python string, not a TensorFlow tensor\n    file_path = file_path.numpy().decode('utf-8')\n\n    # Load the audio file\n    y, sr = librosa.load(file_path, sr=None)\n\n    # Generate the spectrogram\n    spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)\n    spectrogram_db = librosa.power_to_db(spectrogram, ref=np.max)\n\n    # Plot the spectrogram\n    plt.figure(figsize=(10, 4))\n    librosa.display.specshow(spectrogram_db, sr=sr, x_axis='time', y_axis='mel')\n    plt.colorbar(format='%+2.0f dB')\n    plt.title('Mel Spectrogram')\n    plt.tight_layout()\n\n    # Save the plot to a buffer\n    buf = io.BytesIO()\n    plt.savefig(buf, format='png', bbox_inches='tight')\n    buf.seek(0)\n\n    # Open the saved image from the buffer\n    image = Image.open(buf)\n    image = image.convert(\"RGB\")\n    # Resize the image\n    desired_size = (224, 224)  # Desired size for resizing\n    image = image.resize(desired_size)\n#     image = np.expand_dims(np.array(image), axis=0)\n\n    # Convert the image to a TensorFlow tensor and normalize it\n    image = tf.convert_to_tensor(np.array(image), dtype=tf.float32) \n    \n    img = (image / 127.5) - 1.0 \n    \n    # Close the plot\n    plt.close()\n    image = tf.expand_dims(img, axis=0)\n    return image\n\n# Function to preprocess audio files into images\n@tf.autograph.experimental.do_not_convert\ndef preprocess_audio_files(file_paths):\n    images = []\n    for file_path in file_paths:\n        # Preprocess each audio file\n        image = preprocess_audio_to_image(file_path)\n        images.append(image)\n    return tf.stack(images)\n\n# Function to predict on new data\ndef predict_on_files(file_paths):\n    # Convert file_paths to a TensorFlow dataset\n    file_paths_dataset = tf.data.Dataset.from_tensor_slices(file_paths)\n    # Preprocess the audio files\n    images_dataset = file_paths_dataset.map(lambda x: tf.py_function(preprocess_audio_to_image, [x], tf.float32))\n    # Batch the dataset\n    images_dataset = images_dataset.prefetch(AUTOTUNE)\n    return images_dataset\n\n\n# Make predictions\nprediction_dataset = predict_on_files(file_paths)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:35:15.310720Z","iopub.execute_input":"2024-04-22T07:35:15.311086Z","iopub.status.idle":"2024-04-22T07:35:15.406869Z","shell.execute_reply.started":"2024-04-22T07:35:15.311057Z","shell.execute_reply":"2024-04-22T07:35:15.406117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del prediction_dataset\nlist(prediction_dataset.take(1))","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:35:17.645715Z","iopub.execute_input":"2024-04-22T07:35:17.646071Z","iopub.status.idle":"2024-04-22T07:35:35.584279Z","shell.execute_reply.started":"2024-04-22T07:35:17.646034Z","shell.execute_reply":"2024-04-22T07:35:35.583264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to show images in the dataset\ndef show_dataset(dataset):\n    try:\n        plt.figure(figsize=(10, 10))\n        for images in dataset.take(1):  # Only take a single batch\n            ax = plt.subplot(5, 5,  1)\n            plt.imshow(images)\n            plt.axis(\"off\")\n    except:\n        pass","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:35:45.494224Z","iopub.execute_input":"2024-04-22T07:35:45.495226Z","iopub.status.idle":"2024-04-22T07:35:45.500701Z","shell.execute_reply.started":"2024-04-22T07:35:45.495191Z","shell.execute_reply":"2024-04-22T07:35:45.499808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the dataset\n# show_dataset(prediction_dataset)\n\n# plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_paths[:5]","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:35:50.310341Z","iopub.execute_input":"2024-04-22T07:35:50.310700Z","iopub.status.idle":"2024-04-22T07:35:50.317079Z","shell.execute_reply.started":"2024-04-22T07:35:50.310672Z","shell.execute_reply":"2024-04-22T07:35:50.315994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_batch = next(iter(prediction_dataset))","metadata":{"execution":{"iopub.status.busy":"2024-04-21T21:54:32.910694Z","iopub.execute_input":"2024-04-21T21:54:32.911618Z","iopub.status.idle":"2024-04-21T21:54:38.976156Z","shell.execute_reply.started":"2024-04-21T21:54:32.911583Z","shell.execute_reply":"2024-04-21T21:54:38.975040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tqdm import tqdm\n# import time\n\n# # Initialize an empty list to store all predictions\n# all_predictions = []\n\n# # Initialize a tqdm progress bar with the total number of batches in the dataset\n# progress_bar = tqdm(prediction_dataset, desc=\"Predicting\", unit=\"batch\")\n\n# # Iterate through the dataset and make predictions batch by batch\n# for batch in progress_bar:\n#     start_time = time.time()  # Record the start time\n#     all_predictions.append(model(batch, training=False))\n#     end_time = time.time()    # Record the end time\n#     batch_time = end_time - start_time  # Calculate the time taken for the batch prediction\n    \n#     # Update the progress bar description with the time taken for the current batch\n#     progress_bar.set_description(f\"Predicting Time - {batch_time}\")\n    \n    \n    \n\n# # Close the tqdm progress bar\n# progress_bar.close()\n\n# # Concatenate all predictions into a single array\n# all_predictions = tf.concat(all_predictions, axis=0)\n\n\npredictions = model.predict(prediction_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T07:36:03.506777Z","iopub.execute_input":"2024-04-22T07:36:03.507118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_dataset.take(1)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T21:06:43.541775Z","iopub.execute_input":"2024-04-21T21:06:43.544236Z","iopub.status.idle":"2024-04-21T21:06:44.521805Z","shell.execute_reply.started":"2024-04-21T21:06:43.544190Z","shell.execute_reply":"2024-04-21T21:06:44.520559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}