{"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":"2021-10-18T16:10:31.319176Z","iopub.execute_input":"2021-10-18T16:10:31.31982Z","iopub.status.idle":"2021-10-18T16:10:36.93495Z","shell.execute_reply.started":"2021-10-18T16:10:31.319733Z","shell.execute_reply":"2021-10-18T16:10:36.93403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 512\nBATCH_SIZE = 16\nEPOCHS = 40","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:10:36.939713Z","iopub.execute_input":"2021-10-18T16:10:36.942082Z","iopub.status.idle":"2021-10-18T16:10:36.946182Z","shell.execute_reply.started":"2021-10-18T16:10:36.942038Z","shell.execute_reply":"2021-10-18T16:10:36.945217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/siim-covid19-detection')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:10:36.947573Z","iopub.execute_input":"2021-10-18T16:10:36.948169Z","iopub.status.idle":"2021-10-18T16:10:36.966492Z","shell.execute_reply.started":"2021-10-18T16:10:36.948133Z","shell.execute_reply":"2021-10-18T16:10:36.965583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = '/kaggle/input/siim-covid19-detection'","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:10:36.968305Z","iopub.execute_input":"2021-10-18T16:10:36.969043Z","iopub.status.idle":"2021-10-18T16:10:36.973906Z","shell.execute_reply.started":"2021-10-18T16:10:36.969002Z","shell.execute_reply":"2021-10-18T16:10:36.972987Z"},"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":"2021-10-18T16:10:36.975629Z","iopub.execute_input":"2021-10-18T16:10:36.976604Z","iopub.status.idle":"2021-10-18T16:10:37.027741Z","shell.execute_reply.started":"2021-10-18T16:10:36.976565Z","shell.execute_reply":"2021-10-18T16:10:37.026944Z"},"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":"2021-10-18T16:10:37.031505Z","iopub.execute_input":"2021-10-18T16:10:37.033501Z","iopub.status.idle":"2021-10-18T16:10:37.105934Z","shell.execute_reply.started":"2021-10-18T16:10:37.033462Z","shell.execute_reply":"2021-10-18T16:10:37.105266Z"},"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":"2021-10-18T16:10:37.109667Z","iopub.execute_input":"2021-10-18T16:10:37.111594Z","iopub.status.idle":"2021-10-18T16:10:37.123143Z","shell.execute_reply.started":"2021-10-18T16:10:37.111555Z","shell.execute_reply":"2021-10-18T16:10:37.12234Z"},"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":"2021-10-18T16:10:37.127614Z","iopub.execute_input":"2021-10-18T16:10:37.127995Z","iopub.status.idle":"2021-10-18T16:10:37.141574Z","shell.execute_reply.started":"2021-10-18T16:10:37.127961Z","shell.execute_reply":"2021-10-18T16:10:37.140674Z"},"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":"2021-10-18T16:10:37.145175Z","iopub.execute_input":"2021-10-18T16:10:37.145627Z","iopub.status.idle":"2021-10-18T16:10:37.180563Z","shell.execute_reply.started":"2021-10-18T16:10:37.14559Z","shell.execute_reply":"2021-10-18T16:10:37.17996Z"},"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":"2021-10-18T16:10:37.185989Z","iopub.execute_input":"2021-10-18T16:10:37.187935Z","iopub.status.idle":"2021-10-18T16:10:37.47037Z","shell.execute_reply.started":"2021-10-18T16:10:37.187894Z","shell.execute_reply":"2021-10-18T16:10:37.469606Z"},"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":"2021-10-18T16:10:37.471747Z","iopub.execute_input":"2021-10-18T16:10:37.472007Z","iopub.status.idle":"2021-10-18T16:10:37.655928Z","shell.execute_reply.started":"2021-10-18T16:10:37.471972Z","shell.execute_reply":"2021-10-18T16:10:37.655137Z"},"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":"2021-10-18T16:10:37.659628Z","iopub.execute_input":"2021-10-18T16:10:37.660213Z","iopub.status.idle":"2021-10-18T16:10:37.680413Z","shell.execute_reply.started":"2021-10-18T16:10:37.660175Z","shell.execute_reply":"2021-10-18T16:10:37.679499Z"},"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":"2021-10-18T16:10:37.682155Z","iopub.execute_input":"2021-10-18T16:10:37.682521Z","iopub.status.idle":"2021-10-18T16:10:37.715969Z","shell.execute_reply.started":"2021-10-18T16:10:37.682479Z","shell.execute_reply":"2021-10-18T16:10:37.715228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.sort_values('study_id')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:10:37.71712Z","iopub.execute_input":"2021-10-18T16:10:37.717364Z","iopub.status.idle":"2021-10-18T16:10:37.743915Z","shell.execute_reply.started":"2021-10-18T16:10:37.71734Z","shell.execute_reply":"2021-10-18T16:10:37.743188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.rename(columns={\"id_x\":\"id\"})","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:10:37.745348Z","iopub.execute_input":"2021-10-18T16:10:37.745644Z","iopub.status.idle":"2021-10-18T16:10:37.750627Z","shell.execute_reply.started":"2021-10-18T16:10:37.745605Z","shell.execute_reply":"2021-10-18T16:10:37.749939Z"},"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":"2021-10-18T16:10:37.752044Z","iopub.execute_input":"2021-10-18T16:10:37.752944Z","iopub.status.idle":"2021-10-18T16:11:27.935569Z","shell.execute_reply.started":"2021-10-18T16:10:37.752828Z","shell.execute_reply":"2021-10-18T16:11:27.934786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(dataset + '/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:27.939014Z","iopub.execute_input":"2021-10-18T16:11:27.939244Z","iopub.status.idle":"2021-10-18T16:11:27.950427Z","shell.execute_reply.started":"2021-10-18T16:11:27.939206Z","shell.execute_reply":"2021-10-18T16:11:27.94951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:27.953645Z","iopub.execute_input":"2021-10-18T16:11:27.953845Z","iopub.status.idle":"2021-10-18T16:11:27.965337Z","shell.execute_reply.started":"2021-10-18T16:11:27.953822Z","shell.execute_reply":"2021-10-18T16:11:27.964618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = glob(f'{dataset}/test/*/*/*.dcm')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:27.966747Z","iopub.execute_input":"2021-10-18T16:11:27.967171Z","iopub.status.idle":"2021-10-18T16:11:36.96141Z","shell.execute_reply.started":"2021-10-18T16:11:27.967135Z","shell.execute_reply":"2021-10-18T16:11:36.960611Z"},"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":"2021-10-18T16:11:36.962624Z","iopub.execute_input":"2021-10-18T16:11:36.962915Z","iopub.status.idle":"2021-10-18T16:11:36.983574Z","shell.execute_reply.started":"2021-10-18T16:11:36.962877Z","shell.execute_reply":"2021-10-18T16:11:36.981412Z"},"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":"2021-10-18T16:11:36.987178Z","iopub.execute_input":"2021-10-18T16:11:36.98741Z","iopub.status.idle":"2021-10-18T16:11:37.01155Z","shell.execute_reply.started":"2021-10-18T16:11:36.987386Z","shell.execute_reply":"2021-10-18T16:11:37.010709Z"},"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":"2021-10-18T16:11:37.012747Z","iopub.execute_input":"2021-10-18T16:11:37.013032Z","iopub.status.idle":"2021-10-18T16:11:37.045558Z","shell.execute_reply.started":"2021-10-18T16:11:37.012994Z","shell.execute_reply":"2021-10-18T16:11:37.044745Z"},"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":"2021-10-18T16:11:37.04695Z","iopub.execute_input":"2021-10-18T16:11:37.047218Z","iopub.status.idle":"2021-10-18T16:11:37.069048Z","shell.execute_reply.started":"2021-10-18T16:11:37.047183Z","shell.execute_reply":"2021-10-18T16:11:37.068364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.dropna()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:37.070305Z","iopub.execute_input":"2021-10-18T16:11:37.070572Z","iopub.status.idle":"2021-10-18T16:11:37.090406Z","shell.execute_reply.started":"2021-10-18T16:11:37.070537Z","shell.execute_reply":"2021-10-18T16:11:37.089799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:37.091648Z","iopub.execute_input":"2021-10-18T16:11:37.091915Z","iopub.status.idle":"2021-10-18T16:11:37.104648Z","shell.execute_reply.started":"2021-10-18T16:11:37.091881Z","shell.execute_reply":"2021-10-18T16:11:37.103857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dev = train[:200]\ntrain_dev","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:37.106283Z","iopub.execute_input":"2021-10-18T16:11:37.106529Z","iopub.status.idle":"2021-10-18T16:11:37.128099Z","shell.execute_reply.started":"2021-10-18T16:11:37.106497Z","shell.execute_reply":"2021-10-18T16:11:37.127477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_dev = train[-100:]\nvalid_dev","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:37.130044Z","iopub.execute_input":"2021-10-18T16:11:37.130459Z","iopub.status.idle":"2021-10-18T16:11:37.148427Z","shell.execute_reply.started":"2021-10-18T16:11:37.130425Z","shell.execute_reply":"2021-10-18T16:11:37.147523Z"},"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":"2021-10-18T16:11:37.154399Z","iopub.execute_input":"2021-10-18T16:11:37.154591Z","iopub.status.idle":"2021-10-18T16:11:37.165388Z","shell.execute_reply.started":"2021-10-18T16:11:37.154568Z","shell.execute_reply":"2021-10-18T16:11:37.164684Z"},"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":"2021-10-18T16:11:37.166327Z","iopub.execute_input":"2021-10-18T16:11:37.166675Z","iopub.status.idle":"2021-10-18T16:11:38.266696Z","shell.execute_reply.started":"2021-10-18T16:11:37.16664Z","shell.execute_reply":"2021-10-18T16:11:38.26596Z"},"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":"2021-10-18T16:11:38.267622Z","iopub.execute_input":"2021-10-18T16:11:38.267876Z","iopub.status.idle":"2021-10-18T16:11:39.956002Z","shell.execute_reply.started":"2021-10-18T16:11:38.26784Z","shell.execute_reply":"2021-10-18T16:11:39.955391Z"},"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":"2021-10-18T16:11:39.957264Z","iopub.execute_input":"2021-10-18T16:11:39.957618Z","iopub.status.idle":"2021-10-18T16:11:39.975006Z","shell.execute_reply.started":"2021-10-18T16:11:39.957586Z","shell.execute_reply":"2021-10-18T16:11:39.974308Z"},"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":"2021-10-18T16:11:39.978059Z","iopub.execute_input":"2021-10-18T16:11:39.978297Z","iopub.status.idle":"2021-10-18T16:11:42.055187Z","shell.execute_reply.started":"2021-10-18T16:11:39.978272Z","shell.execute_reply":"2021-10-18T16:11:42.054583Z"},"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":"2021-10-18T16:11:42.056159Z","iopub.execute_input":"2021-10-18T16:11:42.056523Z","iopub.status.idle":"2021-10-18T16:11:42.060791Z","shell.execute_reply.started":"2021-10-18T16:11:42.056487Z","shell.execute_reply":"2021-10-18T16:11:42.060241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:42.061843Z","iopub.execute_input":"2021-10-18T16:11:42.062426Z","iopub.status.idle":"2021-10-18T16:11:42.074909Z","shell.execute_reply.started":"2021-10-18T16:11:42.06239Z","shell.execute_reply":"2021-10-18T16:11:42.074207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:42.07635Z","iopub.execute_input":"2021-10-18T16:11:42.077038Z","iopub.status.idle":"2021-10-18T16:11:44.79402Z","shell.execute_reply.started":"2021-10-18T16:11:42.077002Z","shell.execute_reply":"2021-10-18T16:11:44.793241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolov5\n!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:44.797299Z","iopub.execute_input":"2021-10-18T16:11:44.797528Z","iopub.status.idle":"2021-10-18T16:11:53.158537Z","shell.execute_reply.started":"2021-10-18T16:11:44.797498Z","shell.execute_reply":"2021-10-18T16:11:53.157656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%ls","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:53.162319Z","iopub.execute_input":"2021-10-18T16:11:53.162554Z","iopub.status.idle":"2021-10-18T16:11:53.849411Z","shell.execute_reply.started":"2021-10-18T16:11:53.162525Z","shell.execute_reply":"2021-10-18T16:11:53.848371Z"},"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":"2021-10-18T16:11:53.8509Z","iopub.execute_input":"2021-10-18T16:11:53.851215Z","iopub.status.idle":"2021-10-18T16:11:53.863314Z","shell.execute_reply.started":"2021-10-18T16:11:53.851172Z","shell.execute_reply":"2021-10-18T16:11:53.862368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd data","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:11:53.865891Z","iopub.execute_input":"2021-10-18T16:11:53.867309Z","iopub.status.idle":"2021-10-18T16:11:53.877127Z","shell.execute_reply.started":"2021-10-18T16:11:53.867266Z","shell.execute_reply":"2021-10-18T16:11:53.87615Z"},"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":"2021-10-18T16:11:53.879751Z","iopub.execute_input":"2021-10-18T16:11:53.880733Z","iopub.status.idle":"2021-10-18T16:11:54.705814Z","shell.execute_reply.started":"2021-10-18T16:11:53.880693Z","shell.execute_reply":"2021-10-18T16:11:54.705015Z"},"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":"2021-10-18T16:11:54.707354Z","iopub.execute_input":"2021-10-18T16:11:54.707641Z","iopub.status.idle":"2021-10-18T16:11:54.71909Z","shell.execute_reply.started":"2021-10-18T16:11:54.7076Z","shell.execute_reply":"2021-10-18T16:11:54.718108Z"},"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":"2021-10-18T16:11:54.723121Z","iopub.execute_input":"2021-10-18T16:11:54.723421Z","iopub.status.idle":"2021-10-18T16:13:14.797673Z","shell.execute_reply.started":"2021-10-18T16:11:54.723385Z","shell.execute_reply":"2021-10-18T16:13:14.796837Z"},"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":"2021-10-18T16:13:14.799302Z","iopub.execute_input":"2021-10-18T16:13:14.799643Z","iopub.status.idle":"2021-10-18T16:13:14.831135Z","shell.execute_reply.started":"2021-10-18T16:13:14.799605Z","shell.execute_reply":"2021-10-18T16:13:14.830114Z"},"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":"2021-10-18T16:13:14.832375Z","iopub.execute_input":"2021-10-18T16:13:14.832834Z","iopub.status.idle":"2021-10-18T16:13:14.847723Z","shell.execute_reply.started":"2021-10-18T16:13:14.832795Z","shell.execute_reply":"2021-10-18T16:13:14.847029Z"},"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":"2021-10-18T16:13:14.849133Z","iopub.execute_input":"2021-10-18T16:13:14.849515Z","iopub.status.idle":"2021-10-18T16:13:14.864934Z","shell.execute_reply.started":"2021-10-18T16:13:14.84948Z","shell.execute_reply":"2021-10-18T16:13:14.863838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dev","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:13:14.866276Z","iopub.execute_input":"2021-10-18T16:13:14.866537Z","iopub.status.idle":"2021-10-18T16:13:14.894811Z","shell.execute_reply.started":"2021-10-18T16:13:14.866504Z","shell.execute_reply":"2021-10-18T16:13:14.893845Z"},"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":"2021-10-18T16:13:14.896109Z","iopub.execute_input":"2021-10-18T16:13:14.896382Z","iopub.status.idle":"2021-10-18T16:13:14.908776Z","shell.execute_reply.started":"2021-10-18T16:13:14.89635Z","shell.execute_reply":"2021-10-18T16:13:14.907831Z"},"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":"2021-10-18T16:13:14.910395Z","iopub.execute_input":"2021-10-18T16:13:14.910779Z","iopub.status.idle":"2021-10-18T16:13:14.933461Z","shell.execute_reply.started":"2021-10-18T16:13:14.910739Z","shell.execute_reply":"2021-10-18T16:13:14.932728Z"},"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":"2021-10-18T16:13:14.934768Z","iopub.execute_input":"2021-10-18T16:13:14.93503Z","iopub.status.idle":"2021-10-18T16:13:15.071268Z","shell.execute_reply.started":"2021-10-18T16:13:14.934996Z","shell.execute_reply":"2021-10-18T16:13:15.07049Z"},"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":"2021-10-18T16:13:15.07262Z","iopub.execute_input":"2021-10-18T16:13:15.07289Z","iopub.status.idle":"2021-10-18T16:13:15.163821Z","shell.execute_reply.started":"2021-10-18T16:13:15.072855Z","shell.execute_reply":"2021-10-18T16:13:15.163144Z"},"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":"2021-10-18T16:13:15.164956Z","iopub.execute_input":"2021-10-18T16:13:15.165284Z","iopub.status.idle":"2021-10-18T16:13:15.855202Z","shell.execute_reply.started":"2021-10-18T16:13:15.165248Z","shell.execute_reply":"2021-10-18T16:13:15.854091Z"},"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":"2021-10-18T16:13:15.858609Z","iopub.execute_input":"2021-10-18T16:13:15.859179Z","iopub.status.idle":"2021-10-18T16:13:15.868929Z","shell.execute_reply.started":"2021-10-18T16:13:15.859135Z","shell.execute_reply":"2021-10-18T16:13:15.868092Z"},"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":"2021-10-18T16:13:15.870405Z","iopub.execute_input":"2021-10-18T16:13:15.870646Z","iopub.status.idle":"2021-10-18T16:13:26.834012Z","shell.execute_reply.started":"2021-10-18T16:13:15.870595Z","shell.execute_reply":"2021-10-18T16:13:26.83314Z"},"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":"2021-10-18T16:13:26.837497Z","iopub.execute_input":"2021-10-18T16:13:26.837721Z","iopub.status.idle":"2021-10-18T16:18:34.28136Z","shell.execute_reply.started":"2021-10-18T16:13:26.837691Z","shell.execute_reply":"2021-10-18T16:18:34.280254Z"},"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":"2021-10-18T16:18:34.284841Z","iopub.execute_input":"2021-10-18T16:18:34.285102Z","iopub.status.idle":"2021-10-18T16:18:36.103994Z","shell.execute_reply.started":"2021-10-18T16:18:34.285069Z","shell.execute_reply":"2021-10-18T16:18:36.103268Z"},"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":"2021-10-18T16:18:36.105176Z","iopub.execute_input":"2021-10-18T16:18:36.105903Z","iopub.status.idle":"2021-10-18T16:18:36.792777Z","shell.execute_reply.started":"2021-10-18T16:18:36.105865Z","shell.execute_reply":"2021-10-18T16:18:36.791923Z"},"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":"2021-10-18T16:18:36.796147Z","iopub.execute_input":"2021-10-18T16:18:36.796387Z","iopub.status.idle":"2021-10-18T16:18:37.794172Z","shell.execute_reply.started":"2021-10-18T16:18:36.796356Z","shell.execute_reply":"2021-10-18T16:18:37.793181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(plt.imread('R_curve.png'))","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:18:37.795617Z","iopub.execute_input":"2021-10-18T16:18:37.795865Z","iopub.status.idle":"2021-10-18T16:18:38.776805Z","shell.execute_reply.started":"2021-10-18T16:18:37.795833Z","shell.execute_reply":"2021-10-18T16:18:38.775184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(plt.imread('P_curve.png'))","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:18:38.777999Z","iopub.execute_input":"2021-10-18T16:18:38.778343Z","iopub.status.idle":"2021-10-18T16:18:39.781153Z","shell.execute_reply.started":"2021-10-18T16:18:38.778305Z","shell.execute_reply":"2021-10-18T16:18:39.780263Z"},"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":"2021-10-18T16:18:39.782413Z","iopub.execute_input":"2021-10-18T16:18:39.782799Z","iopub.status.idle":"2021-10-18T16:18:40.803332Z","shell.execute_reply.started":"2021-10-18T16:18:39.782757Z","shell.execute_reply":"2021-10-18T16:18:40.802581Z"},"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":"2021-10-18T16:18:40.804717Z","iopub.execute_input":"2021-10-18T16:18:40.805126Z","iopub.status.idle":"2021-10-18T16:18:41.639388Z","shell.execute_reply.started":"2021-10-18T16:18:40.805091Z","shell.execute_reply":"2021-10-18T16:18:41.638435Z"},"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":"2021-10-18T16:18:41.641073Z","iopub.execute_input":"2021-10-18T16:18:41.641397Z","iopub.status.idle":"2021-10-18T16:18:41.647422Z","shell.execute_reply.started":"2021-10-18T16:18:41.641352Z","shell.execute_reply":"2021-10-18T16:18:41.646298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5/data/images","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:18:41.649013Z","iopub.execute_input":"2021-10-18T16:18:41.649361Z","iopub.status.idle":"2021-10-18T16:18:41.6586Z","shell.execute_reply.started":"2021-10-18T16:18:41.649326Z","shell.execute_reply":"2021-10-18T16:18:41.657565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('test', exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T16:18:41.660475Z","iopub.execute_input":"2021-10-18T16:18:41.6608Z","iopub.status.idle":"2021-10-18T16:18:41.668355Z","shell.execute_reply.started":"2021-10-18T16:18:41.660763Z","shell.execute_reply":"2021-10-18T16:18:41.667421Z"},"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":"2021-10-18T16:18:41.66926Z","iopub.execute_input":"2021-10-18T16:18:41.669452Z","iopub.status.idle":"2021-10-18T16:18:41.681437Z","shell.execute_reply.started":"2021-10-18T16:18:41.669431Z","shell.execute_reply":"2021-10-18T16:18:41.68048Z"},"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":"2021-10-18T16:18:41.683219Z","iopub.execute_input":"2021-10-18T16:18:41.683742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/tmp/yolov5","metadata":{"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":{"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(2, 3, i+1)\n    plt.imshow(img)\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}