File size: 5,539 Bytes
dbd2a18
aed0d09
9b2c5e1
aed0d09
e3e8fd7
aed0d09
dbd2a18
 
d00769c
 
c21abf8
2cfd9ce
aed0d09
24f4b49
f7b8e0e
dbd2a18
c3d8605
dbd2a18
aed0d09
 
 
c21abf8
aca98af
90ff42e
 
 
aca98af
 
c21abf8
dbd2a18
aed0d09
fa09b4a
9d244f3
fa09b4a
9d244f3
dbd2a18
aed0d09
d00769c
 
d3127bb
dbd2a18
d3127bb
 
aed0d09
d00769c
 
d3127bb
aed0d09
dbd2a18
d3127bb
f504910
c21abf8
aed0d09
 
0b08201
7b00853
c21abf8
aed0d09
 
 
c21abf8
 
 
 
 
 
 
 
 
 
 
 
 
9e4b6dc
c21abf8
 
aed0d09
 
 
 
 
 
 
9e4b6dc
f4731f9
dbd2a18
 
 
2cfd9ce
 
 
 
 
 
dbd2a18
 
aed0d09
dbd2a18
aed0d09
9d244f3
aed0d09
7838123
dbd2a18
 
aed0d09
408a665
 
 
 
 
dbd2a18
b30ea65
dbd2a18
aed0d09
dbd2a18
 
d00769c
dbd2a18
 
 
 
 
 
aed0d09
dbd2a18
 
aed0d09
dbd2a18
 
 
aed0d09
dbd2a18
 
 
 
 
 
20ca536
dbd2a18
9e4b6dc
69a852c
aed0d09
 
408a665
 
 
 
 
7991981
aed0d09
dbd2a18
408a665
dbd2a18
 
408a665
1a11002
aed0d09
dbd2a18
f4731f9
2cfd9ce
cf8d654
bccb3e3
e92a893
448ae04
 
e92a893
89962b7
e92a893
 
 
 
89962b7
 
e92a893
 
 
 
89962b7
 
 
 
 
 
 
 
 
e92a893
 
89962b7
e92a893
 
89962b7
 
e92a893
89962b7
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
import gradio as gr
import netron
import os
import threading
import time    
from PIL import Image
import cv2
import numpy as np
from yolov5 import xai_yolov5
from yolov8 import xai_yolov8s
import requests
"""
# Sample images directory
sample_images = {
    "Sample 1": os.path.join(os.getcwd(), "data/xai/sample1.jpeg"),
    "Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
}

# Preloaded model file path
preloaded_model_file = os.path.join(os.getcwd(), "weight_files/yolov5.onnx")  # Example path

# Function to load sample image
def load_sample_image(sample_name):
    image_path = sample_images.get(sample_name)
    if image_path and os.path.exists(image_path):
        return Image.open(image_path)
    return None

# Function to process the image
def process_image(sample_choice, uploaded_image, yolo_versions):
    # Use uploaded or sample image
    if uploaded_image is not None:
        image = uploaded_image
    else:
        image = load_sample_image(sample_choice)

    # Resize and process the image
    image = np.array(image)
    image = cv2.resize(image, (640, 640))
    result_images = []

    for yolo_version in yolo_versions:
        if yolo_version == "yolov5":
            result_images.append(xai_yolov5(image))
        elif yolo_version == "yolov8s":
            result_images.append(xai_yolov8s(image))
        else:
            result_images.append((Image.fromarray(image), f"{yolo_version} not implemented."))

    return result_images

# Start Netron backend
def start_netron_backend(model_file):
    def serve_netron():
        netron.start(model_file, address=("0.0.0.0", 8080), browse=False)
        #netron.start(model_file, address="0.0.0.0:8080", browse=False)  # Updated Netron arguments

    # Launch Netron in a separate thread
    threading.Thread(target=serve_netron, daemon=True).start()

    # Wait until Netron server is ready
    def wait_for_netron(url, timeout=10):
        start_time = time.time()
        while time.time() - start_time < timeout:
            try:
                response = requests.get(url)
                if response.status_code == 200:
                    return True
            except requests.ConnectionError:
                time.sleep(0.5)
        return False

    # Check server readiness
    wait_for_netron("http://localhost:8080/", timeout=15)

# View Netron model
def view_netron_model():
    # Ensure model exists
    if not os.path.exists(preloaded_model_file):
        return "Model file not found."

    # Start Netron backend
    start_netron_backend(preloaded_model_file)
    return gr.HTML('<iframe src="http://localhost:8080/" width="100%" height="600px"></iframe>')

# Custom CSS for styling (optional)
custom_css = """
#run_button {
   # background-color: purple;
   # color: white;
  #  width: 120px;
  #  border-radius: 5px;
  #  font-size: 14px;
#}
"""

# Gradio UI
with gr.Blocks(css=custom_css) as interface:
    gr.Markdown("# XAI: Visualize Object Detection of Your Models")

    # Default sample
    default_sample = "Sample 1"

    with gr.Row():
        # Left: Select sample or upload image
        with gr.Column():
            sample_selection = gr.Radio(
                choices=list(sample_images.keys()),
                label="Select a Sample Image",
                type="value",
                value=default_sample,
            )

            upload_image = gr.Image(label="Upload an Image", type="pil")

            selected_models = gr.CheckboxGroup(
                choices=["yolov5", "yolov8s"],
                value=["yolov5"],
                label="Select Model(s)",
            )

            run_button = gr.Button("Run", elem_id="run_button")

        # Right: Display sample image
        with gr.Column():
            sample_display = gr.Image(
                value=load_sample_image(default_sample),
                label="Selected Sample Image",
            )

    # Results and Netron
    with gr.Row():
        result_gallery = gr.Gallery(
            label="Results",
            elem_id="gallery",
            rows=1,
            height=500,
        )

        # Display Netron iframe
        netron_display = gr.HTML(view_netron_model())

    # Sample selection update
    sample_selection.change(
        fn=load_sample_image,
        inputs=sample_selection,
        outputs=sample_display,
    )

    # Process image
    run_button.click(
        fn=process_image,
        inputs=[sample_selection, upload_image, selected_models],
        outputs=[result_gallery],
    )

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

import gradio as gr
import onnxruntime
import os

def visualize_onnx_model(onnx_model_path):
  """
  Visualizes the given ONNX model using Netron.

  Args:
    onnx_model_path: The path to the ONNX model file.
  """
  try:
    # Save the ONNX model to a temporary file (optional, but can be helpful)
    temp_model_path = "temp_model.onnx"
    with open(temp_model_path, "wb") as f:
      f.write(onnx_model_path.read())  # Assuming onnx_model_path is a file-like object

    # Run Netron 
    os.system(f"netron {temp_model_path}") 

  except Exception as e:
    print(f"Error visualizing model: {e}")

# Create the Gradio interface
iface = gr.Interface(
    fn=visualize_onnx_model,
    inputs="file",  # Accept the ONNX model as a file upload
    outputs="text", 
    title="Netron ONNX Model Visualization",
    description="Upload an ONNX model file to visualize with Netron."
)

# Launch the Gradio app
iface.launch(share=True)