prithivMLmods commited on
Commit
67726ae
·
verified ·
1 Parent(s): 77532e0

Create painting_126.py

Browse files
Files changed (1) hide show
  1. painting_126.py +74 -0
painting_126.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/Painting-126-DomainNet"
10
+ model = SiglipForImageClassification.from_pretrained(model_name)
11
+ processor = AutoImageProcessor.from_pretrained(model_name)
12
+
13
+ @spaces.GPU
14
+ def painting_classification(image):
15
+ """Predicts the painting 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 for the model
20
+ inputs = processor(images=image, return_tensors="pt")
21
+
22
+ # Get predictions from the model without gradient computation
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
+ # Define the label mapping for each class index
30
+ labels = {
31
+ "0": "aircraft_carrier", "1": "alarm_clock", "2": "ant", "3": "anvil", "4": "asparagus",
32
+ "5": "axe", "6": "banana", "7": "basket", "8": "bathtub", "9": "bear",
33
+ "10": "bee", "11": "bird", "12": "blackberry", "13": "blueberry", "14": "bottlecap",
34
+ "15": "broccoli", "16": "bus", "17": "butterfly", "18": "cactus", "19": "cake",
35
+ "20": "calculator", "21": "camel", "22": "camera", "23": "candle", "24": "cannon",
36
+ "25": "canoe", "26": "carrot", "27": "castle", "28": "cat", "29": "ceiling_fan",
37
+ "30": "cell_phone", "31": "cello", "32": "chair", "33": "chandelier", "34": "coffee_cup",
38
+ "35": "compass", "36": "computer", "37": "cow", "38": "crab", "39": "crocodile",
39
+ "40": "cruise_ship", "41": "dog", "42": "dolphin", "43": "dragon", "44": "drums",
40
+ "45": "duck", "46": "dumbbell", "47": "elephant", "48": "eyeglasses", "49": "feather",
41
+ "50": "fence", "51": "fish", "52": "flamingo", "53": "flower", "54": "foot",
42
+ "55": "fork", "56": "frog", "57": "giraffe", "58": "goatee", "59": "grapes",
43
+ "60": "guitar", "61": "hammer", "62": "helicopter", "63": "helmet", "64": "horse",
44
+ "65": "kangaroo", "66": "lantern", "67": "laptop", "68": "leaf", "69": "lion",
45
+ "70": "lipstick", "71": "lobster", "72": "microphone", "73": "monkey", "74": "mosquito",
46
+ "75": "mouse", "76": "mug", "77": "mushroom", "78": "onion", "79": "panda",
47
+ "80": "peanut", "81": "pear", "82": "peas", "83": "pencil", "84": "penguin",
48
+ "85": "pig", "86": "pillow", "87": "pineapple", "88": "potato", "89": "power_outlet",
49
+ "90": "purse", "91": "rabbit", "92": "raccoon", "93": "rhinoceros", "94": "rifle",
50
+ "95": "saxophone", "96": "screwdriver", "97": "sea_turtle", "98": "see_saw", "99": "sheep",
51
+ "100": "shoe", "101": "skateboard", "102": "snake", "103": "speedboat", "104": "spider",
52
+ "105": "squirrel", "106": "strawberry", "107": "streetlight", "108": "string_bean",
53
+ "109": "submarine", "110": "swan", "111": "table", "112": "teapot", "113": "teddy-bear",
54
+ "114": "television", "115": "the_Eiffel_Tower", "116": "the_Great_Wall_of_China",
55
+ "117": "tiger", "118": "toe", "119": "train", "120": "truck", "121": "umbrella",
56
+ "122": "vase", "123": "watermelon", "124": "whale", "125": "zebra"
57
+ }
58
+
59
+ # Map each label to its corresponding probability (rounded)
60
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
61
+ return predictions
62
+
63
+ # Create Gradio interface for the painting classifier
64
+ iface = gr.Interface(
65
+ fn=painting_classification,
66
+ inputs=gr.Image(type="numpy"),
67
+ outputs=gr.Label(label="Prediction Scores"),
68
+ title="Painting-126-DomainNet Classification",
69
+ description="Upload a painting to classify it into one of 126 categories."
70
+ )
71
+
72
+ # Launch the app
73
+ if __name__ == "__main__":
74
+ iface.launch()