-
Notifications
You must be signed in to change notification settings - Fork 1.5k
FocalLoss Runtime Error (view() instead of reshape()) #2296
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
When I try to train my network using FocalLoss instead of DiceLoss, I get the following error:
File "/home/diane/miniconda3/envs/lightning-hydra/lib/python3.8/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py", line 161, in validation_step
return self.lightning_module.validation_step(*args, **kwargs)
File "/home/diane/Documents/KeriNet/src/models/keri_model.py", line 110, in validation_step
loss = self.loss_function(logits, labels)
File "/home/diane/miniconda3/envs/lightning-hydra/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/diane/miniconda3/envs/lightning-hydra/lib/python3.8/site-packages/monai/losses/focal_loss.py", line 116, in forward
i = i.view(i.size(0), i.size(1), -1)
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
Code snippets
# give three times weight to the trapezium class (class 1)
# than to the background or the metacarpus (classes 0 & 2)
self.loss_function = FocalLoss(include_background = True,
to_onehot_y = True, weight = [1.0, 3.0, 1.0])
Expected behavior
When using FocalLoss I want my network to train correctly, i.e., like it does when:
self.loss_function = DiceLoss(include_background=True,
to_onehot_y=True, softmax=True)
Environment
================================
Printing MONAI config...
================================
MONAI version: 0.5.2
Numpy version: 1.19.2
Pytorch version: 1.8.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False
MONAI rev id: feb3a334b7bbf302b13a6da80e0b022a4cf75a4e
Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 3.2.1
scikit-image version: NOT INSTALLED or UNKNOWN VERSION.
Pillow version: 8.2.0
Tensorboard version: 2.4.1
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: 0.9.1
ITK version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: 4.61.0
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.8.0
For details about installing the optional dependencies, please visit:
https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies
================================
Printing system config...
================================
System: Linux
Linux version: Ubuntu 20.04.2 LTS
Platform: Linux-5.8.0-53-generic-x86_64-with-glibc2.10
Processor: x86_64
Machine: x86_64
Python version: 3.8.10
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: []
Num physical CPUs: 8
Num logical CPUs: 16
Num usable CPUs: 16
CPU usage (%): [43.7, 43.8, 47.1, 41.2, 40.0, 44.4, 40.0, 56.3, 46.7, 47.1, 94.1, 88.2, 43.7, 40.0, 40.0, 100.0]
CPU freq. (MHz): 4710
Load avg. in last 1, 5, 15 mins (%): [15.1, 15.7, 15.1]
Disk usage (%): 73.9
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 31.1
Available memory (GB): 21.2
Used memory (GB): 9.1
================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 11.1
cuDNN enabled: True
cuDNN version: 8005
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'compute_37']
GPU 0 Name: GeForce RTX 2080 SUPER
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 48
GPU 0 Total memory (GB): 7.8
GPU 0 CUDA capability (maj.min): 7.5
Additional context
I'm using PyTorch Lightning 1.3.3
Solution
Using .reshape(...) instead of .view(...) in lines 116 and 117 of /monai/losses/focal_loss.py solved the issue for me.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working