Giter Site home page Giter Site logo

Comments (5)

YuanTingHsieh avatar YuanTingHsieh commented on August 20, 2024 1

After your chain of transforms,
Is the data has same dimension?
Seeing from your code your first image is [44, 55, 83], is the second image the same size as this?

Like in the original spleen example, we use

RandCropByPosNegLabeld(
            keys=["image", "label"],
            label_key="label",
            spatial_size=(96, 96, 96),
            pos=1,
            neg=1,
            num_samples=4,
            image_key="image",
            image_threshold=0,
        ),

This ensures that the data passed in will be in batches of [96, 96, 96] cubes.

from tutorials.

dzenanz avatar dzenanz commented on August 20, 2024 1

Thank you! That was the problem. Adding DivisiblePadd with k=32 solves it.

from tutorials.

dzenanz avatar dzenanz commented on August 20, 2024

The images have the same dimensions after the transforms. Updated notebook is here. Adding

        inputs, labels = (
            batch_data["image"].to(device),
            batch_data["label"].to(device),
        )
        print(f"batch_data image: {batch_data['image'].shape}")
        print(f"batch_data label: {batch_data['label'].shape}")

produces the output:

----------
epoch 1/10
batch_data image: torch.Size([20, 1, 44, 55, 83])
batch_data label: torch.Size([20, 1, 44, 55, 83])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-12-e43444a90bc8> in <module>
     22 
     23         optimizer.zero_grad()
---> 24         outputs = model(inputs)
     25         loss = loss_function(outputs, labels)
     26         loss.backward()

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\monai\networks\nets\unet.py in forward(self, x)
    190 
    191     def forward(self, x: torch.Tensor) -> torch.Tensor:
--> 192         x = self.model(x)
    193         return x
    194 

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
     98     def forward(self, input):
     99         for module in self:
--> 100             input = module(input)
    101         return input
    102 

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\monai\networks\layers\simplelayers.py in forward(self, x)
     37 
     38     def forward(self, x: torch.Tensor) -> torch.Tensor:
---> 39         return torch.cat([x, self.submodule(x)], self.cat_dim)
     40 
     41 

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
     98     def forward(self, input):
     99         for module in self:
--> 100             input = module(input)
    101         return input
    102 

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\monai\networks\layers\simplelayers.py in forward(self, x)
     37 
     38     def forward(self, x: torch.Tensor) -> torch.Tensor:
---> 39         return torch.cat([x, self.submodule(x)], self.cat_dim)
     40 
     41 

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
     98     def forward(self, input):
     99         for module in self:
--> 100             input = module(input)
    101         return input
    102 

c:\dev\monai\pyenv\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

c:\dev\monai\pyenv\lib\site-packages\monai\networks\layers\simplelayers.py in forward(self, x)
     37 
     38     def forward(self, x: torch.Tensor) -> torch.Tensor:
---> 39         return torch.cat([x, self.submodule(x)], self.cat_dim)
     40 
     41 

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 7 and 8 in dimension 3 at C:\w\1\s\windows\pytorch\aten\src\TH/generic/THTensor.cpp:612

from tutorials.

YuanTingHsieh avatar YuanTingHsieh commented on August 20, 2024

Hi
The problem is the data shape changed after down-sample then up-sample.

You can take a look at the answer here: https://stackoverflow.com/questions/60063797/not-understanding-the-data-flow-in-unet-like-architetures-and-having-problems-wi
and here: http://makeyourownneuralnetwork.blogspot.com/2020/02/calculating-output-size-of-convolutions.html

So you will need to do some calculations and get a valid shape
Then either pad or rescale to that shape should solve this problem

from tutorials.

Ontheroad123 avatar Ontheroad123 commented on August 20, 2024

Thank you! That was the problem. Adding DivisiblePadd with k=32 solves it.

I meet same error, can you show detail in code?

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.