{"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":"from tensorflow.keras.models import load_model\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Input, Flatten, LSTM, GRU, Dense, Reshape, TimeDistributed\nfrom tensorflow.keras.models import Model\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.models import load_model\n\n\nfrom types import SimpleNamespace\nimport keras\n\nfrom scipy.ndimage import zoom\nimport numpy as np\nimport cv2\nimport pandas as pd\nfrom glob import glob\nimport pydicom\nfrom sklearn import preprocessing\nimport random\nimport re\n\nimport matplotlib.pyplot as plt\nimport os \nfrom glob import glob\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\ntf.config.run_functions_eagerly(True)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-02T18:57:31.899443Z","iopub.execute_input":"2023-11-02T18:57:31.899837Z","iopub.status.idle":"2023-11-02T18:57:40.190969Z","shell.execute_reply.started":"2023-11-02T18:57:31.899809Z","shell.execute_reply":"2023-11-02T18:57:40.189928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/rsna-2023-abdominal-trauma-detection\"\nMODEL_PATH = \"/kaggle/input/main-model-lstm-rsna-atd/LSTM_V1.h5\"\n\nTARGET_COLS  = [\"bowel_healthy\", \"bowel_injury\", \"extravasation_healthy\",\n                   \"extravasation_injury\", \"kidney_healthy\", \"kidney_low\",\n                   \"kidney_high\", \"liver_healthy\", \"liver_low\", \"liver_high\",\n                   \"spleen_healthy\", \"spleen_low\", \"spleen_high\"]","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.19308Z","iopub.execute_input":"2023-11-02T18:57:40.194067Z","iopub.status.idle":"2023-11-02T18:57:40.198751Z","shell.execute_reply.started":"2023-11-02T18:57:40.194028Z","shell.execute_reply":"2023-11-02T18:57:40.197848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****LOAD TEST SET****","metadata":{}},{"cell_type":"code","source":"TEST_CSV = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.199889Z","iopub.execute_input":"2023-11-02T18:57:40.200205Z","iopub.status.idle":"2023-11-02T18:57:40.209198Z","shell.execute_reply.started":"2023-11-02T18:57:40.200168Z","shell.execute_reply":"2023-11-02T18:57:40.208436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****PREPROCESSING FUNCTIONS****","metadata":{}},{"cell_type":"code","source":"def window_converter(image, window_width: int=400, window_level: int=50) -> np.ndarray:\n\n    \"\"\"_Uses the window values in order to create desired contrast to the image_\n\n        Returns\n        -------\n        _np.ndarray_\n            _returns a numpy array with the desired window level applied_\n    \"\"\"\n    img_min = window_level - window_width // 2\n    img_max = window_level + window_width // 2\n    window_image = image.copy()\n    window_image[window_image < img_min] = img_min\n    window_image[window_image > img_max] = img_max\n    return window_image\n\ndef transform_to_hu(medical_image: str, image: np.ndarray) -> np.ndarray:\n\n    \"\"\"_Tranforms Hounsfield Units considering\n            an input image and image path for reading\n            metadata_\n\n        Returns\n        -------\n        _np.ndarray_\n            _Returns a numpy array_\n    \"\"\"\n    meta_image = pydicom.dcmread(medical_image)\n    intercept = meta_image.RescaleIntercept\n    slope = meta_image.RescaleSlope\n    hu_image = image * slope + intercept\n    return hu_image\n\ndef standardize_pixel_array(dcm: pydicom.dataset.FileDataset) -> np.ndarray:\n    \"\"\"_Correct DICOM pixel_array if PixelRepresentation == 1._\n\n        Returns\n        -------\n        _np.ndarray_\n            _returns the pixel array from the dicom file with the\n            fixed pixel representation value_\n    \"\"\"\n    # Correct DICOM pixel_array if PixelRepresentation == 1.\n    pixel_array = dcm.pixel_array\n    if dcm.PixelRepresentation == 1:\n        bit_shift = dcm.BitsAllocated - dcm.BitsStored\n        dtype = pixel_array.dtype \n        pixel_array = (pixel_array << bit_shift).astype(dtype) >> bit_shift\n    return pixel_array\n\ndef resize_img(img_paths: list, target_size: tuple=(128, 128)) -> np.ndarray:\n    \n    \"\"\"_Resize and fix pixel array_\n\n        Returns\n        -------\n        _np.ndarray_\n            _Returns fixed and normalized image_\n    \"\"\"\n    volume_shape = (target_size[0], target_size[1], len(img_paths)) \n    volume = np.zeros(volume_shape, dtype=np.float64)\n    for i, image_path in enumerate(img_paths):\n        image = pydicom.read_file(image_path)\n        image = standardize_pixel_array(image)\n        hu_image = transform_to_hu(image_path, image)\n        window_image = window_converter(hu_image)\n        image = cv2.resize(window_image, target_size)\n        volume[:,:,i] = image\n    return volume\n\ndef normalize_volume(resized_volume: np.array) -> np.array:\n\n    \"\"\"_Normalizes the volume of images for going from 0 to 1 values_\n\n    Returns\n    -------\n    _np.array_\n        _returns the same object type but with the normalization applied_\n    \"\"\"\n    original_shape = resized_volume.shape\n    flattened_image = resized_volume.reshape((-1,))\n    scaler = preprocessing.MinMaxScaler()\n    normalized_flattened_image = scaler.fit_transform(flattened_image.reshape((-1, 1)))\n    normalized_volume_image = normalized_flattened_image.reshape(original_shape)\n    return normalized_volume_image\n\ndef change_depth_siz(patient_volume: np.ndarray, target_depth: int=64) -> np.ndarray:\n\n    \"\"\"_Change the depth of an input volume as a numpy array\n        considering SIZ algorithm and a desired target depth_\n\n    Returns\n    -------\n    _np.ndarray_\n        _Volume reduced from the original input volume containing\n         the target depth as the total number of slices per volume_\n    \"\"\"\n    current_depth = patient_volume.shape[0]\n    depth = current_depth / target_depth\n    depth_factor = 1 / depth\n    img_new = zoom(patient_volume, (depth_factor, 1, 1, 1), mode='nearest')\n    return img_new\n\n\ndef generate_patient_processed_data(list_img_paths: list, target_size: tuple=(128,128)):\n\n    height = target_size[0]\n    width = target_size[1]\n    depth = len(list_img_paths)\n\n    volume_array = np.zeros((height, width, depth), dtype=np.float64)\n\n    print(\"Initializing data preprocessing with the following dimensions-> Volumes:{}\".format(volume_array.shape))\n\n    resized_images = resize_img(list_img_paths, target_size=target_size)\n    normalized_siz_volume = normalize_volume(resized_images)\n\n    volume_array = normalized_siz_volume\n    volume_array = volume_array.transpose(2, 0, 1)\n    return np.expand_dims(volume_array, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.211113Z","iopub.execute_input":"2023-11-02T18:57:40.211374Z","iopub.status.idle":"2023-11-02T18:57:40.228952Z","shell.execute_reply.started":"2023-11-02T18:57:40.211351Z","shell.execute_reply":"2023-11-02T18:57:40.228147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def __reduce_data_with_prediction__(model, x_data: np.ndarray) -> np.ndarray:\n\n    \"\"\"_This function takes the model to use for prediction\n        and a set of data from which to predict the segmentation\n        the model trained in this case was a U-Net and it was \n        trained for segmentation of 6 different classes\n        \n        The model prediction is then used to delimit an\n        upper and lower limit as indexes from the original x_data_\n\n    Returns\n    -------\n    _np.array_\n        _Returns the exact same type of input x_data\n         but reduced considering the upper and lower limit_\n    \"\"\"\n\n    #model = load_model(model_path, compile=False)\n    \n    y_pred = model.predict(x_data)\n    y_pred_argmax=np.argmax(y_pred, axis=3)\n\n    upper_limit_class = 1\n    lower_limit_class = 5\n    \n    start_idx = None\n    end_idx = None\n\n    for i, _slice in enumerate(y_pred_argmax):\n        class_mask = _slice == upper_limit_class\n        masked_trues = class_mask * _slice\n        summed_pixels = np.sum(masked_trues)\n        class_mask_ = _slice == lower_limit_class\n        masked_trues_ = class_mask_ * _slice\n        summed_pixels_ = np.sum(masked_trues_)\n\n    \n        if summed_pixels > 100:\n            if start_idx is None:\n                start_idx = i\n\n        if summed_pixels_ > 100:\n            end_idx = i\n\n    if (start_idx != None) & (end_idx != None):\n        thresholded_array = x_data[start_idx+1: end_idx, :, :]\n\n        print(\"Shape of original array:\", x_data.shape)\n        print(\"Shape of thresholded array:\", thresholded_array.shape)\n        return thresholded_array\n    else:\n        print(\"Classes 1 and 5 not found in the array.\")","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.229958Z","iopub.execute_input":"2023-11-02T18:57:40.230191Z","iopub.status.idle":"2023-11-02T18:57:40.240224Z","shell.execute_reply.started":"2023-11-02T18:57:40.23017Z","shell.execute_reply":"2023-11-02T18:57:40.239493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_number_from_path(path: str):\n\n    \"\"\"_Auxiliary function that helps process_train_data() function\n        to sort image paths_\n\n    Returns\n    -------\n    _int_\n    \"\"\"\n\n    match = re.search(r'(\\d+)\\.dcm$', path)\n    if match:\n        return int(match.group(1))\n    return 0\n\ndef process_test_data(data: pd.DataFrame,\n                          path: str, number_idx: int) -> pd.DataFrame:\n\n    \"\"\"_This function process training data based on two input DataFrames\n        in this case because data is fragmented in more than one csv file_\n\n    Returns\n    -------\n    _pd.DataFrame_\n        _Returns a pd.DataFrame, if extended_data equal True\n        it will return the DataFrame with paths and labels, different from\n        normal functionality that returns a list of paths per patient study,\n        if extract_paths equal True then the list of paths will have\n        a length of 64 random but ordered slices from the study,\n        if both parameters are True then it will return the pd.DataFrame\n        with paths and labels but paths are reduced by extract_paths condition_\n    \"\"\"\n    # Cut columns from dataframe that are redundant or not useful\n    data_to_merge = data[[\"patient_id\", \"series_id\"]]\n    # Shuffle merged DataFrame\n    shuffled_data = data_to_merge.sample(frac=1, random_state=42)\n    shuffled_indexes = shuffled_data.index[:number_idx]\n    selected_rows = shuffled_data.loc[shuffled_indexes]\n    data_to_merge_processed = selected_rows.reset_index()\n    \n    total_paths = []\n    patient_ids = []\n    series_ids = []\n    # Iterate merged Dataframe and extract image paths, store them in a list\n    for patient_id in range(len(data_to_merge_processed)):\n    \n        p_id = str(data_to_merge_processed[\"patient_id\"][patient_id]) + \"/\" + str(data_to_merge_processed[\"series_id\"][patient_id])\n        str_imgs_path = path + p_id + '/'\n        patient_img_paths = []\n\n        for file in glob(str_imgs_path + '/*'):\n            patient_img_paths.append(file)\n        # Sort lists paths\n        sorted_file_paths = sorted(patient_img_paths, key=extract_number_from_path)\n        total_paths.append(sorted_file_paths)\n        \n        patient_ids.append(data_to_merge_processed[\"patient_id\"][patient_id])\n        series_ids.append(data_to_merge_processed[\"series_id\"][patient_id])\n    \n    final_data = pd.DataFrame(list(zip(patient_ids, series_ids, total_paths)),\n               columns =[\"Patient_id\", \"Series_id\", \"Patient_paths\"])\n\n    return final_data","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.241491Z","iopub.execute_input":"2023-11-02T18:57:40.241777Z","iopub.status.idle":"2023-11-02T18:57:40.253355Z","shell.execute_reply.started":"2023-11-02T18:57:40.241753Z","shell.execute_reply":"2023-11-02T18:57:40.252477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_test_images = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/\"\n#\"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images/\"\ntest = pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv\")\ntest_data = process_test_data(test, path=path_test_images, number_idx=2)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.25449Z","iopub.execute_input":"2023-11-02T18:57:40.254738Z","iopub.status.idle":"2023-11-02T18:57:40.652118Z","shell.execute_reply.started":"2023-11-02T18:57:40.254716Z","shell.execute_reply":"2023-11-02T18:57:40.651318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.653284Z","iopub.execute_input":"2023-11-02T18:57:40.653599Z","iopub.status.idle":"2023-11-02T18:57:40.668301Z","shell.execute_reply.started":"2023-11-02T18:57:40.653573Z","shell.execute_reply":"2023-11-02T18:57:40.66748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = test_data.copy()\nmodel = load_model(\"/kaggle/input/unet-model/Unet_Fine_Tune_128_plus_CKP.h5\", compile=False)\nfor i in range(len(data)):\n    print(f'Generating data for patient -> {str(data[\"Patient_id\"][i])} \\n')\n    try:\n        patient_data_volumes = generate_patient_processed_data(data[\"Patient_paths\"][i], target_size=(128,128))\n        print(\"Initializing prediction and data reduction \\n\")\n        patient_data_volumes_reduced = __reduce_data_with_prediction__(model, patient_data_volumes)\n    except:\n        print(f\"Empty list of dicom found at {str(data['Patient_id'][i])}_{str(data['Series_id'][i])}\")\n    if len(data[\"Patient_paths\"][i]) == 0:\n        print(\"Passing to next study..\")\n    else:\n        print(\"Changing volume depth with zoom... \\n\")\n        try:\n            patient_data_volumes_zoom = change_depth_siz(patient_data_volumes_reduced, target_depth=64)\n            print(\"Reduced volume\")\n        except:\n            patient_data_volumes_zoom = change_depth_siz(patient_data_volumes, target_depth=64)\n            print(\"Original volume\")\n            \n        with open(f'/kaggle/working/{str(data[\"Patient_id\"][i])}_{str(data[\"Series_id\"][i])}.npy', 'wb') as f:\n            print(f'/kaggle/working/{str(data[\"Patient_id\"][i])}_{str(data[\"Series_id\"][i])}.npy')\n            np.save(f, patient_data_volumes_zoom)\n            print(f'Process finished for patient -> {str(data[\"Patient_id\"][i])}', f\"Final shape saved: {patient_data_volumes_zoom.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:57:40.66964Z","iopub.execute_input":"2023-11-02T18:57:40.670006Z","iopub.status.idle":"2023-11-02T18:58:31.704858Z","shell.execute_reply.started":"2023-11-02T18:57:40.669972Z","shell.execute_reply":"2023-11-02T18:58:31.703941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****FEATURE VECTOR GENERATOR****","metadata":{}},{"cell_type":"code","source":"model_unet = load_model(\"/kaggle/input/unet-model/Unet_Fine_Tune_128_plus_CKP.h5\")\n#model_unet.summary() U_Net\\Unet_Fine_Tune_128_plus_CKP.h5\nlast_enc_layer = model_unet.get_layer('conv2d_9')\nmodel_encoder = Model(inputs=model_unet.input, outputs=last_enc_layer.output)\n#model_encoder.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:31.708566Z","iopub.execute_input":"2023-11-02T18:58:31.708857Z","iopub.status.idle":"2023-11-02T18:58:32.533729Z","shell.execute_reply.started":"2023-11-02T18:58:31.708832Z","shell.execute_reply":"2023-11-02T18:58:32.532895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unet_encoder_for_lstm_input(input_shape):\n    unet_encoder = model_encoder\n    # Convierte la salida del encoder en una secuencia 1D para la LSTM\n    encoder_output = unet_encoder.output\n    output_layer = Flatten()(encoder_output)\n    model = Model(inputs=unet_encoder.input, outputs=output_layer)\n    return model\n\ninput_shape = (128, 128, 1)\nmodel2 = unet_encoder_for_lstm_input(input_shape)\nmodel2.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.534839Z","iopub.execute_input":"2023-11-02T18:58:32.535128Z","iopub.status.idle":"2023-11-02T18:58:32.589795Z","shell.execute_reply.started":"2023-11-02T18:58:32.535104Z","shell.execute_reply":"2023-11-02T18:58:32.588937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Try except por si no encuentra el archivo\n\nfor patient in range(len(data)):\n    try:\n        #print(f'/kaggle/working/{str(data.iloc[patient][\"Patient_id\"])}_{str(data.iloc[patient][\"Series_id\"])}.npy')\n        with open(f'/kaggle/working/{str(data.iloc[patient][\"Patient_id\"])}_{str(data.iloc[patient][\"Series_id\"])}.npy', 'rb') as f:\n            X = np.load(f, allow_pickle=True)\n            print(f\"Data dimension of X: {X.shape}\")\n        Feature_Extraction = model2.predict(X)    \n        X_max = Feature_Extraction.max()\n        X_min = Feature_Extraction.min()\n        normalized_prediction = (Feature_Extraction - X_min) / (X_max - X_min)\n        print(f\"Final Feature vectors shape: {normalized_prediction.shape}\")\n        with open(f'/kaggle/working/Features_{str(data[\"Patient_id\"][patient])}_{str(data[\"Series_id\"][patient])}.npy', 'wb') as f:\n            np.save(f, normalized_prediction)\n            #print(f'Process finished for patient -> {str(data[\"Patient_id\"][patient])}', f\"Final shape saved: {prediction.shape} and labels {labels.shape}\")\n    except:\n        print(\"File not found\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.591006Z","iopub.execute_input":"2023-11-02T18:58:32.591278Z","iopub.status.idle":"2023-11-02T18:58:32.799792Z","shell.execute_reply.started":"2023-11-02T18:58:32.591254Z","shell.execute_reply":"2023-11-02T18:58:32.798846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"in_files_ = os.listdir(\"/kaggle/working/\")","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.800932Z","iopub.execute_input":"2023-11-02T18:58:32.801221Z","iopub.status.idle":"2023-11-02T18:58:32.805636Z","shell.execute_reply.started":"2023-11-02T18:58:32.801195Z","shell.execute_reply":"2023-11-02T18:58:32.80475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_files = [file for file in in_files_ if file.startswith(\"Features_\")]\nfiltered_files","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.806785Z","iopub.execute_input":"2023-11-02T18:58:32.808505Z","iopub.status.idle":"2023-11-02T18:58:32.816351Z","shell.execute_reply.started":"2023-11-02T18:58:32.80847Z","shell.execute_reply":"2023-11-02T18:58:32.815508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f'/kaggle/input/zero-indexes-for-lstm-model/zeros_indexes.npy', 'rb') as f:\n    zeros_indexes = np.load(f, allow_pickle=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.817571Z","iopub.execute_input":"2023-11-02T18:58:32.817849Z","iopub.status.idle":"2023-11-02T18:58:32.830317Z","shell.execute_reply.started":"2023-11-02T18:58:32.817826Z","shell.execute_reply":"2023-11-02T18:58:32.829494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zeros_indexes","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.831422Z","iopub.execute_input":"2023-11-02T18:58:32.831746Z","iopub.status.idle":"2023-11-02T18:58:32.837611Z","shell.execute_reply.started":"2023-11-02T18:58:32.831714Z","shell.execute_reply":"2023-11-02T18:58:32.836741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_X = []\n\nfor i, listed_file_ in enumerate(filtered_files):\n    try:\n        with open(f'/kaggle/working/{listed_file_}', 'rb') as f:\n            X = np.load(f, allow_pickle=True)\n            X_filtered = np.delete(X, zeros_indexes, axis=1)\n            dataset_X.append(X_filtered)\n    except:\n        print(\"opening failed\")\n        continue","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.83879Z","iopub.execute_input":"2023-11-02T18:58:32.839076Z","iopub.status.idle":"2023-11-02T18:58:32.852657Z","shell.execute_reply.started":"2023-11-02T18:58:32.839053Z","shell.execute_reply":"2023-11-02T18:58:32.851846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data ready to be inputed in the LSTM model\n\nX_data = np.array(dataset_X)\nX_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.853818Z","iopub.execute_input":"2023-11-02T18:58:32.854216Z","iopub.status.idle":"2023-11-02T18:58:32.860927Z","shell.execute_reply.started":"2023-11-02T18:58:32.854184Z","shell.execute_reply":"2023-11-02T18:58:32.860113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('X_data.npy', X_data)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:32.861946Z","iopub.execute_input":"2023-11-02T18:58:32.862205Z","iopub.status.idle":"2023-11-02T18:58:32.868694Z","shell.execute_reply.started":"2023-11-02T18:58:32.86218Z","shell.execute_reply":"2023-11-02T18:58:32.867966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****LOAD MODEL****","metadata":{}},{"cell_type":"code","source":"CNN_LSTM_MODEL = load_model(\"/kaggle/input/cnn-lstm-under-represented-classes/CNN_LSTM_UNDER_represented_classes_1697407337.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:59:14.110086Z","iopub.execute_input":"2023-11-02T18:59:14.110973Z","iopub.status.idle":"2023-11-02T18:59:14.743287Z","shell.execute_reply.started":"2023-11-02T18:59:14.110935Z","shell.execute_reply":"2023-11-02T18:59:14.742505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAIN_MODEL = load_model(\"/kaggle/input/cnn-lstm-under-represented-classes/CNN_LSTM_UNDER_represented_classes_1697407337.h5\")\nMAIN_MODEL.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:59:50.97876Z","iopub.execute_input":"2023-11-02T18:59:50.979395Z","iopub.status.idle":"2023-11-02T18:59:51.636205Z","shell.execute_reply.started":"2023-11-02T18:59:50.979363Z","shell.execute_reply":"2023-11-02T18:59:51.6353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****INFERENCE/PREDICTIONS****","metadata":{}},{"cell_type":"code","source":"# 624 concatenar con un 0 y hacer un reshape de 0, 25, 25","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:58:34.93858Z","iopub.status.idle":"2023-11-02T18:58:34.93891Z","shell.execute_reply.started":"2023-11-02T18:58:34.938752Z","shell.execute_reply":"2023-11-02T18:58:34.938767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMAGES_PROCESSED = np.load('X_data.npy')\nTEST_IMAGES_PROCESSED\nprint(\"Len: \", len(TEST_IMAGES_PROCESSED))\nprint(\"Shape: \", TEST_IMAGES_PROCESSED.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:59:55.670842Z","iopub.execute_input":"2023-11-02T18:59:55.671225Z","iopub.status.idle":"2023-11-02T18:59:55.677833Z","shell.execute_reply.started":"2023-11-02T18:59:55.671192Z","shell.execute_reply":"2023-11-02T18:59:55.676944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = TEST_IMAGES_PROCESSED","metadata":{"execution":{"iopub.status.busy":"2023-11-02T18:59:57.039064Z","iopub.execute_input":"2023-11-02T18:59:57.039987Z","iopub.status.idle":"2023-11-02T18:59:57.043855Z","shell.execute_reply.started":"2023-11-02T18:59:57.039952Z","shell.execute_reply":"2023-11-02T18:59:57.042842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data_ = []\nfor estudio in X_data:\n    estudio_cortes = []\n    for core in estudio:\n        placeholder = np.zeros((625,))\n        placeholder[:624] = estudio[0]\n        estudio_cortes.append(np.reshape(placeholder,(25,25)))\n    X_data_.append(np.array(estudio_cortes))\nX_data_ = np.array(X_data_)\nX_data_.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:00:41.141026Z","iopub.execute_input":"2023-11-02T19:00:41.141439Z","iopub.status.idle":"2023-11-02T19:00:41.152404Z","shell.execute_reply.started":"2023-11-02T19:00:41.141406Z","shell.execute_reply":"2023-11-02T19:00:41.151438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in TEST_IMAGES_PROCESSED[0]:\n    print(np.isnan(i))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:00:59.46062Z","iopub.execute_input":"2023-11-02T19:00:59.460981Z","iopub.status.idle":"2023-11-02T19:00:59.587831Z","shell.execute_reply.started":"2023-11-02T19:00:59.460951Z","shell.execute_reply":"2023-11-02T19:00:59.5868Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PREDICTIONS = MAIN_MODEL.predict(X_data_)\nprint(\"Len: \", len(PREDICTIONS))\nprint(\"Shape: \", PREDICTIONS.shape)\nPREDICTIONS","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:01:25.968594Z","iopub.execute_input":"2023-11-02T19:01:25.969276Z","iopub.status.idle":"2023-11-02T19:01:26.301354Z","shell.execute_reply.started":"2023-11-02T19:01:25.969242Z","shell.execute_reply":"2023-11-02T19:01:26.300443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PREDICTIONS[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:01:30.571058Z","iopub.execute_input":"2023-11-02T19:01:30.572027Z","iopub.status.idle":"2023-11-02T19:01:30.579285Z","shell.execute_reply.started":"2023-11-02T19:01:30.571983Z","shell.execute_reply":"2023-11-02T19:01:30.578175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = list(PREDICTIONS[0]) \npredictions","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:01:33.748135Z","iopub.execute_input":"2023-11-02T19:01:33.748536Z","iopub.status.idle":"2023-11-02T19:01:33.754662Z","shell.execute_reply.started":"2023-11-02T19:01:33.748505Z","shell.execute_reply":"2023-11-02T19:01:33.753785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in predictions:\n    if not np.nan is i:\n        print(i)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:01:36.986346Z","iopub.execute_input":"2023-11-02T19:01:36.987109Z","iopub.status.idle":"2023-11-02T19:01:36.992075Z","shell.execute_reply.started":"2023-11-02T19:01:36.987074Z","shell.execute_reply":"2023-11-02T19:01:36.991229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAIN_MODEL.predict()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****SUBMISSION****","metadata":{}},{"cell_type":"code","source":"\ndf_submission = pd.DataFrame({\"patient_id\": patient_ids})\ndf_submission[TARGET_COLS] = PREDICTIONS.astype(\"float32\")\n\nsub_df = pd.read_csv(f\"{BASE_PATH}/sample_submission.csv\")\nsub_df = sub_df[[\"patient_id\"]]\nsub_df = sub_df.merge(df_submission, on=\"patient_id\", how=\"left\")\n\n# Store submission\nsub_df.to_csv(\"submission.csv\",index=False)\nsub_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:01:40.159561Z","iopub.execute_input":"2023-11-02T19:01:40.159915Z","iopub.status.idle":"2023-11-02T19:01:40.203044Z","shell.execute_reply.started":"2023-11-02T19:01:40.159882Z","shell.execute_reply":"2023-11-02T19:01:40.201823Z"},"trusted":true},"execution_count":null,"outputs":[]}]}