{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":"none","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"},{"sourceId":6523471,"sourceType":"datasetVersion","datasetId":3771357},{"sourceId":7355410,"sourceType":"datasetVersion","datasetId":4271951}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install monai","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-18T12:37:36.795115Z","iopub.execute_input":"2024-01-18T12:37:36.795918Z","iopub.status.idle":"2024-01-18T12:37:55.30245Z","shell.execute_reply.started":"2024-01-18T12:37:36.795861Z","shell.execute_reply":"2024-01-18T12:37:55.300862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from monai.networks.nets import efficientnet\nimport torch","metadata":{"execution":{"iopub.status.busy":"2024-01-18T12:37:55.30537Z","iopub.execute_input":"2024-01-18T12:37:55.305921Z","iopub.status.idle":"2024-01-18T12:38:54.744517Z","shell.execute_reply.started":"2024-01-18T12:37:55.305883Z","shell.execute_reply":"2024-01-18T12:38:54.743569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 128\nmodel = efficientnet.EfficientNetBN(\"efficientnet-b0\", spatial_dims=3, num_classes = 14)\ninputs = torch.rand(1, 3, image_size, image_size, image_size)\noutputs = model(inputs)\nprint(model)\nprint(outputs.shape)","metadata":{"execution":{"iopub.status.busy":"2024-01-18T12:38:54.745846Z","iopub.execute_input":"2024-01-18T12:38:54.748348Z","iopub.status.idle":"2024-01-18T12:38:58.139224Z","shell.execute_reply.started":"2024-01-18T12:38:54.748297Z","shell.execute_reply":"2024-01-18T12:38:58.137717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nfrom monai.networks.nets import EfficientNetBN\n\nclass EfficientNetBNSingleChannel(EfficientNetBN):\n    def __init__(self, *args, **kwargs):\n        super(EfficientNetBNSingleChannel, self).__init__(*args, **kwargs)\n        # Modify the number of input channels in the first convolutional layer\n        self._conv_stem = nn.Conv3d(1, self._conv_stem.out_channels, kernel_size=3, stride=2, padding=1, bias=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-18T12:38:58.14136Z","iopub.execute_input":"2024-01-18T12:38:58.142651Z","iopub.status.idle":"2024-01-18T12:38:58.150562Z","shell.execute_reply.started":"2024-01-18T12:38:58.142593Z","shell.execute_reply":"2024-01-18T12:38:58.149472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport pandas as pd\nfrom torch.utils.data import DataLoader\nfrom monai.data import Dataset\nfrom monai.transforms import Compose, LoadImaged, ScaleIntensityd, ToTensord\nfrom monai.networks.nets import EfficientNetBN\nfrom monai.losses import DiceLoss\nfrom monai.metrics import DiceMetric\nfrom monai.utils import set_determinism\nfrom monai.data.utils import pad_list_data_collate\n\n# Set random seed for reproducibility\nset_determinism(seed=0)\n\n# Define image size and model\nimage_size = 128\nnum_classes = 14\nmodel = EfficientNetBN(\"efficientnet-b0\", spatial_dims=3, num_classes=num_classes)\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)\n\ncriterion = torch.nn.BCEWithLogitsLoss()\n\n\n# Create a DataFrame from your CSV file\ncsv_file_path = \"/kaggle/input/final-shuffled-csv/combined_and_shuffled_file.csv\"\ndf = pd.read_csv(csv_file_path).head(100)\n\n# Create a Dataset from the DataFrame\ndata = []\nfor _, row in df.iterrows():\n    data.append({\"image\": row.iloc[1], \"label\": list(map(int, row.iloc[5:].values))})\ndataset = Dataset(data, transform=Compose([LoadImaged(keys=[\"image\"]), \n                                           ScaleIntensityd(keys=[\"image\"]), ToTensord(keys=[\"image\"])]))\n\n# Split the dataset into training, validation, and test sets\ntrain_dataset, val_dataset, test_dataset = dataset[:60], dataset[60:80], dataset[80:]\n\n# Create data loaders\nbatch_size = 2\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=pad_list_data_collate)\nval_loader = DataLoader(val_dataset, batch_size=1, collate_fn=pad_list_data_collate)\ntest_loader = DataLoader(test_dataset, batch_size=1, collate_fn=pad_list_data_collate)\n\n\n# Define loss function, optimizer, and metrics\nloss_function = DiceLoss(sigmoid=True, reduction=\"mean\")\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\ndice_metric = DiceMetric(include_background=False, reduction=\"mean\")\n\n# Training loop\nmax_epochs = 10\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)\nmodel.train()\n\nfor epoch in range(max_epochs):\n    model.train()\n    total_loss = 0.0\n    total_samples = 0\n\n    for batch in train_loader:\n        grayscale_image = batch[\"image\"][0].unsqueeze(0)\n        rgb_image = grayscale_image.repeat(1, 3, 1, 1, 1)\n        inputs, targets_list = rgb_image.to(device), batch[\"label\"]  # Adjusted for multi-label\n#         print(target_list.shape)\n        # Assuming targets_list contains binary tensors for each class\n        binary_targets = [label.unsqueeze(0) for label in targets_list]\n        targets = torch.cat(binary_targets, dim=1).to(device)\n        print(targets.shape)\n\n        optimizer.zero_grad()\n        outputs = model(inputs)\n\n        loss = loss_function(outputs, targets.float())\n        loss.backward()\n        optimizer.step()\n\n        total_loss += loss.item()\n        total_samples += targets.size(0)\n\n    average_loss = total_loss / len(train_loader)\n    print(f\"Epoch [{epoch+1}/{max_epochs}], Loss: {average_loss:.4f}\")\n\n# Validation\nmodel.eval()\ntotal_correct = 0\ntotal_samples = 0\nwith torch.no_grad():\n    for val_batch in val_loader:\n        val_inputs, val_targets = val_batch[\"image\"].to(device), val_batch[\"label\"].to(device)\n        val_outputs = model(val_inputs)\n\n        # Calculate accuracy\n        predicted_labels = torch.argmax(val_outputs, dim=1)\n        total_correct += torch.sum(torch.eq(predicted_labels, val_targets))\n        total_samples += val_targets.numel()\n\n        dice_metric(y_pred=val_outputs, y=val_targets)\n\naccuracy = total_correct / total_samples\ndice_score = dice_metric.aggregate().item()\ndice_metric.reset()\nprint(f\"Validation Accuracy: {accuracy:.4f}, Dice Score: {dice_score:.4f}\")\n\n# Testing\nmodel.eval()\ntotal_correct = 0\ntotal_samples = 0\nwith torch.no_grad():\n    for test_batch in test_loader:\n        test_inputs, test_targets = test_batch[\"image\"].to(device), test_batch[\"label\"].to(device)\n        test_outputs = model(test_inputs)\n\n        # Calculate accuracy\n        predicted_labels = torch.argmax(test_outputs, dim=1)\n        total_correct += torch.sum(torch.eq(predicted_labels, test_targets))\n        total_samples += test_targets.numel()\n\n        dice_metric(y_pred=test_outputs, y=test_targets)\n\naccuracy = total_correct / total_samples\ndice_score = dice_metric.aggregate().item()\ndice_metric.reset()\nprint(f\"Test Accuracy: {accuracy:.4f}, Dice Score: {dice_score:.4f}\")\n\n# Save the trained model\ntorch.save(model.state_dict(), \"efficientnet_model.pth\")\n","metadata":{"execution":{"iopub.status.busy":"2024-01-18T13:53:38.046667Z","iopub.execute_input":"2024-01-18T13:53:38.047115Z","iopub.status.idle":"2024-01-18T13:53:41.562293Z","shell.execute_reply.started":"2024-01-18T13:53:38.047079Z","shell.execute_reply":"2024-01-18T13:53:41.560954Z"},"trusted":true},"execution_count":null,"outputs":[]}]}