dome272 / Diffusion-Models-pytorch

Pytorch implementation of Diffusion Models (https://arxiv.org/pdf/2006.11239.pdf)
Apache License 2.0
1.11k stars 256 forks source link

Getting shape mismatch error when using a different input #31

Open RajShekhorRoy opened 1 year ago

RajShekhorRoy commented 1 year ago

getting this error RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x128 and 256x128)

when I am using the code below which is basically an array of size 32x32x32 and timesteps of 128:

size = 32 device_val="cuda" x_input = torch.tensor(np.random.rand(1,size, size, size)).to(device_val).type(torch.cuda.FloatTensor) diffusion = Diffusion(img_size=size, device=device_val) t = diffusion.sample_timesteps(128).to(device_val) model = UNet().to(device_val) xmodel=model(x_input,t)

In addition to that can you please explain a bit more how is this time embedding working especially in terms of the dimension?

dome272 commented 1 year ago

Can you show the full error message? One problem seems to be that your size is 32 also for the channels. You need to change the c_in to your diffusion model: model = UNet(c_in=32).to(device_val). But do you really want that? This means you have images with 32 channels?

Am Fr., 30. Juni 2023 um 19:22 Uhr schrieb Raj Shekhor Roy < @.***>:

getting this error RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x128 and 256x128)

when I am using the code below which is basically an array of size 32x32x32 and timesteps of 128:

size = 32 device_val="cuda" x_input = torch.tensor(np.random.rand(1,size, size, size)).to(device_val).type(torch.cuda.FloatTensor) diffusion = Diffusion(img_size=size, device=device_val) t = diffusion.sample_timesteps(128).to(device_val) model = UNet().to(device_val) xmodel=model(x_input,t)

In addition to that can you please explain a bit more how is this time embedding working especially in terms of the dimension?

— Reply to this email directly, view it on GitHub https://github.com/dome272/Diffusion-Models-pytorch/issues/31, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOYRYBRHSIAQWUJPKQEOPLDXN4DL7ANCNFSM6AAAAAAZ2DQG7Y . You are receiving this because you are subscribed to this thread.Message ID: @.***>

RajShekhorRoy commented 1 year ago

Here is the full error:

Traceback (most recent call last): File "/home/rajroy/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/212.4746.96/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rajroy/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/212.4746.96/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/rajroy/Diffusion-Models-pytorch/test.py", line 80, in xmodel=model(x_input,t) ^^^^^^^^^^^^^^^^ File "/home/rajroy/anaconda3/envs/py_cryo/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(*args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rajroy/Diffusion-Models-pytorch/original_moduels.py", line 170, in forward x2 = self.down1(x1, t) ^^^^^^^^^^^^^^^^^ File "/home/rajroy/anaconda3/envs/py_cryo/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(*args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rajroy/Diffusion-Models-pytorch/original_moduels.py", line 98, in forward emb = self.emb_layer(t)[:, :, None, None].repeat(1, 1, x.shape[-2], x.shape[-1]) ^^^^^^^^^^^^^^^^^ File "/home/rajroy/anaconda3/envs/py_cryo/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rajroy/anaconda3/envs/py_cryo/lib/python3.11/site-packages/torch/nn/modules/container.py", line 217, in forward input = module(input) ^^^^^^^^^^^^^ File "/home/rajroy/anaconda3/envs/py_cryo/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rajroy/anaconda3/envs/py_cryo/lib/python3.11/site-packages/torch/nn/modules/linear.py", line 114, in forward return F.linear(input, self.weight, self.bias) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x128 and 256x128) python-BaseException

One more thing is that I changed the default parameter in the model to provide the input channel number, in this way:

class UNet(nn.Module): def init(self, c_in=32, c_out=32, time_dim=128, device="cuda"):