{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"},{"sourceId":200550904,"sourceType":"kernelVersion"}],"dockerImageVersionId":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!git clone https://github.com/THU-MIG/yolov10\n!cd yolov10 && pip install -r requirements.txt\n!cd yolov10 && pip install .|","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T09:55:58.918264Z","iopub.execute_input":"2024-10-23T09:55:58.918582Z","iopub.status.idle":"2024-10-23T09:58:52.756545Z","shell.execute_reply.started":"2024-10-23T09:55:58.918549Z","shell.execute_reply":"2024-10-23T09:58:52.755406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --upgrade ultralytics\n!pip install albumentations==1.4\n!pip install ray[tune]==2.9.3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T09:58:52.758589Z","iopub.execute_input":"2024-10-23T09:58:52.758927Z","iopub.status.idle":"2024-10-23T09:59:39.422384Z","shell.execute_reply.started":"2024-10-23T09:58:52.758892Z","shell.execute_reply":"2024-10-23T09:59:39.421203Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Convert the starfish dataset in the YOLO dataset.","metadata":{}},{"cell_type":"code","source":"# !rm -r starfish_yolo\n# !mkdir starfish_yolo\n# !mkdir starfish_yolo/train\n# !mkdir starfish_yolo/test\n# !mkdir starfish_yolo/train/images\n# !mkdir starfish_yolo/test/images\n# !mkdir starfish_yolo/train/labels\n# !mkdir starfish_yolo/test/labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T11:30:27.095162Z","iopub.execute_input":"2024-10-22T11:30:27.095519Z","iopub.status.idle":"2024-10-22T11:30:27.100251Z","shell.execute_reply.started":"2024-10-22T11:30:27.095481Z","shell.execute_reply":"2024-10-22T11:30:27.099242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import os\n# os.environ['WANDB_DISABLED'] = 'true'\n\n# import os.path\n# import shutil\n\n# import cv2\n# import pandas\n# df = pandas.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\n\n# def write_annotations(annotations, img_w, img_h):\n#     text = \"\"\n#     for box in eval(annotations.values[0]):\n#         x = int(box['x']) / img_w\n#         y = int(box['y']) / img_h\n#         w = int(box['width']) / img_w\n#         h = int(box['height']) / img_h\n\n#         centre_x = x + w / 2\n#         centre_y = y + h / 2\n        \n#         text = text + f\"0 {centre_x} {centre_y} {w} {h}\\n\"\n    \n#     return text\n\n# for i in range(3):\n#     temp = [\"train\", \"train\", \"test\"][i]\n#     path = f\"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_{i}\"\n#     for file in os.listdir(path):\n#         img = os.path.join(path, file)\n#         shutil.copy(img, f\"starfish_yolo/{temp}/images/video_{i}_{file}\")\n        \n#         img_h, img_w, _ = cv2.imread(img).shape\n#         annotations = df[df[\"image_id\"] == f\"{i}-{file.replace('.jpg', '')}\"][\"annotations\"]\n#         with open(f\"starfish_yolo/{temp}/labels/video_{i}_{file.replace('.jpg', '')}.txt\", \"w+\") as f:\n#             f.write(write_annotations(annotations, img_w, img_h))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T11:30:27.102804Z","iopub.execute_input":"2024-10-22T11:30:27.103343Z","iopub.status.idle":"2024-10-22T11:30:27.115173Z","shell.execute_reply.started":"2024-10-22T11:30:27.103282Z","shell.execute_reply":"2024-10-22T11:30:27.114336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !rm starfish.yaml","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T11:30:27.116319Z","iopub.execute_input":"2024-10-22T11:30:27.116613Z","iopub.status.idle":"2024-10-22T11:30:27.124614Z","shell.execute_reply.started":"2024-10-22T11:30:27.116582Z","shell.execute_reply":"2024-10-22T11:30:27.123819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%writefile -a starfish.yaml\npath: /kaggle/working/starfish_yolo\ntrain: /kaggle/working/starfish_yolo/train/images\nval: /kaggle/working/starfish_yolo/test/images\n\nnames:\n    0: starfish","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T09:59:39.423807Z","iopub.execute_input":"2024-10-23T09:59:39.424150Z","iopub.status.idle":"2024-10-23T09:59:39.431671Z","shell.execute_reply.started":"2024-10-23T09:59:39.424116Z","shell.execute_reply":"2024-10-23T09:59:39.430377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !cd yolov10 && yolo detect train data=../starfish.yaml model=yolov10n.yaml epochs=500 batch=16 imgsz=640 device=0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T11:30:27.134083Z","iopub.execute_input":"2024-10-22T11:30:27.134390Z","iopub.status.idle":"2024-10-22T11:30:27.142897Z","shell.execute_reply.started":"2024-10-22T11:30:27.134358Z","shell.execute_reply":"2024-10-22T11:30:27.142099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!rm -r /kaggle/working/starfish_yolo\n!cp -r ../input/yolov10-test/starfish_yolo /kaggle/working/starfish_yolo","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T09:59:39.433952Z","iopub.execute_input":"2024-10-23T09:59:39.434244Z","iopub.status.idle":"2024-10-23T10:04:23.162636Z","shell.execute_reply.started":"2024-10-23T09:59:39.434213Z","shell.execute_reply":"2024-10-23T10:04:23.161561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls ../input/yolov10-test/starfish_yolo","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T10:04:23.164261Z","iopub.execute_input":"2024-10-23T10:04:23.164699Z","iopub.status.idle":"2024-10-23T10:04:24.156334Z","shell.execute_reply.started":"2024-10-23T10:04:23.164653Z","shell.execute_reply":"2024-10-23T10:04:24.155363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.environ['WANDB_DISABLED'] = 'true'\n\nfrom ultralytics import YOLO\n\nmodel = YOLO(\"yolo11n.pt\")\nmodel.train(data='starfish.yaml', batch=32, epochs=10)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-10-23T10:21:25.232974Z","iopub.execute_input":"2024-10-23T10:21:25.233717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T10:16:00.762587Z","iopub.execute_input":"2024-10-23T10:16:00.763031Z","iopub.status.idle":"2024-10-23T10:16:01.809791Z","shell.execute_reply.started":"2024-10-23T10:16:00.762990Z","shell.execute_reply":"2024-10-23T10:16:01.808589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nresults = model([\"starfish_yolo/test/images/\" + x for x in os.listdir(\"starfish_yolo/test/images\")][:200])  # return a list of Results objects\n\n# Process results list\ni = 0\nfor result in results:\n    boxes = result.boxes  # Boxes object for bounding box outputs\n    masks = result.masks  # Masks object for segmentation masks outputs\n    keypoints = result.keypoints  # Keypoints object for pose outputs\n    probs = result.probs  # Probs object for classification outputs\n    obb = result.obb  # Oriented boxes object for OBB outputs\n    result.save(filename=f\"results/results_{i}.jpg\")  # save to disk\n\n    i += 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-23T10:20:49.239001Z","iopub.execute_input":"2024-10-23T10:20:49.239409Z","iopub.status.idle":"2024-10-23T10:20:50.274064Z","shell.execute_reply.started":"2024-10-23T10:20:49.239368Z","shell.execute_reply":"2024-10-23T10:20:50.273212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !ls /kaggle/working/runs/detect/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T07:00:44.555655Z","iopub.status.idle":"2024-10-22T07:00:44.556196Z","shell.execute_reply.started":"2024-10-22T07:00:44.555919Z","shell.execute_reply":"2024-10-22T07:00:44.555945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n\n# plt.imshow(cv2.imread(\"/kaggle/working/runs/detect/train2/labels_correlogram.jpg\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T07:00:44.557762Z","iopub.status.idle":"2024-10-22T07:00:44.558335Z","shell.execute_reply.started":"2024-10-22T07:00:44.558052Z","shell.execute_reply":"2024-10-22T07:00:44.558084Z"}},"outputs":[],"execution_count":null}]}