Anirudh Subramanyam
Initial commit
f5e45b8
raw
history blame contribute delete
No virus
1.29 kB
import datasets
import transformers
import torch
import gradio as gr
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
dataset = datasets.load_dataset("beans") # This should be the same as the first line of Python code in this Colab notebook
feature_extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
model = model.eval() # set to eval mode for predictions
labels = dataset['train'].features['labels'].names
def classify(im):
features = feature_extractor(im, return_tensors='pt')
logits = model(features["pixel_values"])[-1]
probability = torch.nn.functional.softmax(logits, dim=-1)
probs = probability[0].detach().numpy()
confidences = {label: float(probs[i]) for i, label in enumerate(labels)}
return confidences
# gradio app
interface = gr.Interface(fn = classify,
inputs = "image",
outputs = "label",
title = "Leaf Health Classifier",
examples =["beans1.jpg", "beans2.jpg", "beans3.jpg"],
description = "A fine tuned ViT based image classfier which returns the health of a bean leaf")
interface.launch(debug=True)