snu-mllab / PuzzleMix

Official PyTorch implementation of "Puzzle Mix: Exploiting Saliency and Local Statistics for Optimal Mixup" (ICML'20)
MIT License
158 stars 17 forks source link

expected CUDA, but got CPU #7

Closed redorangeyellowy closed 2 years ago

redorangeyellowy commented 2 years ago

Hi.

I implemented your Visulaization.ipynb file with other dataset,

but in the 'Transport' part, I got this problem:

RuntimeError                              Traceback (most recent call last)
<ipython-input-33-90b59f47c9ca> in <module>
     18                      neigh_size=2, mean=mean_torch, std=std_torch,
     19                      transport=transport, t_eps=t_eps, t_size=t_size,
---> 20                      device='cpu')
     21 
     22 print_fig(output[0] * std_torch + mean_torch)

~/PuzzleMix/mixup.py in mixup_graph(input1, grad1, indices, block_num, alpha, beta, gamma, eta, neigh_size, n_labels, mean, std, transport, t_eps, t_size, noise, adv_mask1, adv_mask2, device, mp)
    324 
    325         # input1
--> 326         plan = mask_transport(mask, unary1_torch, eps=t_eps)
    327         input1 = transport_image(input1, plan, batch_size, t_block_num, t_size)
    328 

~/PuzzleMix/mixup.py in mask_transport(mask, grad_pool, eps)
    347 
    348     z = (mask > 0).float()
--> 349     cost = eps * C - grad_pool.reshape(-1, block_num**2, 1) * z.reshape(-1, 1, block_num**2)
    350 
    351     # row and col

RuntimeError: expected backend CUDA and dtype Float but got backend CPU and dtype Float

So, I changed device=cpu to device=cuda and re-implemented.

But I got another problem:

RuntimeError                              Traceback (most recent call last)
<ipython-input-38-9c6b80b68758> in <module>
     18                      neigh_size=2, mean=mean_torch, std=std_torch,
     19                      transport=transport, t_eps=t_eps, t_size=t_size,
---> 20                      device='cuda')
     21 
     22 print_fig(output[0] * std_torch + mean_torch)

~/PuzzleMix/mixup.py in mixup_graph(input1, grad1, indices, block_num, alpha, beta, gamma, eta, neigh_size, n_labels, mean, std, transport, t_eps, t_size, noise, adv_mask1, adv_mask2, device, mp)
    262     unary2 = unary2_torch.clone()
    263 
--> 264     unary2[:, :-1, :] += (pw_x[:, 1, 0] + pw_x[:, 1, 1]) / 2.
    265     unary1[:, :-1, :] += (pw_x[:, 0, 1] + pw_x[:, 0, 0]) / 2.
    266     unary2[:, 1:, :] += (pw_x[:, 0, 1] + pw_x[:, 1, 1]) / 2.

RuntimeError: expected backend CPU and dtype Float but got backend CUDA and dtype Float

I don't understand why this happened.

Do you know how can I solve this problem?

Janghyun1230 commented 2 years ago

Hello!

The cost matrices (defined in mixup.py line 36) are located in GPUs as default when GPUs are available.
Change this line https://github.com/snu-mllab/PuzzleMix/blob/e2dbf3a2371026411d5741d129f46bf3eb3d3465/mixup.py#L6 to

device = 'cpu'

then I believe it will work!

If there exist other problems please let me know.

redorangeyellowy commented 2 years ago

Thanks. It works without any problems.