Spaces:
Sleeping
Sleeping
File size: 1,664 Bytes
3d7d3eb d20ea93 b95c723 d20ea93 d702dc4 d20ea93 ce15296 aeeae70 ce15296 |
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 |
import transformers
import torch
import torchvision
from transformers import TrainingArguments, Trainer
from transformers import ViTImageProcessor
from transformers import ViTForImageClassification
from torch.utils.data import DataLoader
from datasets import load_dataset
from torchvision.transforms import (CenterCrop,
Compose,
Normalize,
RandomHorizontalFlip,
RandomResizedCrop,
Resize,
ToTensor)
from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import torch
import torch.nn.functional as F
import time
import gradio as gr
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
processor = ViTImageProcessor.from_pretrained("ViT_LCZs_v2",local_files_only=True)
model = ViTForImageClassification.from_pretrained("ViT_LCZs_v2",local_files_only=True).to(device)
def predict(image):
inputs = processor(images=image, return_tensors="pt").to(device)
outputs = model(**inputs)
logits = outputs.logits
predicted_class_prob = F.softmax(logits, dim=-1).detach().cpu().numpy().max()
predicted_class_idx = logits.argmax(-1).item()
label = model.config.id2label[predicted_class_idx].split(",")[0]
time.sleep(2)
return {label: float(predicted_class_prob)}
examples = [['data/closed_highrise.png'], ['data/open_lowrise.png'],['data/dense_trees.png'],['data/large_lowrise.png']]
gr.Interface(predict, gr.Image(type="pil"), "label", examples=examples).launch() |