Skip to content

Instantly share code, notes, and snippets.

@mahasak
Created September 13, 2018 16:27
Show Gist options
  • Select an option

  • Save mahasak/5ccc7cc3ce9f1d1c01e2144c6b44c7fb to your computer and use it in GitHub Desktop.

Select an option

Save mahasak/5ccc7cc3ce9f1d1c01e2144c6b44c7fb to your computer and use it in GitHub Desktop.
pytorch vision image prediction
# Resize the input image nad preprocess it.
image = T.Resize(target_size=(224, 224))(image)
image = T.ToTensor()(image)
# Convert to Torch.Tensor and normalize.
image = T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(image)
with torch.no_grad():
image = torch.autograd.Variable(image[None])
# Get prediction results
preds = F.softmax(model(image), dim=1)
# Prepare first k items, from preditions result
results = torch.topk(preds.cpu().data, k=4, dim=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment