File size: 1,303 Bytes
2e8269a
3945011
 
 
 
 
208b551
 
3945011
099b055
3945011
 
 
 
099b055
d24e2d9
099b055
 
 
6bf820d
 
431831a
 
099b055
6bf820d
 
431831a
 
099b055
 
3945011
 
 
 
 
 
0530cb8
3945011
 
03e442c
1
2
3
4
5
6
7
8
9
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
__all__ = ['learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf', 'multi_classification']

from fastai.vision.all import *
import gradio as gr
import timm

def get_y(path): return path.parent.name.split('_')

#load model
learn = load_learner('Model_ConvNext_Base.pkl')

categories = learn.dls.vocab

def tensor2labels(img):
    output = learn.predict(img)
    index = torch.where(output[1].sigmoid()>0.3)[0]
    category = categories[index]
    print(category)
    if 'dog' in category:
        category.remove('dog')
        if len(category)==0:
           return f"Das ist ein Hund and I am not smart enough to know what breed it is :(("
        return f"Das ist ein Hund and belongs to the breed: {category[0]}"
    elif 'cat' in category:
        category.remove('cat')
        if len(category)==0:
            return f"Das ist eine Katze and I am not smart enough to know what breed it is :(("
        return f"Das ist eine Katze and belongs to the breed: {category[0]}"
    else:
      raise Exception


gr.components

image = gr.inputs.Image(shape=(192, 192))
label = gr.outputs.Label()
examples = ['test.jpg', 'test1.jpg','test2.jpg','test3.jpg','test4.jpg','test5.jpg']

intf = gr.Interface(fn=tensor2labels, inputs=image, outputs=label, examples=examples)
intf.launch()