{"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":"gpu","dataSources":[{"sourceId":90316,"databundleVersionId":10477888,"sourceType":"competition"},{"sourceId":10358720,"sourceType":"datasetVersion","datasetId":6415214},{"sourceId":10358903,"sourceType":"datasetVersion","datasetId":6415334},{"sourceId":10363358,"sourceType":"datasetVersion","datasetId":6418542}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"rm -rf /kaggle/working/*","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:28:43.654275Z","iopub.execute_input":"2025-01-04T08:28:43.654589Z","iopub.status.idle":"2025-01-04T08:28:43.775703Z","shell.execute_reply.started":"2025-01-04T08:28:43.654551Z","shell.execute_reply":"2025-01-04T08:28:43.774319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport shutil\n\ndef rle_decode(mask_rle, shape):\n    \"\"\"Decode run-length encoded mask.\"\"\"\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        img[start:end] = 1\n    return img.reshape(shape)\n\ndef mask_to_bbox(mask):\n    \"\"\"Convert mask to YOLO format bounding box.\"\"\"\n    rows = np.any(mask, axis=1)\n    cols = np.any(mask, axis=0)\n    rmin, rmax = np.where(rows)[0][[0, -1]]\n    cmin, cmax = np.where(cols)[0][[0, -1]]\n    \n    # Convert to YOLO format (x_center, y_center, width, height) normalized\n    x_center = (cmin + cmax) / 2 / mask.shape[1]\n    y_center = (rmin + rmax) / 2 / mask.shape[0]\n    width = (cmax - cmin) / mask.shape[1]\n    height = (rmax - rmin) / mask.shape[0]\n    \n    return [x_center, y_center, width, height]\n\n# Read the CSV file\ndf = pd.read_csv('/kaggle/input/ai-dl-multiclass-segmentation/train.csv')\n\n# Create necessary directories\nos.makedirs('train/labels', exist_ok=True)\nos.makedirs('train_final/images', exist_ok=True)\n\n# Path to the source images\nimg_source_dir = '/kaggle/input/ai-dl-multiclass-segmentation/TrainImages/'\n\n# Assuming the images are .jpg files, you can adjust this based on your actual file extension\nimg_extension = '.jpg'  # Change this if your images have a different extension\n\n# Process each unique image\nfor img_name in df['ImageName'].unique():\n    img_data = df[df['ImageName'] == img_name]\n    height = img_data['ImageHeight'].iloc[0]\n    width = img_data['ImageWidth'].iloc[0]\n    \n    # Create label file\n    label_path = os.path.join('train/labels', f\"{img_name}.txt\")\n    \n    with open(label_path, 'w') as f:\n        for _, row in img_data.iterrows():\n            # Decode RLE mask\n            mask = rle_decode(row['Encoding'], (height, width))\n            \n            # Convert mask to bbox\n            bbox = mask_to_bbox(mask)\n            \n            # Write in YOLO format: class_id x_center y_center width height\n            class_id = int(row['ClassNumber']) - 1  # Assuming classes start from 1\n            f.write(f\"{class_id} {' '.join(map(str, bbox))}\\n\")\n    \n    # Add the file extension to the image name\n    img_name_with_extension = img_name + img_extension\n    \n    # Copy image to the new location\n    img_source_path = os.path.join(img_source_dir, img_name_with_extension)\n    img_dest_path = os.path.join('train_final/images', img_name_with_extension)\n    \n    # Copy the image to the target directory\n    shutil.copy(img_source_path, img_dest_path)\n\nprint(\"Conversion complete and images copied!\")","metadata":{"execution":{"iopub.status.busy":"2025-01-04T08:29:34.034851Z","iopub.execute_input":"2025-01-04T08:29:34.035149Z","iopub.status.idle":"2025-01-04T08:29:55.084713Z","shell.execute_reply.started":"2025-01-04T08:29:34.035128Z","shell.execute_reply":"2025-01-04T08:29:55.083769Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\nimport os\n\n# Define the source and destination paths\nsource_path = '/kaggle/working/train/labels'\ndestination_path = '/kaggle/working/train_final'\n\n# Check if the source directory exists\nif os.path.isdir(source_path):\n    # Move the entire directory\n    shutil.move(source_path, destination_path)\n    print(f\"Directory moved successfully from {source_path} to {destination_path}\")\nelse:\n    print(f\"The source directory {source_path} does not exist.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:29:57.269883Z","iopub.execute_input":"2025-01-04T08:29:57.270244Z","iopub.status.idle":"2025-01-04T08:29:57.275874Z","shell.execute_reply.started":"2025-01-04T08:29:57.270212Z","shell.execute_reply":"2025-01-04T08:29:57.274933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport shutil\nimport random\n\n# Define directories\ntrain_images_dir = '/kaggle/working/train_final/images'\ntrain_labels_dir = '/kaggle/working/train_final/labels'\nval_images_dir = '/kaggle/working/train_final/val/images'\nval_labels_dir = '/kaggle/working/train_final/val/labels'\n\n# Create directories for validation if they don't exist\nos.makedirs(val_images_dir, exist_ok=True)\nos.makedirs(val_labels_dir, exist_ok=True)\n\n# Get the list of image filenames (without extension)\nimage_files = [f for f in os.listdir(train_images_dir) if f.endswith('.jpg')]  # Adjust extension if needed\n\n# Define the percentage of data to be used for validation (e.g., 20%)\nvalidation_percentage = 0.4\nnum_val_images = int(len(image_files) * validation_percentage)\n\n# Randomly select a subset of images for validation\nval_images = random.sample(image_files, num_val_images)\n\n# Move the selected validation images and labels to the validation directory\nfor img_name in val_images:\n    # Move image file\n    img_source_path = os.path.join(train_images_dir, img_name)\n    img_dest_path = os.path.join(val_images_dir, img_name)\n    shutil.move(img_source_path, img_dest_path)\n    \n    # Move the corresponding label file\n    label_name = img_name.replace('.jpg', '.txt')  # Assuming labels are .txt\n    label_source_path = os.path.join(train_labels_dir, label_name)\n    label_dest_path = os.path.join(val_labels_dir, label_name)\n    shutil.move(label_source_path, label_dest_path)\n\nprint(f\"Moved {num_val_images} images and labels to the validation set.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:29:58.992042Z","iopub.execute_input":"2025-01-04T08:29:58.992525Z","iopub.status.idle":"2025-01-04T08:29:59.060506Z","shell.execute_reply.started":"2025-01-04T08:29:58.992488Z","shell.execute_reply":"2025-01-04T08:29:59.059573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport shutil\nfrom sklearn.model_selection import train_test_split\n# Create YAML configuration\nnum_classes = df['ClassNumber'].nunique()\nyaml_content = {\n    'train': '/kaggle/working/train_final/images',\n    'val': '/kaggle/working/train_final/val/images',\n    'nc': num_classes,\n    'names': [f'class{i}' for i in range(num_classes)],  # Update with actual class names if available\n    'task': 'detect'\n}\n\n# Save the YAML file\nimport yaml\nwith open('data.yaml', 'w') as f:\n    yaml.safe_dump(yaml_content, f, sort_keys=False)\n\nprint(\"Conversion complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:30:01.188149Z","iopub.execute_input":"2025-01-04T08:30:01.188512Z","iopub.status.idle":"2025-01-04T08:30:01.214777Z","shell.execute_reply.started":"2025-01-04T08:30:01.188478Z","shell.execute_reply":"2025-01-04T08:30:01.213942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import necessary libraries\n!pip install ultralytics\nimport os\nimport cv2\nimport glob\nimport yaml\nimport random\nimport pathlib\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport seaborn as sns\nfrom tqdm import tqdm\nfrom ultralytics import YOLO\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom tqdm.notebook import trange, tqdm\nfrom IPython.display import Image, display, Video\nfrom sklearn.metrics import precision_score, recall_score\n\n# Disable WANDB (Weights & Biases) logging to avoid unnecessary clutter\n!wandb disabled\n\n# Suppress warnings for cleaner outputs\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:30:03.233135Z","iopub.execute_input":"2025-01-04T08:30:03.233428Z","iopub.status.idle":"2025-01-04T08:30:07.902868Z","shell.execute_reply.started":"2025-01-04T08:30:03.233404Z","shell.execute_reply":"2025-01-04T08:30:07.901630Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output_path = '/kaggle/working/data.yaml'\n# Read and display the class names from the YAML file\nwith open(output_path, 'r') as file:\n    data = yaml.safe_load(file)\nclass_names = data['names']\nprint(f\"Class Names: {class_names}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:30:11.481364Z","iopub.execute_input":"2025-01-04T08:30:11.481784Z","iopub.status.idle":"2025-01-04T08:30:11.489648Z","shell.execute_reply.started":"2025-01-04T08:30:11.481749Z","shell.execute_reply":"2025-01-04T08:30:11.488781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display metadata for a random image in the training dataset\ntrain_path = '/kaggle/working/train_final/images'\nfrom PIL import Image\nimages_folder = '/kaggle/working/train_final/images'\nimage_files = [f for f in os.listdir(images_folder) if f.endswith(('.jpg', '.png', '.jpeg'))]\nrandom_image = random.choice(image_files)\nrandom_image_path = os.path.join(images_folder, random_image)\nimage = Image.open(random_image_path)\n\n# Display image properties like size, mode, and channels\nprint(f\"Random Image: {random_image}\")\nprint(f\"Image Size: {image.size}\")\nprint(f\"Image Mode: {image.mode}\")\nprint(f\"Number of Channels: {len(image.getbands())}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:30:15.144881Z","iopub.execute_input":"2025-01-04T08:30:15.145182Z","iopub.status.idle":"2025-01-04T08:30:15.164283Z","shell.execute_reply.started":"2025-01-04T08:30:15.145158Z","shell.execute_reply":"2025-01-04T08:30:15.163458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualize a grid of random images from the training dataset\nselected_images = random.sample(image_files, 16)\nfig, axes = plt.subplots(4, 4, figsize=(10, 10))\nfor i, ax in enumerate(axes.flat):\n    img_path = os.path.join(train_path, selected_images[i])\n    img = mpimg.imread(img_path)\n    ax.imshow(img)\n    ax.axis('off')  # Hide axes for a cleaner look\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:30:16.876887Z","iopub.execute_input":"2025-01-04T08:30:16.877211Z","iopub.status.idle":"2025-01-04T08:30:18.432094Z","shell.execute_reply.started":"2025-01-04T08:30:16.877186Z","shell.execute_reply":"2025-01-04T08:30:18.430583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\nmodel = YOLO(\"yolo11n.pt\")  # load a pretrained model (recommended for training)\n# Train the model\n# results = model.train(data=\"/kaggle/working/data.yaml\", epochs=100, imgsz=640)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:31:07.216459Z","iopub.execute_input":"2025-01-04T08:31:07.216807Z","iopub.status.idle":"2025-01-04T08:31:07.275658Z","shell.execute_reply.started":"2025-01-04T08:31:07.216779Z","shell.execute_reply":"2025-01-04T08:31:07.274912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Perform predictions on random images from the dataset\nrandom_images = random.sample(image_files, 10)\nfor image_file in random_images:\n    image_path = os.path.join(images_folder, image_file)\n    results = model.predict(source=image_path, imgsz=416)  # Predict with YOLOv8\n    original_image = Image.open(image_path)\n    display(original_image)  # Show the original image\n    for result in results:\n        plot = result.plot()  # Visualize the predictions\n        plot = cv2.cvtColor(plot, cv2.COLOR_BGR2RGB)\n        display(Image.fromarray(plot))  # Display prediction overlays","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:31:08.102615Z","iopub.execute_input":"2025-01-04T08:31:08.102987Z","iopub.status.idle":"2025-01-04T08:31:09.372012Z","shell.execute_reply.started":"2025-01-04T08:31:08.102959Z","shell.execute_reply":"2025-01-04T08:31:09.371118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\nmodel = YOLO(\"yolo11n.pt\")\n\n# Train the model\ntrain_results = model.train(\n    data=\"/kaggle/working/data.yaml\",  # path to dataset YAML\n    epochs=100,  # number of training epochs\n    imgsz=640,  # training image size\n    device=0  # device to run on, i.e. device=0 or device=0,1,2,3 or device=cpu\n)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate model performance on the validation set\nmetrics = model.val()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import Image, display  # Ensure correct import\n\n# Visualize training metrics like confusion matrix, F1 curve, and results\nresult_files = [\n    '/kaggle/working/runs/detect/train5/confusion_matrix_normalized.png',\n    '/kaggle/working/runs/detect/train5/F1_curve.png',\n    '/kaggle/working/runs/detect/train5/results.png'\n]\nfor file in result_files:\n    display(Image(file))  # Correct usage of Image from IPython.display","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nfrom pathlib import Path\n\n# Initialize the model\nbest_model = YOLO('/kaggle/input/checkpoints-deep-learning-v2/kaggle/working/runs/detect/train3/weights/best.pt')\n\n# Get the list of images in the test folder\ntest_images_folder = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\nimage_paths = list(Path(test_images_folder).glob('*.*'))  # Adjust the file type filter if necessary\n\n# Select a random image\nrandom_image_path = random.choice(image_paths)\n\n# Perform inference on the random image\nresults = best_model.predict(str(random_image_path))\n\n# Iterate over the results\nfor result in results:\n    # Extracting bounding box coordinates in different formats\n    boxes_xyxy = result.boxes.xyxy  # Box with xyxy format, (N, 4)\n    boxes_xywh = result.boxes.xywh  # Box with xywh format, (N, 4)\n    boxes_xyxyn = result.boxes.xyxyn  # Box with xyxy format but normalized, (N, 4)\n    boxes_xywhn = result.boxes.xywhn  # Box with xywh format but normalized, (N, 4)\n\n    # Extracting confidence scores\n    confidence_scores = result.boxes.conf  # Confidence score, (N, 1)\n\n    # Extracting class labels\n    class_labels = result.boxes.cls  # Class labels, (N, 1)\n\n    # For segmentation (if available)\n    if hasattr(result.masks, 'masks'):\n        masks = result.masks.masks  # Masks, (N, H, W)\n    if hasattr(result.masks, 'segments'):\n        segments = result.masks.segments  # Bounding coordinates of masks, List[segment] * N\n\n    # For classification probabilities (if available)\n    if hasattr(result, 'probs'):\n        class_probabilities = result.probs  # Class probabilities, (num_class, )\n\n    # Print or use the extracted information\n    print(\"Bounding Boxes (xyxy format):\", boxes_xyxy)\n    print(\"Bounding Boxes (xywh format):\", boxes_xywh)\n    print(\"Bounding Boxes (xyxyn normalized):\", boxes_xyxyn)\n    print(\"Bounding Boxes (xywhn normalized):\", boxes_xywhn)\n    print(\"Confidence Scores:\", confidence_scores)\n    print(\"Class Labels:\", class_labels)\n\n    if hasattr(result.masks, 'masks'):\n        print(\"Masks:\", masks)\n    if hasattr(result.masks, 'segments'):\n        print(\"Segments:\", segments)\n    \n    if hasattr(result, 'probs'):\n        print(\"Class Probabilities:\", class_probabilities)\n\n# Display the image with bounding boxes and results\nresults[0].show()  # Display the first image's result with bounding boxes\nresults[0].save()  # Save the image with the predicted bounding boxes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:19.623908Z","iopub.execute_input":"2025-01-04T08:36:19.624237Z","iopub.status.idle":"2025-01-04T08:36:20.189881Z","shell.execute_reply.started":"2025-01-04T08:36:19.624210Z","shell.execute_reply":"2025-01-04T08:36:20.189079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(boxes_xyxy.tolist())\nprint(confidence_scores.tolist())\nprint(class_labels.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:24.683799Z","iopub.execute_input":"2025-01-04T08:36:24.684131Z","iopub.status.idle":"2025-01-04T08:36:24.690318Z","shell.execute_reply.started":"2025-01-04T08:36:24.684105Z","shell.execute_reply":"2025-01-04T08:36:24.689543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert boxes_xyxy to the desired structure\nconverted_boxes = [[box] for box in boxes_xyxy.tolist()]\n\n# Print the result\nprint(converted_boxes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:25.027787Z","iopub.execute_input":"2025-01-04T08:36:25.028111Z","iopub.status.idle":"2025-01-04T08:36:25.033561Z","shell.execute_reply.started":"2025-01-04T08:36:25.028085Z","shell.execute_reply":"2025-01-04T08:36:25.032640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\ndef show_mask(mask, ax, random_color=False):\n    if random_color:\n        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)\n    else:\n        color = np.array([30/255, 144/255, 255/255, 0.6])\n    h, w = mask.shape[-2:]\n    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)\n    ax.imshow(mask_image)\n\n\ndef show_box(box, ax):\n    x0, y0 = box[0], box[1]\n    w, h = box[2] - box[0], box[3] - box[1]\n    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))  \n\ndef show_boxes_on_image(raw_image, boxes):\n    plt.figure(figsize=(10,10))\n    plt.imshow(raw_image)\n    for box in boxes:\n      show_box(box, plt.gca())\n    plt.axis('on')\n    plt.show()\n\ndef show_points_on_image(raw_image, input_points, input_labels=None):\n    plt.figure(figsize=(10,10))\n    plt.imshow(raw_image)\n    input_points = np.array(input_points)\n    if input_labels is None:\n      labels = np.ones_like(input_points[:, 0])\n    else:\n      labels = np.array(input_labels)\n    show_points(input_points, labels, plt.gca())\n    plt.axis('on')\n    plt.show()\n\ndef show_points_and_boxes_on_image(raw_image, boxes, input_points, input_labels=None):\n    plt.figure(figsize=(10,10))\n    plt.imshow(raw_image)\n    input_points = np.array(input_points)\n    if input_labels is None:\n      labels = np.ones_like(input_points[:, 0])\n    else:\n      labels = np.array(input_labels)\n    show_points(input_points, labels, plt.gca())\n    for box in boxes:\n      show_box(box, plt.gca())\n    plt.axis('on')\n    plt.show()\n\n\ndef show_points_and_boxes_on_image(raw_image, boxes, input_points, input_labels=None):\n    plt.figure(figsize=(10,10))\n    plt.imshow(raw_image)\n    input_points = np.array(input_points)\n    if input_labels is None:\n      labels = np.ones_like(input_points[:, 0])\n    else:\n      labels = np.array(input_labels)\n    show_points(input_points, labels, plt.gca())\n    for box in boxes:\n      show_box(box, plt.gca())\n    plt.axis('on')\n    plt.show()\n\n\ndef show_points(coords, labels, ax, marker_size=375):\n    pos_points = coords[labels==1]\n    neg_points = coords[labels==0]\n    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n\n\ndef show_masks_on_image(raw_image, masks, scores):\n    if len(masks.shape) == 4:\n      masks = masks.squeeze()\n    if scores.shape[0] == 1:\n      scores = scores.squeeze()\n\n    nb_predictions = scores.shape[-1]\n    fig, axes = plt.subplots(1, nb_predictions, figsize=(15, 15))\n\n    for i, (mask, score) in enumerate(zip(masks, scores)):\n      mask = mask.cpu().detach()\n      axes[i].imshow(np.array(raw_image))\n      show_mask(mask, axes[i])\n      axes[i].title.set_text(f\"Mask {i+1}, Score: {score.item():.3f}\")\n      axes[i].axis(\"off\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:26.500832Z","iopub.execute_input":"2025-01-04T08:36:26.501144Z","iopub.status.idle":"2025-01-04T08:36:26.515135Z","shell.execute_reply.started":"2025-01-04T08:36:26.501121Z","shell.execute_reply":"2025-01-04T08:36:26.514216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\n\n# Open the local image and convert to RGB\nraw_image = Image.open(random_image_path).convert(\"RGB\")\n\n# Display the image using matplotlib\nplt.imshow(raw_image)\nplt.axis('off')  # Turn off the axis for better visualization\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:29.110494Z","iopub.execute_input":"2025-01-04T08:36:29.110852Z","iopub.status.idle":"2025-01-04T08:36:29.383082Z","shell.execute_reply.started":"2025-01-04T08:36:29.110824Z","shell.execute_reply":"2025-01-04T08:36:29.382179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom transformers import SamModel, SamProcessor\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = SamModel.from_pretrained(\"facebook/sam-vit-huge\").to(device)\nprocessor = SamProcessor.from_pretrained(\"facebook/sam-vit-huge\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:32:13.330484Z","iopub.execute_input":"2025-01-04T08:32:13.330862Z","iopub.status.idle":"2025-01-04T08:32:44.440508Z","shell.execute_reply.started":"2025-01-04T08:32:13.330833Z","shell.execute_reply":"2025-01-04T08:32:44.439499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs = processor(raw_image, return_tensors=\"pt\").to(device)\nimage_embeddings = model.get_image_embeddings(inputs[\"pixel_values\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:32.134943Z","iopub.execute_input":"2025-01-04T08:36:32.135371Z","iopub.status.idle":"2025-01-04T08:36:33.252066Z","shell.execute_reply.started":"2025-01-04T08:36:32.135337Z","shell.execute_reply":"2025-01-04T08:36:33.251058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_boxes_on_image(raw_image, boxes):\n    plt.figure(figsize=(10,10))\n    plt.imshow(raw_image)\n    \n    for box_list in boxes:  # box_list is a list containing a single bounding box\n        for box in box_list:  # Extract the box from the list\n            print(f\"Box: {box}\")  # Debugging line to show the box contents\n            show_box(box, plt.gca())  # Call show_box with the actual coordinates\n    plt.axis('on')\n    plt.show()\n# Make sure you are passing the whole list (not just one element)\nshow_boxes_on_image(raw_image, converted_boxes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:34.238819Z","iopub.execute_input":"2025-01-04T08:36:34.239152Z","iopub.status.idle":"2025-01-04T08:36:34.829288Z","shell.execute_reply.started":"2025-01-04T08:36:34.239127Z","shell.execute_reply":"2025-01-04T08:36:34.828164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs = processor(raw_image, input_boxes=[converted_boxes], return_tensors=\"pt\").to(device)\ninputs[\"input_boxes\"].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:36.408486Z","iopub.execute_input":"2025-01-04T08:36:36.408874Z","iopub.status.idle":"2025-01-04T08:36:36.461711Z","shell.execute_reply.started":"2025-01-04T08:36:36.408841Z","shell.execute_reply":"2025-01-04T08:36:36.460866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs.pop(\"pixel_values\", None)\ninputs.update({\"image_embeddings\": image_embeddings})\n\nwith torch.no_grad():\n    outputs = model(**inputs)\n\nmasks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs[\"original_sizes\"].cpu(), inputs[\"reshaped_input_sizes\"].cpu())\nscores = outputs.iou_scores","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:38.575318Z","iopub.execute_input":"2025-01-04T08:36:38.575609Z","iopub.status.idle":"2025-01-04T08:36:39.027364Z","shell.execute_reply.started":"2025-01-04T08:36:38.575585Z","shell.execute_reply":"2025-01-04T08:36:39.026565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(masks)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\n\ndef convert_mask_to_bw_image(mask, output_path=None):\n    \"\"\"\n    Convert a SAM mask to a black and white image\n    \"\"\"\n    # Convert mask to numpy array and ensure binary values\n    mask_array = mask.squeeze().numpy()  # Remove extra dimensions\n    \n    # Check if we need to transpose dimensions\n    if len(mask_array.shape) == 3:\n        # Take the first mask if there are multiple\n        mask_array = mask_array[0]\n    \n    # Convert boolean/float mask to uint8 with 0 and 255 values\n    bw_mask = (mask_array * 255).astype(np.uint8)\n    \n    # Create PIL Image\n    mask_image = Image.fromarray(bw_mask, mode='L')  # 'L' mode for grayscale\n    \n    # Save if output path is provided\n    if output_path:\n        mask_image.save(output_path)\n    \n    return mask_image\n\ndef process_all_masks(masks, base_output_path=\"mask\"):\n    \"\"\"\n    Process all masks from SAM output\n    \"\"\"\n    mask_images = []\n    \n    # Handle different possible mask structures\n    if isinstance(masks, list):\n        masks_to_process = masks[0]\n    else:\n        masks_to_process = masks.squeeze()\n    \n    for idx, mask in enumerate(masks_to_process):\n        output_path = f\"{base_output_path}_{idx}.png\"\n        mask_image = convert_mask_to_bw_image(mask, output_path)\n        mask_images.append(mask_image)\n    \n    return mask_images\n\n# Usage example:\nmask_images = process_all_masks(masks, \"output_mask\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:54.104841Z","iopub.execute_input":"2025-01-04T08:36:54.105226Z","iopub.status.idle":"2025-01-04T08:36:54.125109Z","shell.execute_reply.started":"2025-01-04T08:36:54.105188Z","shell.execute_reply":"2025-01-04T08:36:54.123781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Ensure show_mask is defined to overlay the mask on the image\ndef show_mask(mask, ax):\n    # If the mask has 3 channels, convert it to grayscale\n    if len(mask.shape) == 3 and mask.shape[0] == 3:\n        mask = np.mean(mask, axis=0)  # Convert to grayscale by averaging the channels\n    \n    # Now, show the mask (binary or grayscale)\n    ax.imshow(mask, alpha=0.5, cmap=\"jet\")  # Use a colormap and transparency for the mask\n    ax.axis(\"off\")\n\ndef show_masks_on_image(raw_image, masks, scores):\n    # Squeeze the masks and scores to remove any singleton dimensions\n    if len(masks.shape) == 4:\n        masks = masks.squeeze()  # Remove batch dimension if present\n    if scores.shape[0] == 1:\n        scores = scores.squeeze()  # Remove batch dimension if present\n\n    nb_predictions = masks.shape[0]  # Get the number of masks, which should be the same as scores\n\n    # Create subplots for each mask\n    fig, axes = plt.subplots(1, nb_predictions, figsize=(15, 15))\n\n    # If only one prediction, axes might not be a list, so ensure it's iterable\n    if nb_predictions == 1:\n        axes = [axes]  # Make axes iterable\n\n    for i, (mask, score) in enumerate(zip(masks, scores)):\n        mask = mask.cpu().detach().numpy()  # Ensure mask is on CPU and convert to numpy array\n        \n        # Ensure score is a scalar before calling .item()\n        score_value = score.item() if score.numel() == 1 else score[0].item()  # Select the first element if score has more than one\n        \n        axes[i].imshow(np.array(raw_image))  # Display the raw image\n        show_mask(mask, axes[i])  # Overlay the mask\n        axes[i].title.set_text(f\"Mask {i+1}, Score: {score_value:.3f}\")  # Title with score\n        axes[i].axis(\"off\")  # Hide the axes\n\n    plt.tight_layout()  # Adjust layout for better spacing\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:54.742514Z","iopub.execute_input":"2025-01-04T08:36:54.742881Z","iopub.status.idle":"2025-01-04T08:36:54.750964Z","shell.execute_reply.started":"2025-01-04T08:36:54.742849Z","shell.execute_reply":"2025-01-04T08:36:54.749595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_masks_on_image(raw_image, masks[0], scores)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:36:57.070894Z","iopub.execute_input":"2025-01-04T08:36:57.071194Z","iopub.status.idle":"2025-01-04T08:36:57.797647Z","shell.execute_reply.started":"2025-01-04T08:36:57.071168Z","shell.execute_reply":"2025-01-04T08:36:57.796611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\ndef combine_masks_into_one(masks, output_path=None):\n    \"\"\"\n    Combine all masks into one image with white masks on black background\n    \"\"\"\n    # Convert from list and get dimensions\n    masks_tensor = masks[0]  # Get first element of list\n    height, width = masks_tensor.shape[-2:]  # Get height and width\n    \n    # Create empty array for combined mask (black background)\n    combined_mask = np.zeros((height, width), dtype=np.uint8)\n    print()\n    # Iterate through each batch and instance\n    for batch_idx in range(masks_tensor.shape[0]):\n        for instance_idx in range(masks_tensor.shape[1]):\n            mask_array = masks_tensor[batch_idx, instance_idx].numpy()\n            # Convert boolean/float mask to binary (0 or 1)\n            mask_array = (mask_array > 0).astype(np.uint8)\n            # Add this mask to the combined image (255 for white)\n            combined_mask = np.maximum(combined_mask, mask_array * 255)\n            \n    # Convert to PIL Image (mode 'L' for grayscale)\n    combined_image = Image.fromarray(combined_mask, mode='L')\n    \n    # Save if output path is provided\n    if output_path:\n        combined_image.save(output_path)\n    \n    return combined_image\n\n# Usage example:\ncombined_masks = combine_masks_into_one(masks, output_path=\"combined_masks.png\")\n\n# Display the result\nplt.figure(figsize=(10, 10))\nplt.imshow(combined_masks, cmap='gray')\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T08:37:02.988558Z","iopub.execute_input":"2025-01-04T08:37:02.988958Z","iopub.status.idle":"2025-01-04T08:37:03.208986Z","shell.execute_reply.started":"2025-01-04T08:37:02.988925Z","shell.execute_reply":"2025-01-04T08:37:03.207685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Ensure show_mask is defined to overlay the mask on the image\ndef show_mask(mask, ax):\n    # If the mask has 3 channels, convert it to grayscale\n    if len(mask.shape) == 3 and mask.shape[0] == 3:\n        mask = np.mean(mask, axis=0)  # Convert to grayscale by averaging the channels\n    \n    # Now, show the mask (binary or grayscale)\n    ax.imshow(mask, alpha=0.5, cmap=\"jet\")  # Use a colormap and transparency for the mask\n    ax.axis(\"off\")\n\ndef show_masks_on_image(raw_image, masks, scores):\n    # Squeeze the masks and scores to remove any singleton dimensions\n    if len(masks.shape) == 4:\n        masks = masks.squeeze()  # Remove batch dimension if present\n    if scores.shape[0] == 1:\n        scores = scores.squeeze()  # Remove batch dimension if present\n\n    nb_predictions = masks.shape[0]  # Get the number of masks, which should be the same as scores\n\n    # Create subplots for each mask\n    fig, axes = plt.subplots(1, nb_predictions, figsize=(15, 15))\n\n    # If only one prediction, axes might not be a list, so ensure it's iterable\n    if nb_predictions == 1:\n        axes = [axes]  # Make axes iterable\n\n    for i, (mask, score) in enumerate(zip(masks, scores)):\n        mask = mask.cpu().detach().numpy()  # Ensure mask is on CPU and convert to numpy array\n        \n        # Ensure score is a scalar before calling .item()\n        score_value = score.item() if score.numel() == 1 else score[0].item()  # Select the first element if score has more than one\n        \n        axes[i].imshow(np.array(raw_image))  # Display the raw image\n        show_mask(mask, axes[i])  # Overlay the mask\n        axes[i].title.set_text(f\"Mask {i+1}, Score: {score_value:.3f}\")  # Title with score\n        axes[i].axis(\"off\")  # Hide the axes\n\n    plt.tight_layout()  # Adjust layout for better spacing\n    plt.show()\n\n# Assuming raw_image, masks, and scores are already defined and have the correct shapes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:24:03.459559Z","iopub.execute_input":"2025-01-03T03:24:03.459859Z","iopub.status.idle":"2025-01-03T03:24:03.466740Z","shell.execute_reply.started":"2025-01-03T03:24:03.459835Z","shell.execute_reply":"2025-01-03T03:24:03.465889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport numpy as np\nfrom PIL import Image\n\ndef show_individual_masks(masks, size):\n    result = []\n    for mask in masks[0]:\n        mask_np = mask[0].bool().cpu().numpy()  # Take first channel, convert to boolean\n        mask_img = Image.fromarray(mask_np.astype(np.uint8) * 255, mode='L')\n        mask_img = mask_img.resize(size)\n        result.append(mask_img)\n    return result\n\ndef combine_masks(masks, size):\n    combined = np.zeros_like(masks[0][0][0].cpu().numpy(), dtype=bool)\n    for mask in masks[0]:\n        combined = np.logical_or(combined, mask[0].bool().cpu().numpy())\n    \n    combined_img = Image.fromarray(combined.astype(np.uint8) * 255, mode='L')\n    combined_img = combined_img.resize(size)\n    return combined_img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T14:06:53.888749Z","iopub.execute_input":"2025-01-03T14:06:53.889049Z","iopub.status.idle":"2025-01-03T14:06:53.894747Z","shell.execute_reply.started":"2025-01-03T14:06:53.889005Z","shell.execute_reply":"2025-01-03T14:06:53.893860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Masks shape: {masks[0].shape}\")\n\n# Check if masks contain any True values\nprint(f\"Contains True values: {masks[0].any()}\")\n\n# Print number of True pixels in each mask\nfor i, mask in enumerate(masks[0]):\n    print(f\"Mask {i} True pixels: {mask.sum().item()}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T14:06:54.900965Z","iopub.execute_input":"2025-01-03T14:06:54.901321Z","iopub.status.idle":"2025-01-03T14:06:54.915917Z","shell.execute_reply.started":"2025-01-03T14:06:54.901294Z","shell.execute_reply":"2025-01-03T14:06:54.915095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"width = raw_image.size[0]\nheight = raw_image.size[1]\n\nmask_images = show_individual_masks(masks, size=(width, height))\ncombined_mask = combine_masks(masks, size=(width, height))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T14:06:57.307449Z","iopub.execute_input":"2025-01-03T14:06:57.307770Z","iopub.status.idle":"2025-01-03T14:06:57.314000Z","shell.execute_reply.started":"2025-01-03T14:06:57.307743Z","shell.execute_reply":"2025-01-03T14:06:57.313262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport cv2  # Make sure OpenCV is imported\n\ndef show_combined_mask_on_image(raw_image, masks, scores):\n    # Convert the raw_image from PIL Image to NumPy array\n    raw_image_np = np.array(raw_image)\n    \n    # Convert the masks to numpy if they are still in tensor format\n    masks_np = masks.cpu().detach().numpy()  # Convert masks to numpy array\n    \n    # If the mask has 3 channels (e.g., RGB), convert to 2D by averaging across channels\n    if masks_np.ndim == 3 and masks_np.shape[0] == 3:\n        masks_np = np.mean(masks_np, axis=0)\n    \n    # Combine all masks by taking the maximum value at each pixel across all masks\n    if masks_np.ndim == 3:  # If we have a single mask (N, H, W)\n        combined_mask = masks_np\n    elif masks_np.ndim == 4:  # If we have multiple masks (B, N, H, W)\n        combined_mask = np.max(masks_np, axis=0)  # Combine masks by taking the max along the batch dimension\n    \n    # Ensure combined_mask is 2D and binary (0 and 1)\n    combined_mask = (combined_mask > 0).astype(np.float32)\n    \n    # Resize the combined mask to match the size of the raw_image (if necessary)\n    if raw_image_np.shape[0] != combined_mask.shape[0] or raw_image_np.shape[1] != combined_mask.shape[1]:\n        combined_mask = cv2.resize(combined_mask, (raw_image_np.shape[1], raw_image_np.shape[0]))\n\n    # Display the original image and combined mask\n    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))\n    \n    # Left subplot: Original image with combined mask overlay\n    ax1.imshow(raw_image_np)\n    ax1.imshow(combined_mask, alpha=0.5, cmap='binary', vmin=0, vmax=1)\n    ax1.set_title(\"Image with Combined Mask\")\n    ax1.axis(\"off\")\n    \n    # Right subplot: Binary combined mask\n    ax2.imshow(combined_mask, cmap='binary', vmin=0, vmax=1)\n    ax2.set_title(\"Binary Combined Mask\")\n    ax2.axis(\"off\")\n    \n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:49:40.006256Z","iopub.execute_input":"2025-01-03T03:49:40.006641Z","iopub.status.idle":"2025-01-03T03:49:40.014111Z","shell.execute_reply.started":"2025-01-03T03:49:40.006610Z","shell.execute_reply":"2025-01-03T03:49:40.013172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_masks_on_image(raw_image, masks[0], scores)\nshow_combined_mask_on_image(raw_image, masks[0], scores)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:49:41.854457Z","iopub.execute_input":"2025-01-03T03:49:41.854809Z","iopub.status.idle":"2025-01-03T03:49:44.215180Z","shell.execute_reply.started":"2025-01-03T03:49:41.854783Z","shell.execute_reply":"2025-01-03T03:49:44.213889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\ndef overlay_all_masks(raw_image, masks, scores, alpha=0.5):\n    \"\"\"\n    Overlays all the masks on the raw image with transparency and enhances clarity.\n\n    Parameters:\n    - raw_image: The raw image to be shown.\n    - masks: A tensor or numpy array containing all the masks (should be 3D or 4D).\n    - scores: A tensor or numpy array containing the scores for each mask.\n    - alpha: Transparency level for the masks (default 0.5).\n\n    Returns:\n    - None\n    \"\"\"\n    # Ensure the masks and scores are in the correct format\n    if len(masks.shape) == 4:\n        masks = masks.squeeze()  # Remove batch dimension if present\n    if scores.shape[0] == 1:\n        scores = scores.squeeze()  # Remove batch dimension if present\n\n    # Create the plot for the raw image\n    fig, ax = plt.subplots(figsize=(10, 10))\n    ax.imshow(np.array(raw_image))  # Display the raw image\n\n    # Overlay all the masks\n    for i, (mask, score) in enumerate(zip(masks, scores)):\n        mask = mask.cpu().detach().numpy()  # Ensure mask is on CPU and convert to numpy array\n\n        # If the mask has multiple channels (e.g., RGB), convert to grayscale (average the channels)\n        if len(mask.shape) == 3 and mask.shape[0] == 3:\n            mask = np.mean(mask, axis=0)  # Convert to grayscale by averaging the channels\n\n        # Ensure score is a scalar before calling .item()\n        score_value = score.item() if score.numel() == 1 else score[0].item()  # Select the first element if score has more than one\n\n        # Overlay the mask with transparency\n        ax.imshow(mask, alpha=alpha, cmap=\"jet\")  # Use the 'jet' colormap with transparency\n\n        # Enhance visibility by adding contours\n        contours = plt.contour(mask, levels=[0.5], colors='white', linewidths=1)  # Add contours around the mask area\n\n    # Add a title and remove axis labels\n    ax.set_title(f\"Overlayed Masks on Image\")\n    ax.axis(\"off\")  # Hide the axes\n    plt.tight_layout()  # Ensure the layout is adjusted\n    plt.show()\n\n# Assuming raw_image, masks, and scores are already defined and have the correct shapes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:24:17.467879Z","iopub.execute_input":"2025-01-03T03:24:17.468216Z","iopub.status.idle":"2025-01-03T03:24:17.477351Z","shell.execute_reply.started":"2025-01-03T03:24:17.468186Z","shell.execute_reply":"2025-01-03T03:24:17.476412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"overlay_all_masks(raw_image, masks[0], scores)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:24:18.186425Z","iopub.execute_input":"2025-01-03T03:24:18.186945Z","iopub.status.idle":"2025-01-03T03:24:18.986763Z","shell.execute_reply.started":"2025-01-03T03:24:18.186910Z","shell.execute_reply":"2025-01-03T03:24:18.985949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport cv2\nimport numpy as np\nimport torch\nfrom torch.utils.data import Dataset\nimport os\nfrom PIL import Image\n\nclass RobustDetectionDataset(Dataset):\n    def __init__(self, image_dir, label_dir, transform=None):\n        self.image_dir = image_dir\n        self.label_dir = label_dir\n        self.image_files = [f for f in os.listdir(image_dir) if f.endswith(('.jpg', '.png', '.jpeg'))]\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_files)\n\n    def __getitem__(self, idx):\n        # Load image and labels\n        img_name = self.image_files[idx]\n        img_path = os.path.join(self.image_dir, img_name)\n        label_path = os.path.join(self.label_dir, img_name.rsplit('.', 1)[0] + '.txt')\n        \n        # Read image\n        image = cv2.imread(img_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        # Read labels\n        boxes = []\n        classes = []\n        if os.path.exists(label_path):\n            with open(label_path, 'r') as f:\n                for line in f.readlines():\n                    class_id, x_center, y_center, width, height = map(float, line.strip().split())\n                    boxes.append([x_center, y_center, width, height])\n                    classes.append(class_id)\n        \n        # Convert to numpy arrays\n        boxes = np.array(boxes)\n        classes = np.array(classes)\n        \n        # Apply transformations\n        if self.transform:\n            transformed = self.transform(image=image, bboxes=boxes)\n            image = transformed['image']\n            boxes = transformed['bboxes']\n        \n        return image, boxes, classes\n\n# Define robust augmentation pipeline\ndef get_robust_transforms(height=640, width=640):\n    return A.Compose([\n        # Scale invariance\n        A.RandomScale(scale_limit=0.5, p=0.5),\n        A.LongestMaxSize(max_size=height),\n        A.PadIfNeeded(height, width, border_mode=cv2.BORDER_CONSTANT),\n        \n        # Rotation invariance\n        A.RandomRotate90(p=0.5),\n        A.Rotate(limit=45, p=0.5),\n        \n        # Lighting invariance\n        A.OneOf([\n            A.RandomBrightness(limit=0.2, p=0.5),\n            A.RandomContrast(limit=0.2, p=0.5),\n            A.RandomGamma(gamma_limit=(80, 120), p=0.5)\n        ], p=0.5),\n        \n        # Color invariance\n        A.OneOf([\n            A.RGBShift(r_shift_limit=20, g_shift_limit=20, b_shift_limit=20, p=0.5),\n            A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5)\n        ], p=0.5),\n        \n        # Additional robustness\n        A.OneOf([\n            A.GaussNoise(var_limit=(10.0, 50.0), p=0.5),\n            A.ISONoise(color_shift=(0.01, 0.05), intensity=(0.1, 0.5), p=0.5),\n            A.GaussianBlur(blur_limit=(3, 7), p=0.5)\n        ], p=0.3),\n        \n        # Weather simulation\n        A.OneOf([\n            A.RandomFog(fog_coef_lower=0.1, fog_coef_upper=0.3, p=0.5),\n            A.RandomShadow(shadow_roi=(0, 0.5, 1, 1), p=0.5),\n            A.RandomSunFlare(flare_roi=(0, 0, 1, 0.5), p=0.5)\n        ], p=0.2),\n        \n        ToTensorV2()\n    ], bbox_params=A.BboxParams(format='yolo', label_fields=['classes']))\n\ndef train_robust_model(data_yaml_path, epochs=100):\n    # Initialize model\n    model = YOLO('yolo11m.pt')\n    \n    # Enhanced training configuration\n    model.train(\n        data=data_yaml_path,\n        epochs=epochs,\n        imgsz=640,\n        batch=16,\n        patience=50,  # Early stopping patience\n        augment=True,  # Enable built-in augmentations\n        mixup=0.3,    # Apply mixup augmentation\n        mosaic=1.0,   # Enable mosaic augmentation\n        scale=0.5,    # Random scaling\n        degrees=45,   # Random rotation\n        hsv_h=0.015,  # HSV augmentation for hue\n        hsv_s=0.7,    # HSV augmentation for saturation\n        hsv_v=0.4,    # HSV augmentation for value\n        translate=0.2, # Random translation\n        fliplr=0.5,   # Horizontal flip probability\n        flipud=0.2,   # Vertical flip probability\n        perspective=0.0005,  # Perspective transformation\n        overlap_mask=True,   # Mask overlap handling\n        mask_ratio=4,        # Mask downsampling ratio\n        dropout=0.2,         # Dropout for regularization\n        label_smoothing=0.1, # Label smoothing\n        optimizer='Adam',    # Use Adam optimizer\n        lr0=0.001,          # Initial learning rate\n        lrf=0.01,           # Final learning rate factor\n        momentum=0.937,     # SGD momentum/Adam beta1\n        weight_decay=0.0005,# Weight decay\n        warmup_epochs=3,    # Warmup epochs\n        warmup_momentum=0.8,# Warmup momentum\n        warmup_bias_lr=0.1, # Warmup bias learning rate\n        box=7.5,           # Box loss gain\n        cls=0.5,           # Classification loss gain\n        dfl=1.5,           # DFL loss gain\n        close_mosaic=10,   # Close mosaic for final epochs\n        amp=True,          # Automatic mixed precision\n        device=0           # GPU device\n    )\n\n# Usage example\nif __name__ == \"__main__\":\n    # Train the model with enhanced robustness\n    train_robust_model(\n        data_yaml_path=\"/kaggle/working/data.yaml\",\n        epochs=100\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T00:47:45.458258Z","iopub.execute_input":"2025-01-03T00:47:45.458584Z","iopub.status.idle":"2025-01-03T03:15:50.249938Z","shell.execute_reply.started":"2025-01-03T00:47:45.458560Z","shell.execute_reply":"2025-01-03T03:15:50.248989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r /kaggle/working/runs_v2.zip /kaggle/working/runs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:16:26.773867Z","iopub.execute_input":"2025-01-03T03:16:26.774190Z","iopub.status.idle":"2025-01-03T03:16:30.907059Z","shell.execute_reply.started":"2025-01-03T03:16:26.774159Z","shell.execute_reply":"2025-01-03T03:16:30.905997Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nfrom pathlib import Path\n\n# Initialize the model\nbest_model = YOLO('/kaggle/working/runs/detect/train3/weights/best.pt')\n\n# Get the list of images in the test folder\ntest_images_folder = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\nimage_paths = list(Path(test_images_folder).glob('*.*'))  # Adjust the file type filter if necessary\n\n# Select a random image\nrandom_image_path = random.choice(image_paths)\n\n# Perform inference on the random image\nresults = best_model.predict(str(random_image_path))\n\n# Iterate over the results\nfor result in results:\n    # Extracting bounding box coordinates in different formats\n    boxes_xyxy = result.boxes.xyxy  # Box with xyxy format, (N, 4)\n    boxes_xywh = result.boxes.xywh  # Box with xywh format, (N, 4)\n    boxes_xyxyn = result.boxes.xyxyn  # Box with xyxy format but normalized, (N, 4)\n    boxes_xywhn = result.boxes.xywhn  # Box with xywh format but normalized, (N, 4)\n\n    # Extracting confidence scores\n    confidence_scores = result.boxes.conf  # Confidence score, (N, 1)\n\n    # Extracting class labels\n    class_labels = result.boxes.cls  # Class labels, (N, 1)\n\n    # For segmentation (if available)\n    if hasattr(result.masks, 'masks'):\n        masks = result.masks.masks  # Masks, (N, H, W)\n    if hasattr(result.masks, 'segments'):\n        segments = result.masks.segments  # Bounding coordinates of masks, List[segment] * N\n\n    # For classification probabilities (if available)\n    if hasattr(result, 'probs'):\n        class_probabilities = result.probs  # Class probabilities, (num_class, )\n\n    # Print or use the extracted information\n    print(\"Bounding Boxes (xyxy format):\", boxes_xyxy)\n    print(\"Bounding Boxes (xywh format):\", boxes_xywh)\n    print(\"Bounding Boxes (xyxyn normalized):\", boxes_xyxyn)\n    print(\"Bounding Boxes (xywhn normalized):\", boxes_xywhn)\n    print(\"Confidence Scores:\", confidence_scores)\n    print(\"Class Labels:\", class_labels)\n\n    if hasattr(result.masks, 'masks'):\n        print(\"Masks:\", masks)\n    if hasattr(result.masks, 'segments'):\n        print(\"Segments:\", segments)\n    \n    if hasattr(result, 'probs'):\n        print(\"Class Probabilities:\", class_probabilities)\n\n# Display the image with bounding boxes and results\nresults[0].show()  # Display the first image's result with bounding boxes\nresults[0].save()  # Save the image with the predicted bounding boxes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T03:18:31.933532Z","iopub.execute_input":"2025-01-03T03:18:31.933825Z","iopub.status.idle":"2025-01-03T03:18:32.427922Z","shell.execute_reply.started":"2025-01-03T03:18:31.933803Z","shell.execute_reply":"2025-01-03T03:18:32.427065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport shutil\nfrom sklearn.model_selection import train_test_split\nimport cv2\nimport yaml\n\ndef rle_decode(mask_rle, shape):\n    if pd.isna(mask_rle):\n        return np.zeros(shape, dtype=np.uint8)\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        img[start:end] = 1\n    return img.reshape(shape)\n\ndef create_dataset_structure():\n    os.makedirs('dataset', exist_ok=True)\n    directories = [\n        'train/images',\n        'val/images',\n        'train/labels',\n        'val/labels'\n    ]\n    for dir_path in directories:\n        os.makedirs(os.path.join('dataset', dir_path), exist_ok=True)\n\ndef process_data(df, train_size=0.7):\n    image_names = df['ImageName'].unique()\n    train_images, val_images = train_test_split(image_names, train_size=train_size, random_state=42)\n    \n    for mode, image_subset in [('train', train_images), ('val', val_images)]:\n        for img_name in image_subset:\n            img_data = df[df['ImageName'] == img_name]\n            height = img_data['ImageHeight'].iloc[0]\n            width = img_data['ImageWidth'].iloc[0]\n            \n            for _, row in img_data.iterrows():\n                mask = rle_decode(row['Encoding'], (height, width))\n                class_id = int(row['ClassNumber']) - 1\n                mask_path = os.path.join('dataset', mode, 'labels', f'{img_name}_class{class_id}.png')\n                cv2.imwrite(mask_path, (mask * 255).astype(np.uint8))\n            \n            src_img = f\"/kaggle/input/ai-dl-multiclass-segmentation/TrainImages/{img_name}.jpg\"\n            dst_img = os.path.join('dataset', mode, 'images', f'{img_name}.jpg')\n            shutil.copy(src_img, dst_img)\n\ndef create_yaml():\n    yaml_content = {\n        'path': '/kaggle/working/dataset',  # Base path\n        'train': 'train/images',\n        'val': 'val/images',\n        'nc': 4,\n        'names': ['class0', 'class1', 'class2', 'class3']\n    }\n    \n    with open('dataset.yaml', 'w') as f:\n        yaml.safe_dump(yaml_content, f, sort_keys=False)\n\nif __name__ == \"__main__\":\n    df = pd.read_csv('/kaggle/input/ai-dl-multiclass-segmentation/train.csv')\n    create_dataset_structure()\n    process_data(df)\n    create_yaml()\n    print(\"Dataset preparation complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T19:14:51.864654Z","iopub.execute_input":"2025-01-03T19:14:51.864968Z","iopub.status.idle":"2025-01-03T19:15:20.707108Z","shell.execute_reply.started":"2025-01-03T19:14:51.864945Z","shell.execute_reply":"2025-01-03T19:15:20.706349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport shutil\nfrom sklearn.model_selection import train_test_split\nimport cv2\nimport yaml\n\ndef rle_decode(mask_rle, shape):\n    if pd.isna(mask_rle):\n        return np.zeros(shape, dtype=np.uint8)\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        img[start:end] = 1\n    return img.reshape(shape)\n\ndef mask_to_polygon(mask):\n    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    if not contours:\n        return None\n    \n    # Get the largest contour\n    largest_contour = max(contours, key=cv2.contourArea)\n    \n    # Simplify the contour\n    epsilon = 0.005 * cv2.arcLength(largest_contour, True)\n    approx = cv2.approxPolyDP(largest_contour, epsilon, True)\n    \n    return approx.squeeze()\n\ndef create_dataset_structure():\n    directories = ['dataset/train/images', 'dataset/val/images', \n                  'dataset/train/labels', 'dataset/val/labels']\n    for dir_path in directories:\n        os.makedirs(dir_path, exist_ok=True)\n\ndef process_data(df, train_size=0.7):\n    image_names = df['ImageName'].unique()\n    train_images, val_images = train_test_split(image_names, train_size=train_size, random_state=42)\n    \n    for mode, image_subset in [('train', train_images), ('val', val_images)]:\n        for img_name in image_subset:\n            img_data = df[df['ImageName'] == img_name]\n            height = img_data['ImageHeight'].iloc[0]\n            width = img_data['ImageWidth'].iloc[0]\n            \n            labels = []\n            for _, row in img_data.iterrows():\n                mask = rle_decode(row['Encoding'], (height, width))\n                class_id = int(row['ClassNumber']) - 1\n                \n                # Convert mask to polygon points\n                polygon = mask_to_polygon(mask.astype(np.uint8))\n                if polygon is not None:\n                    # Normalize coordinates\n                    polygon = polygon.astype(float)\n                    polygon[:, 0] /= width\n                    polygon[:, 1] /= height\n                    \n                    # Format: class_id x1 y1 x2 y2 ... xn yn\n                    points_flat = polygon.ravel()\n                    label_line = f\"{class_id} \" + \" \".join(map(str, points_flat))\n                    labels.append(label_line)\n            \n            if labels:\n                # Write labels\n                label_path = os.path.join('dataset', mode, 'labels', f'{img_name}.txt')\n                with open(label_path, 'w') as f:\n                    f.write('\\n'.join(labels))\n                \n                # Copy image\n                src_img = f\"/kaggle/input/ai-dl-multiclass-segmentation/TrainImages/{img_name}.jpg\"\n                dst_img = os.path.join('dataset', mode, 'images', f'{img_name}.jpg')\n                shutil.copy(src_img, dst_img)\n\ndef create_yaml():\n    yaml_content = {\n        'path': 'dataset',\n        'train': 'train/images',\n        'val': 'val/images',\n        'nc': 4,\n        'names': ['class0', 'class1', 'class2', 'class3']\n    }\n    with open('dataset.yaml', 'w') as f:\n        yaml.safe_dump(yaml_content, f, sort_keys=False)\n\nif __name__ == \"__main__\":\n    df = pd.read_csv('/kaggle/input/ai-dl-multiclass-segmentation/train.csv')\n    create_dataset_structure()\n    process_data(df)\n    create_yaml()\n    print(\"Dataset preparation complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T19:16:26.403498Z","iopub.execute_input":"2025-01-03T19:16:26.403821Z","iopub.status.idle":"2025-01-03T19:16:34.686282Z","shell.execute_reply.started":"2025-01-03T19:16:26.403797Z","shell.execute_reply":"2025-01-03T19:16:34.685285Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml\n\n# Get the absolute paths\ntrain_path = '/kaggle/working/dataset/images/train'\nval_path = '/kaggle/working/dataset/images/val'\n\n# Create YAML configuration\nyaml_content = {\n    'path': '/kaggle/working/dataset',  # Root path to dataset\n    'train': '/kaggle/working/dataset/train/images',  # Relative path to train images\n    'val': '/kaggle/working/dataset/val/images',      # Relative path to val images\n    'test': '',              # Leave empty if no test set\n    \n    'names': {               # Class names and indices\n        0: 'class0',\n        1: 'class1',\n        2: 'class2',\n        3: 'class3'\n    },\n    \n    'nc': 4,                # Number of classes\n    'task': 'segment'       # Task type: segment for segmentation\n}\n\n# Save the YAML file\nwith open('/kaggle/working/dataset.yaml', 'w') as f:\n    yaml.safe_dump(yaml_content, f, default_flow_style=False, sort_keys=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T19:16:53.809549Z","iopub.execute_input":"2025-01-03T19:16:53.809878Z","iopub.status.idle":"2025-01-03T19:16:53.815936Z","shell.execute_reply.started":"2025-01-03T19:16:53.809850Z","shell.execute_reply":"2025-01-03T19:16:53.815032Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport cv2\nimport numpy as np\nimport torch\nfrom torch.utils.data import Dataset\nimport os\nfrom PIL import Image\n\nclass RobustDetectionDataset(Dataset):\n    def __init__(self, image_dir, label_dir, transform=None):\n        self.image_dir = image_dir\n        self.label_dir = label_dir\n        self.image_files = [f for f in os.listdir(image_dir) if f.endswith(('.jpg', '.png', '.jpeg'))]\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_files)\n\n    def __getitem__(self, idx):\n        # Load image and labels\n        img_name = self.image_files[idx]\n        img_path = os.path.join(self.image_dir, img_name)\n        label_path = os.path.join(self.label_dir, img_name.rsplit('.', 1)[0] + '.txt')\n        \n        # Read image\n        image = cv2.imread(img_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        # Read labels\n        boxes = []\n        classes = []\n        if os.path.exists(label_path):\n            with open(label_path, 'r') as f:\n                for line in f.readlines():\n                    class_id, x_center, y_center, width, height = map(float, line.strip().split())\n                    boxes.append([x_center, y_center, width, height])\n                    classes.append(class_id)\n        \n        # Convert to numpy arrays\n        boxes = np.array(boxes)\n        classes = np.array(classes)\n        \n        # Apply transformations\n        if self.transform:\n            transformed = self.transform(image=image, bboxes=boxes)\n            image = transformed['image']\n            boxes = transformed['bboxes']\n        \n        return image, boxes, classes\n\n# Define robust augmentation pipeline\ndef get_robust_transforms(height=640, width=640):\n    return A.Compose([\n        # Scale invariance\n        A.RandomScale(scale_limit=0.5, p=0.5),\n        A.LongestMaxSize(max_size=height),\n        A.PadIfNeeded(height, width, border_mode=cv2.BORDER_CONSTANT),\n        \n        # Rotation invariance\n        A.RandomRotate90(p=0.5),\n        A.Rotate(limit=45, p=0.5),\n        \n        # Lighting invariance\n        A.OneOf([\n            A.RandomBrightness(limit=0.2, p=0.5),\n            A.RandomContrast(limit=0.2, p=0.5),\n            A.RandomGamma(gamma_limit=(80, 120), p=0.5)\n        ], p=0.5),\n        \n        # Color invariance\n        A.OneOf([\n            A.RGBShift(r_shift_limit=20, g_shift_limit=20, b_shift_limit=20, p=0.5),\n            A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5)\n        ], p=0.5),\n        \n        # Additional robustness\n        A.OneOf([\n            A.GaussNoise(var_limit=(10.0, 50.0), p=0.5),\n            A.ISONoise(color_shift=(0.01, 0.05), intensity=(0.1, 0.5), p=0.5),\n            A.GaussianBlur(blur_limit=(3, 7), p=0.5)\n        ], p=0.3),\n        \n        # Weather simulation\n        A.OneOf([\n            A.RandomFog(fog_coef_lower=0.1, fog_coef_upper=0.3, p=0.5),\n            A.RandomShadow(shadow_roi=(0, 0.5, 1, 1), p=0.5),\n            A.RandomSunFlare(flare_roi=(0, 0, 1, 0.5), p=0.5)\n        ], p=0.2),\n        \n        ToTensorV2()\n    ], bbox_params=A.BboxParams(format='yolo', label_fields=['classes']))\n\ndef train_robust_model(data_yaml_path, epochs=100):\n    # Initialize model\n    model = YOLO('yolo11l-seg.pt')\n    \n    # Enhanced training configuration\n    model.train(\n        data=data_yaml_path,\n        epochs=epochs,\n        imgsz=640,\n        batch=16,\n        patience=50,  # Early stopping patience\n        augment=True,  # Enable built-in augmentations\n        mixup=0.3,    # Apply mixup augmentation\n        mosaic=1.0,   # Enable mosaic augmentation\n        scale=0.5,    # Random scaling\n        degrees=45,   # Random rotation\n        hsv_h=0.015,  # HSV augmentation for hue\n        hsv_s=0.7,    # HSV augmentation for saturation\n        hsv_v=0.4,    # HSV augmentation for value\n        translate=0.2, # Random translation\n        fliplr=0.5,   # Horizontal flip probability\n        flipud=0.2,   # Vertical flip probability\n        perspective=0.0005,  # Perspective transformation\n        overlap_mask=True,   # Mask overlap handling\n        mask_ratio=4,        # Mask downsampling ratio\n        dropout=0.2,         # Dropout for regularization\n        label_smoothing=0.1, # Label smoothing\n        optimizer='Adam',    # Use Adam optimizer\n        lr0=0.001,          # Initial learning rate\n        lrf=0.01,           # Final learning rate factor\n        momentum=0.937,     # SGD momentum/Adam beta1\n        weight_decay=0.0005,# Weight decay\n        warmup_epochs=3,    # Warmup epochs\n        warmup_momentum=0.8,# Warmup momentum\n        warmup_bias_lr=0.1, # Warmup bias learning rate\n        box=7.5,           # Box loss gain\n        cls=0.5,           # Classification loss gain\n        dfl=1.5,           # DFL loss gain\n        close_mosaic=10,   # Close mosaic for final epochs\n        amp=True,          # Automatic mixed precision\n        device=0           # GPU device\n    )\n\n# Usage example\nif __name__ == \"__main__\":\n    # Train the model with enhanced robustness\n    train_robust_model(\n        data_yaml_path=\"/kaggle/working/dataset.yaml\",\n        epochs=100\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T19:47:18.442506Z","iopub.execute_input":"2025-01-03T19:47:18.442808Z","iopub.status.idle":"2025-01-03T23:19:58.189006Z","shell.execute_reply.started":"2025-01-03T19:47:18.442784Z","shell.execute_reply":"2025-01-03T23:19:58.188076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\nfrom ultralytics import YOLO\nimport numpy as np\n\n# Initialize the model with the trained weights\nbest_model = YOLO('/kaggle/working/runs/segment/train5/weights/best.pt')\n\n# Get the list of images in the test folder\ntest_images_folder = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\nimage_paths = list(Path(test_images_folder).glob('*.*'))  # Adjust file type filter as needed\n\n# Select a random image for inference\nrandom_image_path = random.choice(image_paths)\n\n# Perform inference on the random image\nresults = best_model.predict(str(random_image_path))\n\n# Debugging: print the result structure to understand what's inside\nprint(f\"Results: {results}\")\n\n# Iterate over the results (YOLOv8 supports multi-image inference)\nfor result in results:\n    print(f\"Result: {result}\")  # Print the entire result to inspect its contents\n    \n    # Check if masks are available\n    if hasattr(result.masks, 'data') and result.masks.data is not None:\n        masks = result.masks.data  # Masks data (N, H, W) where N is number of objects\n        print(f\"Masks found: {masks.shape[0]} masks detected.\")\n        \n        # Prepare the image for visualization\n        img = result.orig_img  # Get the original image\n        plt.imshow(img)  # Display the image\n\n        # Overlay the segmentation masks on the image\n        for i in range(masks.shape[0]):  # Loop through all the masks\n            mask = masks[i, :, :].cpu().numpy()  # Move the mask to CPU and convert to numpy array\n            plt.imshow(mask, alpha=0.5)  # Apply alpha for transparency\n\n        # Show the image with only masks\n        plt.axis('off')  # Turn off axis for clean visualization\n        plt.show()\n\n    else:\n        print(\"No masks found in the result.\")\n\n    # Optionally, save the image with the masks\n#    result.save()  # Save the image with the segmentation masks\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T23:37:49.538377Z","iopub.execute_input":"2025-01-03T23:37:49.538605Z","iopub.status.idle":"2025-01-03T23:37:50.569846Z","shell.execute_reply.started":"2025-01-03T23:37:49.538585Z","shell.execute_reply":"2025-01-03T23:37:50.568936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\nfrom ultralytics import YOLO\nimport numpy as np\nimport cv2  # OpenCV for resizing\n\n# Initialize the model with the trained weights\nbest_model = YOLO('/kaggle/working/runs/segment/train5/weights/best.pt')\n\n# Get the list of images in the test folder\ntest_images_folder = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\nimage_paths = list(Path(test_images_folder).glob('*.*'))  # Adjust file type filter as needed\n\n# Select a random image for inference\nrandom_image_path = random.choice(image_paths)\n\n# Perform inference on the random image\nresults = best_model.predict(str(random_image_path))\n\n# Debugging: print the result structure to understand what's inside\nprint(f\"Results: {results}\")\n\n# Iterate over the results (YOLOv8 supports multi-image inference)\nfor result in results:\n    print(f\"Result: {result}\")  # Print the entire result to inspect its contents\n    \n    # Check if masks are available\n    if hasattr(result.masks, 'data') and result.masks.data is not None:\n        masks = result.masks.data  # Masks data (N, H, W) where N is number of objects\n        print(f\"Masks found: {masks.shape[0]} masks detected.\")\n        \n        # Prepare the image for visualization\n        img = result.orig_img  # Get the original image\n        original_height, original_width = img.shape[:2]  # Get the dimensions of the original image\n        \n        # Show the original image\n        plt.figure(figsize=(10, 10))\n        plt.imshow(img)  # Display the original image\n        plt.title(\"Original Image\")\n        plt.axis('off')  # Turn off axis for clean visualization\n        plt.show()\n\n        # Create an empty black image with the same dimensions as the original image\n        black_background = np.zeros_like(img, dtype=np.uint8)\n\n        # Overlay the segmentation masks on the new black background\n        for i in range(masks.shape[0]):  # Loop through all the masks\n            mask = masks[i, :, :].cpu().numpy()  # Move the mask to CPU and convert to numpy array\n            \n            # Resize the mask to match the original image size\n            mask_resized = cv2.resize(mask, (original_width, original_height), interpolation=cv2.INTER_NEAREST)\n\n            # Set pixels where the mask is 1 to white (255) in the black background\n            black_background[mask_resized == 1] = 255\n\n        # Display the mask-only image (white mask on black background)\n        plt.figure(figsize=(10, 10))\n        plt.imshow(black_background)  # Display the mask image\n        plt.title(\"Segmentation Mask (White on Black Background)\")\n        plt.axis('off')  # Turn off axis for clean visualization\n        plt.show()\n\n    else:\n        print(\"No masks found in the result.\")\n\n    # Optionally, save the image with the masks\n    result.save()  # Save the image with the segmentation masks","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T23:45:53.568658Z","iopub.execute_input":"2025-01-03T23:45:53.568976Z","iopub.status.idle":"2025-01-03T23:45:54.848580Z","shell.execute_reply.started":"2025-01-03T23:45:53.568953Z","shell.execute_reply":"2025-01-03T23:45:54.847690Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\nfrom ultralytics import YOLO\nimport numpy as np\nimport cv2  # OpenCV for resizing\n\n# Initialize the model with the trained weights\nbest_model = YOLO('/kaggle/working/runs/segment/train5/weights/best.pt')\n\n# Get the list of images in the test folder\ntest_images_folder = '/kaggle/input/ai-dl-multiclass-segmentation/TestImages'\nimage_paths = list(Path(test_images_folder).glob('*.*'))  # Adjust file type filter as needed\n\n# Select a random image for inference\nrandom_image_path = random.choice(image_paths)\n\n# Perform inference on the random image\nresults = best_model.predict(str(random_image_path))\n\n# Iterate over the results (YOLOv8 supports multi-image inference)\nfor result in results:\n    print(f\"Result: {result}\")  # Print the entire result to inspect its contents\n    \n    # Check if masks are available\n    if hasattr(result.masks, 'data') and result.masks.data is not None:\n        masks = result.masks.data  # Masks data (N, H, W) where N is number of objects\n        print(f\"Masks found: {masks.shape[0]} masks detected.\")\n        \n        # Prepare the image for visualization\n        img = result.orig_img  # Get the original image\n        original_height, original_width = img.shape[:2]  # Get the dimensions of the original image\n        \n        # Show the original image\n        plt.figure(figsize=(10, 10))\n        plt.imshow(img)  # Display the original image\n        plt.title(\"Original Image\")\n        plt.axis('off')  # Turn off axis for clean visualization\n        plt.show()\n\n        # Create an empty black image with the same dimensions as the original image\n        black_background = np.zeros_like(img, dtype=np.uint8)\n\n        # Overlay the segmentation masks on the new black background\n        for i in range(masks.shape[0]):  # Loop through all the masks\n            mask = masks[i, :, :].cpu().numpy()  # Move the mask to CPU and convert to numpy array\n            \n            # Ensure the mask is binary (0 or 1)\n            mask[mask > 0] = 1  # Set all non-zero values to 1 (binary mask)\n\n            # Resize the mask to match the original image size\n            mask_resized = cv2.resize(mask, (original_width, original_height), interpolation=cv2.INTER_NEAREST)\n\n            # Set pixels where the mask is 1 to white (255) in the black background\n            black_background[mask_resized == 1] = 255\n\n        # Display the mask-only image (white mask on black background)\n        plt.figure(figsize=(10, 10))\n        plt.imshow(black_background)  # Display the mask image\n        plt.title(\"Segmentation Mask (White on Black Background)\")\n        plt.axis('off')  # Turn off axis for clean visualization\n        plt.show()\n\n    else:\n        print(\"No masks found in the result.\")\n\n    # Optionally, save the image with the masks\n    result.save()  # Save the image with the segmentation masks\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-03T23:46:53.517914Z","iopub.execute_input":"2025-01-03T23:46:53.518220Z","iopub.status.idle":"2025-01-03T23:46:55.312745Z","shell.execute_reply.started":"2025-01-03T23:46:53.518197Z","shell.execute_reply":"2025-01-03T23:46:55.312030Z"}},"outputs":[],"execution_count":null}]}