Ahsen Khaliq
commited on
Commit
•
98d4fbe
1
Parent(s):
9cef26b
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,63 @@
|
|
1 |
-
import
|
2 |
-
import tensorflow as tf
|
3 |
-
import numpy as np
|
4 |
-
import pandas as pd
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
from tensorflow import keras
|
7 |
|
8 |
-
import requests
|
9 |
-
import PIL
|
10 |
-
import io
|
11 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
-
from
|
15 |
|
16 |
import gradio as gr
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def inference(img):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
pred_names = keras.applications.imagenet_utils.decode_predictions(pred.numpy())[0]
|
29 |
|
30 |
result = {}
|
31 |
-
|
32 |
for i in range(5):
|
33 |
-
|
|
|
|
|
34 |
|
35 |
return result
|
36 |
|
37 |
-
inputs = gr.inputs.Image(type='
|
38 |
outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
|
39 |
|
40 |
title = "ConvNeXt"
|
|
|
1 |
+
import sys
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
+
import PIL
|
5 |
+
from PIL import Image
|
6 |
+
import json
|
7 |
+
|
8 |
+
import torch
|
9 |
+
import torchvision
|
10 |
+
import torchvision.transforms as T
|
11 |
|
12 |
|
13 |
+
from timm import create_model
|
14 |
|
15 |
import gradio as gr
|
16 |
|
17 |
+
|
18 |
+
model_name = "convnext_xlarge_in22k"
|
19 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
20 |
+
# create a ConvNeXt model : https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/convnext.py
|
21 |
+
model = create_model(model_name, pretrained=True).to(device)
|
22 |
+
|
23 |
+
# Define transforms for test
|
24 |
+
from timm.data.constants import \
|
25 |
+
IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
26 |
+
|
27 |
+
NORMALIZE_MEAN = IMAGENET_DEFAULT_MEAN
|
28 |
+
NORMALIZE_STD = IMAGENET_DEFAULT_STD
|
29 |
+
SIZE = 256
|
30 |
+
|
31 |
+
# Here we resize smaller edge to 256, no center cropping
|
32 |
+
transforms = [
|
33 |
+
T.Resize(SIZE, interpolation=T.InterpolationMode.BICUBIC),
|
34 |
+
T.ToTensor(),
|
35 |
+
T.Normalize(NORMALIZE_MEAN, NORMALIZE_STD),
|
36 |
+
]
|
37 |
+
|
38 |
+
transforms = T.Compose(transforms)
|
39 |
+
|
40 |
+
os.system("wget https://dl.fbaipublicfiles.com/convnext/label_to_words.json")
|
41 |
+
imagenet_labels = json.load(open('label_to_words.json'))
|
42 |
|
43 |
def inference(img):
|
44 |
+
img_tensor = transforms(img).unsqueeze(0).to(device)
|
45 |
+
# inference
|
46 |
+
output = torch.softmax(model(img_tensor), dim=1)
|
47 |
+
top5 = torch.topk(output, k=5)
|
48 |
+
top5_prob = top5.values[0]
|
49 |
+
top5_indices = top5.indices[0]
|
|
|
|
|
50 |
|
51 |
result = {}
|
52 |
+
|
53 |
for i in range(5):
|
54 |
+
labels = imagenet_labels[str(int(top5_indices[i]))]
|
55 |
+
prob = "{:.2f}%".format(float(top5_prob[i])*100)
|
56 |
+
results[labels] = prob
|
57 |
|
58 |
return result
|
59 |
|
60 |
+
inputs = gr.inputs.Image(type='pil')
|
61 |
outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
|
62 |
|
63 |
title = "ConvNeXt"
|