{"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":"tpu1vmV38","dataSources":[{"sourceId":31703,"databundleVersionId":2871752,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nnp.random.seed(32)\n\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport seaborn as sns\nimport cv2\nimport matplotlib.pyplot as plt\nimport random\n\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport torch\nfrom PIL import Image\nimport ast\nimport albumentations as albu\nimport glob\nimport shutil\nfrom decimal import getcontext\nBASE_DIR = '/kaggle/input/tensorflow-great-barrier-reef'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-28T06:47:01.376386Z","iopub.execute_input":"2023-12-28T06:47:01.377039Z","iopub.status.idle":"2023-12-28T06:47:01.383760Z","shell.execute_reply.started":"2023-12-28T06:47:01.377000Z","shell.execute_reply":"2023-12-28T06:47:01.382983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install albumentations","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:46:45.360413Z","iopub.execute_input":"2023-12-28T06:46:45.361726Z","iopub.status.idle":"2023-12-28T06:46:51.218263Z","shell.execute_reply.started":"2023-12-28T06:46:45.361674Z","shell.execute_reply":"2023-12-28T06:46:51.217220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(precision = 4, suppress = True)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:18.159971Z","iopub.execute_input":"2023-12-28T06:47:18.160368Z","iopub.status.idle":"2023-12-28T06:47:18.164753Z","shell.execute_reply.started":"2023-12-28T06:47:18.160334Z","shell.execute_reply":"2023-12-28T06:47:18.163992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing Sequences","metadata":{}},{"cell_type":"code","source":"df_tr = pd.read_csv(os.path.join(BASE_DIR ,'train.csv' ))\n\ndef get_full_path(x):\n    vid_id , vid_frame = x.split('-')\n    path = os.path.join(BASE_DIR ,'train_images', 'video_{id}'.format(id = vid_id) ,\n                        '{frame}.jpg'.format(frame = vid_frame))\n    return path\n\ndf_tr['path'] = df_tr.image_id.apply(lambda x: get_full_path(x))\nfig = plt.figure(figsize = (15,5))\nsns.histplot(data = df_tr , x = df_tr.sequence , hue = df_tr.video_id, multiple=\"dodge\")","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:27.405678Z","iopub.execute_input":"2023-12-28T06:47:27.406108Z","iopub.status.idle":"2023-12-28T06:47:28.073787Z","shell.execute_reply.started":"2023-12-28T06:47:27.406069Z","shell.execute_reply":"2023-12-28T06:47:28.073036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing BBox","metadata":{}},{"cell_type":"code","source":"def count_bbox(x):\n    if len(x)<=2:\n        return 0\n    return len(x[1:-1].split('}'))-1    # '[{},{},{}]'\n\ndf_tr['bbox_ct'] = df_tr['annotations'].apply(lambda x: count_bbox(x))\nfig, axs = plt.subplots(nrows = 3 ,ncols = 1 , figsize=(10,10))\n\nfor i in range(3):\n    axs[i].title.set_text('Video-{i}'.format(i =i))\n    df = df_tr[df_tr.video_id == i].bbox_ct\n    axs[i].hist(df , bins = 50)\n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:32.520968Z","iopub.execute_input":"2023-12-28T06:47:32.521376Z","iopub.status.idle":"2023-12-28T06:47:33.143765Z","shell.execute_reply.started":"2023-12-28T06:47:32.521341Z","shell.execute_reply":"2023-12-28T06:47:33.142981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.histplot(df_tr , x = df_tr.bbox_ct)\nx = len(df_tr[df_tr.bbox_ct==0])/len(df_tr)\nprint('Percentage frames with no bbox :{:.2f}%'.format(100*x) )","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:37.766541Z","iopub.execute_input":"2023-12-28T06:47:37.766946Z","iopub.status.idle":"2023-12-28T06:47:37.992143Z","shell.execute_reply.started":"2023-12-28T06:47:37.766913Z","shell.execute_reply":"2023-12-28T06:47:37.991446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Nearly 80% data has no starfish in it**\n\nBut we can't neglect 80% data and do training on just remaining 20% as the model needs to be robust in both ways -->\n\n**a) predicting right bbox**\n\n**b) not predicting bbox when not needed**\n\nThere are also pictures with near about 70 bboxes ,but they are very rare ","metadata":{}},{"cell_type":"code","source":"def no_bbox(x):\n    if x==0:\n        return 1\n    return 0\n\ndf_tr['no_bbox'] = df_tr.bbox_ct.apply(lambda x :no_bbox(x))\ndf_tr.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:41.892994Z","iopub.execute_input":"2023-12-28T06:47:41.893382Z","iopub.status.idle":"2023-12-28T06:47:41.917826Z","shell.execute_reply.started":"2023-12-28T06:47:41.893348Z","shell.execute_reply":"2023-12-28T06:47:41.917072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v2 = df_tr.groupby(['video_id' , 'sequence']).agg({\n    'sequence_frame' :'count' , \n    'no_bbox' : 'sum'\n}).rename(columns = {'sequence_frame': 'total_frames' ,\n                    'no_bbox' : 'frames_without_annot'})\ndf_tr_v2","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:44.570421Z","iopub.execute_input":"2023-12-28T06:47:44.571189Z","iopub.status.idle":"2023-12-28T06:47:44.587165Z","shell.execute_reply.started":"2023-12-28T06:47:44.571147Z","shell.execute_reply":"2023-12-28T06:47:44.586367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_without_annot = df_tr[df_tr.bbox_ct==0].reset_index(drop = True)\ndf_tr_with_annot = df_tr[df_tr.bbox_ct>0]\n\nseq_set = set(df_tr.sequence)\n\ndf = pd.DataFrame()\n\n# I am deciding to choose 8% of total frames with no bbox from each sequence randomly\nfactor = 0.08\nfor seq in seq_set:\n    idxs = df_tr_without_annot[df_tr_without_annot.sequence==seq].index\n    if len(idxs)==0:\n        continue\n    \n    ct = (int)(len(idxs)*factor)+1\n    arr = np.random.choice(idxs , ct)\n    \n    df_ = df_tr_without_annot.iloc[arr]\n    df = pd.concat([df, df_], ignore_index=True)\n    \nsns.histplot(df , x =  df.sequence, bins = 80)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:50.540633Z","iopub.execute_input":"2023-12-28T06:47:50.541020Z","iopub.status.idle":"2023-12-28T06:47:50.806107Z","shell.execute_reply.started":"2023-12-28T06:47:50.540988Z","shell.execute_reply":"2023-12-28T06:47:50.805294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v3 = pd.concat([df_tr_with_annot , df] , ignore_index = True).reset_index(drop = True)\n\ndf_tr_v3.drop(['video_id','video_frame'  ,'no_bbox' ], axis=1 ,inplace =True)\ndf_tr_v3.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:54.440290Z","iopub.execute_input":"2023-12-28T06:47:54.441343Z","iopub.status.idle":"2023-12-28T06:47:54.456091Z","shell.execute_reply.started":"2023-12-28T06:47:54.441299Z","shell.execute_reply":"2023-12-28T06:47:54.455290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,5))\nsns.histplot(df_tr_v3 , x = df_tr_v3.bbox_ct)\nx = 100*(len(df)/len(df_tr_v3))\nprint('{:.2f}% without annotations'.format(x))","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:47:57.640415Z","iopub.execute_input":"2023-12-28T06:47:57.641232Z","iopub.status.idle":"2023-12-28T06:47:58.021237Z","shell.execute_reply.started":"2023-12-28T06:47:57.641187Z","shell.execute_reply":"2023-12-28T06:47:58.020592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v3.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:48:02.225471Z","iopub.execute_input":"2023-12-28T06:48:02.225889Z","iopub.status.idle":"2023-12-28T06:48:02.235744Z","shell.execute_reply.started":"2023-12-28T06:48:02.225852Z","shell.execute_reply":"2023-12-28T06:48:02.234952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing BBOX","metadata":{}},{"cell_type":"code","source":"getcontext().prec = 3\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:48:05.115038Z","iopub.execute_input":"2023-12-28T06:48:05.116112Z","iopub.status.idle":"2023-12-28T06:48:05.119703Z","shell.execute_reply.started":"2023-12-28T06:48:05.116069Z","shell.execute_reply":"2023-12-28T06:48:05.119063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nfrom PIL import Image\nimport torch\nimport torchvision\nfrom torchvision.io import read_image\nfrom torchvision.utils import draw_bounding_boxes\n\ndef str2annots(annots):\n    return json.loads(annots.replace(\"'\", '\"'))\n    \ndef annot2str(annots):\n    return str(annots)\n\ndef get_bbox(annots):\n    if type(annots)==str:\n        annots = str2annots(annots)\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\ndef get_imgsize(img_path):\n    img = Image.open(img_path)\n    w,h = img.size\n    return [w,h]\n\n\n\n# COCO       :  [x_min, y_min, width, height]\n# YOLO       :  [x_center, y_center, width, height] \n# Pascal_VOC :  [x_min, y_min, x_max, y_max]     # torch format\n\ndef coco2voc(x1, y1, w, h):\n    return [x1,y1, x1 + w, y1 + h]\n\ndef coco2yolo(x1, y1, w, h, image_w, image_h):\n    return [((2*x1 + w)/(2*image_w)) , ((2*y1 + h)/(2*image_h)), w/image_w, h/image_h]\n\ndef voc2coco(x1, y1, x2, y2):\n    return [x1,y1, x2 - x1, y2 - y1]\n\ndef voc2yolo(x1, y1, x2, y2, image_w, image_h):\n    return [((x2 + x1)/(2*image_w)), ((y2 + y1)/(2*image_h)), (x2 - x1)/image_w, (y2 - y1)/image_h]\n\ndef yolo2coco(x_center, y_center, w, h,  image_w, image_h):\n    w = w * image_w\n    h = h * image_h\n    x1 = ((2 * x_center * image_w) - w)/2\n    y1 = ((2 * y_center * image_h) - h)/2\n    return [x1, y1, w, h]\n\ndef yolo2voc(x_center, y_center, w, h,  image_w, image_h):\n    w = w * image_w\n    h = h * image_h\n    x1 = ((2 * x_center * image_w) - w)/2\n    y1 = ((2 * y_center * image_h) - h)/2\n    x2 = x1 + w\n    y2 = y1 + h\n    return [x1, y1, x2, y2]\n\ndef plot_bbox_image(path , labels, bboxes , colors):\n    # bounding box in (xmin, ymin, xmax, ymax) format\n    ct = len(bboxes)\n    bboxes = torch.tensor(bboxes, dtype=torch.int)\n#     img = path\n    if type(path)==str:\n        img = read_image(path)\n    else:\n        img = Image.fromarray(path, 'RGB')\n    if ct>0:\n        # draw bounding boxes on the input image\n        img=draw_bounding_boxes(image=img , boxes=bboxes, labels = labels ,width=3,\n        colors=colors)\n    img = torchvision.transforms.ToPILImage()(img)\n    return img\n\n'''\nReferences : https://www.tutorialspoint.com/how-to-draw-bounding-boxes-on-an-image-in-pytorch\n           : https://www.geeksforgeeks.org/python-convert-string-to-list-of-dictionaries/\n'''","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:48:16.310304Z","iopub.execute_input":"2023-12-28T06:48:16.310996Z","iopub.status.idle":"2023-12-28T06:48:20.186923Z","shell.execute_reply.started":"2023-12-28T06:48:16.310954Z","shell.execute_reply":"2023-12-28T06:48:20.186230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting Randomly (tr, val, tst)","metadata":{}},{"cell_type":"code","source":"df_tr_v3['bboxes'] = df_tr_v3.annotations.apply(lambda x : get_bbox(x))\ndf_tr_v3['img_dim'] = df_tr_v3.path.apply(lambda x : get_imgsize(x))\n\ntot = df_tr_v3.shape[0]\nidxs = np.argsort(np.random.rand(tot))\ntr_idxs , val_idxs , tst_idxs = idxs[:(int)(0.75*len(idxs))] , idxs[(int)(0.75*len(idxs)):(int)(0.85*len(idxs))] , idxs[(int)(0.85*len(idxs)):]\n\ndf_tr = df_tr_v3.loc[tr_idxs , :].reset_index(drop = True)\ndf_val = df_tr_v3.loc[val_idxs , :].reset_index(drop = True)\ndf_tst = df_tr_v3.loc[tst_idxs , :].reset_index(drop = True)\n\ndf_tr.shape , df_val.shape , df_tst.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:48:30.990995Z","iopub.execute_input":"2023-12-28T06:48:30.991606Z","iopub.status.idle":"2023-12-28T06:50:00.248165Z","shell.execute_reply.started":"2023-12-28T06:48:30.991553Z","shell.execute_reply":"2023-12-28T06:50:00.247314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Processing","metadata":{}},{"cell_type":"code","source":"import albumentations as A\n\n\ntransform = A.Compose([\n    A.CLAHE(clip_limit=5.0, tile_grid_size=(6,6), always_apply=True),\n#     A.Equalize(mode='cv', always_apply=True),\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:50:20.525802Z","iopub.execute_input":"2023-12-28T06:50:20.526196Z","iopub.status.idle":"2023-12-28T06:50:20.531352Z","shell.execute_reply.started":"2023-12-28T06:50:20.526164Z","shell.execute_reply":"2023-12-28T06:50:20.530601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformed = transform(image=cv2.imread(df_tr.iloc[1201, 4]))\ntransformed_image = transformed['image']","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:50:24.540734Z","iopub.execute_input":"2023-12-28T06:50:24.541181Z","iopub.status.idle":"2023-12-28T06:50:24.718009Z","shell.execute_reply.started":"2023-12-28T06:50:24.541141Z","shell.execute_reply":"2023-12-28T06:50:24.717050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(transformed_image)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:50:29.735547Z","iopub.execute_input":"2023-12-28T06:50:29.735940Z","iopub.status.idle":"2023-12-28T06:50:30.101255Z","shell.execute_reply.started":"2023-12-28T06:50:29.735906Z","shell.execute_reply":"2023-12-28T06:50:30.100379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Images","metadata":{}},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/working/dataset\"\nPATH_IMAGES = os.path.join(BASE_PATH , \"images\")\nPATH_LABELS = os.path.join(BASE_PATH , \"labels\")\nif not os.path.exists(BASE_PATH):\n    os.mkdir(BASE_PATH)\n    os.mkdir(PATH_IMAGES)\n    os.mkdir(PATH_LABELS)\n\ndef create_dataset(df:pd.DataFrame, name:str):\n    p1 , p2 = os.path.join(PATH_LABELS , name), os.path.join(PATH_IMAGES , name)\n    os.mkdir(p1)\n    os.mkdir(p2)\n    \n    for i in range(600):\n        img = cv2.imread(df.iloc[i,4])\n        img_id = df.iloc[i,2]\n        transformed_image = transform(image=img)['image']\n        labels_coco = df.iloc[i, 6]\n        label_path = os.path.join(p1,img_id+'.txt')\n        img_path = os.path.join(p2,img_id+'.jpg')\n        cv2.imwrite(img_path, transformed_image)\n        \n        with open(label_path, \"w\", encoding='utf-8') as file:\n            for box in labels_coco:\n                box_ = [0]\n                x,y,w,h = box[0] ,box[1] , box[2] , box[3]\n                image_w, image_h = 1280, 720\n                box_.extend(coco2yolo( x,y,w,h ,image_w, image_h))\n                for x in box_:\n                    file.write(str(x)+'\\t')\n                file.write('\\n')\n        file.close()\n        \n        \ncreate_dataset(df_tr, 'train')\ncreate_dataset(df_tst, 'test')\ncreate_dataset(df_val, 'validation')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:51:25.320547Z","iopub.execute_input":"2023-12-28T06:51:25.320998Z","iopub.status.idle":"2023-12-28T06:53:02.356406Z","shell.execute_reply.started":"2023-12-28T06:51:25.320963Z","shell.execute_reply":"2023-12-28T06:53:02.355287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Images ","metadata":{}},{"cell_type":"code","source":"fig , axs = plt.subplots(3,2 , figsize = (15,12))\n\nimges = np.random.choice(df_tr.index , 6)\nfor i in range(6):\n    r, c = i//2 , i%2\n    path = df_tr_v3.path.iloc[imges[r]]\n    \n    bboxes = df_tr_v3.bboxes.iloc[imges[r]]     # in coco format\n    voc_bbox = [coco2voc(bbox[0], bbox[1], bbox[2] ,bbox[3]) for bbox in bboxes]\n    colors=[(255,0,0) for i in range(len(bboxes))]\n    labels = ['cot']*len(voc_bbox)\n    img = plot_bbox_image(path, labels,voc_bbox, colors)\n    \n    img = plot_bbox_image(path, labels,voc_bbox, colors)\n    axs[r,c].imshow(img)\n    axs[r,c].set_xticks([])\n    axs[r,c].set_yticks([])\n\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:53:17.280784Z","iopub.execute_input":"2023-12-28T06:53:17.281192Z","iopub.status.idle":"2023-12-28T06:53:19.194972Z","shell.execute_reply.started":"2023-12-28T06:53:17.281156Z","shell.execute_reply":"2023-12-28T06:53:19.193766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv5s IMPLEMENTATION","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5\n%cd yolov5    ","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:53:26.000652Z","iopub.execute_input":"2023-12-28T06:53:26.001691Z","iopub.status.idle":"2023-12-28T06:53:28.428641Z","shell.execute_reply.started":"2023-12-28T06:53:26.001646Z","shell.execute_reply":"2023-12-28T06:53:28.427647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Config","metadata":{}},{"cell_type":"code","source":"import yaml\nfrom yaml.loader import SafeLoader\n\ndata = {\n    \"train\": '/kaggle/working/dataset/images/train',\n    \"val\": '/kaggle/working/dataset/images/validation',\n    \"test\": '/kaggle/working/dataset/images/test',\n    'nc': 1, \n    'names' : ['Cots']\n}\n\nwith open(\"/kaggle/working/yolov5/data/cots_data.yaml\", \"w\") as f:\n    yaml.dump(data, f)\n    \n!cat data/cots_data.yaml","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:53:33.540347Z","iopub.execute_input":"2023-12-28T06:53:33.540725Z","iopub.status.idle":"2023-12-28T06:53:33.684710Z","shell.execute_reply.started":"2023-12-28T06:53:33.540691Z","shell.execute_reply":"2023-12-28T06:53:33.683858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('models/yolov5s.yaml', 'r') as f:\n    data = yaml.load(f, Loader=SafeLoader)\n\ndata['nc'] = 1\nwith open(\"models/yolov5s.yaml\", \"w\") as f:\n    yaml.dump(data, f)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:53:37.590424Z","iopub.execute_input":"2023-12-28T06:53:37.591265Z","iopub.status.idle":"2023-12-28T06:53:37.613151Z","shell.execute_reply.started":"2023-12-28T06:53:37.591227Z","shell.execute_reply":"2023-12-28T06:53:37.612493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Model","metadata":{}},{"cell_type":"code","source":"#BATCH_SIZE = 32\n#EPOCHS = 50","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:53:44.885471Z","iopub.execute_input":"2023-12-28T06:53:44.885817Z","iopub.status.idle":"2023-12-28T06:53:44.889357Z","shell.execute_reply.started":"2023-12-28T06:53:44.885787Z","shell.execute_reply":"2023-12-28T06:53:44.888623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --batch 32 --epochs 1 --data 'data/cots_data.yaml' --weights 'models/yolov5s.pt' --project 'cots_detection' --name 'feature_extraction' --cache --freeze 12","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:53:47.095666Z","iopub.execute_input":"2023-12-28T06:53:47.096009Z","iopub.status.idle":"2023-12-28T06:54:05.330035Z","shell.execute_reply.started":"2023-12-28T06:53:47.095981Z","shell.execute_reply":"2023-12-28T06:54:05.329094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# INFERENCE","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\n\nimage_folder = \"/kaggle/working/yolov5/cots_detection/feature_extraction\"\n\n\nimage_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]\n\n\n\n\nfor i, file in enumerate(image_files):\n    image_path = os.path.join(image_folder, file)\n    img = Image.open(image_path)\n    fig, ax = plt.subplots(1,1,figsize=(15, 12))\n    # Display the image using matplotlib\n    plt.imshow(img)\n    plt.axis('off')  # Turn off axis labels\n    plt.title(f\"Image {i + 1}\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T14:32:02.777440Z","iopub.execute_input":"2023-12-28T14:32:02.777816Z","iopub.status.idle":"2023-12-28T14:32:02.784027Z","shell.execute_reply.started":"2023-12-28T14:32:02.777785Z","shell.execute_reply":"2023-12-28T14:32:02.783228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python val.py --weights '/kaggle/working/yolov5/cots_detection/feature_extraction/weights/best.pt' --batch 32 --data 'data/cots_data.yaml' --task test --project 'val_cots' --name 'validation_on_test_data' --augment\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:56:10.243565Z","iopub.execute_input":"2023-12-28T06:56:10.244072Z","iopub.status.idle":"2023-12-28T06:56:13.209963Z","shell.execute_reply.started":"2023-12-28T06:56:10.244035Z","shell.execute_reply":"2023-12-28T06:56:13.208813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_result('www.kaggle.com/datasets/pattanaikpritam/under-water-image')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T11:34:17.695288Z","iopub.execute_input":"2023-12-28T11:34:17.695614Z","iopub.status.idle":"2023-12-28T11:34:17.713355Z","shell.execute_reply.started":"2023-12-28T11:34:17.695584Z","shell.execute_reply":"2023-12-28T11:34:17.712573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_result('/kaggle/working/yolov5/val_cots/validation_on_test_data/PR_curve.png')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T06:56:41.491673Z","iopub.execute_input":"2023-12-28T06:56:41.492113Z","iopub.status.idle":"2023-12-28T06:56:41.519178Z","shell.execute_reply.started":"2023-12-28T06:56:41.492077Z","shell.execute_reply":"2023-12-28T06:56:41.518209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_result('/kaggle/working/yolov5/val_cots/validation_on_test_data/F1_curve.png')","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:58.3201Z","iopub.execute_input":"2023-10-01T22:07:58.321121Z","iopub.status.idle":"2023-10-01T22:07:58.964008Z","shell.execute_reply.started":"2023-10-01T22:07:58.321085Z","shell.execute_reply":"2023-10-01T22:07:58.963022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_PATH = '/kaggle/working/yolov5/runs_cots/feature_extraction/weights/best.pt'","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:58.96547Z","iopub.execute_input":"2023-10-01T22:07:58.966521Z","iopub.status.idle":"2023-10-01T22:07:58.971744Z","shell.execute_reply.started":"2023-10-01T22:07:58.966484Z","shell.execute_reply":"2023-10-01T22:07:58.970381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python detect.py --weights {MODEL_PATH}  --conf 0.6 --source '/kaggle/working/dataset/images/test' --project 'runs_test' --name 'detect_test' --augment\n","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:58.973397Z","iopub.execute_input":"2023-10-01T22:07:58.97407Z","iopub.status.idle":"2023-10-01T22:08:53.275662Z","shell.execute_reply.started":"2023-10-01T22:07:58.974034Z","shell.execute_reply":"2023-10-01T22:08:53.27455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\n\nimage_folder = '/kaggle/working/yolov5/cots_detection/detect_test'\n\n\nimage_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]\n\n\nrandom_indices = random.sample(range(len(image_files)), 10)\n\n\nfor i, index in enumerate(random_indices):\n    image_path = os.path.join(image_folder, image_files[index])\n    img = Image.open(image_path)\n    fig, ax = plt.subplots(figsize=(15, 12))\n    # Display the image using matplotlib\n    plt.imshow(img)\n    plt.axis('off')  # Turn off axis labels\n    plt.title(f\"Image {i + 1}\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:08:53.277394Z","iopub.execute_input":"2023-10-01T22:08:53.277736Z","iopub.status.idle":"2023-10-01T22:09:00.852121Z","shell.execute_reply.started":"2023-10-01T22:08:53.2777Z","shell.execute_reply":"2023-10-01T22:09:00.850874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\n#","metadata":{"execution":{"iopub.status.busy":"2023-10-08T08:12:05.643963Z","iopub.execute_input":"2023-10-08T08:12:05.644858Z","iopub.status.idle":"2023-10-08T08:12:05.72312Z","shell.execute_reply.started":"2023-10-08T08:12:05.644786Z","shell.execute_reply":"2023-10-08T08:12:05.721112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = greatbarrierreef.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T08:42:43.291202Z","iopub.execute_input":"2024-01-09T08:42:43.291686Z","iopub.status.idle":"2024-01-09T08:42:43.523974Z","shell.execute_reply.started":"2024-01-09T08:42:43.291638Z","shell.execute_reply":"2024-01-09T08:42:43.523072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T10:29:20.849576Z","iopub.execute_input":"2024-01-11T10:29:20.849851Z","iopub.status.idle":"2024-01-11T10:29:20.864775Z","shell.execute_reply.started":"2024-01-11T10:29:20.849821Z","shell.execute_reply":"2024-01-11T10:29:20.864069Z"},"trusted":true},"execution_count":null,"outputs":[]}]}