{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":31703,"databundleVersionId":2871752,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5\n%cd yolov5\n!pip install -r requirements.txt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-06T22:23:57.785386Z","iopub.execute_input":"2024-06-06T22:23:57.78626Z","iopub.status.idle":"2024-06-06T22:24:13.530655Z","shell.execute_reply.started":"2024-06-06T22:23:57.786213Z","shell.execute_reply":"2024-06-06T22:24:13.529743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport shutil\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:07.030342Z","iopub.execute_input":"2024-06-06T22:25:07.030719Z","iopub.status.idle":"2024-06-06T22:25:07.933495Z","shell.execute_reply.started":"2024-06-06T22:25:07.030689Z","shell.execute_reply":"2024-06-06T22:25:07.932719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv_path = '/kaggle/input/tensorflow-great-barrier-reef/train.csv'\ntest_csv_path = '/kaggle/input/tensorflow-great-barrier-reef/test.csv'\ntrain_images_path = '/kaggle/input/tensorflow-great-barrier-reef/train_images'\ndataset_path = '/kaggle/working/dataset'\nimage_folder = os.path.join(dataset_path, 'images')\nlabel_folder = os.path.join(dataset_path, 'labels')","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:13.687233Z","iopub.execute_input":"2024-06-06T22:25:13.687717Z","iopub.status.idle":"2024-06-06T22:25:13.692945Z","shell.execute_reply.started":"2024-06-06T22:25:13.687688Z","shell.execute_reply":"2024-06-06T22:25:13.69195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(os.path.join(image_folder, 'train'), exist_ok=True)\nos.makedirs(os.path.join(image_folder, 'val'), exist_ok=True)\nos.makedirs(os.path.join(image_folder, 'test'), exist_ok=True)\nos.makedirs(os.path.join(label_folder, 'train'), exist_ok=True)\nos.makedirs(os.path.join(label_folder, 'val'), exist_ok=True)\nos.makedirs(os.path.join(label_folder, 'test'), exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:19.84915Z","iopub.execute_input":"2024-06-06T22:25:19.849517Z","iopub.status.idle":"2024-06-06T22:25:19.856675Z","shell.execute_reply.started":"2024-06-06T22:25:19.849486Z","shell.execute_reply":"2024-06-06T22:25:19.855783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(train_csv_path)\ntest_df = pd.read_csv(test_csv_path)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:22.973097Z","iopub.execute_input":"2024-06-06T22:25:22.973514Z","iopub.status.idle":"2024-06-06T22:25:23.040282Z","shell.execute_reply.started":"2024-06-06T22:25:22.973483Z","shell.execute_reply":"2024-06-06T22:25:23.03948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[train_df['annotations'] != '[]']","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:27.712277Z","iopub.execute_input":"2024-06-06T22:25:27.713203Z","iopub.status.idle":"2024-06-06T22:25:27.729397Z","shell.execute_reply.started":"2024-06-06T22:25:27.713149Z","shell.execute_reply":"2024-06-06T22:25:27.728485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df[['video_id', 'video_frame', 'annotations']].head(10))\n","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:32.028326Z","iopub.execute_input":"2024-06-06T22:25:32.028688Z","iopub.status.idle":"2024-06-06T22:25:32.043391Z","shell.execute_reply.started":"2024-06-06T22:25:32.028658Z","shell.execute_reply":"2024-06-06T22:25:32.042379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_to_yolo(x, y, w, h, img_width, img_height):\n    x_center = (x + w / 2) / img_width\n    y_center = (y + h / 2) / img_height\n    w = w / img_width\n    h = h / img_height\n    return x_center, y_center, w, h","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:38.281412Z","iopub.execute_input":"2024-06-06T22:25:38.281805Z","iopub.status.idle":"2024-06-06T22:25:38.287322Z","shell.execute_reply.started":"2024-06-06T22:25:38.281772Z","shell.execute_reply":"2024-06-06T22:25:38.286308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set, val_set = train_test_split(train_df, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:43.934225Z","iopub.execute_input":"2024-06-06T22:25:43.934521Z","iopub.status.idle":"2024-06-06T22:25:43.945049Z","shell.execute_reply.started":"2024-06-06T22:25:43.934494Z","shell.execute_reply":"2024-06-06T22:25:43.944177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_row(row, img_folder, lbl_folder, img_width=1280, img_height=720):\n    img_path = os.path.join(train_images_path, f'video_{row[\"video_id\"]}', f'{row[\"video_frame\"]}.jpg')\n    annotations = eval(row['annotations'])  # Assuming annotations are stored as a string representation of a list of dictionaries\n    \n    if os.path.exists(img_path):\n        shutil.copy(img_path, img_folder)\n        \n        label_file = os.path.splitext(os.path.basename(img_path))[0] + '.txt'\n        with open(os.path.join(lbl_folder, label_file), 'w') as f:\n            for ann in annotations:\n                if 'x' in ann and 'y' in ann and 'width' in ann and 'height' in ann:\n                    bbox = [ann['x'], ann['y'], ann['width'], ann['height']]\n                    if not any(pd.isna(bbox)):  # Check if there are no NaN values in bbox\n                        x, y, w, h = convert_to_yolo(bbox[0], bbox[1], bbox[2], bbox[3], img_width, img_height)\n                        f.write(f\"0 {x} {y} {w} {h}\\n\")  # Assuming single class with id 0\n                    else:\n                        print(f\"Skipping invalid bbox: {bbox}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:49.303975Z","iopub.execute_input":"2024-06-06T22:25:49.304362Z","iopub.status.idle":"2024-06-06T22:25:49.313785Z","shell.execute_reply.started":"2024-06-06T22:25:49.30432Z","shell.execute_reply":"2024-06-06T22:25:49.312684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:54.371358Z","iopub.execute_input":"2024-06-06T22:25:54.371929Z","iopub.status.idle":"2024-06-06T22:25:54.379022Z","shell.execute_reply.started":"2024-06-06T22:25:54.371891Z","shell.execute_reply":"2024-06-06T22:25:54.378138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport shutil\nfrom sklearn.model_selection import train_test_split\nfrom concurrent.futures import ThreadPoolExecutor, as_completed\nfrom tqdm import tqdm\n\ndef process_dataset(df, img_folder, lbl_folder):\n    with ThreadPoolExecutor(max_workers=8) as executor:\n        futures = [executor.submit(process_row, row, img_folder, lbl_folder) for _, row in df.iterrows()]\n        for _ in tqdm(as_completed(futures), total=len(futures)):\n            pass\n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:25:58.867293Z","iopub.execute_input":"2024-06-06T22:25:58.868091Z","iopub.status.idle":"2024-06-06T22:25:58.880669Z","shell.execute_reply.started":"2024-06-06T22:25:58.868055Z","shell.execute_reply":"2024-06-06T22:25:58.879773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_dataset(train_set, os.path.join(image_folder, 'train'), os.path.join(label_folder, 'train'))\nprocess_dataset(val_set, os.path.join(image_folder, 'val'), os.path.join(label_folder, 'val'))\nprocess_dataset(test_df, os.path.join(image_folder, 'test'), os.path.join(label_folder, 'test'))","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:26:02.009506Z","iopub.execute_input":"2024-06-06T22:26:02.00998Z","iopub.status.idle":"2024-06-06T22:26:12.872486Z","shell.execute_reply.started":"2024-06-06T22:26:02.009945Z","shell.execute_reply":"2024-06-06T22:26:12.871507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking file\nlabel_dir = os.path.join(label_folder, 'train')\nlabel_files = os.listdir(label_dir)\nfor label_file in label_files[:5]:\n    with open(os.path.join(label_dir, label_file), 'r') as f:\n        print(f\"Contents of {label_file}:\")\n        print(f.read())","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:26:21.91648Z","iopub.execute_input":"2024-06-06T22:26:21.917096Z","iopub.status.idle":"2024-06-06T22:26:21.926517Z","shell.execute_reply.started":"2024-06-06T22:26:21.917058Z","shell.execute_reply":"2024-06-06T22:26:21.925534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create dataset.yaml file\ndataset_yaml = f\"\"\"\ntrain: {os.path.join(image_folder, 'train')}\nval: {os.path.join(image_folder, 'val')}\ntest: {os.path.join(image_folder, 'test')}\nnc: 1\nnames: ['COTS']\n\"\"\"\nwith open(os.path.join(dataset_path, 'dataset.yaml'), 'w') as f:\n    f.write(dataset_yaml)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:26:26.747013Z","iopub.execute_input":"2024-06-06T22:26:26.747665Z","iopub.status.idle":"2024-06-06T22:26:28.097988Z","shell.execute_reply.started":"2024-06-06T22:26:26.747631Z","shell.execute_reply":"2024-06-06T22:26:28.09702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --img 640 --batch 16 --epochs 10 --data {os.path.join(dataset_path, 'dataset.yaml')} --weights yolov5s.pt","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:36:19.5201Z","iopub.execute_input":"2024-06-06T22:36:19.520436Z","iopub.status.idle":"2024-06-06T22:53:50.320334Z","shell.execute_reply.started":"2024-06-06T22:36:19.520406Z","shell.execute_reply":"2024-06-06T22:53:50.319128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optimization\n!python train.py --img 640 --batch 64 --epochs 20 --data {os.path.join(dataset_path, 'dataset.yaml')} --weights yolov5s.pt","metadata":{"execution":{"iopub.status.busy":"2024-06-06T22:55:24.273666Z","iopub.execute_input":"2024-06-06T22:55:24.274596Z","iopub.status.idle":"2024-06-06T23:29:52.711362Z","shell.execute_reply.started":"2024-06-06T22:55:24.274557Z","shell.execute_reply":"2024-06-06T23:29:52.710305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nfrom PIL import Image\nfrom torchvision.transforms import functional as F\nfrom utils.general import non_max_suppression\nfrom models.experimental import attempt_load\nimport matplotlib.pyplot as plt\n\n# Function to plot and display bounding boxes on an image\ndef plot_results(img_path, model, device):\n    img = Image.open(img_path)\n    img_tensor = F.to_tensor(img).unsqueeze(0).to(device)\n\n    # Run inference\n    pred = model(img_tensor)[0]\n    pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45)\n\n    # Visualize predictions\n    plt.figure(figsize=(10, 10))\n    plt.imshow(img)\n\n    if pred[0] is not None:\n        for x1, y1, x2, y2, conf, cls in pred[0]:\n            # Scale bounding box coordinates\n            x1, y1, x2, y2 = int(x1.item()), int(y1.item()), int(x2.item()), int(y2.item())\n            # Draw bounding box\n            plt.rectangle((x1, y1), (x2, y2), color='r', linewidth=2)\n            plt.text(x1, y1 - 10, f'{int(cls)}: {conf:.2f}', color='r', fontsize=12)\n\n    plt.axis('off')\n    plt.show()\n\n# Load trained model\nweights_path = '/kaggle/working/yolov5/yolov5s.pt'\nmodel = attempt_load(weights_path)\nmodel.eval()\n\n# Set device\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Path to training images\n# Get the list of image files from the training images folder, excluding directories\ntrain_image_files = [f for f in os.listdir(train_images_folder) if os.path.isfile(os.path.join(image_folder, f))][:5]\n\n\n# Plot results for each training image\nfor img_file in train_image_files:\n    img_path = os.path.join(train_images_files, img_file)\n    plot_results(img_path, model, device)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-06T23:56:46.259313Z","iopub.execute_input":"2024-06-06T23:56:46.260292Z","iopub.status.idle":"2024-06-06T23:56:46.510458Z","shell.execute_reply.started":"2024-06-06T23:56:46.260255Z","shell.execute_reply":"2024-06-06T23:56:46.509561Z"},"trusted":true},"execution_count":null,"outputs":[]}]}