Skip to content

Commit

Permalink
GPU/CPU usage decision
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajpandkar committed Oct 8, 2018
1 parent 3fae5ba commit e0c4623
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def predict(image_path, model_checkpoint, topk=5, device="cpu"):
probabilities = torch.exp(log_probabilities)
probs, indices = probabilities.topk(topk)

probs, indices = probs.to('cpu'), indices.to('cpu')
probs = probs.numpy().squeeze()
indices = indices.numpy().squeeze()
classes = [idx_class_mapping[index] for index in indices]
Expand All @@ -120,10 +121,15 @@ def main():
ap.add_argument("--gpu", help="Use GPU or CPU for training", action="store_true")
args = vars(ap.parse_args())

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

print(args)
device = None
if args["gpu"]:

if args["gpu"] and device == "cuda":
device = "cuda"
elif args["gpu"] and device == "cpu":
print("CUDA not found on device, using CPU instead!")
device = "cpu"
else:
device = "cpu"

Expand Down

0 comments on commit e0c4623

Please sign in to comment.