Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions Get_predictions_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

#%% import relevant modules
import sys
import numpy as np
import torch
import glob # finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in
# arbitrary order
import monai # build on top of pytorch
from monai.transforms import Compose, LoadImaged
from monai.data import list_data_collate
# I added these
import pytorch_lightning

#%% import files
# insert path of modules folders
# sys.path.insert(0, 'D:/eigenvector-grouping')

# import the modules directly
from lightning_module import SegmentatorModule
from src.transforms.transforms import (
PointcloudRandomSubsampled,
ExtractSegmentationLabeld,
ToFloatTensord
)


#%% import data
# change the directory for (vein_only) and (atery_vein)
files_root = glob.glob(r'D:\Data\LungSegmentations\PointClouds(artery_only)\PointClouds\Test_set\*')
test_dict = [{"input": file} for file in files_root[:2]] # only two files
# test_dict = [{"input": file} for file in files_root]

#%% try lightning example
# https://lightning.ai/forums/t/how-to-load-and-use-model-checkpoint-ckpt/677
model = SegmentatorModule()
trainer = pytorch_lightning.Trainer()
chk_path = "D:/eigenvector-grouping/.data/output/01-20-2023-13-49-42/checkpoint/epoch=191-step=3264.ckpt"
model2 = SegmentatorModule.load_from_checkpoint(chk_path)
results = trainer.test(model=model2, datamodule=my_datamodule, verbose=True)
trainer = Trainer()
trainer.fit(model)

# automatically loads the best weights for you
trainer.test(model)


#%% try train loader etc. from lightning_module.py as it should have the save structure
# https://lightning.ai/docs/pytorch/stable/data/datamodule.html
test_transforms = Compose([
LoadImaged(keys=["input"], reader="NumpyReader"),
PointcloudRandomSubsampled(keys=["input"], sub_size=20_000),
ExtractSegmentationLabeld(pcd_key="input"),
ToFloatTensord(keys=["input", "label"]),
])

test_dataset = monai.data.CacheDataset(
data=test_dict,
transform=test_transforms,
cache_rate=1.0,
num_workers=10,
)

test_loader = monai.data.DataLoader(
test_dataset,
batch_size=2,
shuffle=True,
num_workers=10,
collate_fn=list_data_collate,
)

#%% other example thing lightning
chk_path = "D:/eigenvector-grouping/.data/output/01-20-2023-13-49-42/checkpoint/epoch=191-step=3264.ckpt"
model = SegmentatorModule.load_from_checkpoint(chk_path)
trainer = pytorch_lightning.Trainer()
trainer.test(model, dataloaders=test_loader)


#%% test transforms
# provides the ability to chain a series of callables together in a sequential manner. Each transform in the sequence must take a single argument and
# return a single value.
test_transforms = Compose([
LoadImaged(keys=["input"], reader="NumpyReader"),
# PointcloudRandomSubsampled(keys=["input"], sub_size=20_000),
ExtractSegmentationLabeld(pcd_key="input"),
ToFloatTensord(keys=["input", "label"])
])


#%% test dataset
# Dataset with cache mechanism that can load data and cache deterministic transforms’ result during training
test_dataset = monai.data.CacheDataset(
data=test_dict,
transform=test_transforms,
cache_rate=1.0,
num_workers=1, # 10, the number of worker threads if computing cache in the initialization
)



#%% test loader
# Provides an iterable over the given dataset
multiprocessing_context_name = 'fork'
test_loader = monai.data.DataLoader(
test_dataset,
batch_size=1,
#shuffle=True,
num_workers=10,
collate_fn=list_data_collate,
# multiprocessing_context=multiprocessing_context_name,
)

#%% import model from checkpoint
# add different checkpoint_path probably
checkpoint_path = "D:/eigenvector-grouping/.data/output/01-20-2023-13-49-42/checkpoint/epoch=191-step=3264.ckpt"
model = SegmentatorModule.load_from_checkpoint(checkpoint_path)
# don't use .to(device) --> https://lightning.ai/docs/pytorch/stable/common/lightning_module.html
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model.to(device)


#%% evaluate model
model.eval()
i=0
# model.to(device)
for batch_data in test_loader:
input_tensor = batch_data["input"]
# input_tensor.to(device)
label_pred = model(input_tensor)
i+=1
#tensor=label_pred.detach().numpy()
#tensor=tesnor[0,:,:,:].squeeze()



# %%
20 changes: 13 additions & 7 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,32 @@ class TrainSegmentatorConfig:
seed: int = 0

# Paths
input_dir: str = os.path.join(".data", "input")
# input_dir: str = os.path.join(".data", "input") # artery_vein, Laurens, normtot is used
input_dir: str = os.path.join(".data", "input(artery_only)") # artery_only, normtot
# input_dir: str = "D:\eigenvector-grouping\.data\input(artery_only)"
# input_dir: str = os.path.join(".data", "input(vein_only)") # veins_only, normtot
# input_dir: str = os.path.join(".data", "input(artery_vein)") # artery_vein, normtot
output_dir: str = os.path.join(".data", "output")


# Data
size: int = 20_000
size: int = 20_000 # number of point selected in PointCloud, bijv. 95% of points
split: str = None

# Dataloader
batch_size: int = 2
num_workers: int = 10
num_workers: int = 1 # 10 # Number of subprocesses to use for data loading. 0 means
# that the data will be loaded in the main process. Number of CPUs available.

# Model
model: object = PointNet2EVG
model_type: str = "radius"
ncomponents: int = 0
features: int = 0
classes: int = 3
ncomponents: int = 0
features: int = 0 # I don't know what this variable does
classes: int = 2 # 3 # do I need to change this if I'm changing input data? I did

# Trainer
epochs: int = 200
epochs: int = 20 # 200

# Optimizer
optimizer: object = OptimizerConfig(
Expand Down
7 changes: 5 additions & 2 deletions lightning_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, config):
super().__init__()

set_determinism(seed=config.seed)
self.save_hyperparameters()
#self.save_hyperparameters()

self.size = config.size
self.input_dir = config.input_dir
Expand All @@ -41,7 +41,10 @@ def __init__(self, config):

self.model = config.model(config)
self.loss_fn = nn.CrossEntropyLoss()
self.confmat = ConfusionMatrix(num_classes=2)
self.confmat = ConfusionMatrix(task="multiclass", num_classes=2)

# save hyper-parameters to self.hparamsm auto-logged by wandb
self.save_hyperparameters()

def prepare_data(self):

Expand Down
2 changes: 1 addition & 1 deletion pointnet2_ops_lib/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

exec(open(osp.join("pointnet2_ops", "_version.py")).read())

os.environ["TORCH_CUDA_ARCH_LIST"] = "3.7+PTX;5.0;6.0;6.1;6.2;7.0;7.5;8.0;8.6"
os.environ["TORCH_CUDA_ARCH_LIST"] = "3.7+PTX;5.0;6.0;6.1;6.2;7.0;7.5;8.0;8.6;9.0;11.7"
setup(
name="pointnet2_ops",
version=__version__,
Expand Down
27 changes: 27 additions & 0 deletions random_tries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from dataclasses import dataclass
from config import TrainSegmentatorConfig

@dataclass
class OptimizerHyperparamsConfig:
lr: float = 0.0005
weight_decay: float = 0
betas: tuple = (0.9, 0.999)
amsgrad: bool = False

print(OptimizerHyperparamsConfig.lr)

OptimizerHyperparamsConfig.lr = 0.0025

print(OptimizerHyperparamsConfig.lr)

# config
config = TrainSegmentatorConfig()

max_epoch = config.epochs
print(max_epoch)

config.epochs = 100
print(max_epoch)
print(config.epochs)


1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
pytorch-lightning
monai
tensorboard
Expand Down
22 changes: 11 additions & 11 deletions src/models/pointnet2_evg.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ def build_radius(self, classes: int, features: int):
[
PointnetSAModuleEVG(
npoint=2048,
knn_nsamples=[64, 64],
vec_radii=[0.05, 0.1],
vec_lengths=[0.01, 0.02],
knn_nsamples=[64, 64],
vec_radii=[0.80, 1.6],
vec_lengths=[0.16, 0.32],
vec_nsamples=[64, 128],
mlps=[[features, 16, 16, 32], [features, 32, 32, 64]],
),
PointnetSAModuleEVG(
npoint=1024,
knn_nsamples=[16, 16],
vec_radii=[0.1, 0.2],
vec_lengths=[0.01, 0.02],
vec_radii=[1.6, 3.2],
vec_lengths=[0.16, 0.32],
vec_nsamples=[32, 64],
mlps=[[32 + 64, 64, 64, 96], [32 + 64, 64, 96, 96]],
),
PointnetSAModuleEVG(
npoint=256,
knn_nsamples=[8, 8],
vec_radii=[0.2, 0.4],
vec_lengths=[0.01, 0.02],
vec_radii=[3.2, 6.4],
vec_lengths=[0.16, 0.32],
vec_nsamples=[32, 64],
mlps=[[96 + 96, 64, 64, 128], [96 + 96, 64, 96, 128]],
),
PointnetSAModuleEVG(
npoint=64,
knn_nsamples=[8, 8],
vec_radii=[0.4, 0.6],
vec_lengths=[0.01, 0.02],
vec_radii=[6.4, 9.6],
vec_lengths=[0.16, 0.32],
vec_nsamples=[32, 64],
mlps=[[128 + 128, 128, 196, 256], [128 + 128, 128, 196, 256]],
),
PointnetSAModuleEVG(
npoint=16,
knn_nsamples=[4, 4],
vec_radii=[0.4, 0.8],
vec_lengths=[0.01, 0.02],
vec_radii=[6.4, 12.8],
vec_lengths=[0.16, 0.32],
vec_nsamples=[32, 64],
mlps=[[256 + 256, 256, 256, 512], [256 + 256, 256, 384, 512]],
),
Expand Down
3 changes: 2 additions & 1 deletion train.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from lightning_module import SegmentatorModule
from config import TrainSegmentatorConfig


def train():
"""Train surface model"""

# Load config
config = TrainSegmentatorConfig()
config = TrainSegmentatorConfig() # change hyperparameters here
np.random.seed(config.seed)

# Init out directories
Expand Down
Loading