{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"},{"sourceId":9528888,"sourceType":"datasetVersion","datasetId":5788739},{"sourceId":9553895,"sourceType":"datasetVersion","datasetId":5821406},{"sourceId":9577712,"sourceType":"datasetVersion","datasetId":5839069}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nfrom PIL import Image\nimport pydicom\nimport cv2\nimport torch.nn as nn\nimport pandas as pd\nimport torch\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom torchvision import transforms\nfrom transformers import AutoModel\nimport torchvision.models as models\nimport requests\nfrom tensorflow.keras.models import load_model\nimport kagglehub","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-10T17:27:27.021314Z","iopub.execute_input":"2024-10-10T17:27:27.021678Z","iopub.status.idle":"2024-10-10T17:27:46.561118Z","shell.execute_reply.started":"2024-10-10T17:27:27.021641Z","shell.execute_reply":"2024-10-10T17:27:46.560195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Downloading Model**","metadata":{}},{"cell_type":"code","source":"kagglehub.dataset_download('ahmedaabdullah/medicalnet-attention-layers-for-rsna', path='AT2_attention_model_hist.pth')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:27:46.563045Z","iopub.execute_input":"2024-10-10T17:27:46.563690Z","iopub.status.idle":"2024-10-10T17:27:47.233401Z","shell.execute_reply.started":"2024-10-10T17:27:46.563651Z","shell.execute_reply":"2024-10-10T17:27:47.232073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.dataset_download('ahmedaabdullah/medicalnet-attention-layers-for-rsna', path='ST1_attention_model_gsl.pth')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:27:47.234840Z","iopub.execute_input":"2024-10-10T17:27:47.235186Z","iopub.status.idle":"2024-10-10T17:27:47.905898Z","shell.execute_reply.started":"2024-10-10T17:27:47.235138Z","shell.execute_reply":"2024-10-10T17:27:47.904856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.dataset_download('ahmedaabdullah/medicalnet-attention-layers-for-rsna', path='ST2_attention_model_hist.pth')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:27:47.908721Z","iopub.execute_input":"2024-10-10T17:27:47.909462Z","iopub.status.idle":"2024-10-10T17:27:48.572954Z","shell.execute_reply.started":"2024-10-10T17:27:47.909421Z","shell.execute_reply":"2024-10-10T17:27:48.571922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet50_AT2 = models.resnet50(pretrained=False)\nresnet50_ST1 = models.resnet50(pretrained=False)\nresnet50_ST2 = models.resnet50(pretrained=False)\n\nweights_path_AT2 = '/kaggle/input/medicalnet-attention-layers-for-rsna/AT2_attention_model_hist.pth'\nweights_path_ST1 = '/kaggle/input/medicalnet-attention-layers-for-rsna/ST1_attention_model_gsl.pth'\nweights_path_ST2 = '/kaggle/input/medicalnet-attention-layers-for-rsna/ST2_attention_model_hist.pth'\n\ntry:\n    resnet50_AT2.load_state_dict(torch.load(weights_path_AT2), strict=False)\n    resnet50_ST1.load_state_dict(torch.load(weights_path_ST1), strict=False)\n    resnet50_ST2.load_state_dict(torch.load(weights_path_ST2), strict=False)\n    \n    print(\"ResNet-152 model loaded with custom weights (missing keys ignored).\")\nexcept RuntimeError as e:\n    print(f\"Failed to load weights: {e}\")","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:27:48.574245Z","iopub.execute_input":"2024-10-10T17:27:48.574593Z","iopub.status.idle":"2024-10-10T17:28:00.448189Z","shell.execute_reply.started":"2024-10-10T17:27:48.574559Z","shell.execute_reply":"2024-10-10T17:28:00.447202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Greyscale 256x256**","metadata":{}},{"cell_type":"code","source":"# Set a limit for the size of individual image files or whatever you were using before.\nBATCH_SIZE_LIMIT = 3 * (1024 ** 3)  # 3GB\n\ndef get_image_size_in_bytes(img_array):\n    return img_array.nbytes  # Using nbytes to get the precise size of the numpy array in bytes\n\ndef process_dicom_to_numpy(dicom_path, target_size=(256, 256)):\n    # Read the DICOM file\n    dicom_data = pydicom.dcmread(dicom_path)\n    # Get the pixel data\n    image_array = dicom_data.pixel_array\n    \n    # Convert to PIL image for resizing\n    img = Image.fromarray(image_array)\n    if img.mode not in ['RGB', 'L', 'RGBA']:\n        img = img.convert('L')\n    # Resize the image\n    img_resized = img.resize(target_size, Image.LANCZOS)\n    \n    # Convert resized image back to numpy array\n    img_np = np.array(img_resized)\n    \n    return img_np\n\ndef save_image_as_npy(image_data, target_path):\n    # Ensure the directory exists\n    os.makedirs(os.path.dirname(target_path), exist_ok=True)\n    \n    # Save the image data as a .npy file\n    np.save(target_path, image_data)\n\ndef process_directory(source_dir, target_dir, target_size=(256, 256)):\n    for root, dirs, files in os.walk(source_dir):\n        for file in files:\n            if file.endswith(\".dcm\"):\n                dicom_path = os.path.join(root, file)\n                \n                # Extract the relative path to create the target directory structure\n                relative_path = os.path.relpath(dicom_path, source_dir)\n                \n                # Replace the .dcm extension with .npy\n                npy_file_name = os.path.splitext(relative_path)[0] + '.npy'\n                \n                # Define the target path\n                target_path = os.path.join(target_dir, npy_file_name)\n                \n                # Process the DICOM file into a numpy array\n                img_np = process_dicom_to_numpy(dicom_path, target_size)\n                \n                # Save the individual image as .npy\n                save_image_as_npy(img_np, target_path)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.449318Z","iopub.execute_input":"2024-10-10T17:28:00.449681Z","iopub.status.idle":"2024-10-10T17:28:00.461811Z","shell.execute_reply.started":"2024-10-10T17:28:00.449644Z","shell.execute_reply":"2024-10-10T17:28:00.460720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Histogram Equalization**","metadata":{}},{"cell_type":"code","source":"def apply_histogram_equalization(image_data):\n    # Apply histogram equalization\n    equalized_image = cv2.equalizeHist(image_data)\n    return equalized_image\n\ndef save_image_as_npy_hist(image_data, target_path):\n    # Ensure the directory exists\n    os.makedirs(os.path.dirname(target_path), exist_ok=True)\n    \n    # Save the image data as a .npy file\n    np.save(target_path, image_data)\n    \ndef process_directory_hist(source_dir, target_dir):\n    for root, dirs, files in os.walk(source_dir):\n        for file in files:\n            if file.endswith(\".npy\"):\n                npy_path = os.path.join(root, file)\n\n                # Load the existing .npy image\n                img_np = np.load(npy_path)\n\n                # Check if the image is 256x256\n                if img_np.shape == (256, 256):\n                    # Apply histogram equalization\n                    img_equalized = apply_histogram_equalization(img_np)\n\n                    # Define the relative path from the source directory\n                    relative_path = os.path.relpath(npy_path, source_dir)\n                    \n                    # Create the target path by joining the target directory with the relative path\n                    target_path = os.path.join(target_dir, relative_path)\n\n                    # Save the equalized image as .npy\n                    save_image_as_npy_hist(img_equalized, target_path)\n                else:\n                    print(f\"Image {npy_path} is not 256x256. Skipping.\")","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.463429Z","iopub.execute_input":"2024-10-10T17:28:00.463823Z","iopub.status.idle":"2024-10-10T17:28:00.474638Z","shell.execute_reply.started":"2024-10-10T17:28:00.463772Z","shell.execute_reply":"2024-10-10T17:28:00.473735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Code for attention embeddings","metadata":{}},{"cell_type":"code","source":"class MRIEmbeddingModel(torch.nn.Module):\n    def __init__(self, base_model, embedding_dim):\n        super(MRIEmbeddingModel, self).__init__()\n        self.base_model = base_model\n        self.attention_layer = torch.nn.Linear(embedding_dim, 1) \n        self.embedding_dim = embedding_dim\n\n    def forward(self, x):\n        attention_weights = self.attention_layer(x)\n        final_embedding = torch.sum(x * attention_weights, dim=1)\n        return final_embedding, attention_weights","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.475814Z","iopub.execute_input":"2024-10-10T17:28:00.476170Z","iopub.status.idle":"2024-10-10T17:28:00.487416Z","shell.execute_reply.started":"2024-10-10T17:28:00.476104Z","shell.execute_reply":"2024-10-10T17:28:00.486361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom torchvision import transforms\n\ndef attention_embeddings(model, df, img_path, result_path_csv, result_path_pth):\n    # Modify the final layer to output 512-dimensional embeddings\n    model.fc = torch.nn.Linear(model.fc.in_features, 512)\n    embedding_model = MRIEmbeddingModel(model, embedding_dim=512)\n\n    # Move models to GPU\n    model = model.to('cuda')\n    embedding_model = embedding_model.to('cuda')\n\n    results = []\n    for index, row in tqdm(df.iterrows()):\n        patient_id = str(row['study_id'])\n        series_id = str(row['series_id'])\n\n        series_path = os.path.join(img_path, patient_id, series_id)\n\n        # Check if the series path exists\n        embeddings = []\n        if os.path.exists(series_path):\n            for slice_file in os.listdir(series_path):\n                if slice_file.endswith('.npy'):\n                    slice_path = os.path.join(series_path, slice_file)\n                    slice_data = np.load(slice_path)\n\n                    # Ensure the input tensor is in the correct format\n                    if slice_data.ndim == 2:\n                        slice_data = np.stack([slice_data] * 3, axis=0)\n                    elif slice_data.ndim == 3 and slice_data.shape[0] == 1:\n                        slice_data = np.repeat(slice_data, 3, axis=0)\n\n                    # Prepare tensor for model input\n                    input_tensor = torch.from_numpy(slice_data).float().to('cuda')\n                    input_tensor = transforms.Resize((224, 224))(input_tensor)\n                    input_tensor = (input_tensor - torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1).to('cuda')) / \\\n                                   torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1).to('cuda')\n                    input_tensor = input_tensor.unsqueeze(0)\n\n                    # Get embedding from the model\n                    with torch.no_grad():\n                        embedding = model(input_tensor)\n                        embeddings.append(embedding)\n\n            if embeddings:\n                slice_embeddings = torch.stack(embeddings, dim=1).to('cuda')\n                with torch.no_grad():\n                    final_embedding, attention_weights = embedding_model(slice_embeddings)\n                    final_embedding = final_embedding.squeeze().cpu()\n            else:\n                # If no embeddings are found, set to zero\n                final_embedding = torch.zeros(512)\n\n        else:\n            # If series_path does not exist, return a zero tensor for the embeddings\n            final_embedding = torch.zeros(512)\n\n        # Convert the final embedding into a dictionary format\n        embedding_dict = {f'{i}': final_embedding[i].item() for i in range(final_embedding.shape[0])}\n        embedding_dict.update({'study_id': patient_id, 'series_id': series_id})\n        results.append(embedding_dict)\n\n    # Save the results to a CSV file\n    results_df = pd.DataFrame(results)\n    results_df.to_csv(result_path_csv, index=False)\n\n    # Save the embedding model state\n    torch.save(embedding_model.state_dict(), result_path_pth)\n    print(f\"Embeddings with attention completed and saved to {result_path_csv}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.488916Z","iopub.execute_input":"2024-10-10T17:28:00.489329Z","iopub.status.idle":"2024-10-10T17:28:00.508915Z","shell.execute_reply.started":"2024-10-10T17:28:00.489292Z","shell.execute_reply":"2024-10-10T17:28:00.507857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_embeddings(csv_path,npy_dir,hist_dir):\n    df = pd.read_csv(csv_path)\n    #attention_embeddings(df[df['series_description'] == 'Axial T2'], npy_dir, 'AT2_attention_embeddings_gsl.csv', 'AT2_attention_model_gsl.pth')\n    attention_embeddings(resnet50_AT2, df[df['series_description'] == 'Axial T2'], hist_dir, 'AT2_attention_embeddings_hist.csv', 'AT2_attention_model_hist.pth')\n    attention_embeddings(resnet50_ST1, df[df['series_description'] == 'Sagittal T1'], npy_dir, 'ST1_attention_embeddings_gsl.csv', 'ST1_attention_model_gsl.pth')\n    #attention_embeddings(df[df['series_description'] == 'Sagittal T1'], hist_dir, 'ST1_attention_embeddings_hist.csv', 'ST1_attention_model_hist.pth')\n    #attention_embeddings(df[df['series_description'] == 'Sagittal T2/STIR'], npy_dir, 'ST2_attention_embeddings_gsl.csv', 'ST2_attention_model_gsl.pth')\n    attention_embeddings(resnet50_ST2,df[df['series_description'] == 'Sagittal T2/STIR'], hist_dir, 'ST2_attention_embeddings_hist.csv', 'ST2_attention_model_hist.pth')\n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.513134Z","iopub.execute_input":"2024-10-10T17:28:00.513560Z","iopub.status.idle":"2024-10-10T17:28:00.520739Z","shell.execute_reply.started":"2024-10-10T17:28:00.513506Z","shell.execute_reply":"2024-10-10T17:28:00.519739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Conditions list","metadata":{}},{"cell_type":"code","source":"AT2_conditions = ['left_neural_foraminal_narrowing_l1_l2',\n       'left_neural_foraminal_narrowing_l2_l3',\n       'left_neural_foraminal_narrowing_l3_l4',\n       'left_neural_foraminal_narrowing_l4_l5',\n       'left_neural_foraminal_narrowing_l5_s1',\n       'right_neural_foraminal_narrowing_l1_l2',\n       'right_neural_foraminal_narrowing_l2_l3',\n       'right_neural_foraminal_narrowing_l3_l4',\n       'right_neural_foraminal_narrowing_l4_l5',\n       'right_neural_foraminal_narrowing_l5_s1']\n\nST1_conditions = ['left_subarticular_stenosis_l1_l2', 'left_subarticular_stenosis_l2_l3',\n       'left_subarticular_stenosis_l3_l4', 'left_subarticular_stenosis_l4_l5',\n       'left_subarticular_stenosis_l5_s1', 'right_subarticular_stenosis_l1_l2',\n       'right_subarticular_stenosis_l2_l3',\n       'right_subarticular_stenosis_l3_l4',\n       'right_subarticular_stenosis_l4_l5',\n       'right_subarticular_stenosis_l5_s1']\n\nST2_conditions = ['spinal_canal_stenosis_l1_l2', 'spinal_canal_stenosis_l2_l3',\n       'spinal_canal_stenosis_l3_l4', 'spinal_canal_stenosis_l4_l5',\n       'spinal_canal_stenosis_l5_s1']","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.522191Z","iopub.execute_input":"2024-10-10T17:28:00.522911Z","iopub.status.idle":"2024-10-10T17:28:00.532631Z","shell.execute_reply.started":"2024-10-10T17:28:00.522862Z","shell.execute_reply":"2024-10-10T17:28:00.531679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_embeddings(path):\n    df = pd.read_csv(path) \n    return df\ndef create_prediction_dataframe(model, df, conditions):\n    embeddings = df[df.columns[:512]]\n    study_ids = df[df.columns[512]]\n    predictions = model.predict(embeddings)\n    \n    # Initialize a list to store final DataFrame rows\n    all_rows = []\n    # Split predictions into sets of 3 (normal_mild, moderate, severe) for each condition\n    num_conditions = len(conditions)\n    \n    if predictions.shape[1] != num_conditions * 3:\n        raise ValueError(\"The number of predicted values doesn't match the number of expected conditions.\")\n    \n    # Loop through each study_id and the corresponding predictions\n    for i, study_id in enumerate(study_ids):\n        study_predictions = predictions[i]\n        condition_predictions = np.split(study_predictions, num_conditions)\n        \n        # Loop through each condition and add the results to all_rows\n        for condition, pred in zip(conditions, condition_predictions):\n            row_id = f\"{study_id}_{condition}\"  # Create the row_id by concatenating study_id and condition\n            all_rows.append([row_id, pred[0], pred[1], pred[2]])  # Append the row_id and the predictions\n    \n    # Convert the list to a DataFrame\n    df = pd.DataFrame(all_rows, columns=['row_id', 'normal_mild', 'moderate', 'severe'])\n    \n    # Return the DataFrame or save to a CSV file\n    return df","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.533905Z","iopub.execute_input":"2024-10-10T17:28:00.534262Z","iopub.status.idle":"2024-10-10T17:28:00.544880Z","shell.execute_reply.started":"2024-10-10T17:28:00.534225Z","shell.execute_reply":"2024-10-10T17:28:00.543689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Full pipeline","metadata":{}},{"cell_type":"code","source":"kagglehub.dataset_download('ibtehajali1/models', path='AT2 - HIST - Attention Network_best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:00.546451Z","iopub.execute_input":"2024-10-10T17:28:00.546864Z","iopub.status.idle":"2024-10-10T17:28:01.149915Z","shell.execute_reply.started":"2024-10-10T17:28:00.546818Z","shell.execute_reply":"2024-10-10T17:28:01.148899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.dataset_download('ibtehajali1/models', path='ST1 - GSL - Attention Network_best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:01.151107Z","iopub.execute_input":"2024-10-10T17:28:01.151449Z","iopub.status.idle":"2024-10-10T17:28:01.980055Z","shell.execute_reply.started":"2024-10-10T17:28:01.151407Z","shell.execute_reply":"2024-10-10T17:28:01.979212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.dataset_download('ibtehajali1/models', path='ST2 - HIST - Attention Network_best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:01.981437Z","iopub.execute_input":"2024-10-10T17:28:01.981812Z","iopub.status.idle":"2024-10-10T17:28:02.710168Z","shell.execute_reply.started":"2024-10-10T17:28:01.981776Z","shell.execute_reply":"2024-10-10T17:28:02.709057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def full_pipeline(dcm_dir):\n    # Step 1: Process DICOM to NPY\n    npy_dir = '/kaggle/working/grey_scale_train'\n    process_directory(dcm_dir, npy_dir)\n    \n    # Step 2: NPY Histogram Equalization\n    hist_dir = '/kaggle/working/hist_norm_train'\n    process_directory_hist(npy_dir,hist_dir)\n    \n    # Step 3: Load csv_data\n    csv_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_series_descriptions.csv'\n    \n    # Step 4: Generate Embeddings\n    generate_embeddings(csv_path,npy_dir,hist_dir)\n    \n    # Step 5: Load Embeddings\n    AT2 = load_embeddings('/kaggle/working/AT2_attention_embeddings_hist.csv')\n    ST1 = load_embeddings('/kaggle/working/ST1_attention_embeddings_gsl.csv')\n    ST2 = load_embeddings('/kaggle/working/ST2_attention_embeddings_hist.csv')\n\n    # Step 6: Load Models\n    model1 = load_model('/kaggle/input/models/AT2 - HIST - Attention Network_best_model.h5',compile=False)\n    model2 = load_model('/kaggle/input/models/ST1 - GSL - Attention Network_best_model.h5',compile=False)\n    model3 = load_model('/kaggle/input/models/ST2 - HIST - Attention Network_best_model.h5',compile=False)\n    \n    # Step 7: Generate predictions\n    AT2_predictions = create_prediction_dataframe(model1,AT2,AT2_conditions)\n    ST1_predictions = create_prediction_dataframe(model2,ST1,ST1_conditions)\n    ST2_predictions = create_prediction_dataframe(model3,ST2,ST2_conditions)\n    \n    # Step 8: return submissions dataframe\n    submission = pd.concat([AT2_predictions, ST1_predictions, ST2_predictions], axis=0, ignore_index=True)\n    \n    return submission\n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:02.711433Z","iopub.execute_input":"2024-10-10T17:28:02.711773Z","iopub.status.idle":"2024-10-10T17:28:02.720921Z","shell.execute_reply.started":"2024-10-10T17:28:02.711737Z","shell.execute_reply":"2024-10-10T17:28:02.719687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.dataset_download('ahmedembedded/preprocessed-dataset', path='unseen_unheard/unseen_unheard_data_complete.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:02.722568Z","iopub.execute_input":"2024-10-10T17:28:02.723025Z","iopub.status.idle":"2024-10-10T17:28:03.480110Z","shell.execute_reply.started":"2024-10-10T17:28:02.722977Z","shell.execute_reply":"2024-10-10T17:28:03.479020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.competition_download('rsna-2024-lumbar-spine-degenerative-classification', path='test_series_descriptions.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:03.481477Z","iopub.execute_input":"2024-10-10T17:28:03.481825Z","iopub.status.idle":"2024-10-10T17:28:04.048479Z","shell.execute_reply.started":"2024-10-10T17:28:03.481788Z","shell.execute_reply":"2024-10-10T17:28:04.047511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = full_pipeline('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_series_descriptions.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:43:32.251710Z","iopub.execute_input":"2024-10-10T17:43:32.252643Z","iopub.status.idle":"2024-10-10T17:43:32.583821Z","shell.execute_reply.started":"2024-10-10T17:43:32.252597Z","shell.execute_reply":"2024-10-10T17:43:32.582729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### saving submission file","metadata":{}},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:37:31.959845Z","iopub.execute_input":"2024-10-10T17:37:31.960290Z","iopub.status.idle":"2024-10-10T17:37:31.978206Z","shell.execute_reply.started":"2024-10-10T17:37:31.960248Z","shell.execute_reply":"2024-10-10T17:37:31.977020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r /kaggle/working/*","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:06.957277Z","iopub.execute_input":"2024-10-10T17:28:06.957599Z","iopub.status.idle":"2024-10-10T17:28:08.191174Z","shell.execute_reply.started":"2024-10-10T17:28:06.957564Z","shell.execute_reply":"2024-10-10T17:28:08.189765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kagglehub.competition_download('rsna-2024-lumbar-spine-degenerative-classification', path='sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:08.192898Z","iopub.execute_input":"2024-10-10T17:28:08.193295Z","iopub.status.idle":"2024-10-10T17:28:08.981030Z","shell.execute_reply.started":"2024-10-10T17:28:08.193256Z","shell.execute_reply":"2024-10-10T17:28:08.979972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/sample_submission.csv')\nsubmission.set_index('row_id', inplace=True)\nsubmission = submission.reindex(df['row_id']).reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:08.982534Z","iopub.execute_input":"2024-10-10T17:28:08.982979Z","iopub.status.idle":"2024-10-10T17:28:08.999344Z","shell.execute_reply.started":"2024-10-10T17:28:08.982930Z","shell.execute_reply":"2024-10-10T17:28:08.998440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#saving the sumbission file\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:28:09.000530Z","iopub.execute_input":"2024-10-10T17:28:09.000877Z","iopub.status.idle":"2024-10-10T17:28:09.007728Z","shell.execute_reply.started":"2024-10-10T17:28:09.000839Z","shell.execute_reply":"2024-10-10T17:28:09.006619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run these two to verify the results","metadata":{}},{"cell_type":"code","source":"!cp -r /kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_images /kaggle/working/\n!cp -r /kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/100206310 /kaggle/working/test_images\n!rm -r /kaggle/working/test_images/100206310/1792451510","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:42:44.461445Z","iopub.execute_input":"2024-10-10T17:42:44.462192Z","iopub.status.idle":"2024-10-10T17:42:48.958712Z","shell.execute_reply.started":"2024-10-10T17:42:44.462153Z","shell.execute_reply":"2024-10-10T17:42:48.957495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%file test_series_descriptions.csv\nstudy_id,series_id,series_description\n44036939,2828203845,Sagittal T1\n44036939,3481971518,Axial T2\n44036939,3844393089,Sagittal T2/STIR\n100206310,1012284084,Sagittal T1\n100206310,2092806862,Axial T2","metadata":{"execution":{"iopub.status.busy":"2024-10-10T17:42:48.961018Z","iopub.execute_input":"2024-10-10T17:42:48.961894Z","iopub.status.idle":"2024-10-10T17:42:48.968959Z","shell.execute_reply.started":"2024-10-10T17:42:48.961835Z","shell.execute_reply":"2024-10-10T17:42:48.967920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = full_pipeline('/kaggle/working/test_series_descriptions.csv')\nsubmission","metadata":{},"execution_count":null,"outputs":[]}]}