{"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":"# Covid detection using yolov5","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport ast\nimport shutil\nimport glob\nimport cv2 as cv\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.486709Z","iopub.execute_input":"2021-11-27T12:19:05.487383Z","iopub.status.idle":"2021-11-27T12:19:05.493964Z","shell.execute_reply.started":"2021-11-27T12:19:05.487335Z","shell.execute_reply":"2021-11-27T12:19:05.492319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path=\"../input/siim-covid19-resized-to-256px-jpg/train\"\nBATCH_SIZE=32\nEPOCHS=50\nIMG_SIZE=256","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.495718Z","iopub.execute_input":"2021-11-27T12:19:05.495971Z","iopub.status.idle":"2021-11-27T12:19:05.504405Z","shell.execute_reply.started":"2021-11-27T12:19:05.495941Z","shell.execute_reply":"2021-11-27T12:19:05.503526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/siim-covid19-detection/train_image_level.csv\")\ndf['image_label']=df.label.apply(lambda x: x.split(\" \")[0])\ndf['id']=df['id'].apply(lambda x: x.split(\"_\")[0])\ndf['filepath']=df['id'].apply(lambda x: train_path+'/' +x+'.jpg')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.505514Z","iopub.execute_input":"2021-11-27T12:19:05.506022Z","iopub.status.idle":"2021-11-27T12:19:05.559700Z","shell.execute_reply.started":"2021-11-27T12:19:05.505991Z","shell.execute_reply":"2021-11-27T12:19:05.559034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Trying a sample image\nplt.imshow(plt.imread(df.filepath[0]))","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.560729Z","iopub.execute_input":"2021-11-27T12:19:05.561523Z","iopub.status.idle":"2021-11-27T12:19:05.796843Z","shell.execute_reply.started":"2021-11-27T12:19:05.561491Z","shell.execute_reply":"2021-11-27T12:19:05.796252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../\nos.makedirs('/kaggle/tmp',exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.798279Z","iopub.execute_input":"2021-11-27T12:19:05.798846Z","iopub.status.idle":"2021-11-27T12:19:05.804836Z","shell.execute_reply.started":"2021-11-27T12:19:05.798812Z","shell.execute_reply":"2021-11-27T12:19:05.803970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd tmp","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.806267Z","iopub.execute_input":"2021-11-27T12:19:05.806478Z","iopub.status.idle":"2021-11-27T12:19:05.816262Z","shell.execute_reply.started":"2021-11-27T12:19:05.806434Z","shell.execute_reply":"2021-11-27T12:19:05.815532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df=pd.read_csv(\"../input/siim-covid19-resized-to-256px-jpg/meta.csv\")\ntrain_df=meta_df[meta_df.split=='train']\ntrain_df.drop(columns=['split'],inplace=True)\ntrain_df.columns=['id','dim0','dim1']\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.817552Z","iopub.execute_input":"2021-11-27T12:19:05.818118Z","iopub.status.idle":"2021-11-27T12:19:05.843398Z","shell.execute_reply.started":"2021-11-27T12:19:05.818085Z","shell.execute_reply":"2021-11-27T12:19:05.842866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.merge(train_df,on='id',how='left')\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.844437Z","iopub.execute_input":"2021-11-27T12:19:05.844826Z","iopub.status.idle":"2021-11-27T12:19:05.864217Z","shell.execute_reply.started":"2021-11-27T12:19:05.844779Z","shell.execute_reply":"2021-11-27T12:19:05.863485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train validation split","metadata":{}},{"cell_type":"code","source":"df_train,df_valid=train_test_split(df,test_size=0.3,random_state=42, stratify=df.image_label.values)\ndf_train.loc[:,\"split\"]='train'\ndf_valid.loc[:,\"split\"]='valid'\n\ndf=pd.concat([df_train,df_valid]).reset_index(drop=True)\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.865610Z","iopub.execute_input":"2021-11-27T12:19:05.865980Z","iopub.status.idle":"2021-11-27T12:19:05.901672Z","shell.execute_reply.started":"2021-11-27T12:19:05.865940Z","shell.execute_reply":"2021-11-27T12:19:05.900742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.903123Z","iopub.execute_input":"2021-11-27T12:19:05.904025Z","iopub.status.idle":"2021-11-27T12:19:05.911001Z","shell.execute_reply.started":"2021-11-27T12:19:05.903983Z","shell.execute_reply":"2021-11-27T12:19:05.910035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('covid/images/train',exist_ok=True)\nos.makedirs('covid/images/valid',exist_ok=True)\n\nos.makedirs('covid/labels/train',exist_ok=True)\nos.makedirs('covid/labels/valid',exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.912934Z","iopub.execute_input":"2021-11-27T12:19:05.913511Z","iopub.status.idle":"2021-11-27T12:19:05.920246Z","shell.execute_reply.started":"2021-11-27T12:19:05.913453Z","shell.execute_reply":"2021-11-27T12:19:05.919484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls covid/images/","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:05.921193Z","iopub.execute_input":"2021-11-27T12:19:05.921787Z","iopub.status.idle":"2021-11-27T12:19:06.725082Z","shell.execute_reply.started":"2021-11-27T12:19:05.921757Z","shell.execute_reply":"2021-11-27T12:19:06.723997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:06.726876Z","iopub.execute_input":"2021-11-27T12:19:06.727568Z","iopub.status.idle":"2021-11-27T12:19:06.734340Z","shell.execute_reply.started":"2021-11-27T12:19:06.727519Z","shell.execute_reply":"2021-11-27T12:19:06.733512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:06.736918Z","iopub.execute_input":"2021-11-27T12:19:06.737191Z","iopub.status.idle":"2021-11-27T12:19:06.758222Z","shell.execute_reply.started":"2021-11-27T12:19:06.737160Z","shell.execute_reply":"2021-11-27T12:19:06.757338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(len(df))):\n    x=df.loc[i]\n    if x.split==\"train\":\n        shutil.copyfile(f'../input/siim-covid19-resized-to-256px-jpg/train/{x.id}.jpg',f'/kaggle/tmp/covid/images/train/{x.id}.jpg')\n    else:\n         shutil.copyfile(f'../input/siim-covid19-resized-to-256px-jpg/train/{x.id}.jpg',f'/kaggle/tmp/covid/images/valid/{x.id}.jpg')\n        ","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:06.759355Z","iopub.execute_input":"2021-11-27T12:19:06.759601Z","iopub.status.idle":"2021-11-27T12:19:14.608332Z","shell.execute_reply.started":"2021-11-27T12:19:06.759573Z","shell.execute_reply":"2021-11-27T12:19:14.607534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5  # clone repo\n%cd yolov5\n# Install dependencies\n%pip install -qr requirements.txt  # install dependencies","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:14.609843Z","iopub.execute_input":"2021-11-27T12:19:14.610845Z","iopub.status.idle":"2021-11-27T12:19:23.733314Z","shell.execute_reply.started":"2021-11-27T12:19:14.610800Z","shell.execute_reply":"2021-11-27T12:19:23.732302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/tmp","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:23.734795Z","iopub.execute_input":"2021-11-27T12:19:23.735046Z","iopub.status.idle":"2021-11-27T12:19:23.742559Z","shell.execute_reply.started":"2021-11-27T12:19:23.735014Z","shell.execute_reply":"2021-11-27T12:19:23.740712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import yaml\ndata_yaml=dict(\ntrain='/kaggle/tmp/covid/images/train',\nval='/kaggle/tmp/covid/images/valid',\nnc=2,\nnames=['none','opacity'])\n\nwith open('/kaggle/tmp/yolov5/data/data.yaml','w') as outfile:\n    yaml.dump(data_yaml,outfile,default_flow_style=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:19:23.744222Z","iopub.execute_input":"2021-11-27T12:19:23.744524Z","iopub.status.idle":"2021-11-27T12:19:23.753884Z","shell.execute_reply.started":"2021-11-27T12:19:23.744483Z","shell.execute_reply":"2021-11-27T12:19:23.752820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}