{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30823,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision import transforms\nimport pydicom as dicom\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T07:45:02.775363Z","iopub.execute_input":"2025-01-08T07:45:02.775663Z","iopub.status.idle":"2025-01-08T07:45:04.170574Z","shell.execute_reply.started":"2025-01-08T07:45:02.775641Z","shell.execute_reply":"2025-01-08T07:45:04.169713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load training and test datasets\ntrain_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/'\ntrain = pd.read_csv(train_path + 'train.csv')\nlabel = pd.read_csv(train_path + 'train_label_coordinates.csv')\ntest_desc = pd.read_csv(train_path + 'test_series_descriptions.csv')\nsample_submission = pd.read_csv(train_path + 'sample_submission.csv')\n\nprint(train.head())\nprint(label.head())\nprint(test_desc.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T07:45:10.839064Z","iopub.execute_input":"2025-01-08T07:45:10.839798Z","iopub.status.idle":"2025-01-08T07:45:10.999154Z","shell.execute_reply.started":"2025-01-08T07:45:10.839753Z","shell.execute_reply":"2025-01-08T07:45:10.998465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pydicom as dicom\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Function to process DICOM images\ndef preprocess_dicom(path):\n    dicom_file = dicom.dcmread(path)\n    pixel_array = dicom_file.pixel_array\n    pixel_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array))\n    pixel_array = (pixel_array * 255).astype(np.uint8)\n    return pixel_array\n\n# Function to recursively find all DICOM files in a directory\ndef find_dicom_files(base_path):\n    dicom_files = []\n    for root, dirs, files in os.walk(base_path):\n        for file in files:\n            if file.endswith('.dcm'):\n                dicom_files.append(os.path.join(root, file))\n    return dicom_files\n\n# Base directory containing DICOM files\nbase_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\n\n# Finding all DICOM files\ndicom_paths = find_dicom_files(base_dir)\n\n# Example: Preprocessing the first image if DICOM files are found\nif dicom_paths:\n    print(f\"Found {len(dicom_paths)} DICOM files.\")\n    dicom_path = dicom_paths[0]  # Use the first DICOM file for demonstration\n    processed_image = preprocess_dicom(dicom_path)\n    plt.imshow(processed_image, cmap='gray')\n    plt.title('Example Preprocessed Image')\n    plt.show()\nelse:\n    print(\"No DICOM files found.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T07:45:14.909249Z","iopub.execute_input":"2025-01-08T07:45:14.909615Z","iopub.status.idle":"2025-01-08T07:47:14.680731Z","shell.execute_reply.started":"2025-01-08T07:45:14.909587Z","shell.execute_reply":"2025-01-08T07:47:14.67997Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\n\n# train_images klasörünün yolu\ntrain_images_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\n\n# Koordinatları oluşturacağımız yeni liste\ndata = []\n\n# train_images klasöründeki tüm study_id'ler üzerinde işlem yapalım\nfor study_id in os.listdir(train_images_dir):\n    study_path = os.path.join(train_images_dir, study_id)\n    \n    if os.path.isdir(study_path):  # Eğer study_id bir dizinse\n        for series_id in os.listdir(study_path):\n            series_path = os.path.join(study_path, series_id)\n            \n            if os.path.isdir(series_path):  # Eğer series_id bir dizinse\n                # Her bir (study_id, series_id) çifti için etiket/koordinat verisi alın\n                # Burada, koordinat veya etiketleri çıkartmanız gerekiyor. \n                # Örneğin, etiketler veya koordinatlar dosyadan veya başka bir kaynaktan alınabilir.\n\n                # Bu örnekte, train_label_coordinates.csv dosyasındaki verilere göre etiketleri ve koordinatları çıkaracağız\n                # Önce CSV dosyasını okuyoruz\n                labels_df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv')\n\n                # study_id ve series_id ile ilişkili etiket/koordinatları alıyoruz\n                matching_labels = labels_df[(labels_df['study_id'] == int(study_id)) & \n                                            (labels_df['series_id'] == int(series_id))]\n\n                # Her bir eşleşen etiket ve koordinat için işlemleri yapalım\n                for _, row in matching_labels.iterrows():\n                    label = row['condition']\n                    coord_x = row['x']\n                    coord_y = row['y']\n                    level = row['level']\n                    \n                    # Her bir (study_id, series_id) ve level için bilgileri data listesine ekleyelim\n                    data.append({\n                        'study_id': study_id,\n                        'series_id': series_id,\n                        'level': level,\n                        'label': label,\n                        'coord_x': coord_x,\n                        'coord_y': coord_y\n                    })\n\n# Yeni DataFrame oluşturma\ndf_coordinates = pd.DataFrame(data)\n\n# Çıktıyı /kaggle/working dizinine kaydedelim\noutput_csv_path = \"/kaggle/working/train_label_coordinates.csv\"\ndf_coordinates.to_csv(output_csv_path, index=False)\n\nprint(f\"Yeni train_label_coordinates.csv dosyası oluşturuldu: {output_csv_path}\")\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T07:47:43.518796Z","iopub.execute_input":"2025-01-08T07:47:43.519123Z","iopub.status.idle":"2025-01-08T07:53:37.74763Z","shell.execute_reply.started":"2025-01-08T07:47:43.519094Z","shell.execute_reply":"2025-01-08T07:53:37.746843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\n\n# CSV dosyalarını yükleyin\ntrain_df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\nlabel_coordinates_df = pd.read_csv('/kaggle/working/train_label_coordinates.csv')\n\n# Etiketlerin ve koordinatların her iki dosyada da doğru şekilde eşleştiğinden emin olun\n# Her study_id'ye ait etiketler\nlabels = train_df.set_index('study_id')\n\n# Koordinatlar dosyasını da study_id ve series_id'ye göre indeksleyelim\ncoordinates = label_coordinates_df.set_index(['study_id', 'series_id'])\n\n# train_images dizinini gezin\nimage_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\n\n# Yeni etiketler için bir dictionary oluşturun\nimage_labels = {}\n\n# Dosya yapısını gezerek her bir dosya için etiketleri ekleyin\nfor study_id in os.listdir(image_dir):\n    study_path = os.path.join(image_dir, study_id)\n    \n    if os.path.isdir(study_path):  # study_id bir dizinse\n        for series_id in os.listdir(study_path):\n            series_path = os.path.join(study_path, series_id)\n            \n            if os.path.isdir(series_path):  # series_id bir dizinse\n                # Etiketleri alın (doğru etiket sütunu ile)\n                study_row = labels.loc[int(study_id)]  # study_id'ye göre satırı al\n                label = study_row['spinal_canal_stenosis_l1_l2']  # Örnek: 'spinal_canal_stenosis_l1_l2' etiketini al\n                \n                try:\n                    # Koordinatları da alalım\n                    coord = coordinates.loc[(int(study_id), int(series_id))]  # (study_id, series_id) ikilisine göre koordinatlar\n                    # Koordinat ve etiket bilgilerini dosyalarla ilişkilendir\n                    image_labels[series_path] = {\n                        'label': label,\n                        'coordinates': coord[['coord_x', 'coord_y']].values.tolist()  # 'coord_x' ve 'coord_y' sütunlarını kullanın\n                    }\n                except KeyError:\n                    # Eğer belirtilen study_id, series_id kombinasyonu bulunmazsa atla\n                    print(f\"Warning: Koordinatlar bulunamadı (study_id: {study_id}, series_id: {series_id})\")\n                    continue\n\n# image_labels dictionary'si, her image_path için etiket ve koordinatları içeriyor\nprint(image_labels)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-07T22:49:04.885654Z","iopub.execute_input":"2025-01-07T22:49:04.885971Z","iopub.status.idle":"2025-01-07T22:49:16.861366Z","shell.execute_reply.started":"2025-01-07T22:49:04.885947Z","shell.execute_reply":"2025-01-07T22:49:16.860448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(labels_df.columns)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T07:54:34.23817Z","iopub.execute_input":"2025-01-08T07:54:34.238533Z","iopub.status.idle":"2025-01-08T07:54:34.243237Z","shell.execute_reply.started":"2025-01-08T07:54:34.238504Z","shell.execute_reply":"2025-01-08T07:54:34.242359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(labels_df.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T07:54:27.800925Z","iopub.execute_input":"2025-01-08T07:54:27.8012Z","iopub.status.idle":"2025-01-08T07:54:27.807692Z","shell.execute_reply.started":"2025-01-08T07:54:27.801181Z","shell.execute_reply":"2025-01-08T07:54:27.806712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nprint(\"GPU Available:\", torch.cuda.is_available())\nprint(\"Current Device:\", torch.cuda.get_device_name(0) if torch.cuda.is_available() else \"CPU\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T08:00:23.632208Z","iopub.execute_input":"2025-01-08T08:00:23.632531Z","iopub.status.idle":"2025-01-08T08:00:23.660976Z","shell.execute_reply.started":"2025-01-08T08:00:23.632507Z","shell.execute_reply":"2025-01-08T08:00:23.66024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nimport numpy as np\nimport os\nimport pydicom as dicom\nimport pandas as pd\nfrom sklearn.metrics import accuracy_score\nimport cv2\n\n# DICOM Görüntülerini İşleme\ndef preprocess_dicom(path):\n    dicom_file = dicom.dcmread(path)\n    pixel_array = dicom_file.pixel_array\n\n    if pixel_array.dtype == np.int16:\n        pixel_array = (pixel_array / 256).astype(np.uint8)\n\n    min_val = np.min(pixel_array)\n    max_val = np.max(pixel_array)\n    if max_val == min_val:\n        pixel_array = np.zeros_like(pixel_array)\n    else:\n        pixel_array = (pixel_array - min_val) / (max_val - min_val)\n        pixel_array = (pixel_array * 255).astype(np.uint8)\n\n    pixel_array = cv2.resize(pixel_array, (28, 28))\n    return pixel_array\n\n\ndef find_dicom_files(base_path):\n    dicom_files = []\n    for root, dirs, files in os.walk(base_path):\n        for file in files:\n            if file.endswith('.dcm'):\n                dicom_files.append(os.path.join(root, file))\n    return dicom_files\n\n# PrimaryCapsLayer\nclass PrimaryCapsLayer(nn.Module):\n    def __init__(self, in_channels, out_caps, kernel_size, stride):\n        super(PrimaryCapsLayer, self).__init__()\n        self.conv = nn.Conv2d(in_channels, out_caps * 8, kernel_size, stride)\n        self.out_caps = out_caps\n\n    def forward(self, x):\n        x = self.conv(x)\n        N, C, H, W = x.shape\n        x = x.view(N, self.out_caps, 8, -1)\n        x = x.permute(0, 3, 1, 2).contiguous()\n        x = x.view(N, -1, 8)\n        return x\n\n# DigitCapsLayer\nclass DigitCapsLayer(nn.Module):\n    def __init__(self, in_caps, out_caps, n_class):\n        super(DigitCapsLayer, self).__init__()\n        self.in_caps = in_caps\n        self.out_caps = out_caps\n        self.n_class = n_class\n        self.capsules = nn.ModuleList(\n            [nn.Linear(in_caps * 8, out_caps) for _ in range(n_class)]\n        )\n\n    def forward(self, x):\n        N, num_capsules, capsule_dim = x.size()\n        x = x.view(N, -1)\n        outputs = [capsule(x) for capsule in self.capsules]\n        outputs = torch.stack(outputs, dim=1)\n        return outputs\n\n# Decoder\nclass Decoder(nn.Module):\n    def __init__(self, in_features, h1, h2, out_features):\n        super(Decoder, self).__init__()\n        self.fc = nn.Sequential(\n            nn.Linear(in_features, h1),\n            nn.ReLU(),\n            nn.Linear(h1, h2),\n            nn.ReLU(),\n            nn.Linear(h2, out_features),\n            nn.Sigmoid()\n        )\n\n    def forward(self, x):\n        x = x.view(x.size(0), -1)\n        x = self.fc(x)\n        return x\n\n# Model\nclass CapsuleNetwork(nn.Module):\n    def __init__(self, n_class=2):\n        super(CapsuleNetwork, self).__init__()\n        self.conv1 = nn.Conv2d(1, 256, kernel_size=9, stride=1, padding=0)\n        self.primary_caps = PrimaryCapsLayer(256, 32, 9, 2)\n        self.digit_caps = DigitCapsLayer(32 * 6 * 6, 16, n_class)\n        self.decoder = Decoder(16 * n_class, 512, 256, 28 * 28)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.primary_caps(x)\n        x = self.digit_caps(x)\n        x = x.view(x.size(0), -1)\n        output = self.decoder(x)\n        return output\n\n# Dataset\nclass DICOMDataset(Dataset):\n    def __init__(self, dicom_paths, labels):\n        self.dicom_paths = dicom_paths\n        self.labels = labels\n\n    def __len__(self):\n        return len(self.dicom_paths)\n\n    def __getitem__(self, idx):\n        dicom_path = self.dicom_paths[idx]\n        image = preprocess_dicom(dicom_path)\n        image = torch.tensor(image, dtype=torch.float32).unsqueeze(0)\n        label = self.labels[idx]\n        return image, torch.tensor(label, dtype=torch.long)\n\n# Cihaz seçimi\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")\n\n# Ana Program\nbase_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\nlabels_df = pd.read_csv('/kaggle/working/train_label_coordinates.csv')\ndicom_paths = find_dicom_files(base_dir)\n\nlabel_map = labels_df.set_index('level')['label'].to_dict()\ndicom_labels = [label_map.get(os.path.basename(path), 0) for path in dicom_paths]\n\n# Veriyi eğitim setine ayırıyoruz (test setini kaldırdık)\ntrain_paths, train_labels = dicom_paths, dicom_labels  # Test seti geçici olarak kaldırıldı\n\n# Dataset ve DataLoader tanımlıyoruz\ntrain_dataset = DICOMDataset(train_paths, train_labels)\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\n\n# Model, optimizer ve loss fonksiyonu\nmodel = CapsuleNetwork(n_class=2).to(device)\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Eğitim döngüsü\nnum_epochs = 10\nfor epoch in range(num_epochs):\n    model.train()\n    running_loss = 0.0\n    all_preds, all_labels = [], []\n    for batch_idx, (images, labels) in enumerate(train_loader):\n        images, labels = images.to(device), labels.to(device)\n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n\n        # Her batch için doğruluk hesaplama\n        _, preds = torch.max(outputs, 1)\n        all_preds.extend(preds.cpu().numpy())\n        all_labels.extend(labels.cpu().numpy())\n\n        # Eğer isterseniz her batch sonrası loss yazdırabilirsiniz\n        if (batch_idx + 1) % 10 == 0:  # Her 10 batch'te bir loss yazdırma\n            print(f\"Batch {batch_idx + 1}/{len(train_loader)} - Loss: {loss.item():.4f}\")\n\n    # Her epoch sonunda sonuçları yazdırma\n    train_acc = accuracy_score(all_labels, all_preds)\n    print(f'Epoch {epoch + 1}/{num_epochs} - Loss: {running_loss / len(train_loader):.4f}, Accuracy: {train_acc:.4f}')\n\n# Modeli kaydetme (Eğitim tamamlandığında)\ntorch.save(model.state_dict(), 'capsule_network.pth')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T08:09:21.734469Z","iopub.execute_input":"2025-01-08T08:09:21.734811Z"}},"outputs":[],"execution_count":null}]}