{"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":"! pip install pylibjpeg pylibjpeg-libjpeg pydicom\n! pip install -U python-gdcm","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:35:53.517336Z","iopub.execute_input":"2022-11-28T10:35:53.517736Z","iopub.status.idle":"2022-11-28T10:36:20.478078Z","shell.execute_reply.started":"2022-11-28T10:35:53.517658Z","shell.execute_reply":"2022-11-28T10:36:20.477016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>1 Libraries</b></p>\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport seaborn as sns\nimport cv2\nimport os\nfrom os import listdir\nimport re\nimport gc\nimport gdcm\nimport pydicom\nfrom pydicom import dcmread\nimport pylibjpeg\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport scipy.ndimage\nfrom tqdm import tqdm\nfrom pprint import pprint\nfrom time import time\nimport itertools\nfrom skimage import measure \nfrom mpl_toolkits.mplot3d.art3d import Poly3DCollection\nimport nibabel as nib\nfrom glob import glob\nimport warnings\nimport dask.array as da\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import losses, callbacks\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom PIL import Image as im\n%matplotlib inline\nsns.set(style='darkgrid', font_scale=1.6)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:20.482165Z","iopub.execute_input":"2022-11-28T10:36:20.48263Z","iopub.status.idle":"2022-11-28T10:36:27.179718Z","shell.execute_reply.started":"2022-11-28T10:36:20.482589Z","shell.execute_reply":"2022-11-28T10:36:27.178756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>2 Activating Devices</b></p>\n</div>","metadata":{"execution":{"iopub.status.busy":"2022-08-28T04:51:55.813965Z","iopub.execute_input":"2022-08-28T04:51:55.814311Z","iopub.status.idle":"2022-08-28T04:51:55.821418Z","shell.execute_reply.started":"2022-08-28T04:51:55.814281Z","shell.execute_reply":"2022-08-28T04:51:55.819608Z"}}},{"cell_type":"code","source":"DEVICE = \"GPU\"\nif DEVICE == \"TPU\":\n    print(\"connecting to TPU...\")\n    try:\n        tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n        print('Running on TPU ', tpu.master())\n    except ValueError:\n        print(\"Could not connect to TPU\")\n        tpu = None\n\n    if tpu:\n        try:\n            print(\"initializing  TPU ...\")\n            tf.config.experimental_connect_to_cluster(tpu)\n            tf.tpu.experimental.initialize_tpu_system(tpu)\n            strategy = tf.distribute.experimental.TPUStrategy(tpu)\n            print(\"TPU initialized\")\n        except _:\n            print(\"failed to initialize TPU\")\n    else:\n        DEVICE = \"GPU\"\n\nif DEVICE != \"TPU\":\n    print(\"Using default strategy for CPU and single GPU\")\n    strategy = tf.distribute.get_strategy()\n\nif DEVICE == \"GPU\":\n    print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n    \n\nAUTO     = tf.data.experimental.AUTOTUNE\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:27.181036Z","iopub.execute_input":"2022-11-28T10:36:27.181833Z","iopub.status.idle":"2022-11-28T10:36:27.258272Z","shell.execute_reply.started":"2022-11-28T10:36:27.181793Z","shell.execute_reply":"2022-11-28T10:36:27.257293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>3 Data</b></p>\n</div>\n","metadata":{}},{"cell_type":"code","source":"#For Segmentation data\ndef load_NIfTI(path):\n    mask = nib.load(path)\n    \n    # Convert to numpy array\n    seg = mask.get_fdata()\n    \n    # Align orientation with images\n    seg = seg[:, ::-1, ::-1].transpose(2, 1, 0)\n    \n    return seg","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:27.259764Z","iopub.execute_input":"2022-11-28T10:36:27.260396Z","iopub.status.idle":"2022-11-28T10:36:27.266412Z","shell.execute_reply.started":"2022-11-28T10:36:27.260354Z","shell.execute_reply":"2022-11-28T10:36:27.265463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting patient with mask\nseg_paths = glob(f\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/*\")\ntraining_patient=[]\nfor path in seg_paths:\n    training_patient.append((path.rsplit(\"/\",1)[-1])[:-4])#Patient with mask present","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:27.271247Z","iopub.execute_input":"2022-11-28T10:36:27.271658Z","iopub.status.idle":"2022-11-28T10:36:27.298998Z","shell.execute_reply.started":"2022-11-28T10:36:27.271633Z","shell.execute_reply":"2022-11-28T10:36:27.298154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Example segment image\npath_mask=f\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/{training_patient[0]}.nii\"\npatient_mask=load_NIfTI(path_mask)\n\npatient_mask.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:27.301341Z","iopub.execute_input":"2022-11-28T10:36:27.301745Z","iopub.status.idle":"2022-11-28T10:36:27.964244Z","shell.execute_reply.started":"2022-11-28T10:36:27.301709Z","shell.execute_reply":"2022-11-28T10:36:27.963247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot segment images\nfig, axes = plt.subplots(nrows=3, ncols=6, figsize=(24,12))\nfig.suptitle(f'ID: {training_patient[0]}', weight=\"bold\", size=20)\n\nstart=110\nfor i in range(start,start+18):\n    mask = patient_mask[i]\n    slice_no = i\n\n    # Plot the image\n    x = (i-110) // 6\n    y = (i-110) % 6\n\n    axes[x, y].imshow(mask, cmap='bone')\n    axes[x, y].set_title(f\"Slice: {slice_no}\", fontsize=14, weight='bold')\n    axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:27.965748Z","iopub.execute_input":"2022-11-28T10:36:27.966098Z","iopub.status.idle":"2022-11-28T10:36:30.127041Z","shell.execute_reply.started":"2022-11-28T10:36:27.966064Z","shell.execute_reply":"2022-11-28T10:36:30.12602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading Scans\ndef atoi(text):\n    return int(text) if text.isdigit() else text\ndef natural_keys(text):\n    return [atoi(c) for c in re.split(r'(\\d+)', text)]\n\n# Load the scans in given folder path\ndef load_scan(path):\n    \n    dcm_paths = glob(f\"{path}/*\")\n    dcm_paths.sort(key=natural_keys)\n    \n    patient_scan = [pydicom.dcmread(paths) for paths in dcm_paths]\n    \n    return patient_scan","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:30.128199Z","iopub.execute_input":"2022-11-28T10:36:30.12857Z","iopub.status.idle":"2022-11-28T10:36:30.135344Z","shell.execute_reply.started":"2022-11-28T10:36:30.12853Z","shell.execute_reply":"2022-11-28T10:36:30.134313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Example Scan\npath_scan=f\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/{training_patient[0]}\"\nimage=load_scan(path_scan)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:30.136774Z","iopub.execute_input":"2022-11-28T10:36:30.137379Z","iopub.status.idle":"2022-11-28T10:36:32.513334Z","shell.execute_reply.started":"2022-11-28T10:36:30.137341Z","shell.execute_reply":"2022-11-28T10:36:32.512347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot images\nfig, axes = plt.subplots(nrows=3, ncols=6, figsize=(24,12))\nfig.suptitle(f'ID: {training_patient[0]}', weight=\"bold\", size=20)\n\nstart = 110\nfor i in range(start,start+18):\n    img = image[i].pixel_array\n    slice_no = i\n\n    # Plot the image\n    x = (i-start) // 6\n    y = (i-start) % 6\n\n    axes[x, y].imshow(img, cmap=\"bone\")\n    axes[x, y].set_title(f\"Slice: {slice_no}\", fontsize=14, weight='bold')\n    axes[x, y].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:32.514778Z","iopub.execute_input":"2022-11-28T10:36:32.515122Z","iopub.status.idle":"2022-11-28T10:36:34.947715Z","shell.execute_reply.started":"2022-11-28T10:36:32.515087Z","shell.execute_reply":"2022-11-28T10:36:34.946355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>4 Preprocessing</b></p>\n</div>\n","metadata":{"execution":{"iopub.status.busy":"2022-08-28T05:26:32.102808Z","iopub.execute_input":"2022-08-28T05:26:32.104407Z","iopub.status.idle":"2022-08-28T05:26:32.112038Z","shell.execute_reply.started":"2022-08-28T05:26:32.104366Z","shell.execute_reply":"2022-08-28T05:26:32.11075Z"}}},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>4.1 Loding and Conversion to HU</b></p>\n</div>","metadata":{}},{"cell_type":"code","source":"def get_pixels_hu(slices):\n   \n    image = np.stack([cv2.resize(s.pixel_array,(512,512),interpolation = cv2.INTER_NEAREST) for s in slices])\n    \n    # Convert to int16 (from sometimes int16), \n    # should be possible as values should always be low enough (<32k)\n    image = image.astype(np.int16)\n    image = da.from_array(image) #Using Dask to speed up processing\n    \n    # Set outside-of-scan pixels to 0\n    # The intercept is usually -1024, so air is approximately 0\n    image[image <= -1000] = 0\n    \n    # Convert to Hounsfield units (HU)\n        \n    intercept = da.from_array([slices[slice_number].RescaleIntercept for slice_number in range(len(slices))])\n    slope = da.from_array([slices[slice_number].RescaleSlope for slice_number in range(len(slices))])\n    \n    intercept=intercept.reshape((-1,1,1))\n    slope=slope.reshape((-1,1,1))\n    \n    image= slope * image.astype(\"float64\")\n        \n    image+= intercept\n     \n    return image.astype(\"int16\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:34.949264Z","iopub.execute_input":"2022-11-28T10:36:34.949638Z","iopub.status.idle":"2022-11-28T10:36:34.959485Z","shell.execute_reply.started":"2022-11-28T10:36:34.949603Z","shell.execute_reply":"2022-11-28T10:36:34.958443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_slice=get_pixels_hu(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:34.960734Z","iopub.execute_input":"2022-11-28T10:36:34.961539Z","iopub.status.idle":"2022-11-28T10:36:35.266438Z","shell.execute_reply.started":"2022-11-28T10:36:34.961502Z","shell.execute_reply":"2022-11-28T10:36:35.265524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Ploting pixel array\nplt.imshow(image[110].pixel_array,cmap='bone')\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:35.267942Z","iopub.execute_input":"2022-11-28T10:36:35.268689Z","iopub.status.idle":"2022-11-28T10:36:35.479175Z","shell.execute_reply.started":"2022-11-28T10:36:35.268644Z","shell.execute_reply":"2022-11-28T10:36:35.478211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Ploting pixel array distribution\nplt.hist(image[110].pixel_array.flatten(),color=\"r\",bins=50)\nplt.xlabel(\"Pixel Values\")\nplt.ylabel(\"Fequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:35.483929Z","iopub.execute_input":"2022-11-28T10:36:35.484877Z","iopub.status.idle":"2022-11-28T10:36:35.779166Z","shell.execute_reply.started":"2022-11-28T10:36:35.48484Z","shell.execute_reply":"2022-11-28T10:36:35.77822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Ploting HU array\nplt.imshow(patient_slice[110],cmap='bone')\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:35.780483Z","iopub.execute_input":"2022-11-28T10:36:35.781231Z","iopub.status.idle":"2022-11-28T10:36:36.476271Z","shell.execute_reply.started":"2022-11-28T10:36:35.781193Z","shell.execute_reply":"2022-11-28T10:36:36.475216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Ploting HU distribution\nplt.hist(patient_slice[110].flatten().compute(),color=\"r\",bins=50)\nplt.xlabel(\"HU Values\")\nplt.ylabel(\"Fequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:36.477838Z","iopub.execute_input":"2022-11-28T10:36:36.478193Z","iopub.status.idle":"2022-11-28T10:36:37.367794Z","shell.execute_reply.started":"2022-11-28T10:36:36.478157Z","shell.execute_reply":"2022-11-28T10:36:37.366812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>4.2 Normalization</b></p>\n</div>\n\nAs HU values range from **150** to **2050**. So we will use this values for normalization.","metadata":{"execution":{"iopub.status.busy":"2022-08-28T05:52:59.856532Z","iopub.execute_input":"2022-08-28T05:52:59.857196Z","iopub.status.idle":"2022-08-28T05:52:59.864458Z","shell.execute_reply.started":"2022-08-28T05:52:59.857163Z","shell.execute_reply":"2022-08-28T05:52:59.862917Z"}}},{"cell_type":"code","source":"MIN_BOUND = 150.0\nMAX_BOUND = 2050.0\n    \ndef normalize(image):\n    image = (image - MIN_BOUND)*255.0 / (MAX_BOUND - MIN_BOUND)\n    image[image>255] = 255.\n    image[image<0] = 255.\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:37.371957Z","iopub.execute_input":"2022-11-28T10:36:37.372817Z","iopub.status.idle":"2022-11-28T10:36:37.383551Z","shell.execute_reply.started":"2022-11-28T10:36:37.372777Z","shell.execute_reply":"2022-11-28T10:36:37.382454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=normalize(patient_slice)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:37.387528Z","iopub.execute_input":"2022-11-28T10:36:37.390147Z","iopub.status.idle":"2022-11-28T10:36:37.417219Z","shell.execute_reply.started":"2022-11-28T10:36:37.390112Z","shell.execute_reply":"2022-11-28T10:36:37.41636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image[110],cmap=\"bone\")\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:37.426215Z","iopub.execute_input":"2022-11-28T10:36:37.428663Z","iopub.status.idle":"2022-11-28T10:36:38.833759Z","shell.execute_reply.started":"2022-11-28T10:36:37.428626Z","shell.execute_reply":"2022-11-28T10:36:38.832801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Delete all unused objects to free up memory\ndel patient_slice\ndel image\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:38.835077Z","iopub.execute_input":"2022-11-28T10:36:38.835709Z","iopub.status.idle":"2022-11-28T10:36:39.066293Z","shell.execute_reply.started":"2022-11-28T10:36:38.83567Z","shell.execute_reply":"2022-11-28T10:36:39.065123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color: #A020F0;font-size:150%;\n            font-family:Nexa;letter-spacing:0.5px\">\n    <p style=\"padding: 4px;color:white;\"><b>5 Model</b></p>\n</div>","metadata":{}},{"cell_type":"code","source":"X_train_patient, X_Val_patient = train_test_split(training_patient,train_size=70,test_size=17,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:39.067634Z","iopub.execute_input":"2022-11-28T10:36:39.06984Z","iopub.status.idle":"2022-11-28T10:36:39.076135Z","shell.execute_reply.started":"2022-11-28T10:36:39.069801Z","shell.execute_reply":"2022-11-28T10:36:39.075062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Segreggate the preprocessed image in these folders based on Segmentation data in Training Folder.\ndef segregate_Train(start):\n    \n    train_ds_x=[]\n    train_ds_y=[]\n    for i in range(start,start+10):\n        patient_ID=X_train_patient[i]\n        \n        patient_seg=load_NIfTI(f\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/{patient_ID}.nii\")\n        \n        patient_scan=load_scan(f\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/{patient_ID}\")\n        patient_hu=get_pixels_hu(patient_scan)\n        patient_hu_normalised=normalize(patient_hu)\n        \n        for j in tqdm(range(0,len(patient_seg))):\n            classes=np.unique(patient_seg[j])\n        \n            temp_lables=np.zeros(9)\n            for k in classes:\n                if int(k)!=0:\n                    if int(k)<8:\n                        temp_lables[int(k)]=1\n                    else:\n                        temp_lables[8]=1\n                else:\n                    temp_lables[0]=1\n            \n            train_ds_x.append(patient_hu_normalised[j].astype(np.uint8))\n            train_ds_y.append(temp_lables.astype(np.uint8))\n        \n    return train_ds_x,train_ds_y","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:39.077501Z","iopub.execute_input":"2022-11-28T10:36:39.078315Z","iopub.status.idle":"2022-11-28T10:36:39.088107Z","shell.execute_reply.started":"2022-11-28T10:36:39.078255Z","shell.execute_reply":"2022-11-28T10:36:39.08706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Segreggate the preprocessed image in these folders based on Segmentation data in Training Folder.\ndef segregate_Val():\n    \n    val_ds_x=[]\n    val_ds_y=[]\n    for i in range(0,3):\n        patient_ID=X_Val_patient[i]\n        \n        patient_seg=load_NIfTI(f\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/{patient_ID}.nii\")\n        \n        patient_scan=load_scan(f\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/{patient_ID}\")\n        patient_hu=get_pixels_hu(patient_scan)\n        patient_hu_normalised=normalize(patient_hu)\n        \n        for j in tqdm(range(0,len(patient_seg))):\n            classes=np.unique(patient_seg[j])\n        \n            temp_lables=np.zeros(9)\n            for k in classes:\n                if int(k)!=0:\n                    if int(k)<8:\n                        temp_lables[int(k)]=1\n                    else:\n                        temp_lables[8]=1\n                else:\n                    temp_lables[0]=1\n            \n            val_ds_x.append(patient_hu_normalised[j].astype(np.uint8))\n            val_ds_y.append(temp_lables.astype(np.uint8))\n        \n    return val_ds_x,val_ds_y","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:39.089782Z","iopub.execute_input":"2022-11-28T10:36:39.090192Z","iopub.status.idle":"2022-11-28T10:36:39.1015Z","shell.execute_reply.started":"2022-11-28T10:36:39.0901Z","shell.execute_reply":"2022-11-28T10:36:39.100603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Call this in a loop to train the model in folds\ntrain_ds_x,train_ds_y=segregate_Train(0)\nval_ds_x,val_ds_y=segregate_Val()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:36:39.102808Z","iopub.execute_input":"2022-11-28T10:36:39.103376Z","iopub.status.idle":"2022-11-28T10:38:47.67969Z","shell.execute_reply.started":"2022-11-28T10:36:39.103342Z","shell.execute_reply":"2022-11-28T10:38:47.678439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convert into specific format from tensorflow model\ntrain_ds_x=np.array(train_ds_x)\ntrain_ds_y=np.array(train_ds_y)\nval_ds_x=np.array(val_ds_x)\nval_ds_y=np.array(val_ds_y)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T10:38:47.681223Z","iopub.execute_input":"2022-11-28T10:38:47.681813Z","iopub.status.idle":"2022-11-28T12:45:25.01811Z","shell.execute_reply.started":"2022-11-28T10:38:47.681773Z","shell.execute_reply":"2022-11-28T12:45:25.017074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_ds_x.shape)\nprint(train_ds_y.shape)\nprint(val_ds_x.shape)\nprint(val_ds_y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.019665Z","iopub.execute_input":"2022-11-28T12:45:25.020023Z","iopub.status.idle":"2022-11-28T12:45:25.026939Z","shell.execute_reply.started":"2022-11-28T12:45:25.019986Z","shell.execute_reply":"2022-11-28T12:45:25.025953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds_x=np.reshape(train_ds_x,(-1,512,512,1))\nval_ds_x=np.reshape(val_ds_x,(-1,512,512,1))","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.028225Z","iopub.execute_input":"2022-11-28T12:45:25.02888Z","iopub.status.idle":"2022-11-28T12:45:25.035683Z","shell.execute_reply.started":"2022-11-28T12:45:25.028844Z","shell.execute_reply":"2022-11-28T12:45:25.034707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_ds_x.shape)\nprint(train_ds_y.shape)\nprint(val_ds_x.shape)\nprint(val_ds_y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.036896Z","iopub.execute_input":"2022-11-28T12:45:25.03844Z","iopub.status.idle":"2022-11-28T12:45:25.048238Z","shell.execute_reply.started":"2022-11-28T12:45:25.038411Z","shell.execute_reply":"2022-11-28T12:45:25.04483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_ds_y[170])","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.04973Z","iopub.execute_input":"2022-11-28T12:45:25.050015Z","iopub.status.idle":"2022-11-28T12:45:25.058051Z","shell.execute_reply.started":"2022-11-28T12:45:25.04999Z","shell.execute_reply":"2022-11-28T12:45:25.056958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_ds_x[170],cmap=\"bone\")\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.059439Z","iopub.execute_input":"2022-11-28T12:45:25.059858Z","iopub.status.idle":"2022-11-28T12:45:25.253749Z","shell.execute_reply.started":"2022-11-28T12:45:25.059824Z","shell.execute_reply":"2022-11-28T12:45:25.252729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.255307Z","iopub.execute_input":"2022-11-28T12:45:25.255927Z","iopub.status.idle":"2022-11-28T12:45:25.464221Z","shell.execute_reply.started":"2022-11-28T12:45:25.255888Z","shell.execute_reply":"2022-11-28T12:45:25.463264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data augmentation\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=5,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.1, # Randomly zoom image 10%\n        width_shift_range=0.1,  # randomly shift images horizontally 10%\n        height_shift_range=0.1,  # randomly shift images vertically 10%\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=True)  # randomly flip images\n\ndatagen.fit(train_ds_x)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:25.465907Z","iopub.execute_input":"2022-11-28T12:45:25.466306Z","iopub.status.idle":"2022-11-28T12:45:32.426198Z","shell.execute_reply.started":"2022-11-28T12:45:25.466257Z","shell.execute_reply":"2022-11-28T12:45:32.42512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the model\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (512,512,1)),\n    tf.keras.layers.Conv2D(filters = 8, kernel_size = (3,3), activation ='relu'),\n    tf.keras.layers.MaxPool2D(pool_size=(2, 2)),\n    tf.keras.layers.Dropout(0.30),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (7,7),padding = 'Same', activation ='relu'),\n    tf.keras.layers.Conv2D(filters = 8, kernel_size = (5,5), activation ='relu'),\n    tf.keras.layers.MaxPool2D(pool_size=(4, 4)),\n    tf.keras.layers.Dropout(0.30),\n    tf.keras.layers.Conv2D(filters = 8, kernel_size = (7,7),padding = 'Same', activation ='relu'),\n    tf.keras.layers.Conv2D(filters = 64, kernel_size = (5,5), activation ='relu'),\n    tf.keras.layers.MaxPool2D(pool_size=(6, 6)),\n    tf.keras.layers.Dropout(0.30),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same', activation ='relu'),\n    tf.keras.layers.Conv2D(filters = 16, kernel_size = (7,7), activation ='relu'),\n    tf.keras.layers.MaxPool2D(pool_size=(3, 3)),\n    tf.keras.layers.Dropout(0.30),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(12,activation=\"ReLU\"),\n    tf.keras.layers.Dense(9, activation='softmax')\n])\nmodel.build([None, 512, 512, 1])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:32.427612Z","iopub.execute_input":"2022-11-28T12:45:32.428261Z","iopub.status.idle":"2022-11-28T12:45:34.717269Z","shell.execute_reply.started":"2022-11-28T12:45:32.428224Z","shell.execute_reply":"2022-11-28T12:45:34.716219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = callbacks.EarlyStopping(\n    min_delta=0.001, # minimium amount of change to count as an improvement\n    patience=10, # how many epochs to wait before stopping\n    restore_best_weights=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:34.718854Z","iopub.execute_input":"2022-11-28T12:45:34.7192Z","iopub.status.idle":"2022-11-28T12:45:34.724537Z","shell.execute_reply.started":"2022-11-28T12:45:34.719165Z","shell.execute_reply":"2022-11-28T12:45:34.723211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.0005),\n              loss=tf.keras.losses.BinaryCrossentropy(),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:34.726427Z","iopub.execute_input":"2022-11-28T12:45:34.727139Z","iopub.status.idle":"2022-11-28T12:45:34.74257Z","shell.execute_reply.started":"2022-11-28T12:45:34.727103Z","shell.execute_reply":"2022-11-28T12:45:34.741532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(datagen.flow(train_ds_x,train_ds_y),epochs=10,use_multiprocessing=True,shuffle=True,callbacks=[early_stopping],validation_data=(val_ds_x,val_ds_y))","metadata":{"execution":{"iopub.status.busy":"2022-11-28T12:45:34.745722Z","iopub.execute_input":"2022-11-28T12:45:34.74626Z","iopub.status.idle":"2022-11-28T13:05:20.602323Z","shell.execute_reply.started":"2022-11-28T12:45:34.746226Z","shell.execute_reply":"2022-11-28T13:05:20.601013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","metadata":{"execution":{"iopub.status.busy":"2022-11-28T13:05:20.60479Z","iopub.execute_input":"2022-11-28T13:05:20.605213Z","iopub.status.idle":"2022-11-28T13:05:20.971701Z","shell.execute_reply.started":"2022-11-28T13:05:20.605171Z","shell.execute_reply":"2022-11-28T13:05:20.97078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"Classifier.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T13:05:20.97597Z","iopub.execute_input":"2022-11-28T13:05:20.978316Z","iopub.status.idle":"2022-11-28T13:05:21.069726Z","shell.execute_reply.started":"2022-11-28T13:05:20.978242Z","shell.execute_reply":"2022-11-28T13:05:21.068741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-11-28T13:05:21.074175Z","iopub.execute_input":"2022-11-28T13:05:21.076423Z","iopub.status.idle":"2022-11-28T13:05:21.089514Z","shell.execute_reply.started":"2022-11-28T13:05:21.076386Z","shell.execute_reply":"2022-11-28T13:05:21.088341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_probs = model.predict(val_ds_x, verbose=0)\nfrom sklearn.metrics import precision_score\nprecision_score(val_ds_y,yhat_probs,average=\"macro\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T13:48:17.129214Z","iopub.execute_input":"2022-11-28T13:48:17.129611Z","iopub.status.idle":"2022-11-28T13:48:20.31173Z","shell.execute_reply.started":"2022-11-28T13:48:17.129581Z","shell.execute_reply":"2022-11-28T13:48:20.309644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(\n    model,\n    to_file=\"model.png\",\n    show_shapes=False,\n    show_dtype=False,\n    show_layer_names=True,\n    rankdir=\"TB\",\n    expand_nested=False,\n    dpi=96,\n    layer_range=None,\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T13:44:04.910323Z","iopub.status.idle":"2022-11-28T13:44:04.91154Z","shell.execute_reply.started":"2022-11-28T13:44:04.911246Z","shell.execute_reply":"2022-11-28T13:44:04.911274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}