{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport tifffile\nimport torch\nimport numpy as np\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, TensorDataset\nfrom sklearn.model_selection import train_test_split\nfrom skimage import transform\nimport matplotlib.pyplot as plt\n\ndef load_and_resize_kidney_images(image_folder_path, target_size=(128, 128)):\n    image_files = sorted([file for file in os.listdir(image_folder_path) if file.endswith(\".tif\")])\n    image_data = []\n\n    for file in image_files:\n        image = tifffile.imread(os.path.join(image_folder_path, file)).astype(np.float32)\n        resized_image = transform.resize(image, target_size, mode='constant')\n        image_data.append(resized_image)\n\n    return torch.from_numpy(np.stack(image_data))\n\nimage_folder_path = r'/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/images'\n\n\ndata = load_and_resize_kidney_images(image_folder_path, target_size=(128, 128))\ndata = data.unsqueeze(1).float()  # Add channel dimension and convert to float\n\n\nlabels = torch.from_numpy(np.random.randint(0, 2, size=data.shape)).float()\n\n\nX_train, X_temp, y_train, y_temp = train_test_split(data, labels, test_size=0.3, random_state=42)\nX_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)\n\n\ntrain_dataset = TensorDataset(X_train, y_train)\ntrain_dataloader = DataLoader(train_dataset, batch_size=2, shuffle=True)\n\n\nval_dataset = TensorDataset(X_val, y_val)\nval_dataloader = DataLoader(val_dataset, batch_size=2, shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-01T11:51:23.362922Z","iopub.execute_input":"2024-01-01T11:51:23.363241Z","iopub.status.idle":"2024-01-01T11:54:46.396921Z","shell.execute_reply.started":"2024-01-01T11:51:23.363210Z","shell.execute_reply":"2024-01-01T11:54:46.395999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install plotly","metadata":{"execution":{"iopub.status.busy":"2024-01-01T11:54:46.398731Z","iopub.execute_input":"2024-01-01T11:54:46.399172Z","iopub.status.idle":"2024-01-01T11:54:59.643417Z","shell.execute_reply.started":"2024-01-01T11:54:46.399144Z","shell.execute_reply":"2024-01-01T11:54:59.642257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objs as go\nfrom plotly.subplots import make_subplots\n\n# Specify the path to your labels folder\nlabels_folder_path = r'/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/labels'  # Adjust the path accordingly\n\n# Load the first 100 label images\nlabel_images = []\nfor file in sorted(os.listdir(labels_folder_path))[:110]:\n    if file.endswith(\".tif\"):\n        label_image = tifffile.imread(os.path.join(labels_folder_path, file))\n        label_images.append(torch.from_numpy(label_image).float())\n\n# Combine the first 100 images into one 3D tensor\nall_images = torch.stack(label_images, dim=0)\n\n# Extract coordinates of non-zero values (assuming binary segmentation)\nnon_zero_coords = all_images.nonzero(as_tuple=True)\n\n# Normalize aspect ratio for better visualization\naspect_ratio = [1.0, 1.0, 0.1]  # Adjust the aspect ratio values as needed\n\n# Create an interactive 3D scatter plot using plotly\nfig = make_subplots(specs=[[{'type': 'scatter3d'}]])\n\n# Scatter plot with varying colors, marker size, and transparency\nfig.add_trace(\n    go.Scatter3d(\n        x=non_zero_coords[2].numpy(),\n        y=non_zero_coords[1].numpy(),\n        z=non_zero_coords[0].numpy(),\n        mode='markers',\n        marker=dict(\n            size=2,\n            opacity=0.5,\n            color=non_zero_coords[0].numpy(),\n            colorscale='RdYlBu'\n        )\n    )\n)\n\n# Set labels and title\nfig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z', aspectratio=dict(x=aspect_ratio[0], y=aspect_ratio[1], z=aspect_ratio[2])))\nfig.update_layout(title_text='Interactive 3D Segmentation Result for the First 100 Images')\n\n# Show the plot\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-01T12:02:39.339602Z","iopub.execute_input":"2024-01-01T12:02:39.339970Z","iopub.status.idle":"2024-01-01T12:02:41.065481Z","shell.execute_reply.started":"2024-01-01T12:02:39.339941Z","shell.execute_reply":"2024-01-01T12:02:41.064327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}