File size: 11,102 Bytes
4a4a3ed
 
 
8328d75
f370b56
4a4a3ed
 
 
 
 
 
 
 
 
 
751073d
4a4a3ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751073d
4a4a3ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751073d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a4a3ed
751073d
 
 
 
 
 
 
 
4a4a3ed
 
 
751073d
4a4a3ed
 
 
 
 
 
 
 
751073d
 
 
 
 
4a4a3ed
 
 
 
 
 
 
529626d
4a4a3ed
 
 
751073d
 
 
 
 
 
 
 
4a4a3ed
 
751073d
 
 
 
 
 
 
 
 
 
 
 
4a4a3ed
751073d
4a4a3ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751073d
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""
Copyright (c) 2024 The D-FINE Authors. All Rights Reserved.
"""
import gradio as gr
import spaces
import os
import sys
import torch
import torch.nn as nn
import torchvision.transforms as T
import supervision as sv
from PIL import Image
import requests
import yaml
import numpy as np
import gc

from src.core import YAMLConfig


model_configs = {
    "dfine_n_coco":
        {"cfgfile": "configs/dfine/dfine_hgnetv2_n_coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_n_coco.pth"},
    "dfine_s_coco":
        {"cfgfile": "configs/dfine/dfine_hgnetv2_s_coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_s_coco.pth"},
    "dfine_m_coco":
        {"cfgfile": "configs/dfine/dfine_hgnetv2_m_coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_m_coco.pth"},
    "dfine_l_coco":
        {"cfgfile": "configs/dfine/dfine_hgnetv2_l_coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_l_coco.pth"},
    "dfine_x_coco":
        {"cfgfile": "configs/dfine/dfine_hgnetv2_x_coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_x_coco.pth"},
    "dfine_s_obj365":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_s_obj365.yml",
         "classinfofile": "configs/obj365.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_s_obj365.pth"},
    "dfine_m_obj365":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_m_obj365.yml",
         "classinfofile": "configs/obj365.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_m_obj365.pth"},
    "dfine_l_obj365":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_l_obj365.yml",
         "classinfofile": "configs/obj365.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_l_obj365.pth"},
    "dfine_l_obj365_e25":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_l_obj365.yml",
         "classinfofile": "configs/obj365.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_l_obj365_e25.pth"},
    "dfine_x_obj365":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_x_obj365.yml",
         "classinfofile": "configs/obj365.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_x_obj365.pth"},
    "dfine_s_obj2coco":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_s_obj2coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_s_obj2coco.pth"},
    "dfine_m_obj2coco":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_m_obj2coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_m_obj2coco.pth"},
    "dfine_l_obj2coco_e25":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_l_obj2coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_l_obj2coco_e25.pth"},
    "dfine_x_obj2coco":
        {"cfgfile": "configs/dfine/objects365/dfine_hgnetv2_x_obj2coco.yml",
         "classinfofile": "configs/coco.yml",
         "weights": "https://github.com/Peterande/storage/releases/download/dfinev1.0/dfine_x_obj2coco.pth"},
}


def download_weights(model_name):
    """Download model weights if not already present"""
    weights_url = model_configs[model_name]["weights"]
    # Directory path to save weight files
    weights_dir = os.path.join(os.path.dirname(__file__), "weights")
    # Weight file path
    weights_path = os.path.join(weights_dir, model_name + ".pth")
    
    # Create weights directory if it doesn't exist
    if not os.path.exists(weights_dir):
        os.makedirs(weights_dir)
        print(f"Created directory: {weights_dir}")
    
    # Check if file already exists
    if os.path.exists(weights_path):
        print(f"Weights file already exists at: {weights_path}")
        return weights_path
    
    # Download file
    print(f"Downloading weights from {weights_url} to {weights_path}...")
    
    response = requests.get(weights_url, stream=True)
    response.raise_for_status()  # Check for download errors
    
    with open(weights_path, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    
    print(f"Downloaded weights to: {weights_path}")
    return weights_path

@torch.no_grad()
def process_image_for_gradio(model, device, image, model_name, threshold=0.4):
    """Process image function for Gradio interface"""
    if isinstance(image, np.ndarray):
        # Convert NumPy array to PIL image
        im_pil = Image.fromarray(image)
    else:
        im_pil = image
    
    # Load class information
    classinfofile = model_configs[model_name]["classinfofile"]
    classinfo = yaml.load(open(classinfofile, "r"), Loader=yaml.FullLoader)["names"]
    indexing_method = "0-based" if "coco" in classinfofile else "1-based"
    
    w, h = im_pil.size
    orig_size = torch.tensor([[w, h]]).to(device)

    transforms = T.Compose(
        [
            T.Resize((640, 640)),
            T.ToTensor(),
        ]
    )
    im_data = transforms(im_pil).unsqueeze(0).to(device)

    output = model(im_data, orig_size)
    labels, boxes, scores = output

    # Visualize results
    detections = sv.Detections(
        xyxy=boxes[0].detach().cpu().numpy(),
        confidence=scores[0].detach().cpu().numpy(),
        class_id=labels[0].detach().cpu().numpy().astype(int),
    )
    detections = detections[detections.confidence > threshold]

    text_scale = sv.calculate_optimal_text_scale(resolution_wh=im_pil.size)
    line_thickness = sv.calculate_optimal_line_thickness(resolution_wh=im_pil.size)

    box_annotator = sv.BoxAnnotator(thickness=line_thickness)
    label_annotator = sv.LabelAnnotator(text_scale=text_scale, smart_position=True)

    label_texts = [
        f"{classinfo[class_id if indexing_method == '0-based' else class_id - 1]} {confidence:.2f}"
        for class_id, confidence
        in zip(detections.class_id, detections.confidence)
    ]

    result_image = im_pil.copy()
    result_image = box_annotator.annotate(scene=result_image, detections=detections)
    result_image = label_annotator.annotate(
        scene=result_image,
        detections=detections,
        labels=label_texts
    )

    detection_info = [
        f"{classinfo[class_id if indexing_method == '0-based' else class_id - 1]}: {confidence:.2f}, bbox: [{xyxy[0]:.1f}, {xyxy[1]:.1f}, {xyxy[2]:.1f}, {xyxy[3]:.1f}]"
        for class_id, confidence, xyxy
        in zip(detections.class_id, detections.confidence, detections.xyxy)
    ]
    
    return result_image, "\n".join(detection_info)


class ModelWrapper(nn.Module):
    def __init__(self, cfg):
        super().__init__()
        self.model = cfg.model.deploy()
        self.postprocessor = cfg.postprocessor.deploy()

    def forward(self, images, orig_target_sizes):
        outputs = self.model(images)
        outputs = self.postprocessor(outputs, orig_target_sizes)
        return outputs


# YAMLConfig ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜ ์ถ”๊ฐ€
def reset_yaml_config():
    """YAMLConfig ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐํ™”"""
    # ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์บ์‹ฑ๋œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ์‚ญ์ œ
    if hasattr(YAMLConfig, '_instances'):
        YAMLConfig._instances = {}
    if hasattr(YAMLConfig, '_configs'):
        YAMLConfig._configs = {}
    
    # ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋ชจ๋“ˆ ์บ์‹œ ๋ฆฌ์…‹
    import importlib
    for module_name in list(sys.modules.keys()):
        if module_name.startswith('src.'):
            try:
                importlib.reload(sys.modules[module_name])
            except:
                pass

def load_model(model_name):
    # ๋ชจ๋ธ ๋กœ๋“œ ์ „์— CUDA ์บ์‹œ์™€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ์ •๋ฆฌ
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    gc.collect()
    
    # YAMLConfig ๋‚ด๋ถ€ ์ƒํƒœ ์ดˆ๊ธฐํ™”
    reset_yaml_config()
    
    cfgfile = model_configs[model_name]["cfgfile"]
    weights_path = download_weights(model_name)
    
    # ์™„์ „ํžˆ ์ƒˆ๋กœ์šด YAMLConfig ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    cfg = YAMLConfig(cfgfile, resume=weights_path)

    if "HGNetv2" in cfg.yaml_cfg:
        cfg.yaml_cfg["HGNetv2"]["pretrained"] = False

    checkpoint = torch.load(weights_path, map_location="cpu")
    state = checkpoint["ema"]["module"] if "ema" in checkpoint else checkpoint["model"]
    
    # ๋ชจ๋ธ ์ƒ์„ฑ ์ „ ํ•œ๋ฒˆ ๋” ํ™•์ธ
    torch.cuda.empty_cache()
    gc.collect()
    
    cfg.model.load_state_dict(state, strict=False)

    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = ModelWrapper(cfg).to(device)
    model.eval()

    return model, device

@spaces.GPU
def process_image(image, model_name, confidence_threshold):
    """Main processing function for Gradio interface"""
    
    # ๋ชจ๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ CUDA ์žฅ์น˜ ๋ฉ”๋ชจ๋ฆฌ ํ™•๋ณด
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    
    # ๋ชจ๋“  Python ๊ฐ์ฒด ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜
    gc.collect()
    
    try:
        print(f"Loading model: {model_name}")
        model, device = load_model(model_name)
        
        # ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ
        result = process_image_for_gradio(model, device, image, model_name, confidence_threshold)
        
        # ๋ชจ๋ธ ๊ฐ์ฒด ๋ฐ ๊ด€๋ จ ๋ฐ์ดํ„ฐ ๋ช…์‹œ์  ์ œ๊ฑฐ
        del model
        
    finally:
        # ํ•ญ์ƒ ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ ๋ณด์žฅ
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        gc.collect()
    
    return result


# Create Gradio interface
demo = gr.Interface(
    fn=process_image,
    inputs=[
        gr.Image(type="pil", label="Input Image"),
        gr.Dropdown(
            choices=list(model_configs.keys()), 
            value="dfine_n_coco", 
            label="Model Selection"
        ),
        gr.Slider(
            minimum=0.1, 
            maximum=0.9, 
            value=0.4, 
            step=0.05, 
            label="Confidence Threshold"
        )
    ],
    outputs=[
        gr.Image(type="pil", label="Detection Result"),
        gr.Textbox(label="Detected Objects")
    ],
    title="D-FINE Object Detection Demo",
    description="Upload an image to see object detection results using the D-FINE model. You can select different models and adjust the confidence threshold.",
    examples=[
        ["examples/image1.jpg", "dfine_n_coco", 0.4],
    ]
)

if __name__ == "__main__":
    # Launch the Gradio app
    demo.launch(share=True)