Spaces:
Sleeping
Sleeping
File size: 1,906 Bytes
3d7d3eb d20ea93 b95c723 d20ea93 4666a7e 075db70 f52cc6a 075db70 d20ea93 075db70 ac06398 075db70 ce15296 a833d2a 075db70 a833d2a ce15296 dc04f54 |
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 |
import torch
from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import torch.nn.functional as F
import gradio as gr
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
processor = ViTImageProcessor.from_pretrained("ViT_LCZs_v3",local_files_only=True)
model = ViTForImageClassification.from_pretrained("ViT_LCZs_v3",local_files_only=True).to(device)
def classify_image(image):
with torch.no_grad():
model.eval()
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
prob = torch.nn.functional.softmax(logits, dim=1)
top10_prob, top10_indices = torch.topk(prob, 10)
top10_confidences = {}
for i in range(10):
top10_confidences[model.config.id2label[int(top10_indices[0][i])]] = float(top10_prob[0][i])
return top10_confidences #confidences
with gr.Blocks(title="ViT LCZ Classification - ClassCat",
css=".gradio-container {background:white;}"
) as demo:
gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">LCZ Classification with ViT</div>""")
with gr.Row():
input_image = gr.Image(type="pil")
output_label=gr.Label(label="Probabilities", num_top_classes=3)
send_btn = gr.Button("Classify")
send_btn.click(fn=classify_image, inputs=input_image, outputs=output_label)
with gr.Row():
gr.Examples(['data/closed_highrise.png'], label='Closed highrise', inputs=input_image)
gr.Examples(['data/open_lowrise.png'], label='Sparsey built', inputs=input_image)
gr.Examples(['data/dense_trees.png'], label='Dense trees', inputs=input_image)
gr.Examples(['data/large_lowrise.png'], label='Large lowrise', inputs=input_image)
demo.launch(debug=True) |