Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch size = 1 leads to error #28

Open
matteosodano opened this issue Sep 3, 2021 · 1 comment
Open

Batch size = 1 leads to error #28

matteosodano opened this issue Sep 3, 2021 · 1 comment

Comments

@matteosodano
Copy link

I tried to train with default parameters and sunrgbd dataset with batch_size set to 1. The following error arises:

  File "train.py", line 577, in <module>
    train_main()
  File "train.py", line 206, in train_main
    label_downsampling_rates, args.epochs, args.results_dir, monitoring_images, debug_mode=args.debug)
  File "train.py", line 310, in train_one_epoch
    pred_scales = model(image, depth)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/matteo/Code/Semantic_Segmentation/ESANet/src/models/model.py", line 238, in forward
    out = self.context_module(fuse)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/matteo/Code/Semantic_Segmentation/ESANet/src/models/context_modules.py", line 72, in forward
    y = f(x)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/modules/batchnorm.py", line 81, in forward
    exponential_average_factor, self.eps)
  File "/home/matteo/anaconda3/envs/rgbd_segmentation/lib/python3.7/site-packages/torch/nn/functional.py", line 1666, in batch_norm
    raise ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))
ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 256, 1, 1])

Any hint on how to fix it?

@danielS91
Copy link
Member

The error comes from the global average pooling in the context module. The output of the global branch has no spatial resolution any more (see your error). If you are using batchsize 1, there is only one element left for determining mean and std in the subsequent batch normalization, which is just not possible.
So you need a batchsize > 1 or you have to remove the global branch or the entire context module.

Note that this is requirement is only for training. Inference with batchsize 1 works well as the inference mode for batch normalization is different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants