{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6098,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":4629}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ[\"KERAS_BACKEND\"] = \"jax\" # or \"tensorflow\", \"torch\"\n\nimport cv2\nimport pickle\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Set the style for the plot\nsns.set(style=\"whitegrid\")\n\nimport tensorflow as tf\nimport keras_cv\nimport keras_core as keras\nfrom keras_core import ops","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image =plt.imread('/kaggle/input/UBC-OCEAN/train_thumbnails/10642_thumbnail.png')\nplt.imshow(image)\nplt.axis('off')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\n# Directory containing the images\nimage_dir = '/kaggle/input/UBC-OCEAN/train_thumbnails'\n# Directory to store normalized images\noutput_dir = '/kaggle/working/normalized_images'\n\n# Create the output directory if it doesn't exist\nos.makedirs(output_dir, exist_ok=True)\n\n# List all image files in the directory\nimage_files = os.listdir(image_dir)\n\nnormalized_images = []\nfor image_file in image_files:\n    # Load the image using OpenCV\n    image_path = os.path.join(image_dir, image_file)\n    image = cv2.imread(image_path)\n\n    # Convert the image to float and normalize to the range 0-1\n    normalized_image = image.astype(np.float32) / 255.0\n\n    # Multiply by 255 to scale the values to the range 0-255\n    #normalized_image = (normalized_image * 255).astype(np.uint8)\n\n    normalized_images.append(normalized_image)\n # Save normalized images to the output directory\nfor i, normalized_image in enumerate(normalized_images):\n    output_path = os.path.join(output_dir, f'normalized_image_{i}.png')\n    cv2.imwrite(output_path, cv2.cvtColor(normalized_image, cv2.COLOR_BGR2RGB))\n\nprint(f\"Normalized images saved to: {output_dir}\")","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    df = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\n\n    # Create the thumbnail df where is_tma == False\n    df = df[df[\"is_tma\"] == False]\n    \n    # Get basic statistics about the dataset\n    num_rows = df.shape[0]\n    num_unique_images = df['image_id'].nunique()\n    num_unique_labels = df['label'].nunique()\n    unique_labels = df['label'].unique()\n\n    print(f\"{num_rows=}\")\n    print(f\"{num_unique_images=}\")\n    print(f\"{num_unique_labels=}\")\n    print(f\"{unique_labels=}\")\n    \n    # Plot the distribution of the target classes\n    plt.figure(figsize=(10, 6))\n    sns.countplot(data=df, x='label', order=df['label'].value_counts().index)\n    plt.title('Distribution of Target Classes')\n    plt.xlabel('Label')\n    plt.ylabel('Count')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T11:46:36.235758Z","iopub.execute_input":"2024-01-28T11:46:36.236568Z","iopub.status.idle":"2024-01-28T11:46:36.553365Z","shell.execute_reply.started":"2024-01-28T11:46:36.236525Z","shell.execute_reply":"2024-01-28T11:46:36.551874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-01-28T11:46:51.166333Z","iopub.execute_input":"2024-01-28T11:46:51.16676Z","iopub.status.idle":"2024-01-28T11:46:51.195956Z","shell.execute_reply.started":"2024-01-28T11:46:51.166722Z","shell.execute_reply":"2024-01-28T11:46:51.194501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_image(path):\n    file = tf.io.read_file(path)\n    image = tf.io.decode_png(file, 3)\n    image = tf.image.resize(image, (256, 256))\n    # Data augmentation\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    image = tf.image.random_brightness(image, max_delta=0.2)\n    image = tf.image.random_contrast(image, lower=0.8, upper=1.2)\n    image = tf.image.per_image_standardization(image)\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:27:17.641167Z","iopub.execute_input":"2024-01-28T16:27:17.641611Z","iopub.status.idle":"2024-01-28T16:27:17.650679Z","shell.execute_reply.started":"2024-01-28T16:27:17.641571Z","shell.execute_reply":"2024-01-28T16:27:17.649323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform one-hot encoding of the 'label' column and explicitly convert to integer type\ndf_one_hot = pd.get_dummies(df[\"label\"],prefix=\"label\").astype(int)\n\n# Concatenate the original DataFrame with the one-hot encoded labels\ntrain_df = pd.concat([df[\"image_id\"], df_one_hot], axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:26:57.64329Z","iopub.execute_input":"2024-01-28T12:26:57.645095Z","iopub.status.idle":"2024-01-28T12:26:57.661516Z","shell.execute_reply.started":"2024-01-28T12:26:57.645Z","shell.execute_reply":"2024-01-28T12:26:57.659561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # Get the thumbnail image paths\ntrain_df[\"image_thumbnail_path\"] = train_df[\"image_id\"].apply(lambda x: f\"{'/kaggle/input/UBC-OCEAN/train_thumbnails'}/{x}_thumbnail.png\")\nimage_thumbnail_paths = train_df[\"image_thumbnail_path\"].values\nlabels = train_df[[col for col in train_df.columns if col.startswith(\"label_\")]].values\n\nlabel_names = [col for col in train_df.columns if col.startswith(\"label_\")]\nname_to_id = {key.replace(\"label_\", \"\"):value for value,key in enumerate(label_names)}\nid_to_name = {key:value for value, key in name_to_id.items()}\n    \n    # Save to dictionary to disk\nwith open(\"id_to_name.pkl\", \"wb\") as f:\n    pickle.dump(id_to_name, f)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_thumbnail_paths","metadata":{"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:33:58.923937Z","iopub.execute_input":"2024-01-28T12:33:58.924462Z","iopub.status.idle":"2024-01-28T12:33:58.935103Z","shell.execute_reply.started":"2024-01-28T12:33:58.924422Z","shell.execute_reply":"2024-01-28T12:33:58.932895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:34:10.000548Z","iopub.execute_input":"2024-01-28T12:34:10.001014Z","iopub.status.idle":"2024-01-28T12:34:10.012587Z","shell.execute_reply.started":"2024-01-28T12:34:10.000973Z","shell.execute_reply":"2024-01-28T12:34:10.010063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T21:20:34.312378Z","iopub.execute_input":"2024-01-28T21:20:34.313796Z","iopub.status.idle":"2024-01-28T21:20:34.32639Z","shell.execute_reply.started":"2024-01-28T21:20:34.313746Z","shell.execute_reply":"2024-01-28T21:20:34.324879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-01-28T22:09:20.158662Z","iopub.execute_input":"2024-01-28T22:09:20.159204Z","iopub.status.idle":"2024-01-28T22:09:20.177487Z","shell.execute_reply.started":"2024-01-28T22:09:20.159153Z","shell.execute_reply":"2024-01-28T22:09:20.175706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = (\n        tf.data.Dataset.from_tensor_slices(image_thumbnail_paths)\n        .map(read_image, num_parallel_calls=tf.data.AUTOTUNE)\n    )\ny = tf.data.Dataset.from_tensor_slices(labels)\n\n    # Zip the x and y together\nds = tf.data.Dataset.zip((x, y))\n    \n    # Create the training and validation splits\ntrain_size = 366\nval_size = 76\ntest_size = 71\n\n# Create training dataset\ntrain_ds = (\n    ds\n    .take(train_size)\n    .shuffle(8 * 10)\n    .batch(5)\n    .prefetch(tf.data.AUTOTUNE)\n)\n\n# Create validation dataset\nval_ds = (\n    ds\n    .skip(train_size)\n    .take(val_size)\n    .batch(5)\n    .prefetch(tf.data.AUTOTUNE)\n)\n\n# Create test dataset\ntest_ds = (\n    ds\n    .skip(train_size + val_size)\n    .take(test_size)\n    .batch(5)\n    .prefetch(tf.data.AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T22:14:39.413737Z","iopub.execute_input":"2024-01-28T22:14:39.414277Z","iopub.status.idle":"2024-01-28T22:14:39.522037Z","shell.execute_reply.started":"2024-01-28T22:14:39.414185Z","shell.execute_reply":"2024-01-28T22:14:39.520514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2024-01-28T21:43:03.967181Z","iopub.execute_input":"2024-01-28T21:43:03.967565Z","iopub.status.idle":"2024-01-28T21:43:03.975564Z","shell.execute_reply.started":"2024-01-28T21:43:03.967538Z","shell.execute_reply":"2024-01-28T21:43:03.974691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:01:43.715177Z","iopub.execute_input":"2024-01-28T13:01:43.715629Z","iopub.status.idle":"2024-01-28T13:01:43.724828Z","shell.execute_reply.started":"2024-01-28T13:01:43.715599Z","shell.execute_reply":"2024-01-28T13:01:43.723269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_ds),len(val_ds),len(train_ds)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:37:14.047051Z","iopub.execute_input":"2024-01-28T13:37:14.048811Z","iopub.status.idle":"2024-01-28T13:37:14.058074Z","shell.execute_reply.started":"2024-01-28T13:37:14.048747Z","shell.execute_reply":"2024-01-28T13:37:14.056329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(ds))","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:01:02.921301Z","iopub.execute_input":"2024-01-28T13:01:02.921759Z","iopub.status.idle":"2024-01-28T13:01:02.931736Z","shell.execute_reply.started":"2024-01-28T13:01:02.921727Z","shell.execute_reply":"2024-01-28T13:01:02.929828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(val_ds),len(train_ds))","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:09:52.009054Z","iopub.execute_input":"2024-01-28T13:09:52.009561Z","iopub.status.idle":"2024-01-28T13:09:52.018161Z","shell.execute_reply.started":"2024-01-28T13:09:52.009511Z","shell.execute_reply":"2024-01-28T13:09:52.016232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**(CNN) for image classification**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import layers, models\n\n\n# Define the CNN model\nmodel = models.Sequential()\n\n# Convolutional layers\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n# Add more convolutional layers\nmodel.add(layers.Conv2D(256, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n# Add dropout layer\nmodel.add(layers.Dropout(0.5))\n\n# ... Continue with additional layers\n# Flatten layer\nmodel.add(layers.Flatten())\n# Dense layers\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dropout(0.5))  # Optional dropout layer for regularization\nmodel.add(layers.Dense(len(label_names), activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Display the model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T17:09:43.67715Z","iopub.execute_input":"2024-01-28T17:09:43.677738Z","iopub.status.idle":"2024-01-28T17:09:43.919782Z","shell.execute_reply.started":"2024-01-28T17:09:43.677698Z","shell.execute_reply":"2024-01-28T17:09:43.918283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(train_ds, epochs=10, validation_data=val_ds)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T17:09:51.102409Z","iopub.execute_input":"2024-01-28T17:09:51.102831Z","iopub.status.idle":"2024-01-28T17:21:34.227426Z","shell.execute_reply.started":"2024-01-28T17:09:51.1028Z","shell.execute_reply":"2024-01-28T17:21:34.224701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have already trained your model and stored it in the 'model' variable\n\n# Evaluate the model on the test dataset\ntest_results = model.evaluate(test_ds)\n\n# Print the test loss and accuracy\ntest_loss, test_accuracy = test_results\nprint(f'Test Loss: {test_loss:.4f}')\nprint(f'Test Accuracy: {test_accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T17:21:39.372957Z","iopub.execute_input":"2024-01-28T17:21:39.373816Z","iopub.status.idle":"2024-01-28T17:21:58.867383Z","shell.execute_reply.started":"2024-01-28T17:21:39.373775Z","shell.execute_reply":"2024-01-28T17:21:58.8662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Assuming you have an image you want to predict on\nnew_image_path = '/kaggle/input/UBC-OCEAN/train_thumbnails/16325_thumbnail.png'\n\n# Read and preprocess the new image\nnew_image = read_image(new_image_path)\nnew_image = tf.expand_dims(new_image, axis=0)  # Add batch dimension\n\n# Make predictions\npredictions = model.predict(new_image)\n\n# Get the predicted class index\npredicted_class_index = np.argmax(predictions)\n\n# Map the predicted class index to the corresponding label\npredicted_label = label_names[predicted_class_index]\n\nprint(f'Predicted Label: {predicted_label}')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T17:24:01.437371Z","iopub.execute_input":"2024-01-28T17:24:01.437841Z","iopub.status.idle":"2024-01-28T17:24:01.631497Z","shell.execute_reply.started":"2024-01-28T17:24:01.437805Z","shell.execute_reply":"2024-01-28T17:24:01.630104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training and validation accuracy over epochs\nplt.figure(figsize=(12, 6))\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:32:37.260518Z","iopub.execute_input":"2024-01-28T19:32:37.260928Z","iopub.status.idle":"2024-01-28T19:32:37.59557Z","shell.execute_reply.started":"2024-01-28T19:32:37.260899Z","shell.execute_reply":"2024-01-28T19:32:37.593603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training and validation loss over epochs\nplt.figure(figsize=(12, 6))\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:37:03.763912Z","iopub.execute_input":"2024-01-28T19:37:03.764405Z","iopub.status.idle":"2024-01-28T19:37:04.087664Z","shell.execute_reply.started":"2024-01-28T19:37:03.764369Z","shell.execute_reply":"2024-01-28T19:37:04.085933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Pretrained Model Inception V3**","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet101\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\n\n# Load ResNet101 base model without top (fully connected) layers\nbase_model = ResNet101(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\n\n# Add custom top layers for your specific task\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(128, activation='relu')(x)\npredictions = Dense(len(label_names), activation='softmax')(x)\n\n# Create a new model with the combined base and top layers\nmodel_resnet101 = Model(inputs=base_model.input, outputs=predictions)\n\n# Compile the model\nmodel_resnet101.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Display the model summary\nmodel_resnet101.summary()\n\n# Rest of your code for data preparation and training\n# ...\n\n# For training, use model_resnet101.fit(train_ds, validation_data=val_ds, epochs=num_epochs)\n# For evaluation, use model_resnet101.evaluate(test_ds)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T18:24:33.556135Z","iopub.execute_input":"2024-01-28T18:24:33.556621Z","iopub.status.idle":"2024-01-28T18:24:40.235114Z","shell.execute_reply.started":"2024-01-28T18:24:33.556584Z","shell.execute_reply":"2024-01-28T18:24:40.233527Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory_resnet101 = model_resnet101.fit(train_ds, epochs=10, validation_data=val_ds)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T18:25:14.472735Z","iopub.execute_input":"2024-01-28T18:25:14.47315Z","iopub.status.idle":"2024-01-28T19:18:22.286111Z","shell.execute_reply.started":"2024-01-28T18:25:14.473107Z","shell.execute_reply":"2024-01-28T19:18:22.284662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have already trained your model and stored it in the 'model' variable\n\n# Evaluate the model on the test dataset\ntest_resnet101_results = model_resnet101.evaluate(test_ds)\n\n# Print the test loss and accuracy\ntest_resnet101_loss, test_resnet101_accuracy = test_resnet101_results\nprint(f'Test Loss: {test_resnet101_loss:.4f}')\nprint(f'Test Accuracy: {test_resnet101_accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:20:07.674811Z","iopub.execute_input":"2024-01-28T19:20:07.676308Z","iopub.status.idle":"2024-01-28T19:20:48.708309Z","shell.execute_reply.started":"2024-01-28T19:20:07.676237Z","shell.execute_reply":"2024-01-28T19:20:48.706036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have an image you want to predict on\nnew_image_path = '/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png'\n\n# Read and preprocess the new image\nnew_image = read_image(new_image_path)\nnew_image = tf.expand_dims(new_image, axis=0)  # Add batch dimension\n\n# Make predictions\npredictions_resnet101 = model_resnet101.predict(new_image)\n\n# Get the predicted class index\npredicted_class_index2 = np.argmax(predictions_resnet101)\n\n# Map the predicted class index to the corresponding label\npredicted_label2 = label_names[predicted_class_index2]\n\nprint(f'Predicted Label: {predicted_label2}')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:21:39.743303Z","iopub.execute_input":"2024-01-28T19:21:39.744003Z","iopub.status.idle":"2024-01-28T19:21:40.19665Z","shell.execute_reply.started":"2024-01-28T19:21:39.743974Z","shell.execute_reply":"2024-01-28T19:21:40.195984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Assuming you have already compiled and trained your model\n#history = model_resnet101.fit(train_ds, validation_data=val_ds, epochs=num_epochs)\n\n# Plot training and validation accuracy over epochs\nplt.figure(figsize=(12, 6))\nplt.plot(history_resnet101.history['accuracy'], label='Training Accuracy')\nplt.plot(history_resnet101.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:34:41.549375Z","iopub.execute_input":"2024-01-28T19:34:41.549876Z","iopub.status.idle":"2024-01-28T19:34:41.867594Z","shell.execute_reply.started":"2024-01-28T19:34:41.549836Z","shell.execute_reply":"2024-01-28T19:34:41.866307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training and validation loss over epochs\nplt.figure(figsize=(12, 6))\nplt.plot(history_resnet101.history['loss'], label='Training Loss')\nplt.plot(history_resnet101.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:35:07.306676Z","iopub.execute_input":"2024-01-28T19:35:07.308071Z","iopub.status.idle":"2024-01-28T19:35:07.639012Z","shell.execute_reply.started":"2024-01-28T19:35:07.308014Z","shell.execute_reply":"2024-01-28T19:35:07.637678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.models import Model\n\n# Load the pre-trained VGG16 model without the top (fully connected) layers\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\n\n# Freeze the pre-trained layers\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# Create a new model with additional layers on top of the pre-trained model\nmodel4 = Flatten()(base_model.output)\nmodel4 = Dense(4096, activation='relu')(model4)\nmodel4 = Dense(4096, activation='relu')(model4)\noutput = Dense(len(label_names), activation='softmax')(model4)\n\n# Final model\nfinal_model= Model(inputs=base_model.input, outputs=output)\n\n# Compile the model\nfinal_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print the model summary\nfinal_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T19:40:12.912293Z","iopub.execute_input":"2024-01-28T19:40:12.912804Z","iopub.status.idle":"2024-01-28T19:40:14.851029Z","shell.execute_reply.started":"2024-01-28T19:40:12.912771Z","shell.execute_reply":"2024-01-28T19:40:14.849171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history4 = final_model.fit(train_ds, validation_data=val_ds, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T20:16:58.979693Z","iopub.execute_input":"2024-01-28T20:16:58.980905Z","iopub.status.idle":"2024-01-28T20:57:59.485798Z","shell.execute_reply.started":"2024-01-28T20:16:58.980833Z","shell.execute_reply":"2024-01-28T20:57:59.483997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have already trained your model and stored it in the 'model' variable\n\n# Evaluate the model on the test dataset\ntest_vgg_results = final_model.evaluate(test_ds)\n\n# Print the test loss and accuracy\ntest_vgg_loss, test_vgg_accuracy = test_vgg_results\nprint(f'Test Loss: {test_vgg_loss:.4f}')\nprint(f'Test Accuracy: {test_vgg_accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T21:04:21.165648Z","iopub.execute_input":"2024-01-28T21:04:21.16611Z","iopub.status.idle":"2024-01-28T21:05:01.273267Z","shell.execute_reply.started":"2024-01-28T21:04:21.166078Z","shell.execute_reply":"2024-01-28T21:05:01.27224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training and validation accuracy over epochs\nplt.figure(figsize=(12, 6))\nplt.plot(history4.history['accuracy'], label='Training Accuracy')\nplt.plot(history4.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T22:15:23.778258Z","iopub.execute_input":"2024-01-28T22:15:23.778732Z","iopub.status.idle":"2024-01-28T22:15:24.133992Z","shell.execute_reply.started":"2024-01-28T22:15:23.778693Z","shell.execute_reply":"2024-01-28T22:15:24.132818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training and validation loss over epochs\nplt.figure(figsize=(12, 6))\nplt.plot(history4.history['loss'], label='Training Loss')\nplt.plot(history4.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T22:15:52.291183Z","iopub.execute_input":"2024-01-28T22:15:52.291699Z","iopub.status.idle":"2024-01-28T22:15:52.6185Z","shell.execute_reply.started":"2024-01-28T22:15:52.291663Z","shell.execute_reply":"2024-01-28T22:15:52.61734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have an image you want to predict on\nnew_image_path = '/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png'\n\n# Read and preprocess the new image\nnew_image = read_image(new_image_path)\nnew_image = tf.expand_dims(new_image, axis=0)  # Add batch dimension\n\n# Make predictions\npredictions_vgg = final_model.predict(new_image)\n\n# Get the predicted class index\npredicted_class_index2 = np.argmax(predictions_vgg)\n\n# Map the predicted class index to the corresponding label\npredicted_label2 = label_names[predicted_class_index2]\n\nprint(f'Predicted Label: {predicted_label2}')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T21:42:08.081237Z","iopub.execute_input":"2024-01-28T21:42:08.081603Z","iopub.status.idle":"2024-01-28T21:42:08.593096Z","shell.execute_reply.started":"2024-01-28T21:42:08.081577Z","shell.execute_reply":"2024-01-28T21:42:08.592285Z"},"trusted":true},"execution_count":null,"outputs":[]}]}