fg-mindee
commited on
Commit
·
e6ff5fc
1
Parent(s):
832aa0d
fix: Fixed Gradio app
Browse files
app.py
CHANGED
@@ -9,46 +9,33 @@ from torchvision.transforms.functional import InterpolationMode
|
|
9 |
from holocron import models
|
10 |
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
layout="horizontal",
|
43 |
-
)
|
44 |
-
|
45 |
-
interface.launch(server_port=args.port, show_error=True)
|
46 |
-
|
47 |
-
|
48 |
-
if __name__ == '__main__':
|
49 |
-
parser = argparse.ArgumentParser(description='Holocron image classification demo',
|
50 |
-
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
51 |
-
parser.add_argument("--port", type=int, default=8001, help="Port on which the webserver will be run")
|
52 |
-
args = parser.parse_args()
|
53 |
-
|
54 |
-
main(args)
|
|
|
9 |
from holocron import models
|
10 |
|
11 |
|
12 |
+
model = models.rexnet1_3x(pretrained=True).eval()
|
13 |
+
|
14 |
+
preprocessor = Compose([
|
15 |
+
Resize(model.default_cfg['input_shape'][1:], interpolation=InterpolationMode.BILINEAR),
|
16 |
+
PILToTensor(),
|
17 |
+
ConvertImageDtype(torch.float32),
|
18 |
+
Normalize(model.default_cfg['mean'], model.default_cfg['std'])
|
19 |
+
])
|
20 |
+
|
21 |
+
def predict(img):
|
22 |
+
img = Image.fromarray(img.astype('uint8'), 'RGB')
|
23 |
+
img = preprocessor(img)
|
24 |
+
with torch.inference_mode():
|
25 |
+
prediction = torch.nn.functional.softmax(model(img.unsqueeze(0))[0], dim=0)
|
26 |
+
return {class_name: float(conf) for class_name, conf in zip(model.default_cfg['classes'], prediction)}
|
27 |
+
|
28 |
+
image = gr.inputs.Image()
|
29 |
+
outputs = gr.outputs.Label(num_top_classes=3)
|
30 |
+
|
31 |
+
gr.Interface(
|
32 |
+
fn=predict,
|
33 |
+
inputs=[image],
|
34 |
+
outputs=outputs,
|
35 |
+
title="Holocron: image classification demo",
|
36 |
+
article=("<p style='text-align: center'><a href='https://github.com/frgfm/Holocron'>" "Github Repo</a> | "
|
37 |
+
"<a href='https://frgfm.github.io/Holocron/'>Documentation</a></p>"),
|
38 |
+
live=True,
|
39 |
+
theme="huggingface",
|
40 |
+
layout="horizontal",
|
41 |
+
).launch(enable_queue=True,cache_examples=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|