{"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-08-30T12:56:42.60002Z","iopub.execute_input":"2022-08-30T12:56:42.600834Z","iopub.status.idle":"2022-08-30T12:57:06.188442Z","shell.execute_reply.started":"2022-08-30T12:56:42.600685Z","shell.execute_reply":"2022-08-30T12:57:06.18728Z"},"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-08-30T12:57:06.19188Z","iopub.execute_input":"2022-08-30T12:57:06.192287Z","iopub.status.idle":"2022-08-30T12:57:14.285898Z","shell.execute_reply.started":"2022-08-30T12:57:06.192254Z","shell.execute_reply":"2022-08-30T12:57:14.284927Z"},"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-08-30T12:57:14.287421Z","iopub.execute_input":"2022-08-30T12:57:14.28819Z","iopub.status.idle":"2022-08-30T12:57:14.388767Z","shell.execute_reply.started":"2022-08-30T12:57:14.288151Z","shell.execute_reply":"2022-08-30T12:57:14.387753Z"},"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-08-30T12:57:14.391487Z","iopub.execute_input":"2022-08-30T12:57:14.39245Z","iopub.status.idle":"2022-08-30T12:57:14.398293Z","shell.execute_reply.started":"2022-08-30T12:57:14.392412Z","shell.execute_reply":"2022-08-30T12:57:14.397261Z"},"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-08-30T12:57:14.399676Z","iopub.execute_input":"2022-08-30T12:57:14.400482Z","iopub.status.idle":"2022-08-30T12:57:14.415659Z","shell.execute_reply.started":"2022-08-30T12:57:14.400445Z","shell.execute_reply":"2022-08-30T12:57:14.414836Z"},"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-08-30T12:57:14.417764Z","iopub.execute_input":"2022-08-30T12:57:14.418609Z","iopub.status.idle":"2022-08-30T12:57:15.168041Z","shell.execute_reply.started":"2022-08-30T12:57:14.418575Z","shell.execute_reply":"2022-08-30T12:57:15.167115Z"},"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-08-30T12:57:15.169369Z","iopub.execute_input":"2022-08-30T12:57:15.169735Z","iopub.status.idle":"2022-08-30T12:57:17.276611Z","shell.execute_reply.started":"2022-08-30T12:57:15.169687Z","shell.execute_reply":"2022-08-30T12:57:17.275617Z"},"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-08-30T12:57:17.277688Z","iopub.execute_input":"2022-08-30T12:57:17.278024Z","iopub.status.idle":"2022-08-30T12:57:17.285194Z","shell.execute_reply.started":"2022-08-30T12:57:17.277989Z","shell.execute_reply":"2022-08-30T12:57:17.284215Z"},"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-08-30T12:57:17.286717Z","iopub.execute_input":"2022-08-30T12:57:17.28735Z","iopub.status.idle":"2022-08-30T12:57:19.484732Z","shell.execute_reply.started":"2022-08-30T12:57:17.287315Z","shell.execute_reply":"2022-08-30T12:57:19.483734Z"},"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-08-30T12:57:19.48979Z","iopub.execute_input":"2022-08-30T12:57:19.490191Z","iopub.status.idle":"2022-08-30T12:57:21.702577Z","shell.execute_reply.started":"2022-08-30T12:57:19.490154Z","shell.execute_reply":"2022-08-30T12:57:21.700153Z"},"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-08-30T12:57:21.704227Z","iopub.execute_input":"2022-08-30T12:57:21.70502Z","iopub.status.idle":"2022-08-30T12:57:21.714845Z","shell.execute_reply.started":"2022-08-30T12:57:21.704983Z","shell.execute_reply":"2022-08-30T12:57:21.713759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_slice=get_pixels_hu(image)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T12:57:21.716386Z","iopub.execute_input":"2022-08-30T12:57:21.717136Z","iopub.status.idle":"2022-08-30T12:57:22.029817Z","shell.execute_reply.started":"2022-08-30T12:57:21.717091Z","shell.execute_reply":"2022-08-30T12:57:22.028887Z"},"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-08-30T12:57:22.031515Z","iopub.execute_input":"2022-08-30T12:57:22.031896Z","iopub.status.idle":"2022-08-30T12:57:22.240127Z","shell.execute_reply.started":"2022-08-30T12:57:22.03186Z","shell.execute_reply":"2022-08-30T12:57:22.239049Z"},"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-08-30T12:57:22.241503Z","iopub.execute_input":"2022-08-30T12:57:22.242484Z","iopub.status.idle":"2022-08-30T12:57:22.525531Z","shell.execute_reply.started":"2022-08-30T12:57:22.242448Z","shell.execute_reply":"2022-08-30T12:57:22.524558Z"},"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-08-30T12:57:22.526822Z","iopub.execute_input":"2022-08-30T12:57:22.52789Z","iopub.status.idle":"2022-08-30T12:57:23.206917Z","shell.execute_reply.started":"2022-08-30T12:57:22.527853Z","shell.execute_reply":"2022-08-30T12:57:23.206007Z"},"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-08-30T12:57:23.208527Z","iopub.execute_input":"2022-08-30T12:57:23.209201Z","iopub.status.idle":"2022-08-30T12:57:23.976109Z","shell.execute_reply.started":"2022-08-30T12:57:23.209163Z","shell.execute_reply":"2022-08-30T12:57:23.975012Z"},"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-08-30T12:57:23.977453Z","iopub.execute_input":"2022-08-30T12:57:23.978492Z","iopub.status.idle":"2022-08-30T12:57:23.98457Z","shell.execute_reply.started":"2022-08-30T12:57:23.978451Z","shell.execute_reply":"2022-08-30T12:57:23.983629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=normalize(patient_slice)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T12:57:23.985852Z","iopub.execute_input":"2022-08-30T12:57:23.986567Z","iopub.status.idle":"2022-08-30T12:57:24.003426Z","shell.execute_reply.started":"2022-08-30T12:57:23.98653Z","shell.execute_reply":"2022-08-30T12:57:24.002477Z"},"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-08-30T12:57:24.004772Z","iopub.execute_input":"2022-08-30T12:57:24.006816Z","iopub.status.idle":"2022-08-30T12:57:25.294023Z","shell.execute_reply.started":"2022-08-30T12:57:24.006788Z","shell.execute_reply":"2022-08-30T12:57:25.293002Z"},"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-08-30T12:57:25.29532Z","iopub.execute_input":"2022-08-30T12:57:25.295685Z","iopub.status.idle":"2022-08-30T12:57:25.618821Z","shell.execute_reply.started":"2022-08-30T12:57:25.295649Z","shell.execute_reply":"2022-08-30T12:57:25.617889Z"},"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-08-30T12:57:25.622799Z","iopub.execute_input":"2022-08-30T12:57:25.624378Z","iopub.status.idle":"2022-08-30T12:57:25.648358Z","shell.execute_reply.started":"2022-08-30T12:57:25.624339Z","shell.execute_reply":"2022-08-30T12:57:25.647351Z"},"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-08-30T12:57:25.666606Z","iopub.execute_input":"2022-08-30T12:57:25.668629Z","iopub.status.idle":"2022-08-30T12:57:25.687251Z","shell.execute_reply.started":"2022-08-30T12:57:25.668595Z","shell.execute_reply":"2022-08-30T12:57:25.686135Z"},"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-08-30T12:57:25.689094Z","iopub.execute_input":"2022-08-30T12:57:25.689849Z","iopub.status.idle":"2022-08-30T12:57:25.700952Z","shell.execute_reply.started":"2022-08-30T12:57:25.689813Z","shell.execute_reply":"2022-08-30T12:57:25.699833Z"},"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-08-30T12:57:25.702613Z","iopub.execute_input":"2022-08-30T12:57:25.703444Z","iopub.status.idle":"2022-08-30T12:59:01.223546Z","shell.execute_reply.started":"2022-08-30T12:57:25.703409Z","shell.execute_reply":"2022-08-30T12:59:01.222435Z"},"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-08-30T12:59:01.22516Z","iopub.execute_input":"2022-08-30T12:59:01.225618Z","iopub.status.idle":"2022-08-30T14:29:39.019997Z","shell.execute_reply.started":"2022-08-30T12:59:01.225577Z","shell.execute_reply":"2022-08-30T14:29:39.018975Z"},"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-08-30T14:29:39.021584Z","iopub.execute_input":"2022-08-30T14:29:39.021936Z","iopub.status.idle":"2022-08-30T14:29:39.028727Z","shell.execute_reply.started":"2022-08-30T14:29:39.0219Z","shell.execute_reply":"2022-08-30T14:29:39.027762Z"},"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-08-30T14:29:39.035601Z","iopub.execute_input":"2022-08-30T14:29:39.036398Z","iopub.status.idle":"2022-08-30T14:29:39.042055Z","shell.execute_reply.started":"2022-08-30T14:29:39.036362Z","shell.execute_reply":"2022-08-30T14:29:39.040915Z"},"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-08-30T14:29:39.043812Z","iopub.execute_input":"2022-08-30T14:29:39.044226Z","iopub.status.idle":"2022-08-30T14:29:39.052813Z","shell.execute_reply.started":"2022-08-30T14:29:39.04419Z","shell.execute_reply":"2022-08-30T14:29:39.051853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_ds_y[170])","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:29:39.054362Z","iopub.execute_input":"2022-08-30T14:29:39.054784Z","iopub.status.idle":"2022-08-30T14:29:39.063296Z","shell.execute_reply.started":"2022-08-30T14:29:39.054739Z","shell.execute_reply":"2022-08-30T14:29:39.061977Z"},"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-08-30T14:29:39.065084Z","iopub.execute_input":"2022-08-30T14:29:39.066226Z","iopub.status.idle":"2022-08-30T14:29:39.275226Z","shell.execute_reply.started":"2022-08-30T14:29:39.06619Z","shell.execute_reply":"2022-08-30T14:29:39.274053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:29:39.276803Z","iopub.execute_input":"2022-08-30T14:29:39.27719Z","iopub.status.idle":"2022-08-30T14:29:39.494884Z","shell.execute_reply.started":"2022-08-30T14:29:39.277143Z","shell.execute_reply":"2022-08-30T14:29:39.493494Z"},"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-08-30T14:29:39.496962Z","iopub.execute_input":"2022-08-30T14:29:39.49739Z","iopub.status.idle":"2022-08-30T14:29:44.75675Z","shell.execute_reply.started":"2022-08-30T14:29:39.497351Z","shell.execute_reply":"2022-08-30T14:29:44.755717Z"},"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-08-30T14:29:44.758515Z","iopub.execute_input":"2022-08-30T14:29:44.758907Z","iopub.status.idle":"2022-08-30T14:29:47.413446Z","shell.execute_reply.started":"2022-08-30T14:29:44.758868Z","shell.execute_reply":"2022-08-30T14:29:47.412402Z"},"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-08-30T14:29:47.414928Z","iopub.execute_input":"2022-08-30T14:29:47.41584Z","iopub.status.idle":"2022-08-30T14:29:47.422189Z","shell.execute_reply.started":"2022-08-30T14:29:47.415802Z","shell.execute_reply":"2022-08-30T14:29:47.42109Z"},"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-08-30T14:29:47.423763Z","iopub.execute_input":"2022-08-30T14:29:47.424133Z","iopub.status.idle":"2022-08-30T14:29:47.442567Z","shell.execute_reply.started":"2022-08-30T14:29:47.424098Z","shell.execute_reply":"2022-08-30T14:29:47.441666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(datagen.flow(train_ds_x,train_ds_y),epochs=50,use_multiprocessing=True,shuffle=True,callbacks=[early_stopping],validation_data=(val_ds_x,val_ds_y))","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:29:47.444181Z","iopub.execute_input":"2022-08-30T14:29:47.444662Z","iopub.status.idle":"2022-08-30T15:12:21.427005Z","shell.execute_reply.started":"2022-08-30T14:29:47.444636Z","shell.execute_reply":"2022-08-30T15:12:21.424629Z"},"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-08-30T15:12:21.43304Z","iopub.execute_input":"2022-08-30T15:12:21.43387Z","iopub.status.idle":"2022-08-30T15:13:20.584035Z","shell.execute_reply.started":"2022-08-30T15:12:21.433815Z","shell.execute_reply":"2022-08-30T15:13:20.583003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"Classifier.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:29:09.435416Z","iopub.execute_input":"2022-08-30T15:29:09.436059Z","iopub.status.idle":"2022-08-30T15:29:09.512903Z","shell.execute_reply.started":"2022-08-30T15:29:09.436023Z","shell.execute_reply":"2022-08-30T15:29:09.511963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}