Spaces:
Running
on
Zero
Running
on
Zero
Create multisource_121.py
Browse files- multisource_121.py +73 -0
multisource_121.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
4 |
+
from transformers.image_utils import load_image
|
5 |
+
from PIL import Image
|
6 |
+
import torch
|
7 |
+
|
8 |
+
# Load model and processor
|
9 |
+
model_name = "prithivMLmods/Multisource-121-DomainNet"
|
10 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
11 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
12 |
+
|
13 |
+
@spaces.GPU
|
14 |
+
def multisource_classification(image):
|
15 |
+
"""Predicts the domain category for an input image."""
|
16 |
+
# Convert the input numpy array to a PIL Image and ensure it is in RGB format
|
17 |
+
image = Image.fromarray(image).convert("RGB")
|
18 |
+
|
19 |
+
# Process the image and convert it to model inputs
|
20 |
+
inputs = processor(images=image, return_tensors="pt")
|
21 |
+
|
22 |
+
# Get model predictions without gradient calculations
|
23 |
+
with torch.no_grad():
|
24 |
+
outputs = model(**inputs)
|
25 |
+
logits = outputs.logits
|
26 |
+
# Convert logits to probabilities using softmax
|
27 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
28 |
+
|
29 |
+
# Mapping from class indices to domain labels
|
30 |
+
labels = {
|
31 |
+
"0": "barn", "1": "baseball_bat", "2": "basket", "3": "beach", "4": "bear",
|
32 |
+
"5": "beard", "6": "bee", "7": "bird", "8": "blueberry", "9": "bowtie",
|
33 |
+
"10": "bracelet", "11": "brain", "12": "bread", "13": "broccoli", "14": "bus",
|
34 |
+
"15": "butterfly", "16": "circle", "17": "cloud", "18": "cruise_ship", "19": "dolphin",
|
35 |
+
"20": "dumbbell", "21": "elephant", "22": "eye", "23": "eyeglasses", "24": "feather",
|
36 |
+
"25": "fish", "26": "flower", "27": "foot", "28": "frog", "29": "giraffe",
|
37 |
+
"30": "goatee", "31": "golf_club", "32": "grapes", "33": "grass", "34": "guitar",
|
38 |
+
"35": "hamburger", "36": "hand", "37": "hat", "38": "headphones", "39": "helicopter",
|
39 |
+
"40": "hexagon", "41": "hockey_stick", "42": "horse", "43": "hourglass", "44": "house",
|
40 |
+
"45": "ice_cream", "46": "jacket", "47": "ladder", "48": "leg", "49": "lipstick",
|
41 |
+
"50": "megaphone", "51": "monkey", "52": "moon", "53": "mushroom", "54": "necklace",
|
42 |
+
"55": "owl", "56": "panda", "57": "pear", "58": "peas", "59": "penguin",
|
43 |
+
"60": "pig", "61": "pillow", "62": "pineapple", "63": "pizza", "64": "pool",
|
44 |
+
"65": "popsicle", "66": "rabbit", "67": "rhinoceros", "68": "rifle", "69": "river",
|
45 |
+
"70": "sailboat", "71": "sandwich", "72": "sea_turtle", "73": "shark", "74": "shoe",
|
46 |
+
"75": "skyscraper", "76": "snorkel", "77": "snowman", "78": "soccer_ball", "79": "speedboat",
|
47 |
+
"80": "spider", "81": "spoon", "82": "square", "83": "squirrel", "84": "stethoscope",
|
48 |
+
"85": "strawberry", "86": "streetlight", "87": "submarine", "88": "suitcase", "89": "sun",
|
49 |
+
"90": "sweater", "91": "sword", "92": "table", "93": "teapot", "94": "teddy-bear",
|
50 |
+
"95": "telephone", "96": "tent", "97": "The_Eiffel_Tower", "98": "The_Great_Wall_of_China",
|
51 |
+
"99": "The_Mona_Lisa", "100": "tiger", "101": "toaster", "102": "tooth", "103": "tornado",
|
52 |
+
"104": "tractor", "105": "train", "106": "tree", "107": "triangle", "108": "trombone",
|
53 |
+
"109": "truck", "110": "trumpet", "111": "umbrella", "112": "vase", "113": "violin",
|
54 |
+
"114": "watermelon", "115": "whale", "116": "windmill", "117": "wine_glass", "118": "yoga",
|
55 |
+
"119": "zebra", "120": "zigzag"
|
56 |
+
}
|
57 |
+
|
58 |
+
# Create a dictionary mapping each label to its corresponding probability (rounded)
|
59 |
+
predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
|
60 |
+
return predictions
|
61 |
+
|
62 |
+
# Create Gradio interface
|
63 |
+
iface = gr.Interface(
|
64 |
+
fn=multisource_classification,
|
65 |
+
inputs=gr.Image(type="numpy"),
|
66 |
+
outputs=gr.Label(label="Prediction Scores"),
|
67 |
+
title="Multisource-121-DomainNet Classification",
|
68 |
+
description="Upload an image to classify it into one of 121 domain categories."
|
69 |
+
)
|
70 |
+
|
71 |
+
# Launch the app
|
72 |
+
if __name__ == "__main__":
|
73 |
+
iface.launch()
|