Update README.md
Browse files
README.md
CHANGED
@@ -28,3 +28,50 @@ tags:
|
|
28 |
| mobilenetv3_v0_dist | 0.63G | 4.18M | 99.14% | 0.9986 | [confusion](https://huggingface.co/deepghs/anime_real_cls/blob/main/mobilenetv3_v0_dist/plot_confusion.png) | `anime`, `real` |
|
29 |
| caformer_s36_v0 | 22.10G | 37.21M | 99.34% | 0.9988 | [confusion](https://huggingface.co/deepghs/anime_real_cls/blob/main/caformer_s36_v0/plot_confusion.png) | `anime`, `real` |
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
| mobilenetv3_v0_dist | 0.63G | 4.18M | 99.14% | 0.9986 | [confusion](https://huggingface.co/deepghs/anime_real_cls/blob/main/mobilenetv3_v0_dist/plot_confusion.png) | `anime`, `real` |
|
29 |
| caformer_s36_v0 | 22.10G | 37.21M | 99.34% | 0.9988 | [confusion](https://huggingface.co/deepghs/anime_real_cls/blob/main/caformer_s36_v0/plot_confusion.png) | `anime`, `real` |
|
30 |
|
31 |
+
|
32 |
+
```
|
33 |
+
import json
|
34 |
+
import numpy as np
|
35 |
+
from PIL import Image
|
36 |
+
from imgutils.data import load_image, rgb_encode
|
37 |
+
from onnxruntime import InferenceSession, SessionOptions, GraphOptimizationLevel
|
38 |
+
|
39 |
+
class Anime_Real_Cls():
|
40 |
+
def __init__(self, model_dir):
|
41 |
+
model_path = f'{model_dir}/model.onnx'
|
42 |
+
self.model = self.load_local_onnx_model(model_path)
|
43 |
+
with open(f'{model_dir}/meta.json', 'r') as f:
|
44 |
+
self.labels = json.load(f)['labels']
|
45 |
+
|
46 |
+
def _img_encode(self, image_path, size=(384, 384), normalize=(0.5, 0.5)):
|
47 |
+
image = Image.open(image_path)
|
48 |
+
image = load_image(image, mode='RGB')
|
49 |
+
image = image.resize(size, Image.BILINEAR)
|
50 |
+
data = rgb_encode(image, order_='CHW')
|
51 |
+
if normalize:
|
52 |
+
mean_, std_ = normalize
|
53 |
+
mean = np.asarray([mean_]).reshape((-1, 1, 1))
|
54 |
+
std = np.asarray([std_]).reshape((-1, 1, 1))
|
55 |
+
data = (data - mean) / std
|
56 |
+
return data.astype(np.float32)
|
57 |
+
|
58 |
+
def load_local_onnx_model(self, model_path: str) -> InferenceSession:
|
59 |
+
options = SessionOptions()
|
60 |
+
options.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL
|
61 |
+
return InferenceSession(model_path, options)
|
62 |
+
|
63 |
+
def __call__(self, image_path):
|
64 |
+
input_ = self._img_encode(image_path, size=(384, 384))[None, ...]
|
65 |
+
output, = self.model.run(['output'], {'input': input_})
|
66 |
+
values = dict(zip(self.labels, map(lambda x: x.item(), output[0])))
|
67 |
+
print("values: ", values)
|
68 |
+
max_key = max(values, key=values.get)
|
69 |
+
return max_key
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
classifier = Anime_Real_Cls(model_dir="./caformer_s36_v1.3_fixed")
|
73 |
+
image_path = '1.webp'
|
74 |
+
class_result = classifier(image_path)
|
75 |
+
print("class_result: ", class_result)
|
76 |
+
|
77 |
+
```
|