image_classification/app.py

37 lines
1.4 KiB
Python
Raw Normal View History

2023-03-29 09:29:30 +00:00
import gradio as gr
from transformers import BeitImageProcessor, BeitForImageClassification
def inference(img):
pretrained_model_path = "beit-base-patch16-224-pt22k-ft22k"
processor = BeitImageProcessor.from_pretrained(pretrained_model_path)
model = BeitForImageClassification.from_pretrained(pretrained_model_path)
inputs = processor(images=img, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
# model predicts one of the 21,841 ImageNet-22k classes
predicted_class_idx = logits.argmax(-1).item()
# print("Predicted class:", model.config.id2label[predicted_class_idx])
return model.config.id2label[predicted_class_idx]
2023-03-31 06:53:31 +00:00
2023-03-30 03:33:37 +00:00
title = "Image classification:beit-base-patch16-224-pt22k-ft22k"
2023-04-06 09:20:16 +00:00
description = "这是beit-base-patch16-224-pt22k-ft22k的Gradio Demo。用于图像分类。上传你想要的图像或者点击下面的示例来加载它。"
2023-03-29 09:29:30 +00:00
article = "<p style='text-align: center'><a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>Github Repo Pytorch</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_animegan' alt='visitor badge'></center></p>"
2023-03-31 06:53:31 +00:00
examples = [['example_cat.jpg'], ['Masahiro.png']]
2023-03-29 09:29:30 +00:00
demo = gr.Interface(
fn=inference,
inputs=[gr.inputs.Image(type="pil")],
outputs=gr.outputs.Textbox(),
title=title,
description=description,
article=article,
examples=examples)
2023-04-06 09:20:16 +00:00
demo.launch(server_name="0.0.0.0")
2023-03-29 09:29:30 +00:00