{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":1462296,"sourceType":"datasetVersion","datasetId":857191}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pycocotools","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:39:54.493594Z","iopub.execute_input":"2025-10-17T12:39:54.493746Z","iopub.status.idle":"2025-10-17T12:39:59.433965Z","shell.execute_reply.started":"2025-10-17T12:39:54.493731Z","shell.execute_reply":"2025-10-17T12:39:59.433013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom PIL import Image\nimport cv2\nfrom pycocotools.coco import COCO ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:40:06.431579Z","iopub.execute_input":"2025-10-17T12:40:06.432235Z","iopub.status.idle":"2025-10-17T12:40:07.697893Z","shell.execute_reply.started":"2025-10-17T12:40:06.432207Z","shell.execute_reply":"2025-10-17T12:40:07.697285Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset_root = '/kaggle/input/coco-2017-dataset/coco2017'\nprint(\"Contents of the root dataset directory:\")\nprint(os.listdir(dataset_root))\n# If 'coco2017' exists, check inside it\nif 'coco2017' in os.listdir(dataset_root):\n    coco2017_dir = os.path.join(dataset_root, 'coco2017')\n    print(\"\\nContents of coco2017 directory:\")\n    print(os.listdir(coco2017_dir))\n    \n    # Check for annotations\n    if 'annotations' in os.listdir(coco2017_dir):\n        annotations_dir = os.path.join(coco2017_dir, 'annotations')\n        print(\"\\nContents of annotations directory:\")\n        print(os.listdir(annotations_dir))\n    \n    # Check for images\n    if 'train2017' in os.listdir(coco2017_dir):\n        train2017_dir = os.path.join(coco2017_dir, 'train2017')\n        print(\"\\nContents of train2017 directory (first few files):\")\n        print(os.listdir(train2017_dir)[:10])  # Show only the first 10 for brevity\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:43:29.267899Z","iopub.execute_input":"2025-10-17T12:43:29.268217Z","iopub.status.idle":"2025-10-17T12:43:29.277916Z","shell.execute_reply.started":"2025-10-17T12:43:29.268198Z","shell.execute_reply":"2025-10-17T12:43:29.277146Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dir = '/kaggle/input/coco-2017-dataset/coco2017/'  # Root of the COCO data\nann_file = os.path.join(data_dir, 'annotations', 'instances_train2017.json')  # Path to annotations\nimg_dir = os.path.join(data_dir, 'train2017/')  # Path to training images\n# Load COCO annotations\nfrom pycocotools.coco import COCO\ncoco = COCO(ann_file)\n# Get category names (e.g., person, car, etc.)\ncatIds = coco.getCatIds()\ncategories = coco.loadCats(catIds)\ncategory_names = [cat['name'] for cat in categories]\nprint(f\"Categories in COCO 2017: {category_names}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:43:42.861472Z","iopub.execute_input":"2025-10-17T12:43:42.862231Z","iopub.status.idle":"2025-10-17T12:44:02.701637Z","shell.execute_reply.started":"2025-10-17T12:43:42.862197Z","shell.execute_reply":"2025-10-17T12:44:02.700807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imgIds = coco.getImgIds()  # List of image IDs\nnum_images = len(imgIds)\nnum_annotations = len(coco.anns)  # Total annotations\n# Get per-image stats (e.g., number of objects per image)\nann_per_image = {}\nfor imgId in imgIds:\n    annIds = coco.getAnnIds(imgIds=imgId)\n    ann_per_image[imgId] = len(annIds)\n# Convert to DataFrame for easier analysis\ndf_eda = pd.DataFrame({\n    'Image ID': imgIds,\n    'Number of Annotations': [ann_per_image[imgId] for imgId in imgIds]\n})\nprint(f\"Total images: {num_images}\")\nprint(f\"Total annotations: {num_annotations}\")\nprint(df_eda.describe())  # Summary statistics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:44:20.378474Z","iopub.execute_input":"2025-10-17T12:44:20.378739Z","iopub.status.idle":"2025-10-17T12:44:21.095087Z","shell.execute_reply.started":"2025-10-17T12:44:20.37872Z","shell.execute_reply":"2025-10-17T12:44:21.094438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_counts = {}\nfor cat in categories:\n    catId = cat['id']\n    annIds = coco.getAnnIds(catIds=[catId])\n    cat_counts[cat['name']] = len(annIds)\n# Convert to DataFrame and plot\ndf_cat_counts = pd.DataFrame(list(cat_counts.items()), columns=['Category', 'Count'])\ndf_cat_counts = df_cat_counts.sort_values(by='Count', ascending=False)\nplt.figure(figsize=(12, 6))\nsns.barplot(x='Count', y='Category', data=df_cat_counts)\nplt.title('Distribution of Categories in COCO 2017')\nplt.xlabel('Number of Annotations')\nplt.ylabel('Category')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:44:34.255884Z","iopub.execute_input":"2025-10-17T12:44:34.256537Z","iopub.status.idle":"2025-10-17T12:44:40.904837Z","shell.execute_reply.started":"2025-10-17T12:44:34.256512Z","shell.execute_reply":"2025-10-17T12:44:40.904075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_sizes = []\nfor imgId in imgIds[:100]:  # Limit to first 100 for speed\n    img_info = coco.loadImgs(imgId)[0]\n    img_path = os.path.join(img_dir, img_info['file_name'])\n    with Image.open(img_path) as img:\n        width, height = img.size\n        image_sizes.append((width, height))\ndf_sizes = pd.DataFrame(image_sizes, columns=['Width', 'Height'])\nplt.figure(figsize=(10, 5))\nsns.histplot(df_sizes['Width'], kde=True, color='blue', label='Width')\nsns.histplot(df_sizes['Height'], kde=True, color='red', label='Height')\nplt.title('Distribution of Image Widths and Heights')\nplt.xlabel('Pixels')\nplt.ylabel('Frequency')\nplt.legend()\nplt.show()\nprint(df_sizes.describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:45:17.696634Z","iopub.execute_input":"2025-10-17T12:45:17.696897Z","iopub.status.idle":"2025-10-17T12:45:18.80814Z","shell.execute_reply.started":"2025-10-17T12:45:17.696878Z","shell.execute_reply":"2025-10-17T12:45:18.807585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def visualize_image(img_id):\n    img_info_list = coco.loadImgs(img_id)  # Get the image info\n    if img_info_list is None or len(img_info_list) == 0:\n        print(f\"Error: No image info found for ID {img_id}. This ID might be invalid.\")\n        return  # Exit the function if no info is found\n    img_info = img_info_list[0]  # Safely access the first element\n    img_path = os.path.join(img_dir, img_info['file_name'])  # Now build the path\n    if not os.path.exists(img_path):  # Check if the file actually exists\n        print(f\"Error: Image file not found at {img_path}\")\n        return\n    \n    img = cv2.imread(img_path)\n    if img is None:  # Check if cv2 could load the image\n        print(f\"Error: Could not load image at {img_path}\")\n        return\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert to RGB for Matplotlib\n    \n    annIds = coco.getAnnIds(imgIds=img_id)\n    anns = coco.loadAnns(annIds)\n    \n    plt.figure(figsize=(10, 10))\n    plt.imshow(img)\n    for ann in anns:\n        bbox = ann['bbox']  # [x, y, width, height]\n        category = coco.loadCats(ann['category_id'])[0]['name']\n        # Draw rectangle\n        cv2.rectangle(img, (int(bbox[0]), int(bbox[1]))), (int(bbox[0]+bbox[2]), int(bbox[1]+bbox[3]), (255, 0, 0), 2)\n        # Note: We're drawing on a copy of img, but for display, use plt\n        plt.gca().add_patch(plt.Rectangle((bbox[0], bbox[1]), bbox[2], bbox[3],\n                                          edgecolor='blue', facecolor='none', lw=2))\n        plt.text(bbox[0], bbox[1]-10, category, color='white', fontsize=8, \n                 bbox=dict(facecolor='red', alpha=0.5))\n    \n    plt.axis('off')\n    plt.title(f'Image ID: {img_id}')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:48:20.497006Z","iopub.execute_input":"2025-10-17T12:48:20.497531Z","iopub.status.idle":"2025-10-17T12:48:20.504473Z","shell.execute_reply.started":"2025-10-17T12:48:20.497506Z","shell.execute_reply":"2025-10-17T12:48:20.503782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess_image(img_path, target_size=(224, 224)):\n    img = cv2.imread(img_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert to RGB\n    img_resized = cv2.resize(img, target_size)  # Resize\n    img_normalized = img_resized / 255.0  # Normalize to [0, 1]\n    return img_normalized\n# Example: Preprocess a sample image\nsample_img_id = imgIds[0]  # First image\nsample_img_info = coco.loadImgs(sample_img_id)[0]\nsample_img_path = os.path.join(img_dir, sample_img_info['file_name'])\npreprocessed_img = preprocess_image(sample_img_path)\nplt.imshow(preprocessed_img)\nplt.title('Preprocessed Image (Resized and Normalized)')\nplt.axis('off')\nplt.show()\nprint(f\"Original shape: {sample_img_info['width']}x{sample_img_info['height']}\")\nprint(f\"Preprocessed shape: {preprocessed_img.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:48:46.548864Z","iopub.execute_input":"2025-10-17T12:48:46.549139Z","iopub.status.idle":"2025-10-17T12:48:46.867543Z","shell.execute_reply.started":"2025-10-17T12:48:46.549119Z","shell.execute_reply":"2025-10-17T12:48:46.866823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def adjust_bbox(bbox, original_size, target_size):\n    x, y, w, h = bbox\n    orig_width, orig_height = original_size\n    target_width, target_height = target_size\n    \n    x_scaled = (x / orig_width) * target_width\n    y_scaled = (y / orig_height) * target_height\n    w_scaled = (w / orig_width) * target_width\n    h_scaled = (h / orig_height) * target_height\n    \n    return [x_scaled, y_scaled, w_scaled, h_scaled]\n# Example: Adjust a bbox for a sample image\nannIds = coco.getAnnIds(imgIds=sample_img_id)\nanns = coco.loadAnns(annIds)\noriginal_size = (sample_img_info['width'], sample_img_info['height'])\ntarget_size = (224, 224)\nfor ann in anns:\n    adjusted_bbox = adjust_bbox(ann['bbox'], original_size, target_size)\n    print(f\"Original bbox: {ann['bbox']}\")\n    print(f\"Adjusted bbox: {adjusted_bbox}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:49:01.954522Z","iopub.execute_input":"2025-10-17T12:49:01.954768Z","iopub.status.idle":"2025-10-17T12:49:01.960953Z","shell.execute_reply.started":"2025-10-17T12:49:01.954751Z","shell.execute_reply":"2025-10-17T12:49:01.960222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics\n!pip install --upgrade pip ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:53:58.547511Z","iopub.execute_input":"2025-10-17T12:53:58.547798Z","iopub.status.idle":"2025-10-17T12:55:29.727196Z","shell.execute_reply.started":"2025-10-17T12:53:58.54778Z","shell.execute_reply":"2025-10-17T12:55:29.726431Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml\n# Define your dataset paths (update if needed)\ntrain_path = os.path.join(data_dir, 'train2017')  # Path to training images\nval_path = os.path.join(data_dir, 'val2017')  # Path to validation images\nann_train_path = os.path.join(data_dir, 'annotations', 'instances_train2017.json')\nann_val_path = os.path.join(data_dir, 'annotations', 'instances_val2017.json')\n# Get COCO categories (from your earlier code)\ncategories = coco.loadCats(coco.getCatIds())\nclass_names = [cat['name'] for cat in categories]\n# Create the YAML content\ndata_yaml = {\n    'path': data_dir,  # Root path\n    'train': train_path,\n    'val': val_path,\n    'nc': len(class_names),  # Number of classes\n    'names': class_names  # List of class names\n}\n# Save the YAML file to the working directory\nwith open('/kaggle/working/coco2017.yaml', 'w') as file:\n    yaml.dump(data_yaml, file)\nprint(\"YAML file created at /kaggle/working/coco2017.yaml\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:55:34.400011Z","iopub.execute_input":"2025-10-17T12:55:34.400799Z","iopub.status.idle":"2025-10-17T12:55:34.430601Z","shell.execute_reply.started":"2025-10-17T12:55:34.400767Z","shell.execute_reply":"2025-10-17T12:55:34.42996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\nimport torch\n# Check if GPU is available (Kaggle usually has a T4 GPU)\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f\"Using device: {device}\")\n# Load a pre-trained YOLOv8 model (e.g., nano for speed)\nmodel = YOLO('yolov8n.pt')  # Download pre-trained weights if not cached\n# Train the model\nmodel.train(\n    data='/kaggle/working/coco2017.yaml',  # Path to your YAML file\n    epochs=50,  # Start with 50 epochs; adjust based on time/resources\n    batch=16,  # Batch size; lower if you run out of memory\n    imgsz=640,  # Image size; matches COCO's common size\n    device=device,  # Use GPU if available\n    workers=2,  # Number of worker threads\n    # Optional: Add subset for faster training, e.g., via custom data loader\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T12:55:58.049581Z","iopub.execute_input":"2025-10-17T12:55:58.050253Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}