File size: 2,076 Bytes
3db1434
 
f35bda7
3db1434
f35bda7
3db1434
 
 
f35bda7
3db1434
 
 
 
 
 
 
 
f35bda7
 
 
 
 
 
 
 
 
3db1434
 
 
 
 
 
 
b33894c
 
 
 
3db1434
f35bda7
3db1434
 
 
 
 
 
f35bda7
a51b850
3db1434
 
f35bda7
608ccc6
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
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
from transformers import pipeline
import os

# Modelle laden
vit_classifier = pipeline("image-classification", model="kuhs/vit-base-oxford-iiit-pets")
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")

# Label-Liste für das CLIP-Modell
labels_oxford_pets = [
    'Siamese', 'Birman', 'shiba inu', 'staffordshire bull terrier', 'basset hound', 'Bombay', 'japanese chin',
    'chihuahua', 'german shorthaired', 'pomeranian', 'beagle', 'english cocker spaniel', 'american pit bull terrier',
    'Ragdoll', 'Persian', 'Egyptian Mau', 'miniature pinscher', 'Sphynx', 'Maine Coon', 'keeshond', 'yorkshire terrier',
    'havanese', 'leonberger', 'wheaten terrier', 'american bulldog', 'english setter', 'boxer', 'newfoundland', 'Bengal',
    'samoyed', 'British Shorthair', 'great pyrenees', 'Abyssinian', 'pug', 'saint bernard', 'Russian Blue', 'scottish terrier'
]

# Beispielbilder vorbereiten
example_images = []
example_dir = "example_images"
if os.path.exists(example_dir):
    for img in os.listdir(example_dir):
        if img.endswith(('.jpg', '.jpeg', '.png')):
            example_images.append([os.path.join(example_dir, img)])

# Klassifikationsfunktion
def classify_pet(image):
    vit_results = vit_classifier(image)
    vit_output = {result['label']: result['score'] for result in vit_results}
    
    clip_results = clip_detector(image, candidate_labels=labels_oxford_pets)
    clip_output = {result['label']: result['score'] for result in clip_results}
    
    return {
        "ViT Classification": vit_output,
        "CLIP Zero-Shot Classification": clip_output
    }

# Gradio-Interface mit Beispielbildern
iface = gr.Interface(
    fn=classify_pet,
    inputs=gr.Image(type="filepath"),
    outputs=gr.JSON(),
    title="Pet Classification Comparison",
    description="Upload an image of a pet, and compare results from a trained ViT model and a zero-shot CLIP model.",
    examples=example_images if example_images else None,
    cache_examples=False
)

# App starten
iface.launch()