prithivMLmods commited on
Commit
c9c21a4
·
verified ·
1 Parent(s): 31393b3

Create sketch_126.py

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