{"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":"code","source":"# Let's get all the relevant libraries\n\n# Data Managment  \nimport numpy as np\nimport pandas as pd\nimport os\nimport cv2 \nimport json \nfrom glob import glob\nfrom PIL import Image\n\n# Dicom readers \nimport pydicom \nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\n# Plotting and Vizualization \nimport seaborn as sns \nimport matplotlib.pyplot as plt\n\n# Miscellaneous \nfrom tqdm.auto import tqdm\n\n#Torch \nimport torch ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-16T16:37:38.887823Z","iopub.execute_input":"2022-12-16T16:37:38.888471Z","iopub.status.idle":"2022-12-16T16:37:44.776227Z","shell.execute_reply.started":"2022-12-16T16:37:38.888362Z","shell.execute_reply":"2022-12-16T16:37:44.775023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 512\nBATCH_SIZE = 16\nEPOCHS = 40","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.777960Z","iopub.execute_input":"2022-12-16T16:37:44.778218Z","iopub.status.idle":"2022-12-16T16:37:44.783328Z","shell.execute_reply.started":"2022-12-16T16:37:44.778186Z","shell.execute_reply":"2022-12-16T16:37:44.781923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/siim-covid19-detection')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.784858Z","iopub.execute_input":"2022-12-16T16:37:44.785175Z","iopub.status.idle":"2022-12-16T16:37:44.802109Z","shell.execute_reply.started":"2022-12-16T16:37:44.785143Z","shell.execute_reply":"2022-12-16T16:37:44.801304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = '/kaggle/input/siim-covid19-detection'","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.804042Z","iopub.execute_input":"2022-12-16T16:37:44.804347Z","iopub.status.idle":"2022-12-16T16:37:44.808302Z","shell.execute_reply.started":"2022-12-16T16:37:44.804308Z","shell.execute_reply":"2022-12-16T16:37:44.807661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study_df = pd.read_csv(dataset + '/train_study_level.csv')\ntrain_study_df ","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.809498Z","iopub.execute_input":"2022-12-16T16:37:44.810284Z","iopub.status.idle":"2022-12-16T16:37:44.848018Z","shell.execute_reply.started":"2022-12-16T16:37:44.810252Z","shell.execute_reply":"2022-12-16T16:37:44.847356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_df = pd.read_csv(dataset + '/train_image_level.csv')\ntrain_image_df","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.849143Z","iopub.execute_input":"2022-12-16T16:37:44.849379Z","iopub.status.idle":"2022-12-16T16:37:44.904472Z","shell.execute_reply.started":"2022-12-16T16:37:44.849349Z","shell.execute_reply":"2022-12-16T16:37:44.903632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"There are {} images with no bounding boxes in the dataset\"\n                      .format(train_image_df[\"boxes\"].isna().sum()))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.905902Z","iopub.execute_input":"2022-12-16T16:37:44.906195Z","iopub.status.idle":"2022-12-16T16:37:44.911984Z","shell.execute_reply.started":"2022-12-16T16:37:44.906159Z","shell.execute_reply":"2022-12-16T16:37:44.911235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_df[\"label\"]\n\n# Let's have a look at the labels: the opacity or none class\n# opacity means that the image contains a bouding box, no means that there is no such box. \n# Then, the last 4 numbers correspond to the coordinates of the box, in the following format: \n# xmin ymin xmax ymax \n# and if the class is non, the values are 0 0 1 1 ","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.913584Z","iopub.execute_input":"2022-12-16T16:37:44.914111Z","iopub.status.idle":"2022-12-16T16:37:44.924951Z","shell.execute_reply.started":"2022-12-16T16:37:44.914077Z","shell.execute_reply":"2022-12-16T16:37:44.924001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's get an idea of what is asked in the submission file\n\nsubmission_df = pd.read_csv(dataset + '/sample_submission.csv')\nprint(submission_df.shape)\nfor i in range(10): \n    print(submission_df.loc[i,:])\n    \n# We need to return, for each study in the test dataset, and Predicition String that include\n# the opaque or none label (or, disease or no disease) and if opaque, the values of all coordinates ","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.926664Z","iopub.execute_input":"2022-12-16T16:37:44.927011Z","iopub.status.idle":"2022-12-16T16:37:44.951646Z","shell.execute_reply.started":"2022-12-16T16:37:44.926928Z","shell.execute_reply":"2022-12-16T16:37:44.950982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The train_study file also fives use, for each study, which kind of Pneumonia is \n# associated with the patients.\n\n# Let's plot each subtypes \nsubtypes = train_study_df.groupby(['Negative for Pneumonia', 'Typical Appearance',\n       'Indeterminate Appearance', 'Atypical Appearance']).count().reset_index()\nsubtypes[\"label\"] = ['Atypical Appearance', 'Indeterminate Appearance',\n               'Typical Appearance', 'Negative for Pneumonia']\n\nax = plt.subplots(figsize=(21,10))\nax = sns.barplot(x=subtypes.label, y=subtypes.id, palette=\"deep\", orient='v')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:44.954682Z","iopub.execute_input":"2022-12-16T16:37:44.954869Z","iopub.status.idle":"2022-12-16T16:37:45.240258Z","shell.execute_reply.started":"2022-12-16T16:37:44.954848Z","shell.execute_reply":"2022-12-16T16:37:45.239482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's see the distribution between opacity and none \nclass_df = train_image_df[\"label\"].apply(lambda x: x.split(\" \")[0]).value_counts().reset_index()\nclass_df\nsns.barplot(x=class_df.label, y=[\"opacity\",\"none\"], palette=\"deep\", orient='h')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:45.241893Z","iopub.execute_input":"2022-12-16T16:37:45.242394Z","iopub.status.idle":"2022-12-16T16:37:45.427210Z","shell.execute_reply.started":"2022-12-16T16:37:45.242353Z","shell.execute_reply":"2022-12-16T16:37:45.426435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now let's create a column with the study_ids, to make life a bit easier \ntrain_study_df[\"study_id\"] = train_study_df[\"id\"].apply(lambda x: x.split(\"_\")[0])\ntrain_study_df","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:45.428554Z","iopub.execute_input":"2022-12-16T16:37:45.428788Z","iopub.status.idle":"2022-12-16T16:37:45.449770Z","shell.execute_reply.started":"2022-12-16T16:37:45.428756Z","shell.execute_reply":"2022-12-16T16:37:45.448900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's create a final train dataframe with all the information \ntrain = pd.merge(train_image_df, train_study_df, \n                 left_on=\"StudyInstanceUID\", right_on=\"study_id\")\ntrain.drop([ \"StudyInstanceUID\", \"id_y\"], axis=1, inplace=True)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:45.451261Z","iopub.execute_input":"2022-12-16T16:37:45.451593Z","iopub.status.idle":"2022-12-16T16:37:45.480964Z","shell.execute_reply.started":"2022-12-16T16:37:45.451490Z","shell.execute_reply":"2022-12-16T16:37:45.480168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.sort_values('study_id')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:45.482281Z","iopub.execute_input":"2022-12-16T16:37:45.482541Z","iopub.status.idle":"2022-12-16T16:37:45.504090Z","shell.execute_reply.started":"2022-12-16T16:37:45.482510Z","shell.execute_reply":"2022-12-16T16:37:45.503376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.rename(columns={\"id_x\":\"id\"})","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:45.506148Z","iopub.execute_input":"2022-12-16T16:37:45.506921Z","iopub.status.idle":"2022-12-16T16:37:45.511094Z","shell.execute_reply.started":"2022-12-16T16:37:45.506873Z","shell.execute_reply":"2022-12-16T16:37:45.510662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make a list of all the paths for all the images \ndicom_paths = glob(f'{dataset}/train/*/*/*.dcm')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:37:45.512105Z","iopub.execute_input":"2022-12-16T16:37:45.512598Z","iopub.status.idle":"2022-12-16T16:38:14.207321Z","shell.execute_reply.started":"2022-12-16T16:37:45.512528Z","shell.execute_reply":"2022-12-16T16:38:14.206573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(dataset + '/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:14.208644Z","iopub.execute_input":"2022-12-16T16:38:14.208942Z","iopub.status.idle":"2022-12-16T16:38:14.219544Z","shell.execute_reply.started":"2022-12-16T16:38:14.208897Z","shell.execute_reply":"2022-12-16T16:38:14.218766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:14.220849Z","iopub.execute_input":"2022-12-16T16:38:14.221142Z","iopub.status.idle":"2022-12-16T16:38:14.234943Z","shell.execute_reply.started":"2022-12-16T16:38:14.221107Z","shell.execute_reply":"2022-12-16T16:38:14.234128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = glob(f'{dataset}/test/*/*/*.dcm')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:14.236267Z","iopub.execute_input":"2022-12-16T16:38:14.236539Z","iopub.status.idle":"2022-12-16T16:38:19.522610Z","shell.execute_reply.started":"2022-12-16T16:38:14.236500Z","shell.execute_reply":"2022-12-16T16:38:19.521862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dcm = pd.DataFrame({'dcm_path':test_path})\ntest_dcm['id']  = test_dcm.dcm_path.map(lambda x: x.split('/')[-1].replace('.dcm','_image'))\ntest_dcm","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.523950Z","iopub.execute_input":"2022-12-16T16:38:19.524203Z","iopub.status.idle":"2022-12-16T16:38:19.541638Z","shell.execute_reply.started":"2022-12-16T16:38:19.524168Z","shell.execute_reply":"2022-12-16T16:38:19.540928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get a Dataframe that includes the path \ndcm_df = pd.DataFrame({'dcm_path':dicom_paths})\ndcm_df['id'] = dcm_df.dcm_path.map(lambda x: x.split('/')[-1].replace('.dcm','_image'))\ndcm_df","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.543039Z","iopub.execute_input":"2022-12-16T16:38:19.543516Z","iopub.status.idle":"2022-12-16T16:38:19.563106Z","shell.execute_reply.started":"2022-12-16T16:38:19.543482Z","shell.execute_reply":"2022-12-16T16:38:19.562359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Merge both dataframe to have the paths in the train DataFrame \ntrain = train.merge(dcm_df, on='id', how='left')\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.564285Z","iopub.execute_input":"2022-12-16T16:38:19.564757Z","iopub.status.idle":"2022-12-16T16:38:19.591335Z","shell.execute_reply.started":"2022-12-16T16:38:19.564725Z","shell.execute_reply":"2022-12-16T16:38:19.590718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Merge both dataframe to have the paths in the train DataFrame \ntest = test_df.merge(test_dcm, on='id', how='left')\ntest","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.592610Z","iopub.execute_input":"2022-12-16T16:38:19.593035Z","iopub.status.idle":"2022-12-16T16:38:19.609839Z","shell.execute_reply.started":"2022-12-16T16:38:19.593004Z","shell.execute_reply":"2022-12-16T16:38:19.609164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.611012Z","iopub.execute_input":"2022-12-16T16:38:19.611464Z","iopub.status.idle":"2022-12-16T16:38:19.629728Z","shell.execute_reply.started":"2022-12-16T16:38:19.611433Z","shell.execute_reply":"2022-12-16T16:38:19.629273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.630939Z","iopub.execute_input":"2022-12-16T16:38:19.631388Z","iopub.status.idle":"2022-12-16T16:38:19.643376Z","shell.execute_reply.started":"2022-12-16T16:38:19.631357Z","shell.execute_reply":"2022-12-16T16:38:19.642859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dev = train[:200]\ntrain_dev","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.644549Z","iopub.execute_input":"2022-12-16T16:38:19.644976Z","iopub.status.idle":"2022-12-16T16:38:19.662916Z","shell.execute_reply.started":"2022-12-16T16:38:19.644945Z","shell.execute_reply":"2022-12-16T16:38:19.662185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_dev = train[-100:]\nvalid_dev","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.664202Z","iopub.execute_input":"2022-12-16T16:38:19.664658Z","iopub.status.idle":"2022-12-16T16:38:19.682421Z","shell.execute_reply.started":"2022-12-16T16:38:19.664626Z","shell.execute_reply":"2022-12-16T16:38:19.681800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The dicom to array function simply reads the dicom image, and returns a numpy array\n# Then, the plot_img and plot_imgs functions can plot one or several images\n\n\ndef dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    if voi_lut: \n        array = apply_voi_lut(dicom.pixel_array, dicom)\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        array = np.amax(array) - array\n    array = array - np.min(array)\n    array = array / np.max(array)\n    array = (array * 255).astype(np.uint8)\n    return array\n\ndef plot_img(img, size=(7, 7), is_rgb=True, title=\"\", cmap='gray'):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n\n\ndef plot_imgs(imgs, cols=4, size=7, is_rgb=True, title='',cmap='gray', img_size=(512,512)):\n    rows = len(imgs)//cols + 1 \n    print(rows)\n    fig = plt.figure(figsize=(cols*size, rows*size))\n    for i, img in enumerate(imgs):\n        if img_size is not None: \n            img = cv2.resize(img, img_size)\n        fig.add_subplot(rows, cols, i+1)\n        plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.686735Z","iopub.execute_input":"2022-12-16T16:38:19.687104Z","iopub.status.idle":"2022-12-16T16:38:19.696196Z","shell.execute_reply.started":"2022-12-16T16:38:19.687070Z","shell.execute_reply":"2022-12-16T16:38:19.695473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's look at one image \nimg = dicom2array(dicom_paths[20])\nplot_img(img)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:19.697376Z","iopub.execute_input":"2022-12-16T16:38:19.697846Z","iopub.status.idle":"2022-12-16T16:38:21.101888Z","shell.execute_reply.started":"2022-12-16T16:38:19.697814Z","shell.execute_reply":"2022-12-16T16:38:21.101172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's look at several images \n\nimgs = [dicom2array(path) for path in dicom_paths[:4]]\nplot_imgs(imgs)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:21.102822Z","iopub.execute_input":"2022-12-16T16:38:21.103042Z","iopub.status.idle":"2022-12-16T16:38:23.224787Z","shell.execute_reply.started":"2022-12-16T16:38:21.103011Z","shell.execute_reply":"2022-12-16T16:38:23.220606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's make some bounding boxes, to visualize the task \n# The function plot_bboxes_with_label takes as imput a label, n images, and plots\n# n number of images from the corresping label with the boxes associated \n\n# while I know that in this project, the positive classes for COVID should be green, and every\n# thing else yellow. \n# I will keep it that was for development sake, and we will see later on\n\n# Credits to:  https://www.kaggle.com/piantic/siim-fisabio-rsna-covid-19-detection-basic-eda\n\nfrom colorama import Fore, Back, Style\n\nlabel2color = {\n    '[1, 0, 0]': [255,0,0], # Typical Appearance\n    '[0, 1, 0]': [0,255,0], # Indeterminate Appearance\n    '[0, 0, 1]': [0,0,255], # Atypical Appearance\n    '[0, 0, 0]': None, # negative\n}\n\nclass_names = ['Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance']\n\ndef plot_bboxes_with_label(label_name, n): \n    print('Typical Appearance: ' + Fore.RED + 'Red',Style.RESET_ALL)\n    print('Indeterminate Appearance: '  + Fore.GREEN + 'Green',Style.RESET_ALL)\n    print('Atypical Appearance: ' + Fore.BLUE + 'Blue',Style.RESET_ALL)\n    \n    imgs = []\n    \n    thickness = 2 \n    scale = 5 \n    \n    if label_name == \"Negative for Pneumonia\": \n        flag = 0\n    else: \n        flag = 1\n    \n    for _, row in train[train[label_name]==flag].iloc[:n].iterrows():\n        # _ is the index, row is well, the row \n        study_id=row['study_id'] # get the study ids \n        img_path = glob(f'{dataset}/train/{study_id}/*/*')[0] # get all the path, \n        img = dicom2array(img_path)\n        img = cv2.resize(img, None, fx=1/scale, fy=1/scale)\n        img = np.stack([img, img, img], axis=-1)\n        \n        claz = row[class_names].values\n        color = label2color[str(claz.tolist())]\n\n        bboxes = []\n        bbox = []\n        \n        for i, l in enumerate(row['label'].split(' ')): \n            # i is index, l the label\n            if (i % 6 == 0) | (i % 6 == 1):\n                continue\n            bbox.append(float(l)/scale)\n            if i % 6 == 5: \n                bboxes.append(bbox)\n                bbox = []\n        for box in bboxes: \n            img = cv2.rectangle(\n                img,\n                (int(box[0]), int(box[1])),\n                (int(box[2]), int(box[3])),\n                color, thickness\n            )\n        img = cv2.resize(img, (512,512))\n        imgs.append(img)\n    \n    plot_imgs(imgs, cmap=None)\n    \n    del img, imgs, bbox, bboxes","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:23.226085Z","iopub.execute_input":"2022-12-16T16:38:23.226573Z","iopub.status.idle":"2022-12-16T16:38:23.241111Z","shell.execute_reply.started":"2022-12-16T16:38:23.226537Z","shell.execute_reply":"2022-12-16T16:38:23.240375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This cell will print several images with bounding box\n# You can change the label to print different images from differnt categories \nplot_bboxes_with_label(\"Negative for Pneumonia\", 4)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:23.242510Z","iopub.execute_input":"2022-12-16T16:38:23.242922Z","iopub.status.idle":"2022-12-16T16:38:25.396984Z","shell.execute_reply.started":"2022-12-16T16:38:23.242889Z","shell.execute_reply":"2022-12-16T16:38:25.396370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Now let's clone the model and save the images in a different directories for future use ","metadata":{}},{"cell_type":"code","source":"os.makedirs('/kaggle/working/tmp/', exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:25.398112Z","iopub.execute_input":"2022-12-16T16:38:25.398791Z","iopub.status.idle":"2022-12-16T16:38:25.403362Z","shell.execute_reply.started":"2022-12-16T16:38:25.398751Z","shell.execute_reply":"2022-12-16T16:38:25.402495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:25.404555Z","iopub.execute_input":"2022-12-16T16:38:25.405162Z","iopub.status.idle":"2022-12-16T16:38:25.414449Z","shell.execute_reply.started":"2022-12-16T16:38:25.405129Z","shell.execute_reply":"2022-12-16T16:38:25.413536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:25.416156Z","iopub.execute_input":"2022-12-16T16:38:25.416543Z","iopub.status.idle":"2022-12-16T16:38:28.313949Z","shell.execute_reply.started":"2022-12-16T16:38:25.416501Z","shell.execute_reply":"2022-12-16T16:38:28.313003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolov5\n!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:28.315530Z","iopub.execute_input":"2022-12-16T16:38:28.315811Z","iopub.status.idle":"2022-12-16T16:38:37.144887Z","shell.execute_reply.started":"2022-12-16T16:38:28.315771Z","shell.execute_reply":"2022-12-16T16:38:37.143996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%ls","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:37.147853Z","iopub.execute_input":"2022-12-16T16:38:37.148148Z","iopub.status.idle":"2022-12-16T16:38:38.136865Z","shell.execute_reply.started":"2022-12-16T16:38:37.148108Z","shell.execute_reply":"2022-12-16T16:38:38.135996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('data/images/train', exist_ok=True)\nos.makedirs('data/images/valid', exist_ok=True)\n\nos.makedirs('data/labels/train', exist_ok=True)\nos.makedirs('data/labels/valid', exist_ok=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:38.140460Z","iopub.execute_input":"2022-12-16T16:38:38.140735Z","iopub.status.idle":"2022-12-16T16:38:38.146315Z","shell.execute_reply.started":"2022-12-16T16:38:38.140705Z","shell.execute_reply":"2022-12-16T16:38:38.145553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd data","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:38.147593Z","iopub.execute_input":"2022-12-16T16:38:38.148519Z","iopub.status.idle":"2022-12-16T16:38:38.156519Z","shell.execute_reply.started":"2022-12-16T16:38:38.148474Z","shell.execute_reply":"2022-12-16T16:38:38.155711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create .yaml file \nimport yaml\n\ndata_yaml = dict(\n    train = '/kaggle/working/tmp/yolov5/data/images/train',\n    val = '/kaggle/working/tmp/yolov5/data/images/valid',\n    nc = 2,\n    names = ['none', 'opacity']\n)\n\n# Note that I am creating the file in the yolov5/data/ directory.\nwith open('/kaggle/working/tmp/yolov5/data/data.yaml', 'w') as outfile:\n    yaml.dump(data_yaml, outfile, default_flow_style=True)\n    \n%cat /kaggle/working/tmp/yolov5/data/data.yaml","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:38.157806Z","iopub.execute_input":"2022-12-16T16:38:38.158165Z","iopub.status.idle":"2022-12-16T16:38:39.193127Z","shell.execute_reply.started":"2022-12-16T16:38:38.158132Z","shell.execute_reply":"2022-12-16T16:38:39.192264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\ndim0 = []\ndim1 = []\ndef resize_and_save(end_path, df):\n    dim0 = []\n    dim1 = []\n    filenames = []\n    for index, row in tqdm(df[['study_id', 'dcm_path']].iterrows(), total = df.shape[0]):\n        try: \n            array = dicom2array(row['dcm_path'])\n            dim0.append(array.shape[0])\n            dim1.append(array.shape[1])\n            img = cv2.resize(array, (IMG_SIZE,IMG_SIZE))\n            img = Image.fromarray(img)\n   \n            filename = row['dcm_path'].split('/')[-1].split('.')[0]\n            filenames.append(filename)\n            img.save(os.path.join(end_path, f'{filename}.png'))\n        except RuntimeError:\n            pass\n    return pd.DataFrame({'dim0':dim0, 'dim1': dim1, 'id': filenames})\n        #return filename.replace('dcm','') + '_image', array.shape[0], array.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:39.195832Z","iopub.execute_input":"2022-12-16T16:38:39.196114Z","iopub.status.idle":"2022-12-16T16:38:39.205236Z","shell.execute_reply.started":"2022-12-16T16:38:39.196080Z","shell.execute_reply":"2022-12-16T16:38:39.203665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's save the image in a new file for training \n\ndims_train = resize_and_save('/kaggle/working/tmp/yolov5/data/images/train/', train_dev)\ndims_valid = resize_and_save('/kaggle/working/tmp/yolov5/data/images/valid/', valid_dev)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:38:39.206842Z","iopub.execute_input":"2022-12-16T16:38:39.207140Z","iopub.status.idle":"2022-12-16T16:40:02.954932Z","shell.execute_reply.started":"2022-12-16T16:38:39.207105Z","shell.execute_reply":"2022-12-16T16:40:02.954164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's change the train dataframe to include the name with png\ntrain['id'] = train['id'].apply(lambda x: x.replace('_image','.png'))\n\ntrain_dev['id'] = train_dev['id'].apply(lambda x: x.replace('_image','.png'))\n\nvalid_dev['id'] = valid_dev['id'].apply(lambda x: x.replace('_image','.png'))\nvalid_dev","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:02.956541Z","iopub.execute_input":"2022-12-16T16:40:02.956991Z","iopub.status.idle":"2022-12-16T16:40:02.988843Z","shell.execute_reply.started":"2022-12-16T16:40:02.956954Z","shell.execute_reply":"2022-12-16T16:40:02.988157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dims_valid['id'] = dims_valid['id'].astype(str) + '.png'\ndims_train['id'] = dims_train['id'].astype(str) + '.png'\ndims_train","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:02.989866Z","iopub.execute_input":"2022-12-16T16:40:02.990095Z","iopub.status.idle":"2022-12-16T16:40:03.005987Z","shell.execute_reply.started":"2022-12-16T16:40:02.990064Z","shell.execute_reply":"2022-12-16T16:40:03.004996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dev = train_dev.merge(dims_train, on='id', how='left')\nvalid_dev = valid_dev.merge(dims_valid, on='id', how='left')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.007547Z","iopub.execute_input":"2022-12-16T16:40:03.007856Z","iopub.status.idle":"2022-12-16T16:40:03.021839Z","shell.execute_reply.started":"2022-12-16T16:40:03.007824Z","shell.execute_reply":"2022-12-16T16:40:03.021081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dev","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.023177Z","iopub.execute_input":"2022-12-16T16:40:03.023451Z","iopub.status.idle":"2022-12-16T16:40:03.047948Z","shell.execute_reply.started":"2022-12-16T16:40:03.023399Z","shell.execute_reply":"2022-12-16T16:40:03.047204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the raw bounding box by parsing the row value of the label column.\n# Ref: https://www.kaggle.com/yujiariyasu/plot-3positive-classes\ndef get_bbox(row):\n    bboxes = []\n    bbox = []\n    for i, l in enumerate(row.label.split(' ')):\n        if (i % 6 == 0) | (i % 6 == 1):\n            continue\n        bbox.append(float(l))\n        if i % 6 == 5:\n            bboxes.append(bbox)\n            bbox = []  \n            \n    return bboxes\n\n# Scale the bounding boxes according to the size of the resized image. \ndef scale_bbox(row, bboxes):\n    # Get scaling factor\n    scale_x = IMG_SIZE/row.dim1\n    scale_y = IMG_SIZE/row.dim0\n    \n    scaled_bboxes = []\n    for bbox in bboxes:\n        x = int(np.round(bbox[0]*scale_x, 4))\n        y = int(np.round(bbox[1]*scale_y, 4))\n        x1 = int(np.round(bbox[2]*(scale_x), 4))\n        y1= int(np.round(bbox[3]*scale_y, 4))\n\n        scaled_bboxes.append([x, y, x1, y1]) # xmin, ymin, xmax, ymax\n        \n    return scaled_bboxes\n\n# Convert the bounding boxes in YOLO format.\ndef get_yolo_format_bbox(img_w, img_h, bboxes):\n    yolo_boxes = []\n    for bbox in bboxes:\n        w = bbox[2] - bbox[0] # xmax - xmin\n        h = bbox[3] - bbox[1] # ymax - ymin\n        xc = bbox[0] + int(np.round(w/2)) # xmin + width/2\n        yc = bbox[1] + int(np.round(h/2)) # ymin + height/2\n        \n        yolo_boxes.append([xc/img_w, yc/img_h, w/img_w, h/img_h]) # x_center y_center width height\n    \n    return yolo_boxes","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.049312Z","iopub.execute_input":"2022-12-16T16:40:03.049588Z","iopub.status.idle":"2022-12-16T16:40:03.059924Z","shell.execute_reply.started":"2022-12-16T16:40:03.049553Z","shell.execute_reply":"2022-12-16T16:40:03.058978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dev['image_level'] = train_dev.apply(lambda x: x.label.split(' ')[0], axis=1)\ntrain_dev['id'] = train_dev['id'].apply(lambda x: x.replace('.png', '.txt'))\n\nvalid_dev['image_level'] = valid_dev.apply(lambda x: x.label.split(' ')[0], axis=1)\nvalid_dev['id'] = valid_dev['id'].apply(lambda x: x.replace('.png', '.txt'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.061383Z","iopub.execute_input":"2022-12-16T16:40:03.061849Z","iopub.status.idle":"2022-12-16T16:40:03.087104Z","shell.execute_reply.started":"2022-12-16T16:40:03.061813Z","shell.execute_reply":"2022-12-16T16:40:03.086430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the txt files for bounding box\n\n\nfor i in tqdm(range(len(train_dev))):\n    row = train_dev.loc[i]\n    # Get image id\n    img_id = row.id\n    # Get image-level label\n    label = row.image_level\n    \n\n    file_name = f'/kaggle/working/tmp/yolov5/data/labels/train/{row.id}'\n        \n    try: \n        if label=='opacity':\n            # Get bboxes\n            bboxes = get_bbox(row)\n            # Scale bounding boxes\n            scale_bboxes = scale_bbox(row, bboxes)\n            # Format for YOLOv5\n            yolo_bboxes = get_yolo_format_bbox(IMG_SIZE, IMG_SIZE, scale_bboxes)\n        \n        \n            with open(file_name, 'w') as f:\n                for bbox in yolo_bboxes:\n                    \n                    bbox = [1]+bbox\n                    bbox = [str(i) for i in bbox]\n                    bbox = ' '.join(bbox)\n                    f.write(bbox)\n                    f.write('\\n')\n    except ValueError: \n        pass","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.088145Z","iopub.execute_input":"2022-12-16T16:40:03.088401Z","iopub.status.idle":"2022-12-16T16:40:03.214352Z","shell.execute_reply.started":"2022-12-16T16:40:03.088369Z","shell.execute_reply":"2022-12-16T16:40:03.213707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(len(valid_dev))):\n    row = valid_dev.loc[i]\n    # Get image id\n    img_id = row.id\n    # Get image-level label\n    label = row.image_level\n    \n\n    file_name = f'/kaggle/working/tmp/yolov5/data/labels/valid/{row.id}'\n        \n    try: \n        if label=='opacity':\n            # Get bboxes\n            bboxes = get_bbox(row)\n            # Scale bounding boxes\n            scale_bboxes = scale_bbox(row, bboxes)\n            # Format for YOLOv5\n            yolo_bboxes = get_yolo_format_bbox(IMG_SIZE, IMG_SIZE, scale_bboxes)\n        \n        \n            with open(file_name, 'w') as f:\n                for bbox in yolo_bboxes:\n                    \n                    bbox = [1]+bbox\n                    bbox = [str(i) for i in bbox]\n                    bbox = ' '.join(bbox)\n                    f.write(bbox)\n                    f.write('\\n')\n    except ValueError: \n        pass","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.215297Z","iopub.execute_input":"2022-12-16T16:40:03.215609Z","iopub.status.idle":"2022-12-16T16:40:03.296221Z","shell.execute_reply.started":"2022-12-16T16:40:03.215581Z","shell.execute_reply":"2022-12-16T16:40:03.295465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5/data/labels/train\n%ls","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:03.297395Z","iopub.execute_input":"2022-12-16T16:40:03.297956Z","iopub.status.idle":"2022-12-16T16:40:04.286905Z","shell.execute_reply.started":"2022-12-16T16:40:03.297922Z","shell.execute_reply":"2022-12-16T16:40:04.285857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's verify that this is what we want \n\nf = open('000a312787f2.txt', 'r')\ncontent = f.read()\nf.close\nprint(content)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:04.288303Z","iopub.execute_input":"2022-12-16T16:40:04.288560Z","iopub.status.idle":"2022-12-16T16:40:04.295950Z","shell.execute_reply.started":"2022-12-16T16:40:04.288528Z","shell.execute_reply":"2022-12-16T16:40:04.295099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install W&B, login into your account and paste the API key \n\n# A note here: You can create and wandb account and login by uncommenting the last line of this \n# cell. This will save the run on your account, and allow you to vizualise the results very\n# easily, and give you access to several valuable options and tools \n!pip install -q --upgrade wandb\n# Login \nimport wandb\n#wandb.login()","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:04.297731Z","iopub.execute_input":"2022-12-16T16:40:04.298011Z","iopub.status.idle":"2022-12-16T16:40:15.053135Z","shell.execute_reply.started":"2022-12-16T16:40:04.297980Z","shell.execute_reply":"2022-12-16T16:40:15.052189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# If you are running the model while being logged in a wandb account, remove the \n# calling \"WANDB_MODE=\"dryrun\" \n%cd /kaggle/working/tmp/yolov5\n!WANDB_MODE=\"dryrun\" python train.py --img {IMG_SIZE} \\\n                 --batch {BATCH_SIZE} \\\n                 --epochs {EPOCHS} \\\n                 --data data.yaml \\\n                 --weights yolov5s.pt \\\n                # --save_period 1\\\n                 --project kaggle-siim-covid","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:40:15.054833Z","iopub.execute_input":"2022-12-16T16:40:15.055094Z","iopub.status.idle":"2022-12-16T16:44:38.369907Z","shell.execute_reply.started":"2022-12-16T16:40:15.055055Z","shell.execute_reply":"2022-12-16T16:44:38.368935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('/kaggle/working/tmp/yolov5/runs/train/exp/confusion_matrix.png'));","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:38.371946Z","iopub.execute_input":"2022-12-16T16:44:38.372249Z","iopub.status.idle":"2022-12-16T16:44:40.401565Z","shell.execute_reply.started":"2022-12-16T16:44:38.372207Z","shell.execute_reply":"2022-12-16T16:44:40.400646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5/runs/train/exp\n%ls","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:40.403034Z","iopub.execute_input":"2022-12-16T16:44:40.403313Z","iopub.status.idle":"2022-12-16T16:44:41.409009Z","shell.execute_reply.started":"2022-12-16T16:44:40.403277Z","shell.execute_reply":"2022-12-16T16:44:41.408118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This shows a batch of the validation data with the corresponding label \nplt.figure(figsize=(15,15))\nplt.imshow(plt.imread('val_batch0_labels.jpg'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:41.411026Z","iopub.execute_input":"2022-12-16T16:44:41.411311Z","iopub.status.idle":"2022-12-16T16:44:42.542258Z","shell.execute_reply.started":"2022-12-16T16:44:41.411271Z","shell.execute_reply":"2022-12-16T16:44:42.541583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(plt.imread('R_curve.png'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:42.543527Z","iopub.execute_input":"2022-12-16T16:44:42.544210Z","iopub.status.idle":"2022-12-16T16:44:43.665100Z","shell.execute_reply.started":"2022-12-16T16:44:42.544175Z","shell.execute_reply":"2022-12-16T16:44:43.664303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(plt.imread('P_curve.png'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:43.666555Z","iopub.execute_input":"2022-12-16T16:44:43.666985Z","iopub.status.idle":"2022-12-16T16:44:44.727963Z","shell.execute_reply.started":"2022-12-16T16:44:43.666947Z","shell.execute_reply":"2022-12-16T16:44:44.727248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This prints all the results curves \nplt.figure(figsize=(20,30))\nplt.imshow(plt.imread('results.png'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:44.729321Z","iopub.execute_input":"2022-12-16T16:44:44.729767Z","iopub.status.idle":"2022-12-16T16:44:45.876730Z","shell.execute_reply.started":"2022-12-16T16:44:44.729730Z","shell.execute_reply":"2022-12-16T16:44:45.876062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The weights are stored here, and could be used for inference \n%cd /kaggle/working/tmp/yolov5/kaggle-siim-covid/exp/weights\n%ls","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:45.880023Z","iopub.execute_input":"2022-12-16T16:44:45.880561Z","iopub.status.idle":"2022-12-16T16:44:47.045864Z","shell.execute_reply.started":"2022-12-16T16:44:45.880517Z","shell.execute_reply":"2022-12-16T16:44:47.044852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights = '/kaggle/working/tmp/yolov5/kaggle-siim-covid/exp/weights/best.pt'","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:47.047783Z","iopub.execute_input":"2022-12-16T16:44:47.048093Z","iopub.status.idle":"2022-12-16T16:44:47.052141Z","shell.execute_reply.started":"2022-12-16T16:44:47.048050Z","shell.execute_reply":"2022-12-16T16:44:47.051371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5/data/images","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:47.053455Z","iopub.execute_input":"2022-12-16T16:44:47.053874Z","iopub.status.idle":"2022-12-16T16:44:47.065190Z","shell.execute_reply.started":"2022-12-16T16:44:47.053835Z","shell.execute_reply":"2022-12-16T16:44:47.064370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('test', exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:47.066352Z","iopub.execute_input":"2022-12-16T16:44:47.068680Z","iopub.status.idle":"2022-12-16T16:44:47.073421Z","shell.execute_reply.started":"2022-12-16T16:44:47.068614Z","shell.execute_reply":"2022-12-16T16:44:47.072558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_test(end_path, df):\n\n    filenames = []\n    for index, row in tqdm(df[['id', 'dcm_path']].iterrows(), total = df.shape[0]):\n        try: \n            array = dicom2array(row['dcm_path'])\n            img = cv2.resize(array, (IMG_SIZE,IMG_SIZE))\n            img = Image.fromarray(img)\n   \n            filename = row['dcm_path'].split('/')[-1].split('.')[0]\n            filenames.append(filename)\n            img.save(os.path.join(end_path, f'{filename}.png'))\n        except RuntimeError:\n            pass\n        #return filename.replace('dcm','') + '_image', array.shape[0], array.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:47.074904Z","iopub.execute_input":"2022-12-16T16:44:47.075280Z","iopub.status.idle":"2022-12-16T16:44:47.084057Z","shell.execute_reply.started":"2022-12-16T16:44:47.075243Z","shell.execute_reply":"2022-12-16T16:44:47.083244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We save the test images in a new folder; not all the images are necessary, you can make this smaller by cutting the dataframe\nsave_test('test', test)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:44:47.085147Z","iopub.execute_input":"2022-12-16T16:44:47.085395Z","iopub.status.idle":"2022-12-16T16:51:55.492245Z","shell.execute_reply.started":"2022-12-16T16:44:47.085355Z","shell.execute_reply":"2022-12-16T16:51:55.491495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:51:55.493766Z","iopub.execute_input":"2022-12-16T16:51:55.494768Z","iopub.status.idle":"2022-12-16T16:51:55.501368Z","shell.execute_reply.started":"2022-12-16T16:51:55.494725Z","shell.execute_reply":"2022-12-16T16:51:55.500522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This makes all the necessary predicitions \n\n!python detect.py --weights /kaggle/working/tmp/yolov5/kaggle-siim-covid/exp/weights/best.pt /kaggle/working/tmp/yolov5/kaggle-siim-covid/exp/weights/last.pt --img 512 --source data/images/test","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:51:55.502984Z","iopub.execute_input":"2022-12-16T16:51:55.504052Z","iopub.status.idle":"2022-12-16T16:52:02.296318Z","shell.execute_reply.started":"2022-12-16T16:51:55.504018Z","shell.execute_reply":"2022-12-16T16:52:02.295195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5/runs/detect/\n%ls\ndirectory = os.listdir('exp')\nplt.figure(figsize=(15,15))\nfor i, file in enumerate((directory)[0:5]):\n    img = plt.imread('exp/' + file)\n    plt.subplot(3, 3, i+1)\n    plt.imshow(img)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-12-16T16:53:00.097254Z","iopub.execute_input":"2022-12-16T16:53:00.098042Z","iopub.status.idle":"2022-12-16T16:53:01.086192Z","shell.execute_reply.started":"2022-12-16T16:53:00.097995Z","shell.execute_reply":"2022-12-16T16:53:01.085392Z"},"trusted":true},"execution_count":null,"outputs":[]}]}