Upload anime_image_classification_pro/base.py with huggingface_hub
Browse files
anime_image_classification_pro/base.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from functools import lru_cache
|
| 4 |
+
from typing import Mapping
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from huggingface_hub import HfFileSystem, hf_hub_download
|
| 8 |
+
from imgutils.data import ImageTyping, load_image
|
| 9 |
+
from natsort import natsorted
|
| 10 |
+
|
| 11 |
+
from onnx_ import _open_onnx_model
|
| 12 |
+
from preprocess import _img_encode
|
| 13 |
+
|
| 14 |
+
hfs = HfFileSystem()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@lru_cache()
|
| 18 |
+
def open_model_from_repo(repository, model):
|
| 19 |
+
runtime = _open_onnx_model(hf_hub_download(repository, f'{model}/model.onnx'))
|
| 20 |
+
with open(hf_hub_download(repository, f'{model}/meta.json'), 'r') as f:
|
| 21 |
+
labels = json.load(f)['labels']
|
| 22 |
+
|
| 23 |
+
return runtime, labels
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class Classification:
|
| 27 |
+
def __init__(self, title: str, repository: str, default_model=None, imgsize: int = 384):
|
| 28 |
+
self.title = title
|
| 29 |
+
self.repository = repository
|
| 30 |
+
self.models = natsorted([
|
| 31 |
+
os.path.dirname(os.path.relpath(file, self.repository))
|
| 32 |
+
for file in hfs.glob(f'{self.repository}/*/model.onnx')
|
| 33 |
+
])
|
| 34 |
+
self.default_model = default_model or self.models[0]
|
| 35 |
+
self.imgsize = imgsize
|
| 36 |
+
|
| 37 |
+
def _open_onnx_model(self, model):
|
| 38 |
+
return open_model_from_repo(self.repository, model)
|
| 39 |
+
|
| 40 |
+
def _gr_classification(self, image: ImageTyping, model_name: str, size=384) -> Mapping[str, float]:
|
| 41 |
+
image = load_image(image, mode='RGB')
|
| 42 |
+
input_ = _img_encode(image, size=(size, size))[None, ...]
|
| 43 |
+
model, labels = self._open_onnx_model(model_name)
|
| 44 |
+
output, = model.run(['output'], {'input': input_})
|
| 45 |
+
|
| 46 |
+
values = dict(zip(labels, map(lambda x: x.item(), output[0])))
|
| 47 |
+
return values
|
| 48 |
+
|
| 49 |
+
def create_gr(self):
|
| 50 |
+
with gr.Tab(self.title):
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
gr_input_image = gr.Image(type='pil', label='Original Image')
|
| 54 |
+
gr_model = gr.Dropdown(self.models, value=self.default_model, label='Model')
|
| 55 |
+
gr_infer_size = gr.Slider(224, 640, value=384, step=32, label='Infer Size')
|
| 56 |
+
gr_submit = gr.Button(value='Submit', variant='primary')
|
| 57 |
+
|
| 58 |
+
with gr.Column():
|
| 59 |
+
gr_output = gr.Label(label='Classes')
|
| 60 |
+
|
| 61 |
+
gr_submit.click(
|
| 62 |
+
self._gr_classification,
|
| 63 |
+
inputs=[gr_input_image, gr_model, gr_infer_size],
|
| 64 |
+
outputs=[gr_output],
|
| 65 |
+
)
|