Skip to content

Commit

Permalink
Fix cuda (#31)
Browse files Browse the repository at this point in the history
* Fix cuda

* Update pytorch.py

One more gpu->cpu

* fixed pep8 violations and test coverage
  • Loading branch information
ducha-aiki authored and jonasrauber committed Jul 9, 2017
1 parent 1192b2c commit dc09bb3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions foolbox/models/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def batch_predictions(self, images):
images = Variable(images, volatile=True)
predictions = self._model(images)
predictions = predictions.data
if self.cuda: # pragma: no cover
predictions = predictions.cpu()
predictions = predictions.numpy()
assert predictions.ndim == 2
assert predictions.shape == (n, self.num_classes())
Expand Down Expand Up @@ -82,12 +84,17 @@ def predictions_and_gradient(self, image, label):
grad = images.grad

predictions = predictions.data
if self.cuda: # pragma: no cover
predictions = predictions.cpu()

predictions = predictions.numpy()
predictions = np.squeeze(predictions, axis=0)
assert predictions.ndim == 1
assert predictions.shape == (self.num_classes(),)

grad = grad.data
if self.cuda: # pragma: no cover
grad = grad.cpu()
grad = grad.numpy()
grad = np.squeeze(grad, axis=0)
assert grad.shape == image.shape
Expand Down

0 comments on commit dc09bb3

Please sign in to comment.