File size: 2,625 Bytes
1a96a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
3e07b83
1a96a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
956e5dc
1a96a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import os
import torch

from model import create_vit
from timeit import default_timer as timer
from typing import Tuple, Dict

# Setup class names
with open("class_names.txt", "r") as f:
  class_names = [name.strip() for name in f.readlines()]

### Model and transforms preparation ###
# Create model and transforms
model, _, _, transforms = create_vit(output_shape=len(class_names), classes=class_names)

# Load saved weights
model.load_state_dict(
    torch.load(f="vit.pth",
               map_location=torch.device("cpu")) # load to CPU
)

### Predict function ###
def predict(img) -> Tuple[Dict, float]:
  # Start a timer
  start_time = timer()

  # Transform the input image for use with the model
  img = transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index

  # Put model into eval mode, make prediction
  model.eval()
  with torch.inference_mode():
    # Pass transformed image through the model and turn the prediction logits into probaiblities
    pred_probs = torch.softmax(model(img).logits, dim=1)

  # Create a prediction label and prediction probability dictionary
  pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}

  # Calculate pred time
  end_time = timer()
  pred_time = round(end_time - start_time, 4)

  # Return pred dict and pred time
  return pred_labels_and_probs, pred_time

### 4. Gradio app ###
# Create title, description and article
title = "A ViT cifar10 Classifier"
description = "An [ViT feature extractor](https://huggingface.co./google/vit-base-patch16-224) computer vision model to classify images on the [10 classes of the cifar10 dataset](https://huggingface.co./datasets/cifar10). [Source Code Found Here](https://colab.research.google.com/drive/1j4NbiMpCqmXN1xw9e2_r77gMdr3WpMnO?usp=drive_link)"
article = "Built with [Gradio](https://github.com/gradio-app/gradio) and [PyTorch](https://pytorch.org/). [Source Code Found Here](https://colab.research.google.com/drive/1j4NbiMpCqmXN1xw9e2_r77gMdr3WpMnO?usp=drive_link)"

# Create example list
example_list = [["examples/" + example] for example in os.listdir("examples")]

# Create the Gradio demo
demo = gr.Interface(fn=predict, # maps inputs to outputs
                    inputs=gr.Image(type="pil"),
                    outputs=[gr.Label(num_top_classes=5, label="Predictions"),
                             gr.Number(label="Prediction time (s)")],
                    examples=example_list,
                    title=title,
                    description=description,
                    article=article)

# Launch the demo
demo.launch()