{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"},{"sourceId":7239839,"sourceType":"datasetVersion","datasetId":4193121}],"dockerImageVersionId":30559,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:10:54.335382Z","iopub.execute_input":"2023-09-21T11:10:54.335795Z","iopub.status.idle":"2023-09-21T11:11:11.720699Z","shell.execute_reply.started":"2023-09-21T11:10:54.33576Z","shell.execute_reply":"2023-09-21T11:11:11.719586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nimport cv2\nimport numpy as np\nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm.notebook import tqdm_notebook as tqdm\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:36.419161Z","iopub.execute_input":"2023-09-21T11:15:36.419558Z","iopub.status.idle":"2023-09-21T11:15:36.425651Z","shell.execute_reply.started":"2023-09-21T11:15:36.419528Z","shell.execute_reply":"2023-09-21T11:15:36.424725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ships = pd.read_csv(\"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/airbus-ship-detection/sample_submission_v2.csv\")\nships[\"Ship\"] = ships[\"EncodedPixels\"].map(lambda x:1 if isinstance(x,str) else 0)\nship_unique = ships[[\"ImageId\",\"Ship\"]].groupby(\"ImageId\").agg({\"Ship\":\"sum\"}).reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:37.753098Z","iopub.execute_input":"2023-09-21T11:15:37.753675Z","iopub.status.idle":"2023-09-21T11:15:38.765006Z","shell.execute_reply.started":"2023-09-21T11:15:37.753637Z","shell.execute_reply":"2023-09-21T11:15:38.763843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2bbox(rle, shape):\n    \n    a = np.fromiter(rle.split(), dtype=np.uint)\n    a = a.reshape((-1, 2))\n    a[:,0] -= 1\n    \n    y0 = a[:,0] % shape[0]\n    y1 = y0 + a[:,1]\n    if np.any(y1 > shape[0]):\n        y0 = 0\n        y1 = shape[0]\n    else:\n        y0 = np.min(y0)\n        y1 = np.max(y1)\n    \n    x0 = a[:,0] // shape[0]\n    x1 = (a[:,0] + a[:,1]) // shape[0]\n    x0 = np.min(x0)\n    x1 = np.max(x1)\n    \n    if x1 > shape[1]:\n        raise ValueError(\"invalid RLE or image dimensions: x1=%d > shape[1]=%d\" % (\n            x1, shape[1]\n        ))\n\n    xc = (x0+x1)/(2*768)\n    yc = (y0+y1)/(2*768)\n    w = np.abs(x1-x0)/768\n    h = np.abs(y1-y0)/768\n    return [xc, yc, h, w]","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:39.367876Z","iopub.execute_input":"2023-09-21T11:15:39.368464Z","iopub.status.idle":"2023-09-21T11:15:39.378963Z","shell.execute_reply.started":"2023-09-21T11:15:39.368432Z","shell.execute_reply":"2023-09-21T11:15:39.378043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ships[\"Boundingbox\"] = ships[\"EncodedPixels\"].apply(lambda x:rle2bbox(x,(768,768)) if isinstance(x,str) else np.NaN)\nships.drop(\"EncodedPixels\", axis =1, inplace =True)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:41.440883Z","iopub.execute_input":"2023-09-21T11:15:41.441816Z","iopub.status.idle":"2023-09-21T11:15:51.255245Z","shell.execute_reply.started":"2023-09-21T11:15:41.441764Z","shell.execute_reply":"2023-09-21T11:15:51.254156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ships[\"BoundingboxArea\"]=ships[\"Boundingbox\"].map(lambda x:x[2]*768*x[3]*768 if x==x else 0)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:51.257674Z","iopub.execute_input":"2023-09-21T11:15:51.258109Z","iopub.status.idle":"2023-09-21T11:15:51.443351Z","shell.execute_reply.started":"2023-09-21T11:15:51.258072Z","shell.execute_reply":"2023-09-21T11:15:51.442313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ships = ships[ships[\"BoundingboxArea\"]>np.percentile(ships[\"BoundingboxArea\"],1)]","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:51.444865Z","iopub.execute_input":"2023-09-21T11:15:51.445434Z","iopub.status.idle":"2023-09-21T11:15:51.465341Z","shell.execute_reply.started":"2023-09-21T11:15:51.445382Z","shell.execute_reply":"2023-09-21T11:15:51.464494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"balanced_df = ship_unique.groupby(\"Ship\").apply(lambda x:x.sample(1000) if len(x)>=1000 else x.sample(len(x)))\nbalanced_df.reset_index(drop=True,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:51.46799Z","iopub.execute_input":"2023-09-21T11:15:51.468357Z","iopub.status.idle":"2023-09-21T11:15:51.50467Z","shell.execute_reply.started":"2023-09-21T11:15:51.468307Z","shell.execute_reply":"2023-09-21T11:15:51.503696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"balanced_bbox = ships.merge(balanced_df[[\"ImageId\"]], how =\"inner\", on = \"ImageId\")\nbalanced_bbox.tail(20)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:51.506377Z","iopub.execute_input":"2023-09-21T11:15:51.50676Z","iopub.status.idle":"2023-09-21T11:15:51.568464Z","shell.execute_reply.started":"2023-09-21T11:15:51.506727Z","shell.execute_reply":"2023-09-21T11:15:51.567312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"balanced_bbox.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:15:54.597463Z","iopub.execute_input":"2023-09-21T11:15:54.597869Z","iopub.status.idle":"2023-09-21T11:15:54.604137Z","shell.execute_reply.started":"2023-09-21T11:15:54.59783Z","shell.execute_reply":"2023-09-21T11:15:54.60307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"balanced_bbox['ImageId'][i].replace(\".jpg\",\".txt\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"balanced_bbox['Boundingbox'][i]","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:16:06.989493Z","iopub.execute_input":"2023-09-21T11:16:06.989881Z","iopub.status.idle":"2023-09-21T11:16:07.025001Z","shell.execute_reply.started":"2023-09-21T11:16:06.989848Z","shell.execute_reply":"2023-09-21T11:16:07.023852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"/kaggle/working/labels_for_each_image/\")","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:16:25.274181Z","iopub.execute_input":"2023-09-21T11:16:25.27456Z","iopub.status.idle":"2023-09-21T11:16:25.279742Z","shell.execute_reply.started":"2023-09-21T11:16:25.27453Z","shell.execute_reply":"2023-09-21T11:16:25.278691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,balanced_bbox.shape[0]):\n    image_id = balanced_bbox['ImageId'][i]\n    label_path = \"/kaggle/input/airbus-ship-detection/train_v2\" + image_id.replace(\".jpg\",\".txt\")\n    with open(label_path, \"a\") as file:\n        file.write(\"1 \" + str(balanced_bbox[\"Boundingbox\"][i][0]) + ' ' + str(balanced_bbox[\"Boundingbox\"][i][1]) + ' ' +\n                   str(balanced_bbox[\"Boundingbox\"][i][2]) + ' ' + str(balanced_bbox[\"Boundingbox\"][i][3]) + ' ' +\"\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:17:19.048734Z","iopub.execute_input":"2023-09-21T11:17:19.049446Z","iopub.status.idle":"2023-09-21T11:17:19.106109Z","shell.execute_reply.started":"2023-09-21T11:17:19.049412Z","shell.execute_reply":"2023-09-21T11:17:19.104743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_path = \"/kaggle/working/labels_for_each_image/\"\ncount = 0\n# Iterate directory\nfor path in os.listdir(dir_path):\n    # check if current path is a file\n    if os.path.isfile(os.path.join(dir_path, path)):\n        count += 1\nprint('File count:', count)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:16:37.466639Z","iopub.execute_input":"2023-09-21T11:16:37.467598Z","iopub.status.idle":"2023-09-21T11:16:37.529784Z","shell.execute_reply.started":"2023-09-21T11:16:37.467552Z","shell.execute_reply":"2023-09-21T11:16:37.528825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r file.zip /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:01:24.75045Z","iopub.execute_input":"2023-09-21T11:01:24.750901Z","iopub.status.idle":"2023-09-21T11:01:26.447999Z","shell.execute_reply.started":"2023-09-21T11:01:24.750865Z","shell.execute_reply":"2023-09-21T11:01:26.446605Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r'file.zip')","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:01:33.660666Z","iopub.execute_input":"2023-09-21T11:01:33.662029Z","iopub.status.idle":"2023-09-21T11:01:33.670259Z","shell.execute_reply.started":"2023-09-21T11:01:33.661966Z","shell.execute_reply":"2023-09-21T11:01:33.668974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path =\"../input/airbus-ship-detection/train_v2/\"\nplt.figure(figsize =(20,20))\nfor i in range(15):\n    imageid = balanced_df[balanced_df.Ship ==i].iloc[0][0]\n    image = np.array(cv2.imread(path+imageid)[:,:,::-1])\n    if i>0:\n        bbox = balanced_bbox[balanced_bbox.ImageId==imageid][\"Boundingbox\"]\n        \n        for items in bbox:\n            Xmin  = int((items[0]-items[3]/2)*768)\n            Ymin  = int((items[1]-items[2]/2)*768)\n            Xmax  = int((items[0]+items[3]/2)*768)\n            Ymax  = int((items[1]+items[2]/2)*768)\n            cv2.rectangle(image,\n                          (Xmin,Ymin),\n                          (Xmax,Ymax),\n                          (255,0,0),\n                          thickness = 2)\n    plt.subplot(4,4,i+1)\n    plt.imshow(image)\n    plt.title(\"Number of ships = {}\".format(i))","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:01:50.80896Z","iopub.execute_input":"2023-09-21T11:01:50.80941Z","iopub.status.idle":"2023-09-21T11:01:58.100855Z","shell.execute_reply.started":"2023-09-21T11:01:50.809378Z","shell.execute_reply":"2023-09-21T11:01:58.099182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Checking the sizes of multiple images","metadata":{}},{"cell_type":"code","source":"model = YOLO(\"yolov8n-seg.pt\")\nresults = model.train(data='/kaggle/input/dataset-yaml-file/dataset.yml',epochs=20,batch=64,imgsz=768,save_period=5, device=[0, 1])","metadata":{"execution":{"iopub.status.busy":"2023-09-21T11:09:27.260183Z","iopub.execute_input":"2023-09-21T11:09:27.261049Z","iopub.status.idle":"2023-09-21T11:09:27.606325Z","shell.execute_reply.started":"2023-09-21T11:09:27.261012Z","shell.execute_reply":"2023-09-21T11:09:27.605035Z"},"trusted":true},"execution_count":null,"outputs":[]}]}