question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuntimeError: The expanded size of the tensor (3) must match the existing size (2) at non-singleton dimension 0. Target sizes: [3, -1, -1]. Tensor sizes: [2, 128, 128]

See original GitHub issue

Alright, I’m completely stuck. I keep running into this error. I don’t know much about how GANs work but the error makes me think something is wrong with one of my data samples. I’ve been trying to refine my selection but when there’s well over 3000 samples I can’t really go through one-by-one to try to find some outlier.

I initially thought it was being picky against grayscale images, so I filtered them out, but to no avail.

I’ve tried with and without --transparency in case somehow an image containing some made it in, but output is not different.

Then I thought maybe it was trying to include sub-directories in the data folder, so I removed those, and nothing changed.

I adjusted batch size and related components in case this was a memory-related error but active memory use remains ~3GB and the error persists.

A data pool of ~100 images picked from a different source made it through to iteration 3000+ without encountering this breakage, so that furthers my assumption one of my samples is broken.

I’d appreciate some insight on what these dimensions are and how I can go about troubleshooting.

$ stylegan2_pytorch --data /media/LinuxDATA/nebula/x/generic_old/AAA-normal-stylegan --name illusfront --attn-layers [1,2] --aug-prob 0.2 --batch-size 1 --gradient-accumulate-every 15 --network-capacity 48 --new

illusfront</media/LinuxDATA/nebula/x/generic_old/AAA-normal-stylegan>:   0%|         | 0/150000 [00:00<?, ?it/s]/home/haruka/.local/lib/python3.6/site-packages/torch/nn/functional.py:3121: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode))
/home/haruka/.local/lib/python3.6/site-packages/adamp/adamp.py:80: UserWarning: This overload of add_ is deprecated:
	add_(Number alpha, Tensor other)
Consider using one of the following signatures instead:
	add_(Tensor other, *, Number alpha) (Triggered internally at  /pytorch/torch/csrc/utils/python_arg_parser.cpp:766.)
  exp_avg.mul_(beta1).add_(1 - beta1, grad)
G: 104.41 | D: 9.24 | GP: 371.04 | PL: 0.00 | CR: 0.00 | Q: 0.00
illusfront</media/LinuxDATA/nebula/x/generic_old/AAA-normal-stylegan>:   0%| | 25/150000 [03:56<394:52:38,  9.48
Traceback (most recent call last):
  File "/home/haruka/.local/bin/stylegan2_pytorch", line 92, in <module>
    fire.Fire(train_from_folder)
  File "/home/haruka/.local/lib/python3.6/site-packages/fire/core.py", line 138, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/home/haruka/.local/lib/python3.6/site-packages/fire/core.py", line 468, in _Fire
    target=component.__name__)
  File "/home/haruka/.local/lib/python3.6/site-packages/fire/core.py", line 672, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/home/haruka/.local/bin/stylegan2_pytorch", line 87, in train_from_folder
    retry_call(model.train, tries=3, exceptions=NanException)
  File "/home/haruka/.local/lib/python3.6/site-packages/retry/api.py", line 101, in retry_call
    return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger)
  File "/home/haruka/.local/lib/python3.6/site-packages/retry/api.py", line 33, in __retry_internal
    return f()
  File "/home/haruka/.local/lib/python3.6/site-packages/stylegan2_pytorch/stylegan2_pytorch.py", line 749, in train
    image_batch = next(self.loader).cuda()
  File "/home/haruka/.local/lib/python3.6/site-packages/stylegan2_pytorch/stylegan2_pytorch.py", line 113, in cycle
    for i in iterable:
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 363, in __next__
    data = self._next_data()
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 989, in _next_data
    return self._process_data(data)
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1014, in _process_data
    data.reraise()
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/_utils.py", line 395, in reraise
    raise self.exc_type(msg)
RuntimeError: Caught RuntimeError in DataLoader worker process 5.
Original Traceback (most recent call last):
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 185, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/haruka/.local/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/haruka/.local/lib/python3.6/site-packages/stylegan2_pytorch/stylegan2_pytorch.py", line 244, in __getitem__
    return self.transform(img)
  File "/home/haruka/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 61, in __call__
    img = t(img)
  File "/home/haruka/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 359, in __call__
    return self.lambd(img)
  File "/home/haruka/.local/lib/python3.6/site-packages/stylegan2_pytorch/stylegan2_pytorch.py", line 212, in __call__
    return tensor.expand(self.num_channels, -1, -1)
RuntimeError: The expanded size of the tensor (3) must match the existing size (2) at non-singleton dimension 0.  Target sizes: [3, -1, -1].  Tensor sizes: [2, 128, 128]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
catthoucommented, Aug 11, 2020

Alright, I pulled out all the problematic images and set it loose on 'em. It seems to be spitting out models just fine, so thanks!

0reactions
lucidrainscommented, Aug 10, 2020

@catthou ok, done! https://github.com/lucidrains/stylegan2-pytorch/releases/tag/0.18.4 let me know if it works / doesn’t work

Read more comments on GitHub >

github_iconTop Results From Across the Web

The expanded size of the tensor must match the existing size
I have a tensor with shape [2,3] and I want to expand it to [4,3]. If I use torch.expand directly, a error occurs....
Read more >
RuntimeError: The expanded size of the tensor (3) must match ...
RuntimeError : The expanded size of the tensor (3) must match the existing size (864) at non-singleton dimension 3. Target sizes: [32, 3,...
Read more >
The expanded size of the tensor (64) must match the existing ...
The problem is that norm is missing that extra dimension: masked_embedding is [256, 66, 64] and norm is [256, 66] .
Read more >
How to fix: RuntimeError: The size of tensor a (2) must match ...
Im using PyTorch and I get the RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at...
Read more >
How to perform an expand operation in PyTorch?
Tensor.expand() attribute is used to perform expand operation. It expands the Tensor to new dimensions along the singleton dimension.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found