pytorch-summary icon indicating copy to clipboard operation
pytorch-summary copied to clipboard

model.cuda() needs to be called for summary

Open aneesh-joshi opened this issue 6 years ago • 4 comments

The code or the documentation should have a check/notice about having to call model.cuda()

summary(D, (3, 32, 32))

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

But this works ok:

summary(D.cuda(), (3, 32, 32))

aneesh-joshi avatar Sep 28 '18 21:09 aneesh-joshi

try it summary(D, (3, 32, 32), device="cpu")

mclearning2 avatar Feb 27 '19 14:02 mclearning2

How and where do you fix this? I added:

network = Network()
network.cuda()

And it says the error is on:

19         # (2) 1st hidden conv layer
---> 20         t = self.conv1(t)**
     21         t = F.relu(t)
     22         t = F.max_pool2d(t, kernel_size=3, stride=1)
...
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

daddydrac avatar Oct 21 '19 03:10 daddydrac

hi @joehoeller , did you manage to fix this ? I'm having the same problem just now.

Dicko87 avatar Dec 09 '19 14:12 Dicko87

@Dicko87 yes check my solutions/examples here, you’ll have to do it in various parts of the code:

  1. https://github.com/joehoeller/Computer-Vision-Facial-Key-Point-Detection/blob/master/app/facial-keypoint-detection/2.%20Define%20the%20Network%20Architecture.ipynb

See blocks [3], [9], [12], [14], in following order:

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
net = Net().to(device)
# convert images to CUDA Tensors
images = images.type(torch.cuda.FloatTensor)
key_pts = key_pts.type(torch.cuda.FloatTensor)

Converting back to CPU from GPU:

image = image.cpu().numpy()   # convert to numpy array from a Tensor
#images = images.to(device)
images = images.type(torch.cuda.FloatTensor)
        
  1. https://github.com/joehoeller/Computer-Vision-Facial-Key-Point-Detection/blob/master/app/unit_tests/CPU-GPU-Support.ipynb

daddydrac avatar Dec 09 '19 15:12 daddydrac