{"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":"import pandas as pd\nimport numpy as np\nimport pydicom\nimport cv2\nimport matplotlib.pyplot as plt\nimport torch\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-01T13:04:12.925523Z","iopub.execute_input":"2023-10-01T13:04:12.925848Z","iopub.status.idle":"2023-10-01T13:04:15.849005Z","shell.execute_reply.started":"2023-10-01T13:04:12.925825Z","shell.execute_reply":"2023-10-01T13:04:15.847972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\nimg_dir = '/kaggle/input/rsna-breast-cancer-detection/train_images'\npng_img_dir = '/kaggle/input/rsna-breast-cancer-768-pngs/output'\nyolo_model = '/kaggle/input/rsna-breast-cancer-detection-roi-model/rsna-roi-003.pt'","metadata":{"execution":{"iopub.status.busy":"2023-10-01T13:01:45.194136Z","iopub.execute_input":"2023-10-01T13:01:45.194693Z","iopub.status.idle":"2023-10-01T13:01:45.32085Z","shell.execute_reply.started":"2023-10-01T13:01:45.194662Z","shell.execute_reply":"2023-10-01T13:01:45.320138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_imgs(folder_path,patient_id,img_id,is_dcm):\n    #reading dcm files\n    if is_dcm:\n        img_path = os.path.join(folder_path,patient_id,f'{img_id}.dcm')\n        dcm_img = pydicom.dcmread(img_path)\n        img_arr = dcm_img.pixel_array\n        img_arr = (img_arr - img_arr.min())/(img_arr.max() - img_arr.min())\n        \n        if dcm_img.PhotometricInterpretation == 'MONOCHROME1':\n            img_arr = 1 - img_arr\n        \n        img = (img_arr*255).astype(np.uint8)\n    \n    else:\n        img_path = os.path.join(folder_path,patient_id,f'{img_id}.png')\n        img = cv2.imread(img_path)\n    \n    return img","metadata":{"execution":{"iopub.status.busy":"2023-10-01T13:01:54.620006Z","iopub.execute_input":"2023-10-01T13:01:54.620456Z","iopub.status.idle":"2023-10-01T13:01:54.627524Z","shell.execute_reply.started":"2023-10-01T13:01:54.620422Z","shell.execute_reply":"2023-10-01T13:01:54.626418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cloning yolov5 for ROI extraction\nos.makedirs('../kaggle/yolov5', exist_ok = True)\n!git clone https://github.com/ultralytics/yolov5.git /kaggle/yolov5","metadata":{"execution":{"iopub.status.busy":"2023-10-01T13:01:56.215532Z","iopub.execute_input":"2023-10-01T13:01:56.215941Z","iopub.status.idle":"2023-10-01T13:01:58.883228Z","shell.execute_reply.started":"2023-10-01T13:01:56.215886Z","shell.execute_reply":"2023-10-01T13:01:58.881813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yolo_model = torch.hub.load('/kaggle/yolov5', 'custom', path='/kaggle/input/rsna-breast-cancer-detection-roi-model/rsna-roi-003.pt', source='local')","metadata":{"execution":{"iopub.status.busy":"2023-10-01T13:05:24.384035Z","iopub.execute_input":"2023-10-01T13:05:24.384415Z","iopub.status.idle":"2023-10-01T13:05:37.505946Z","shell.execute_reply.started":"2023-10-01T13:05:24.384391Z","shell.execute_reply":"2023-10-01T13:05:37.505235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class transform_img():\n    def __init__(self):\n        super().__init__()\n    \n    def __call__(self,img):\n        return img\n    \n    def invert_img(self,coords):\n        return coords\n\nclass horizontal_flip(transform_img):\n    def __init__(self):\n        super().__init__()\n    \n    def __call__(self,img):\n        img = cv2.flip(img,1)\n        return img\n    \n    def invert_img(self,coords):\n        # flipping horizontally results in change in x coordinates\n        coords['xmin'] = 767 - coords['xmin']\n        coords['xmax'] = 767 - coords['xmax']\n        return coords\n\nclass vertical_flip(transform_img):\n    def __init__(self):\n        super().__init__()\n    \n    def __call__(self,img):\n        img = cv2.flip(img,0)\n        return img\n    \n    def invert_img(self,coords):\n        #flipping vertically results in change in y coordinates\n        coords['ymin'] = 767 - coords['ymin']\n        coords['ymax'] = 767 - coords['ymax']\n        return coords\n\nclass change_contrast(transform_img):\n    def __init__(self,clipLimit=2.0, tileGridSize=(8,8)):\n        super().__init__()\n        self.clipLimit = clipLimit\n        self.tileGridSize = tileGridSize\n    \n    def __call__(self, img):\n        lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)\n        l_channel, a, b = cv2.split(lab)\n        clahe = cv2.createCLAHE(clipLimit=self.clipLimit, tileGridSize=self.tileGridSize)\n        cl = clahe.apply(l_channel)\n        limg = cv2.merge((cl,a,b))\n        img = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)\n        return img\n    \n    def inverse(self, coords):\n        return coords\n\nclass Translation(transform_img):\n    def __init__(self, x = 30,y=30):\n        super().__init__()\n        \n        self.name = f'Translation_{x}_{y}'\n        \n        self.x = x\n        self.y = y\n        self.M = np.float32([[1,0,x],[0,1,y]])\n    \n    def __call__(self, img):\n        rows,cols,_ = img.shape\n        \n        img = cv2.warpAffine(img,self.M,(cols,rows))\n        \n        return img\n    \n    def inverse(self, coords):\n        coords['xmin'] = coords['xmax']+self.x\n        coords['xmax'] = coords['xmin']+self.x\n        coords['ymin'] = coords['ymax']+self.y\n        coords['ymax'] = coords['ymin']+self.y\n        \n        coords = {key:np.clip(coords[key],0,767) for key in ['xmin','xmax','ymin','ymax']}\n        return coords\n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-01T14:01:29.632563Z","iopub.execute_input":"2023-10-01T14:01:29.633107Z","iopub.status.idle":"2023-10-01T14:01:29.654251Z","shell.execute_reply.started":"2023-10-01T14:01:29.633064Z","shell.execute_reply":"2023-10-01T14:01:29.653117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-09-30T12:33:29.82901Z","iopub.execute_input":"2023-09-30T12:33:29.829788Z","iopub.status.idle":"2023-09-30T12:33:29.839327Z","shell.execute_reply.started":"2023-09-30T12:33:29.829744Z","shell.execute_reply":"2023-09-30T12:33:29.838277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-30T12:33:30.507928Z","iopub.execute_input":"2023-09-30T12:33:30.508949Z","iopub.status.idle":"2023-09-30T12:33:30.533067Z","shell.execute_reply.started":"2023-09-30T12:33:30.508909Z","shell.execute_reply":"2023-09-30T12:33:30.532402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#looking for NAN values\nprint(f'No of rows in dataframes:  {df_train.shape[0]}')\nnan_count = df_train.isna().sum()\nprint(nan_count)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-30T12:33:31.170553Z","iopub.execute_input":"2023-09-30T12:33:31.171642Z","iopub.status.idle":"2023-09-30T12:33:31.188984Z","shell.execute_reply.started":"2023-09-30T12:33:31.1716Z","shell.execute_reply":"2023-09-30T12:33:31.187894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping some features and  null values\ndf_train = df_train.drop(columns=['site_id','biopsy','BIRADS','density','machine_id','density','difficult_negative_case'])\ndf_train = df_train.dropna(subset=['age'])\ndf_train = df_train[(df_train.view == \"MLO\") | (df_train.view == \"CC\")]\nprint(f'No of rows in dataframes:  {df_train.shape[0]}')\nnan_count = df_train.isna().sum()\nprint(nan_count)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T12:33:31.838749Z","iopub.execute_input":"2023-09-30T12:33:31.839427Z","iopub.status.idle":"2023-09-30T12:33:31.877977Z","shell.execute_reply.started":"2023-09-30T12:33:31.839394Z","shell.execute_reply":"2023-09-30T12:33:31.877053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#converting columns into binary values\ndf_train.loc[df_train[\"laterality\"]==\"L\", \"laterality\"] = 0\ndf_train.loc[df_train[\"laterality\"]==\"R\", \"laterality\"] = 1\ndf_train.loc[df_train[\"view\"]==\"MLO\", \"view\"] = 0\ndf_train.loc[df_train[\"view\"]==\"CC\", \"view\"] = 1\n\n# add image paths to df\ndf_train[\"path\"] = img_dir + '/'+ df_train['patient_id'].astype(str)+'/' + df_train['image_id'].astype(str) +\".dcm\"\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-30T12:33:32.485473Z","iopub.execute_input":"2023-09-30T12:33:32.485814Z","iopub.status.idle":"2023-09-30T12:33:32.60351Z","shell.execute_reply.started":"2023-09-30T12:33:32.485788Z","shell.execute_reply":"2023-09-30T12:33:32.602584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.iloc[0]['path'])","metadata":{"execution":{"iopub.status.busy":"2023-09-30T12:33:33.13103Z","iopub.execute_input":"2023-09-30T12:33:33.131352Z","iopub.status.idle":"2023-09-30T12:33:33.136988Z","shell.execute_reply.started":"2023-09-30T12:33:33.131326Z","shell.execute_reply":"2023-09-30T12:33:33.13619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot(image,title):\n    plt.imshow(image)\n    plt.title(title)\n    plt.show()\n\ndef read_dcm(path):\n    dicom = pydicom.dcmread(path)\n    img = dicom.pixel_array\n    plot(img,'image')\n    return img\n\ndef preprocess_img(img):\n    ori_h, ori_w = img.shape[:2]\n    print(img.shape)\n    upper = np.percentile(img, 95)\n    img[img > upper] = np.min(img)\n    img = cv2.GaussianBlur(img,(5,5), 0)\n    \n    #get the thresholded image\n    _,img_bin = cv2.threshold(img, 0, 255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n    plot(img_bin,'OTSU_segementation')\n    \n    #find contours\n    img_bin = img_bin.astype(np.uint8)\n    element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3), (-1, -1))\n    img_bin = cv2.dilate(img_bin, element)\n    contours, _ = cv2.findContours(img_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    img2=img.copy()\n    cv2.drawContours(img2, contours, -1, (255, 255, 255), 20)\n    plot(img2,'Contours')\n    \n    areas = [cv2.contourArea(countour) for countour in contours]\n    select_idx = np.argmax(areas)\n    areas.pop(select_idx)\n    \n    contours = [contour for i, contour in enumerate(contours) if i != select_idx]\n    \n    select_idx = np.argmax(areas)\n    cnt = contours[select_idx]\n    x0, y0, w, h = cv2.boundingRect(cnt)\n    x1 = min(max(int(x0 + w), 0), ori_w)\n    y1 = min(max(int(y0 + h), 0), ori_h)\n    x0 = min(max(int(x0), 0), ori_w)\n    y0 = min(max(int(y0), 0), ori_h)\n    print(x1,y1,x0,y0)\n    crop = img[y0:y1, x0:x1]\n    plot(crop,'cropped_image')\n\n    return crop\n\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-09-30T13:53:26.970655Z","iopub.execute_input":"2023-09-30T13:53:26.971421Z","iopub.status.idle":"2023-09-30T13:53:26.982756Z","shell.execute_reply.started":"2023-09-30T13:53:26.971388Z","shell.execute_reply":"2023-09-30T13:53:26.98158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking the image shape\nfor i in range(0,100):\n    img_path = df_train.iloc[i]['path']\n    img = read_dcm(img_path)\n    img = preprocess_img(img)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-30T13:53:27.501438Z","iopub.execute_input":"2023-09-30T13:53:27.502175Z","iopub.status.idle":"2023-09-30T13:54:35.306837Z","shell.execute_reply.started":"2023-09-30T13:53:27.502145Z","shell.execute_reply":"2023-09-30T13:54:35.305253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-13T16:51:41.128626Z","iopub.status.idle":"2023-09-13T16:51:41.129009Z","shell.execute_reply.started":"2023-09-13T16:51:41.128832Z","shell.execute_reply":"2023-09-13T16:51:41.12885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = df_train.iloc[2]['path']\nimg,img_med,roi = get_imgs(img_path)\nprint(img.shape)\nplot(img,'Original_image')","metadata":{"execution":{"iopub.status.busy":"2023-09-13T16:51:41.132958Z","iopub.status.idle":"2023-09-13T16:51:41.133632Z","shell.execute_reply.started":"2023-09-13T16:51:41.133419Z","shell.execute_reply":"2023-09-13T16:51:41.133443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = df_train.iloc[3]['path']\nimg,img_med,roi = get_imgs(img_path)\nprint(img.shape)\nplot(img,'Original_image')","metadata":{"execution":{"iopub.status.busy":"2023-09-13T16:51:41.134889Z","iopub.status.idle":"2023-09-13T16:51:41.135323Z","shell.execute_reply.started":"2023-09-13T16:51:41.135102Z","shell.execute_reply":"2023-09-13T16:51:41.135122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_invasive = df_train[df_train.invasive == 1]\ndf_cancer = df_train[(df_train.cancer==1) & (df_train.invasive==0)]\ndf_no_cancer = df_train[df_train.cancer==0]","metadata":{"execution":{"iopub.status.busy":"2023-09-13T16:51:41.136479Z","iopub.status.idle":"2023-09-13T16:51:41.136856Z","shell.execute_reply.started":"2023-09-13T16:51:41.136674Z","shell.execute_reply":"2023-09-13T16:51:41.136692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io","metadata":{"execution":{"iopub.status.busy":"2023-09-13T16:51:41.137947Z","iopub.status.idle":"2023-09-13T16:51:41.138349Z","shell.execute_reply.started":"2023-09-13T16:51:41.138133Z","shell.execute_reply":"2023-09-13T16:51:41.13815Z"},"trusted":true},"execution_count":null,"outputs":[]}]}