text: # Copyright 2020 Lorna Authors. All Rights Reserved.
  # Licensed under the Apache License, Version 2.0 (the "License");
  #   you may not use this file except in compliance with the License.
  #   You may obtain a copy of the License at
  #
  #       http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  """
  In this simple example, we load an image, pre-process it, and classify it
  with a pretrained VGG19.
  """
  import json
  
  import torch
  import torchvision.transforms as transforms
  from PIL import Image
  
  from vgg_pytorch import VGG
  
  # Open image
  input_image = Image.open("img.jpg")
  
  # Preprocess image
  preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
  ])
  input_tensor = preprocess(input_image)
  input_batch = input_tensor.unsqueeze(0)  # create a mini-batch as expected by the model
  
  # Load class names
  labels_map = json.load(open("labels_map.txt"))
  labels_map = [labels_map[str(i)] for i in range(1000)]
  
  # Classify with VGG19
  model = VGG.from_pretrained("vgg19")
  model.eval()
  
  # move the input and model to GPU for speed if available
  if torch.cuda.is_available():
    input_batch = input_batch.to("cuda")
    model.to("cuda")
  
  with torch.no_grad():
    logits = model(input_batch)
  preds = torch.topk(logits, k=5).indices.squeeze(0).tolist()
  
  print("-----")
  for idx in preds:
    label = labels_map[idx]
    prob = torch.softmax(logits, dim=1)[0, idx].item()
    print(f"{label:<75} ({prob * 100:.2f}%)")
  