ducer commited on
Commit
696fdb6
·
verified ·
1 Parent(s): c11724a

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +62 -0
  2. classes.txt +64 -0
  3. requirements.txt +3 -0
  4. resnet18_animals_64.pth +3 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision import models, transforms
4
+ from PIL import Image
5
+ import gradio as gr
6
+
7
+ DEVICE = torch.device("cpu")
8
+ NUM_CLASSES = 64
9
+ MODEL_PATH = "resnet18_animals_64.pth"
10
+ CLASS_NAMES_FILE = "classes.txt"
11
+
12
+ with open(CLASS_NAMES_FILE, "r") as f:
13
+ class_names = [line.strip() for line in f.readlines()]
14
+
15
+ def load_model():
16
+ model = models.resnet18(weights=None)
17
+ num_ftrs = model.fc.in_features
18
+ model.fc = nn.Linear(num_ftrs, NUM_CLASSES)
19
+
20
+ model.load_state_dict(torch.load(MODEL_PATH, map_location=DEVICE))
21
+ model.to(DEVICE)
22
+ model.eval()
23
+ return model
24
+
25
+ model = load_model()
26
+
27
+ image_transforms = transforms.Compose([
28
+ transforms.Resize((224, 224)),
29
+ transforms.ToTensor(),
30
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
31
+ ])
32
+
33
+ def predict(image):
34
+ img_tensor = image_transforms(image).unsqueeze(0)
35
+ img_tensor = img_tensor.to(DEVICE)
36
+
37
+ with torch.no_grad():
38
+ outputs = model(img_tensor)
39
+ probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
40
+
41
+ top3_prob, top3_catid = torch.topk(probabilities, 3)
42
+
43
+ confidences = {}
44
+ for i in range(top3_prob.size(0)):
45
+ confidences[class_names[top3_catid[i]]] = float(top3_prob[i])
46
+
47
+ return confidences
48
+
49
+ title = "Animal Classifier 🐾"
50
+ description = "Nhận diện 64 loài động vật sử dụng mô hình ResNet-18."
51
+ examples = []
52
+
53
+ interface = gr.Interface(
54
+ fn=predict,
55
+ inputs=gr.Image(type="pil"),
56
+ outputs=gr.Label(num_top_classes=3),
57
+ title=title,
58
+ description=description
59
+ )
60
+
61
+ if __name__ == "__main__":
62
+ interface.launch()
classes.txt ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ antelope
2
+ bear
3
+ beaver
4
+ bee
5
+ bison
6
+ blackbird
7
+ buffalo
8
+ butterfly
9
+ camel
10
+ cat
11
+ cheetah
12
+ chimpanzee
13
+ chinchilla
14
+ cow
15
+ crab
16
+ crocodile
17
+ deer
18
+ dog
19
+ dolphin
20
+ donkey
21
+ duck
22
+ eagle
23
+ elephant
24
+ falcon
25
+ ferret
26
+ flamingo
27
+ fox
28
+ frog
29
+ giraffe
30
+ goat
31
+ goose
32
+ gorilla
33
+ grasshopper
34
+ hawk
35
+ hedgehog
36
+ hippopotamus
37
+ hyena
38
+ iguana
39
+ jaguar
40
+ kangaroo
41
+ koala
42
+ lemur
43
+ leopard
44
+ lizard
45
+ lynx
46
+ mole
47
+ mongoose
48
+ ostrich
49
+ otter
50
+ owl
51
+ panda
52
+ peacock
53
+ penguin
54
+ porcupine
55
+ raccoon
56
+ seal
57
+ sheep
58
+ snail
59
+ snake
60
+ spider
61
+ squid
62
+ walrus
63
+ whale
64
+ wolf
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
resnet18_animals_64.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1c88225be3e5fe736a5d23aac1bae057916d021f030cd4baec58e012296bcc12
3
+ size 44918155