{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Happy Whale YOLOv8 Train and Predict","metadata":{"papermill":{"duration":0.006204,"end_time":"2023-06-07T01:35:12.459726","exception":false,"start_time":"2023-06-07T01:35:12.453522","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"https://www.kaggle.com/datasets/bsridatta/happierwhale","metadata":{}},{"cell_type":"code","source":"#!rm -rf ","metadata":{"papermill":{"duration":0.01684,"end_time":"2023-06-07T01:35:12.48252","exception":false,"start_time":"2023-06-07T01:35:12.46568","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:18:17.682328Z","iopub.execute_input":"2023-06-20T11:18:17.682720Z","iopub.status.idle":"2023-06-20T11:18:17.687051Z","shell.execute_reply.started":"2023-06-20T11:18:17.682694Z","shell.execute_reply":"2023-06-20T11:18:17.686158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install wandb==0.15.0\n!wandb --version","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:18:17.688673Z","iopub.execute_input":"2023-06-20T11:18:17.689189Z","iopub.status.idle":"2023-06-20T11:18:32.837480Z","shell.execute_reply.started":"2023-06-20T11:18:17.689161Z","shell.execute_reply":"2023-06-20T11:18:32.836063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"_kg_hide-output":true,"papermill":{"duration":16.651395,"end_time":"2023-06-07T01:35:29.139783","exception":false,"start_time":"2023-06-07T01:35:12.488388","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:18:32.842845Z","iopub.execute_input":"2023-06-20T11:18:32.843223Z","iopub.status.idle":"2023-06-20T11:18:43.571234Z","shell.execute_reply.started":"2023-06-20T11:18:32.843187Z","shell.execute_reply":"2023-06-20T11:18:43.569898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport random\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nimport shutil\nfrom shutil import copyfile\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\nfrom PIL import Image\nimport ultralytics\nfrom ultralytics import YOLO\nultralytics.checks()","metadata":{"papermill":{"duration":6.461222,"end_time":"2023-06-07T01:35:35.611088","exception":false,"start_time":"2023-06-07T01:35:29.149866","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:18:43.572801Z","iopub.execute_input":"2023-06-20T11:18:43.573172Z","iopub.status.idle":"2023-06-20T11:18:44.611729Z","shell.execute_reply.started":"2023-06-20T11:18:43.573140Z","shell.execute_reply":"2023-06-20T11:18:44.610975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Wandb login:\nfrom kaggle_secrets import UserSecretsClient\nimport wandb\nuser_secrets = UserSecretsClient()\nsecret_value = user_secrets.get_secret(\"wandb_api_key\")\nwandb.login(key=secret_value)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:18:44.613565Z","iopub.execute_input":"2023-06-20T11:18:44.614345Z","iopub.status.idle":"2023-06-20T11:18:45.146232Z","shell.execute_reply.started":"2023-06-20T11:18:44.614315Z","shell.execute_reply":"2023-06-20T11:18:45.145407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir datasets\n!mkdir datasets/train\n!mkdir datasets/valid\n!mkdir datasets/test\n\ntrain_path='datasets/train/'\nvalid_path='datasets/valid/'\ntest_path='datasets/test/'","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:18:45.147454Z","iopub.execute_input":"2023-06-20T11:18:45.147776Z","iopub.status.idle":"2023-06-20T11:18:49.199041Z","shell.execute_reply.started":"2023-06-20T11:18:45.147749Z","shell.execute_reply":"2023-06-20T11:18:49.197558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{"papermill":{"duration":0.007884,"end_time":"2023-06-07T01:35:35.627307","exception":false,"start_time":"2023-06-07T01:35:35.619423","status":"completed"},"tags":[]}},{"cell_type":"code","source":"ano_paths=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/happierwhale/yolo_annotations/yolo_annotations'):\n    for filename in filenames:\n        ano_paths+=[(os.path.join(dirname, filename))]\n        \nn=200 #len(ano_paths) \nprint(n)\nN=list(range(200)) #len(ano_paths))\nrandom.shuffle(N)\n\ntrain_ratio = 0.7\nvalid_ratio = 0.2\ntest_ratio = 0.1\n\ntrain_size = int(train_ratio*n)\nvalid_size = int(valid_ratio*n)\n\ntrain_i = N[:train_size]\nvalid_i = N[train_size:train_size+valid_size]\ntest_i = N[train_size+valid_size:]\n\nprint(train_i)\nprint(valid_i)\nprint(test_i)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:18:49.201928Z","iopub.execute_input":"2023-06-20T11:18:49.202715Z","iopub.status.idle":"2023-06-20T11:18:49.546675Z","shell.execute_reply.started":"2023-06-20T11:18:49.202665Z","shell.execute_reply":"2023-06-20T11:18:49.545609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_i:\n    ano_path=ano_paths[i]\n    img_path=os.path.join('/kaggle/input/happy-whale-and-dolphin/train_images',\n                          ano_path.split('/')[-1][0:-4]+'.jpg')\n    try:\n        !cp {ano_path} {train_path}\n        !cp {img_path} {train_path}\n    except:\n        continue\nprint(len(os.listdir(train_path)))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:18:49.547919Z","iopub.execute_input":"2023-06-20T11:18:49.548469Z","iopub.status.idle":"2023-06-20T11:20:54.169912Z","shell.execute_reply.started":"2023-06-20T11:18:49.548433Z","shell.execute_reply":"2023-06-20T11:20:54.168578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in valid_i:\n    ano_path=ano_paths[i]\n    img_path=os.path.join('/kaggle/input/happy-whale-and-dolphin/train_images',\n                          ano_path.split('/')[-1][0:-4]+'.jpg')\n    try:\n        !cp {ano_path} {valid_path}\n        !cp {img_path} {valid_path}\n    except:\n        continue\nprint(len(os.listdir(valid_path)))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:20:54.171710Z","iopub.execute_input":"2023-06-20T11:20:54.172214Z","iopub.status.idle":"2023-06-20T11:21:36.323613Z","shell.execute_reply.started":"2023-06-20T11:20:54.172161Z","shell.execute_reply":"2023-06-20T11:21:36.322347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in test_i:\n    ano_path=ano_paths[i]\n    img_path=os.path.join('/kaggle/input/happy-whale-and-dolphin/train_images',\n                          ano_path.split('/')[-1][0:-4]+'.jpg')\n    try:\n        !cp {ano_path} {test_path}\n        !cp {img_path} {test_path}\n    except:\n        continue\nprint(len(os.listdir(test_path)))      ","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:21:36.325273Z","iopub.execute_input":"2023-06-20T11:21:36.325598Z","iopub.status.idle":"2023-06-20T11:22:16.893481Z","shell.execute_reply.started":"2023-06-20T11:21:36.325561Z","shell.execute_reply":"2023-06-20T11:22:16.892060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create yaml file ","metadata":{"papermill":{"duration":0.008131,"end_time":"2023-06-07T01:35:39.116855","exception":false,"start_time":"2023-06-07T01:35:39.108724","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import yaml\n\ndata_yaml = dict(\n    train ='train',\n    val ='valid',\n    test='test',\n    nc =1,\n    names =['whale']\n)\n\nwith open('data.yaml', 'w') as outfile:\n    yaml.dump(data_yaml, outfile, default_flow_style=True)\n    \n%cat data.yaml","metadata":{"papermill":{"duration":1.032165,"end_time":"2023-06-07T01:35:40.157528","exception":false,"start_time":"2023-06-07T01:35:39.125363","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:16.897617Z","iopub.execute_input":"2023-06-20T11:22:16.898505Z","iopub.status.idle":"2023-06-20T11:22:17.920585Z","shell.execute_reply.started":"2023-06-20T11:22:16.898463Z","shell.execute_reply":"2023-06-20T11:22:17.919516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names =['whale']\nM=list(range(len(names)))\nclass_map=dict(zip(M,names))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:22:17.923393Z","iopub.execute_input":"2023-06-20T11:22:17.924291Z","iopub.status.idle":"2023-06-20T11:22:17.930277Z","shell.execute_reply.started":"2023-06-20T11:22:17.924244Z","shell.execute_reply":"2023-06-20T11:22:17.929306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{"papermill":{"duration":0.00807,"end_time":"2023-06-07T01:35:40.174226","exception":false,"start_time":"2023-06-07T01:35:40.166156","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model = YOLO(\"yolov8x.pt\") ","metadata":{"papermill":{"duration":1.316872,"end_time":"2023-06-07T01:35:41.499518","exception":false,"start_time":"2023-06-07T01:35:40.182646","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:17.932134Z","iopub.execute_input":"2023-06-20T11:22:17.932734Z","iopub.status.idle":"2023-06-20T11:22:19.325994Z","shell.execute_reply.started":"2023-06-20T11:22:17.932696Z","shell.execute_reply":"2023-06-20T11:22:19.325162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!yolo task=detect mode=train model=yolov8x.pt data=data.yaml epochs=12 imgsz=480","metadata":{"papermill":{"duration":7.629753,"end_time":"2023-06-07T01:35:49.138021","exception":false,"start_time":"2023-06-07T01:35:41.508268","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:19.327359Z","iopub.execute_input":"2023-06-20T11:22:19.327873Z","iopub.status.idle":"2023-06-20T11:22:40.690426Z","shell.execute_reply.started":"2023-06-20T11:22:19.327841Z","shell.execute_reply":"2023-06-20T11:22:40.689204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Result of Training","metadata":{"papermill":{"duration":0.008697,"end_time":"2023-06-07T01:35:49.15585","exception":false,"start_time":"2023-06-07T01:35:49.147153","status":"completed"},"tags":[]}},{"cell_type":"code","source":"paths2=[]\nfor dirname, _, filenames in os.walk('/kaggle/working/runs/detect/train'):\n    for filename in filenames:\n        if filename[-4:]=='.jpg':\n            paths2+=[(os.path.join(dirname, filename))]\npaths2=sorted(paths2)","metadata":{"papermill":{"duration":0.258393,"end_time":"2023-06-07T01:35:49.42306","exception":true,"start_time":"2023-06-07T01:35:49.164667","status":"failed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:40.691971Z","iopub.execute_input":"2023-06-20T11:22:40.693062Z","iopub.status.idle":"2023-06-20T11:22:40.699521Z","shell.execute_reply.started":"2023-06-20T11:22:40.693025Z","shell.execute_reply":"2023-06-20T11:22:40.698579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for path in paths2:\n    image = Image.open(path)\n    image=np.array(image)\n    plt.figure(figsize=(20,10))\n    plt.imshow(image)\n    plt.show()","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:40.700894Z","iopub.execute_input":"2023-06-20T11:22:40.701231Z","iopub.status.idle":"2023-06-20T11:22:40.713509Z","shell.execute_reply.started":"2023-06-20T11:22:40.701205Z","shell.execute_reply":"2023-06-20T11:22:40.712431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"best_path0='runs/detect/train/weights/best.pt'\nsource0='datasets/test'","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:40.714741Z","iopub.execute_input":"2023-06-20T11:22:40.715057Z","iopub.status.idle":"2023-06-20T11:22:40.726286Z","shell.execute_reply.started":"2023-06-20T11:22:40.715031Z","shell.execute_reply":"2023-06-20T11:22:40.725043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ppaths=[]\nfor dirname, _, filenames in os.walk(source0):\n    for filename in filenames:\n        if filename[-4:]=='.jpg':\n            ppaths+=[(os.path.join(dirname, filename))]\nppaths=sorted(ppaths)\nprint(ppaths[0])\nprint(len(ppaths))","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:40.728033Z","iopub.execute_input":"2023-06-20T11:22:40.728404Z","iopub.status.idle":"2023-06-20T11:22:41.020712Z","shell.execute_reply.started":"2023-06-20T11:22:40.728371Z","shell.execute_reply":"2023-06-20T11:22:41.018999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = YOLO(best_path0)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.021717Z","iopub.status.idle":"2023-06-20T11:22:41.022140Z","shell.execute_reply.started":"2023-06-20T11:22:41.021931Z","shell.execute_reply":"2023-06-20T11:22:41.021950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!yolo task=detect mode=predict model={best_path0} conf=0.3 source={source0}","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.023715Z","iopub.status.idle":"2023-06-20T11:22:41.024105Z","shell.execute_reply.started":"2023-06-20T11:22:41.023911Z","shell.execute_reply":"2023-06-20T11:22:41.023928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Result of Prediction","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"results = model2.predict(source0,conf=0.3)\nprint(len(results))","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.025724Z","iopub.status.idle":"2023-06-20T11:22:41.026073Z","shell.execute_reply.started":"2023-06-20T11:22:41.025903Z","shell.execute_reply":"2023-06-20T11:22:41.025918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print((results[0].boxes.data))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:22:41.027299Z","iopub.status.idle":"2023-06-20T11:22:41.027671Z","shell.execute_reply.started":"2023-06-20T11:22:41.027486Z","shell.execute_reply":"2023-06-20T11:22:41.027512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PBOX=pd.DataFrame(columns=range(6))\nfor i in range(len(results)):\n    arri=pd.DataFrame(results[i].boxes.data.cpu().numpy()).astype(float)\n    path=ppaths[i]\n    file=path.split('/')[-1]\n    arri=arri.assign(file=file)\n    arri=arri.assign(i=i)\n    PBOX=pd.concat([PBOX,arri],axis=0)\nPBOX.columns=['x','y','x2','y2','confidence','class','file','i']\ndisplay(PBOX)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.029465Z","iopub.status.idle":"2023-06-20T11:22:41.029975Z","shell.execute_reply.started":"2023-06-20T11:22:41.029715Z","shell.execute_reply":"2023-06-20T11:22:41.029739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PBOX['class']=PBOX['class'].apply(lambda x: class_map[int(x)])\nPBOX=PBOX.reset_index(drop=True)\ndisplay(PBOX)\ndisplay(PBOX['class'].value_counts())","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.031796Z","iopub.status.idle":"2023-06-20T11:22:41.032620Z","shell.execute_reply.started":"2023-06-20T11:22:41.032333Z","shell.execute_reply":"2023-06-20T11:22:41.032360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_box2(n0):\n    \n    ipath=ppaths[n0]\n    image=cv2.imread(ipath)\n    H,W=image.shape[0],image.shape[1]\n    file=ipath.split('/')[-1]\n    \n    if PBOX[PBOX['file']==file] is not None:\n        box=PBOX[PBOX['file']==file]\n        box=box.reset_index(drop=True)\n        #display(box)\n\n        for i in range(len(box)):\n            label=box.loc[i,'class']\n            x=int(box.loc[i,'x'])\n            y=int(box.loc[i,'y'])\n            x2=int(box.loc[i,'x2']) \n            y2=int(box.loc[i,'y2'])\n            #print(label,x,y,x2,y2)\n            cv2.putText(image, f'{label}', (x, int(y-4)), \n                        cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0),2)\n            cv2.rectangle(image,(x,y),(x2,y2),(0,255,0),2) #green\n    \n    #plt.imshow(image)\n    #plt.show()   \n    \n    return image","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.033664Z","iopub.status.idle":"2023-06-20T11:22:41.034189Z","shell.execute_reply.started":"2023-06-20T11:22:41.033903Z","shell.execute_reply":"2023-06-20T11:22:41.033926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):\n    fig=plt.figure(figsize=(12,8))\n    im=plt.imshow(cv2.cvtColor(ims[0],cv2.COLOR_BGR2RGB))\n    text = plt.text(0.05, 0.05, f'Slide {0}', transform=fig.transFigure, fontsize=14, color='blue')\n    plt.axis('off')\n    plt.close()\n\n    def animate_func(i):\n        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))\n        text.set_text(f'Slide {i}')        \n        return [im]    \n    \n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T11:22:41.035812Z","iopub.status.idle":"2023-06-20T11:22:41.036214Z","shell.execute_reply.started":"2023-06-20T11:22:41.035999Z","shell.execute_reply":"2023-06-20T11:22:41.036017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images2=[]\nfor i in tqdm(range(len(ppaths))):\n    images2+=[draw_box2(i)]","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.038145Z","iopub.status.idle":"2023-06-20T11:22:41.039065Z","shell.execute_reply.started":"2023-06-20T11:22:41.038874Z","shell.execute_reply":"2023-06-20T11:22:41.038893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images2)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-06-20T11:22:41.040217Z","iopub.status.idle":"2023-06-20T11:22:41.040834Z","shell.execute_reply.started":"2023-06-20T11:22:41.040648Z","shell.execute_reply":"2023-06-20T11:22:41.040667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]}]}