BhumikaMak commited on
Commit
393122d
·
verified ·
1 Parent(s): 4025380

Delete app1.py

Browse files
Files changed (1) hide show
  1. app1.py +0 -289
app1.py DELETED
@@ -1,289 +0,0 @@
1
- import netron
2
- import threading
3
- import gradio as gr
4
- import os
5
- from PIL import Image
6
- import cv2
7
- import numpy as np
8
- from yolov5 import xai_yolov5
9
- from yolov8 import xai_yolov8s
10
-
11
- # Sample images directory
12
- sample_images = {
13
- "Sample 1": os.path.join(os.getcwd(), "data/xai/sample1.jpeg"),
14
- "Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
15
- }
16
-
17
- def load_sample_image(sample_name):
18
- """Load a sample image based on user selection."""
19
- image_path = sample_images.get(sample_name)
20
- if image_path and os.path.exists(image_path):
21
- return Image.open(image_path)
22
- return None
23
-
24
- def process_image(sample_choice, uploaded_image, yolo_versions, target_lyr = -5, n_components = 8):
25
- """Process the image using selected YOLO models."""
26
- # Load sample or uploaded image
27
- if uploaded_image is not None:
28
- image = uploaded_image
29
- else:
30
- image = load_sample_image(sample_choice)
31
-
32
- # Preprocess image
33
- image = np.array(image)
34
- image = cv2.resize(image, (640, 640))
35
- result_images = []
36
-
37
- # Apply selected models
38
- for yolo_version in yolo_versions:
39
- if yolo_version == "yolov5":
40
- result_images.append(xai_yolov5(image, target_lyr = -5, n_components = 8))
41
- elif yolo_version == "yolov8s":
42
- result_images.append(xai_yolov8s(image))
43
- else:
44
- result_images.append((Image.fromarray(image), f"{yolo_version} not implemented."))
45
- return result_images
46
-
47
- def view_model(selected_models):
48
- """Generate Netron visualization for the selected models."""
49
- netron_html = ""
50
- for model in selected_models:
51
- if model=="yolov8s":
52
- netron_html = f"""
53
- <iframe
54
- src="https://netron.app/?url=https://huggingface.co/spaces/BhumikaMak/NeuralVista/resolve/main/weight_files/yolov8s.pt"
55
- width="100%"
56
- height="800"
57
- frameborder="0">
58
- </iframe>
59
- """
60
- if model == "yolov5":
61
- netron_html = f"""
62
- <iframe
63
- src="https://netron.app/?url=https://huggingface.co/FFusion/FFusionXL-BASE/blob/main/vae_encoder/model.onnx"
64
- width="100%"
65
- height="800"
66
- frameborder="0">
67
- </iframe>
68
- """
69
- return netron_html if netron_html else "<p>No valid models selected for visualization.</p>"
70
-
71
- # CSS to style the Gradio components and HTML content
72
- custom_css = """
73
- body {
74
- background-color: #FFFAFO; /* Navy blue background */
75
- background-image:
76
- linear-gradient(to right, transparent 39px, #a05252 1px, transparent 40px), /* Vertical dashed lines */
77
- background-size: 1800px 1800px; /* Grid cell size */
78
- height: 100%; /* Ensure body height is 100% of the viewport */
79
- margin: 0; /* Remove default margin */
80
- overflow-y: auto; /* Allow vertical scrolling if needed */
81
- }
82
-
83
- .custom-row {
84
- display: flex;
85
- justify-content: center; /* Align horizontally */
86
- align-items: center; /* Align vertically */
87
- padding: 10px; /* Adjust as needed for spacing */
88
- }
89
-
90
- .custom-button {
91
- background-color: #800000;
92
- color: white;
93
- font-size: 12px; /* Small font size */
94
- width: 100px !important; /* Fixed width */
95
- height: 35px !important; /* Fixed height */
96
- border-radius: 6px; /* Slightly rounded corners */
97
- padding: 0 !important; /* Remove extra padding */
98
- cursor: pointer;
99
- text-align: center;
100
- margin: 0 auto; /* Center within its container */
101
- box-sizing: border-box; /* Ensure consistent sizing */
102
- }
103
- #run-button {
104
- background-color: #800000 !important;
105
- color: white !important;
106
- font-size: 12px !important; /* Small font size */
107
- width: 100px !important; /* Fixed width */
108
- height: 35px !important; /* Fixed height */
109
- border-radius: 6px !important;
110
- padding: 0 !important;
111
- text-align: center !important;
112
- display: block !important; /* Ensure block-level alignment */
113
- margin: 0 auto !important; /* Center horizontally */
114
- box-sizing: border-box !important;
115
- }
116
-
117
-
118
- /* Custom border styles for all Gradio components */
119
- .gradio-container, .gradio-row, .gradio-column, .gradio-input, .gradio-image, .gradio-checkgroup, .gradio-button, .gradio-markdown {
120
- border: 3px #800000 !important; /* Border width and color */
121
- border-radius: 8px !important; /* Rounded corners */
122
- }
123
-
124
- /* Additional customizations for images to enhance visibility of the border */
125
- .gradio-image img {
126
- border-radius: 8px !important;
127
- border: 3px solid black !important; /* Border for image */
128
- }
129
-
130
- /* Custom Row for images and buttons */
131
- .custom-row img {
132
- border-radius: 10px;
133
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
134
- }
135
-
136
- #highlighted-text {
137
- font-weight: bold;
138
- color: #1976d2;
139
- }
140
-
141
- .gradio-block {
142
- max-height: 100vh; /* Allow scrolling within the Gradio blocks */
143
- overflow-y: auto; /* Enable scrolling for the content if it overflows */
144
- }
145
- #neural-vista-title {
146
- color: #800000 !important; /* Purple color for the title */
147
- font-size: 32px; /* Adjust font size as needed */
148
- font-weight: bold;
149
- text-align: center;
150
- }
151
- #neural-vista-text {
152
- color: #800000 !important; /* Purple color for the title */
153
- font-size: 18px; /* Adjust font size as needed */
154
- font-weight: bold;
155
- text-align: center;
156
-
157
- }
158
-
159
- """
160
-
161
- # Then in the Gradio interface:
162
-
163
- with gr.Blocks(css=custom_css) as interface:
164
-
165
- gr.HTML("""
166
- <div style="border: 2px solid #a05252; padding: 20px; border-radius: 8px;">
167
- <span style="color: #800000; font-family: 'Papyrus', cursive; font-weight: bold; font-size: 32px;">NeuralVista</span><br><br>
168
- <span style="color: black; font-family: 'Papyrus', cursive; font-size: 18px;">A harmonious framework of tools ☼ designed to illuminate the inner workings of AI.</span>
169
- </div>
170
- """)
171
-
172
- # Default sample
173
- default_sample = "Sample 1"
174
-
175
- with gr.Row():
176
- # Left side: Sample selection and image upload
177
- with gr.Column():
178
- sample_selection = gr.Radio(
179
- choices=list(sample_images.keys()),
180
- label="Select a Sample Image",
181
- value=default_sample,
182
- )
183
-
184
- upload_image = gr.Image(
185
- label="Upload an Image",
186
- type="pil",
187
- )
188
-
189
- selected_models = gr.CheckboxGroup(
190
- choices=["yolov5", "yolov8s"],
191
- value=["yolov5"],
192
- label="Select Model(s)",
193
- )
194
- #with gr.Row(elem_classes="custom-row"):
195
- run_button = gr.Button("Run", elem_id="run-button")
196
-
197
-
198
- with gr.Column():
199
- sample_display = gr.Image(
200
- value=load_sample_image(default_sample),
201
- label="Selected Sample Image",
202
- )
203
-
204
- gr.HTML("""
205
- <span style="font-size: 14px; font-weight: bold;">The visualization demonstrates object detection and interpretability. Detected objects are highlighted with bounding boxes, while the heatmap reveals regions of focus, offering insights into the model's decision-making process.</span>
206
- """)
207
- # Results and visualization
208
- with gr.Row(elem_classes="custom-row"):
209
- result_gallery = gr.Gallery(
210
- label="Results",
211
- rows=1,
212
- height="auto", # Adjust height automatically based on content
213
- columns=1 ,
214
- object_fit="contain"
215
- )
216
- netron_display = gr.HTML(label="Netron Visualization")
217
-
218
- # Update sample image
219
- sample_selection.change(
220
- fn=load_sample_image,
221
- inputs=sample_selection,
222
- outputs=sample_display,
223
- )
224
-
225
-
226
- gr.HTML("""
227
- <span style="font-size: 14px; ">
228
- <span style="color: #800000;">Concept Discovery</span> is the process of uncovering the hidden, high-level features that a deep learning model has learned. It provides a way to understand the essence of its internal representations, akin to peering into the mind of the model and revealing the meaningful patterns it detects in the data.
229
- <br><br>
230
- <span style="color: #800000;">Deep Feature Factorization</span> (DFF) serves as a tool for breaking down these complex features into simpler, more interpretable components. By applying matrix factorization on activation maps, it untangles the intricate web of learned representations, making it easier to comprehend what the model is truly focusing on. Together, these methods bring us closer to understanding the underlying logic of neural networks, shedding light on the often enigmatic decisions they make.
231
- </span>
232
- """)
233
-
234
- with gr.Row(elem_classes="custom-row"):
235
- dff_gallery = gr.Gallery(
236
- label="Deep Feature Factorization",
237
- rows=2, # 8 rows
238
- columns=4, # 1 image per row
239
- object_fit="fit",
240
- height="auto" # Adjust as needed
241
- )
242
-
243
- # Multi-threaded processing
244
- def run_both(sample_choice, uploaded_image, selected_models):
245
- results = []
246
- netron_html = ""
247
-
248
- # Thread to process the image
249
- def process_thread():
250
- nonlocal results
251
- target_lyr = -5
252
- n_components = 8
253
- results = process_image(sample_choice, uploaded_image, selected_models, target_lyr = -5, n_components = 8)
254
-
255
- # Thread to generate Netron visualization
256
- def netron_thread():
257
- nonlocal netron_html
258
- gr.HTML("""
259
- Generated abstract visualizations of model""")
260
- netron_html = view_model(selected_models)
261
-
262
- # Launch threads
263
- t1 = threading.Thread(target=process_thread)
264
- t2 = threading.Thread(target=netron_thread)
265
- t1.start()
266
- t2.start()
267
- t1.join()
268
- t2.join()
269
- image1, text, image2 = results[0]
270
- if isinstance(image2, list):
271
- # Check if image2 contains exactly 8 images
272
- if len(image2) == 8:
273
- print("image2 contains 8 images.")
274
- else:
275
- print("Warning: image2 does not contain exactly 8 images.")
276
- else:
277
- print("Error: image2 is not a list of images.")
278
- return [(image1, text)], netron_html, image2
279
-
280
- # Run button click
281
- run_button.click(
282
- fn=run_both,
283
- inputs=[sample_selection, upload_image, selected_models],
284
- outputs=[result_gallery, netron_display, dff_gallery],
285
- )
286
-
287
- # Launch Gradio interface
288
- if __name__ == "__main__":
289
- interface.launch(share=True)