|
import datasets |
|
import torch |
|
from transformers import AutoFeatureExtractor, AutoModelForImageClassification |
|
|
|
dataset = datasets.load_dataset('beans') |
|
|
|
feature_extractor = AutoFeatureExtractor.from_pretrained("saved_model_files") |
|
model = AutoModelForImageClassification.from_pretrained("saved_model_files") |
|
|
|
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 |
|
|
|
import gradio as gr |
|
|
|
Instruction = "Submit bean-leaf images with different leaf conditions" |
|
title="Bean-leaf-disease Image classification demo" |
|
description = "Drop an Input image to classify, Observe the model prediction across 3 distinct categories." |
|
article = """ |
|
- Select an image from the examples provided as demo image |
|
- Click submit button to make Image classification |
|
- Click clear button to try new Image for classification |
|
""" |
|
|
|
interface = gr.Interface( |
|
classify, |
|
inputs='image', |
|
outputs='label', |
|
instructuction = Instruction, |
|
title = title, |
|
description = description, |
|
article = article, |
|
examples=["image1.jpg", |
|
"image2.jpg", |
|
"image3.jpg", |
|
"image4.jpg"] |
|
) |
|
|
|
interface.launch(debug=True) |
|
|