Giter Site home page Giter Site logo

3D Classifier about tutorials HOT 17 CLOSED

project-monai avatar project-monai commented on July 19, 2024
3D Classifier

from tutorials.

Comments (17)

desserdmi avatar desserdmi commented on July 19, 2024 1

Hi guys,

I am trying to perform a very similar task: I have 3D images (CT scans) and corresponding masks including 3 classes of labels: background, bleeding and edema..(all the classes are within one mask/segmentation file). I have changed the parameter to softmax as described above and still get the error: "index 5 is out of bounds for dimension 4 with size 2". Restarting the notebook has not solved the issue..
Do you have any idea?
@Sebagam: Could you maybe share/post this part of the code worked for you?

Would appreciate any help and thank you very much in advance!

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

Hi @Sebagam ,

Sorry for the delayed response, thanks for your interest here.
May I know what's the "2" means in your data shape? Seems you missed channel dim in your data?
If yes, you need to add AddChanneld transform.

Thanks.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

Hi Nic-Ma,

My files have 224 x 224 x 160 length, they are T1 nifti files.
I may be dragging an old transform from the tutorial.
Where can I add that transform?

This is the code:

Define transforms

train_transforms = Compose([ScaleIntensity(), AddChannel(), Resize((96, 96, 96)), RandRotate90(), ToTensor()])
val_transforms = Compose([ScaleIntensity(), AddChannel(), Resize((96, 96, 96)), ToTensor()])

Define nifti dataset, data loader

check_ds = NiftiDataset(image_files=images, labels=labels, transform=train_transforms)
check_ds = NiftiDataset(image_files=images, labels=labels)
check_loader = DataLoader(check_ds, batch_size=2, num_workers=2, pin_memory=torch.cuda.is_available())
im, label = monai.utils.misc.first(check_loader)
print(type(im), im.shape, label)

create a training data loader

train_ds = NiftiDataset(image_files=images[:10], labels=labels[:10], transform=train_transforms)
train_ds = NiftiDataset(image_files=images[:10], labels=labels[:10])
train_loader = DataLoader(train_ds, batch_size=2, shuffle=True, num_workers=2, pin_memory=torch.cuda.is_available())

create a validation data loader

val_ds = NiftiDataset(image_files=images[-10:], labels=labels[-10:], transform=val_transforms)
val_ds = NiftiDataset(image_files=images[-10:], labels=labels[-10:])
val_loader = DataLoader(val_ds, batch_size=2, num_workers=2, pin_memory=torch.cuda.is_available())

Create DenseNet121, CrossEntropyLoss and Adam optimizer

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = monai.networks.nets.densenet.densenet121(spatial_dims=3, in_channels=1, out_channels=1).to(device)
loss_function = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), 1e-5)

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

Why you defined duplicated datasets?

check_ds = NiftiDataset(image_files=images, labels=labels, transform=train_transforms)
check_ds = NiftiDataset(image_files=images, labels=labels)

Should apply the transforms.

Thanks.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

Sorry, I left one line uncommented.
If I apply the transforms I get this error:

ValueError: recompute_scale_factor is not meaningful with an explicit size.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

Hello,

How can I transform this tensor?
<class 'torch.Tensor'> torch.Size([2, 224, 224, 160]) tensor([1, 1])

Niftiloader adds another dimension to my 224x224x160 structural T1 nifti.
You mentioned using AddChanneld but I don't know how to fit it in the code.

Thanks

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

Hi @Sebagam ,

When you define your transform chain, you can put the AddChannel transform in it:

train_transforms = Compose([ScaleIntensity(), AddChannel(), Resize((96, 96, 96)), RandRotate90(), ToTensor()])

Thanks.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

I added that transform and I get this error.
I think that there is still something wrong with the tensor dimensions: "ValueError: dictionary update sequence element #0 has length 224; 2 is required".


epoch 1/5

RuntimeError Traceback (most recent call last)
in ()
39 epoch_loss = 0
40 step = 0
---> 41 for batch_data in train_loader:
42 step += 1
43 inputs, labels = batch_data[0].to(device), batch_data[1].to(device)

3 frames
/usr/local/lib/python3.6/dist-packages/torch/_utils.py in reraise(self)
426 # have message field
427 raise self.exc_type(message=msg)
--> 428 raise self.exc_type(msg)
429
430

RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/monai/transforms/utils.py", line 361, in apply_transform
return transform(data)
File "/usr/local/lib/python3.6/dist-packages/monai/transforms/utility/dictionary.py", line 131, in call
d = dict(data)
ValueError: dictionary update sequence element #0 has length 224; 2 is required

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/monai/transforms/utils.py", line 361, in apply_transform
return transform(data)
File "/usr/local/lib/python3.6/dist-packages/monai/transforms/compose.py", line 236, in call
input_ = apply_transform(transform, input)
File "/usr/local/lib/python3.6/dist-packages/monai/transforms/utils.py", line 363, in apply_transform
raise RuntimeError(f"applying transform {transform}") from e
RuntimeError: applying transform <monai.transforms.utility.dictionary.AddChanneld object at 0x7f28baef1cf8>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
data = fetcher.fetch(index)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/monai/data/nifti_reader.py", line 102, in getitem
img = apply_transform(self.transform, img)
File "/usr/local/lib/python3.6/dist-packages/monai/transforms/utils.py", line 363, in apply_transform
raise RuntimeError(f"applying transform {transform}") from e
RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7f296cc74e48>

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

Hi @Sebagam ,

I think the problem is that your data and other transforms are based on array format, but you used AddChanneld.
So you can try to use AddChannel instead.
If still facing the error, could you please help share your program?

Thanks.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

I'm still facing issues.
Here is my code, please ask me for permission

https://colab.research.google.com/drive/1XcHHnNvxVCbTDV7bgnWHrSjGD7XKzY_f#scrollTo=kstCTYNMQ9d7

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

Requested.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

I checked your error log, it said: "IndexError: index 5 is out of bounds for dimension 0 with size 2".
Maybe the dim of your labels is not expected? Please print out the dim of labels to check.

Thanks.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

I don't quite understand why the error message: "index 5 is out of bounds for dimension 0 with size 2" for labels.
Maybe you need to restart the notebook and rerun it again from the beginning cell?
Then please print out the shape of labels.

Thanks.

from tutorials.

Sebagam avatar Sebagam commented on July 19, 2024

from tutorials.

Nic-Ma avatar Nic-Ma commented on July 19, 2024

Maybe the notebook cached some precious unexpected values of the variables, so you need to restart the notebook.
Anyway, please feel free to raise an issue if you face any other problems.

Thanks.

from tutorials.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.