Update app.py
Browse filesOverwritting by local
app.py
CHANGED
|
@@ -33,6 +33,36 @@ model = GenerativeInferenceModel()
|
|
| 33 |
|
| 34 |
# Define example images and their parameters with updated values from the research
|
| 35 |
examples = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
{
|
| 37 |
"image": os.path.join("stimuli", "Neon_Color_Circle.jpg"),
|
| 38 |
"name": "Neon Color Spreading",
|
|
@@ -185,6 +215,36 @@ examples = [
|
|
| 185 |
"iterations": 101,
|
| 186 |
"epsilon": 3.0
|
| 187 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
]
|
| 190 |
|
|
@@ -215,6 +275,10 @@ def run_inference(image, model_type, inference_type, eps_value, num_iterations,
|
|
| 215 |
config['step_size'] = step_size_f
|
| 216 |
config['top_layer'] = model_layer
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
# Adaptive epsilon (Gaussian mask)
|
| 219 |
if use_adaptive_eps:
|
| 220 |
config['adaptive_epsilon'] = {
|
|
@@ -323,7 +387,7 @@ def draw_mask_overlay(image, center_x, center_y, radius):
|
|
| 323 |
draw.ellipse(
|
| 324 |
[cx_px - radius_px, cy_px - radius_px, cx_px + radius_px, cy_px + radius_px],
|
| 325 |
outline="#E11D48",
|
| 326 |
-
width=max(2, min(w, h) // 150),
|
| 327 |
)
|
| 328 |
# Center dot
|
| 329 |
r = max(2, min(w, h) // 80)
|
|
@@ -331,21 +395,29 @@ def draw_mask_overlay(image, center_x, center_y, radius):
|
|
| 331 |
return img
|
| 332 |
|
| 333 |
|
| 334 |
-
# Helper function to apply example parameters (adaptive mask off by default
|
| 335 |
def apply_example(example):
|
|
|
|
| 336 |
return [
|
| 337 |
example["image"],
|
| 338 |
-
"resnet50_robust",
|
| 339 |
example["method"],
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
gr.Group(visible=True),
|
| 350 |
]
|
| 351 |
|
|
@@ -360,16 +432,15 @@ with gr.Blocks(title="Human Hallucination Prediction", css="""
|
|
| 360 |
background-color: #7C3AED !important;
|
| 361 |
}
|
| 362 |
""") as demo:
|
| 363 |
-
gr.Markdown("#
|
| 364 |
-
gr.Markdown("**Predict what visual hallucinations humans
|
| 365 |
|
| 366 |
gr.Markdown("""
|
| 367 |
**How to predict hallucinations:**
|
| 368 |
-
1. **Select an example
|
| 369 |
2. **Click "Run Generative Inference"** to predict what hallucination humans will perceive
|
| 370 |
3. **View the prediction**: Watch as the model reveals the perceptual structures it expects—matching what humans typically hallucinate
|
| 371 |
-
4. **
|
| 372 |
-
""")
|
| 373 |
|
| 374 |
# Main processing interface
|
| 375 |
with gr.Row():
|
|
@@ -403,21 +474,21 @@ with gr.Blocks(title="Human Hallucination Prediction", css="""
|
|
| 403 |
)
|
| 404 |
|
| 405 |
with gr.Row():
|
| 406 |
-
eps_slider = gr.Slider(minimum=0.0, maximum=40.0, value=
|
| 407 |
-
iterations_slider = gr.Slider(minimum=1, maximum=600, value=
|
| 408 |
|
| 409 |
with gr.Row():
|
| 410 |
-
initial_noise_slider = gr.Slider(minimum=0.0, maximum=1.0, value=
|
| 411 |
label="Drift Noise")
|
| 412 |
diffusion_noise_slider = gr.Slider(minimum=0.0, maximum=0.05, value=0.002, step=0.001,
|
| 413 |
label="Diffusion Noise")
|
| 414 |
|
| 415 |
with gr.Row():
|
| 416 |
-
step_size_slider = gr.Slider(minimum=0.01, maximum=2.0, value=
|
| 417 |
label="Update Rate")
|
| 418 |
layer_choice = gr.Dropdown(
|
| 419 |
choices=["all", "conv1", "bn1", "relu", "maxpool", "layer1", "layer2", "layer3", "layer4", "avgpool"],
|
| 420 |
-
value="
|
| 421 |
label="Model Layer"
|
| 422 |
)
|
| 423 |
|
|
@@ -427,17 +498,17 @@ with gr.Blocks(title="Human Hallucination Prediction", css="""
|
|
| 427 |
use_adaptive_eps_check = gr.Checkbox(value=False, label="Use adaptive epsilon (stronger/weaker constraint by region)")
|
| 428 |
use_adaptive_step_check = gr.Checkbox(value=True, label="Use adaptive step size (stronger/weaker updates by region)")
|
| 429 |
with gr.Row():
|
| 430 |
-
mask_center_x_slider = gr.Slider(minimum=-1.0, maximum=1.0, value=0.
|
| 431 |
mask_center_y_slider = gr.Slider(minimum=-1.0, maximum=1.0, value=0.0, step=0.05, label="Mask center Y")
|
| 432 |
with gr.Row():
|
| 433 |
mask_radius_slider = gr.Slider(minimum=0.01, maximum=1.0, value=0.2, step=0.01, label="Mask radius (flat region size)")
|
| 434 |
mask_sigma_slider = gr.Slider(minimum=0.05, maximum=0.5, value=0.2, step=0.01, label="Mask sigma (fall-off outside radius)")
|
| 435 |
with gr.Row():
|
| 436 |
-
eps_max_mult_slider = gr.Slider(minimum=0.1, maximum=
|
| 437 |
eps_min_mult_slider = gr.Slider(minimum=0.1, maximum=10.0, value=1.0, step=0.1, label="Epsilon: multiplier at periphery")
|
| 438 |
with gr.Row():
|
| 439 |
-
step_max_mult_slider = gr.Slider(minimum=0.1, maximum=
|
| 440 |
-
step_min_mult_slider = gr.Slider(minimum=0.1, maximum=10.0, value=
|
| 441 |
|
| 442 |
with gr.Column(scale=2):
|
| 443 |
# Outputs
|
|
@@ -445,8 +516,8 @@ with gr.Blocks(title="Human Hallucination Prediction", css="""
|
|
| 445 |
output_frames = gr.Gallery(label="Hallucination Prediction Process", columns=5, rows=2)
|
| 446 |
|
| 447 |
# Examples section with integrated explanations
|
| 448 |
-
gr.Markdown("##
|
| 449 |
-
gr.Markdown("Select an
|
| 450 |
|
| 451 |
# For each example, create a row with the image and explanation side by side
|
| 452 |
for i, ex in enumerate(examples):
|
|
|
|
| 33 |
|
| 34 |
# Define example images and their parameters with updated values from the research
|
| 35 |
examples = [
|
| 36 |
+
{
|
| 37 |
+
"image": os.path.join("stimuli", "urbanoffice1.jpg"),
|
| 38 |
+
"name": "UrbanOffice1",
|
| 39 |
+
"wiki": "https://en.wikipedia.org/wiki/Visual_perception",
|
| 40 |
+
"papers": [
|
| 41 |
+
"[Adversarially Robust Vision](https://github.com/MadryLab/robustness)",
|
| 42 |
+
"[Generative Inference](https://doi.org/10.1016/j.tics.2003.08.003)"
|
| 43 |
+
],
|
| 44 |
+
"method": "Prior-Guided Drift Diffusion",
|
| 45 |
+
"reverse_diff": {
|
| 46 |
+
"model": "resnet50_robust",
|
| 47 |
+
"layer": "all",
|
| 48 |
+
"initial_noise": 1.0,
|
| 49 |
+
"diffusion_noise": 0.002,
|
| 50 |
+
"step_size": 1.0,
|
| 51 |
+
"iterations": 500,
|
| 52 |
+
"epsilon": 40.0
|
| 53 |
+
},
|
| 54 |
+
"inference_normalization": "off",
|
| 55 |
+
"use_adaptive_eps": False,
|
| 56 |
+
"use_adaptive_step": True,
|
| 57 |
+
"mask_center_x": 0.5,
|
| 58 |
+
"mask_center_y": 0.0,
|
| 59 |
+
"mask_radius": 0.2,
|
| 60 |
+
"mask_sigma": 0.2,
|
| 61 |
+
"eps_max_mult": 20.0,
|
| 62 |
+
"eps_min_mult": 1.0,
|
| 63 |
+
"step_max_mult": 50.0,
|
| 64 |
+
"step_min_mult": 0.2,
|
| 65 |
+
},
|
| 66 |
{
|
| 67 |
"image": os.path.join("stimuli", "Neon_Color_Circle.jpg"),
|
| 68 |
"name": "Neon Color Spreading",
|
|
|
|
| 215 |
"iterations": 101,
|
| 216 |
"epsilon": 3.0
|
| 217 |
}
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"image": os.path.join("stimuli", "urbanoffice1.jpg"),
|
| 221 |
+
"name": "UrbanOffice1",
|
| 222 |
+
"wiki": "https://en.wikipedia.org/wiki/Visual_perception",
|
| 223 |
+
"papers": [
|
| 224 |
+
"[Adversarially Robust Vision](https://github.com/MadryLab/robustness)",
|
| 225 |
+
"[Generative Inference](https://doi.org/10.1016/j.tics.2003.08.003)"
|
| 226 |
+
],
|
| 227 |
+
"method": "Prior-Guided Drift Diffusion",
|
| 228 |
+
"reverse_diff": {
|
| 229 |
+
"model": "resnet50_robust",
|
| 230 |
+
"layer": "all",
|
| 231 |
+
"initial_noise": 1.0,
|
| 232 |
+
"diffusion_noise": 0.002,
|
| 233 |
+
"step_size": 1.0,
|
| 234 |
+
"iterations": 500,
|
| 235 |
+
"epsilon": 40.0
|
| 236 |
+
},
|
| 237 |
+
"inference_normalization": "off",
|
| 238 |
+
"use_adaptive_eps": False,
|
| 239 |
+
"use_adaptive_step": True,
|
| 240 |
+
"mask_center_x": 0.5,
|
| 241 |
+
"mask_center_y": 0.0,
|
| 242 |
+
"mask_radius": 0.2,
|
| 243 |
+
"mask_sigma": 0.2,
|
| 244 |
+
"eps_max_mult": 20.0,
|
| 245 |
+
"eps_min_mult": 1.0,
|
| 246 |
+
"step_max_mult": 50.0,
|
| 247 |
+
"step_min_mult": 0.2,
|
| 248 |
}
|
| 249 |
]
|
| 250 |
|
|
|
|
| 275 |
config['step_size'] = step_size_f
|
| 276 |
config['top_layer'] = model_layer
|
| 277 |
|
| 278 |
+
# Inference normalization off (option removed from UI)
|
| 279 |
+
config['inference_normalization'] = 'off'
|
| 280 |
+
config['recognition_normalization'] = 'off'
|
| 281 |
+
|
| 282 |
# Adaptive epsilon (Gaussian mask)
|
| 283 |
if use_adaptive_eps:
|
| 284 |
config['adaptive_epsilon'] = {
|
|
|
|
| 387 |
draw.ellipse(
|
| 388 |
[cx_px - radius_px, cy_px - radius_px, cx_px + radius_px, cy_px + radius_px],
|
| 389 |
outline="#E11D48",
|
| 390 |
+
width=2 * max(2, min(w, h) // 150),
|
| 391 |
)
|
| 392 |
# Center dot
|
| 393 |
r = max(2, min(w, h) // 80)
|
|
|
|
| 395 |
return img
|
| 396 |
|
| 397 |
|
| 398 |
+
# Helper function to apply example parameters (adaptive mask off by default unless example defines it)
|
| 399 |
def apply_example(example):
|
| 400 |
+
rd = example["reverse_diff"]
|
| 401 |
return [
|
| 402 |
example["image"],
|
| 403 |
+
rd.get("model", "resnet50_robust"),
|
| 404 |
example["method"],
|
| 405 |
+
rd["epsilon"],
|
| 406 |
+
rd["iterations"],
|
| 407 |
+
rd["initial_noise"],
|
| 408 |
+
rd["diffusion_noise"],
|
| 409 |
+
rd["step_size"],
|
| 410 |
+
rd["layer"],
|
| 411 |
+
example.get("use_adaptive_eps", False),
|
| 412 |
+
example.get("use_adaptive_step", False),
|
| 413 |
+
example.get("mask_center_x", 0.0),
|
| 414 |
+
example.get("mask_center_y", 0.0),
|
| 415 |
+
example.get("mask_radius", 0.3),
|
| 416 |
+
example.get("mask_sigma", 0.2),
|
| 417 |
+
example.get("eps_max_mult", 4.0),
|
| 418 |
+
example.get("eps_min_mult", 1.0),
|
| 419 |
+
example.get("step_max_mult", 4.0),
|
| 420 |
+
example.get("step_min_mult", 1.0),
|
| 421 |
gr.Group(visible=True),
|
| 422 |
]
|
| 423 |
|
|
|
|
| 432 |
background-color: #7C3AED !important;
|
| 433 |
}
|
| 434 |
""") as demo:
|
| 435 |
+
gr.Markdown("# Human Hallucination Prediction")
|
| 436 |
+
gr.Markdown("**Predict what visual hallucinations humans may experience** using neural networks.")
|
| 437 |
|
| 438 |
gr.Markdown("""
|
| 439 |
**How to predict hallucinations:**
|
| 440 |
+
1. **Select an example image** below and click "Load Parameters" to set the prediction settings
|
| 441 |
2. **Click "Run Generative Inference"** to predict what hallucination humans will perceive
|
| 442 |
3. **View the prediction**: Watch as the model reveals the perceptual structures it expects—matching what humans typically hallucinate
|
| 443 |
+
4. **You can upload your own images**
|
|
|
|
| 444 |
|
| 445 |
# Main processing interface
|
| 446 |
with gr.Row():
|
|
|
|
| 474 |
)
|
| 475 |
|
| 476 |
with gr.Row():
|
| 477 |
+
eps_slider = gr.Slider(minimum=0.0, maximum=40.0, value=40.0, step=0.01, label="Epsilon (Stimulus Fidelity)")
|
| 478 |
+
iterations_slider = gr.Slider(minimum=1, maximum=600, value=500, step=1, label="Number of Iterations")
|
| 479 |
|
| 480 |
with gr.Row():
|
| 481 |
+
initial_noise_slider = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.01,
|
| 482 |
label="Drift Noise")
|
| 483 |
diffusion_noise_slider = gr.Slider(minimum=0.0, maximum=0.05, value=0.002, step=0.001,
|
| 484 |
label="Diffusion Noise")
|
| 485 |
|
| 486 |
with gr.Row():
|
| 487 |
+
step_size_slider = gr.Slider(minimum=0.01, maximum=2.0, value=1.0, step=0.01,
|
| 488 |
label="Update Rate")
|
| 489 |
layer_choice = gr.Dropdown(
|
| 490 |
choices=["all", "conv1", "bn1", "relu", "maxpool", "layer1", "layer2", "layer3", "layer4", "avgpool"],
|
| 491 |
+
value="all",
|
| 492 |
label="Model Layer"
|
| 493 |
)
|
| 494 |
|
|
|
|
| 498 |
use_adaptive_eps_check = gr.Checkbox(value=False, label="Use adaptive epsilon (stronger/weaker constraint by region)")
|
| 499 |
use_adaptive_step_check = gr.Checkbox(value=True, label="Use adaptive step size (stronger/weaker updates by region)")
|
| 500 |
with gr.Row():
|
| 501 |
+
mask_center_x_slider = gr.Slider(minimum=-1.0, maximum=1.0, value=0.5, step=0.05, label="Mask center X")
|
| 502 |
mask_center_y_slider = gr.Slider(minimum=-1.0, maximum=1.0, value=0.0, step=0.05, label="Mask center Y")
|
| 503 |
with gr.Row():
|
| 504 |
mask_radius_slider = gr.Slider(minimum=0.01, maximum=1.0, value=0.2, step=0.01, label="Mask radius (flat region size)")
|
| 505 |
mask_sigma_slider = gr.Slider(minimum=0.05, maximum=0.5, value=0.2, step=0.01, label="Mask sigma (fall-off outside radius)")
|
| 506 |
with gr.Row():
|
| 507 |
+
eps_max_mult_slider = gr.Slider(minimum=0.1, maximum=350.0, value=20.0, step=0.1, label="Epsilon: multiplier at center")
|
| 508 |
eps_min_mult_slider = gr.Slider(minimum=0.1, maximum=10.0, value=1.0, step=0.1, label="Epsilon: multiplier at periphery")
|
| 509 |
with gr.Row():
|
| 510 |
+
step_max_mult_slider = gr.Slider(minimum=0.1, maximum=150.0, value=50.0, step=0.1, label="Step size: multiplier at center")
|
| 511 |
+
step_min_mult_slider = gr.Slider(minimum=0.1, maximum=10.0, value=0.2, step=0.1, label="Step size: multiplier at periphery")
|
| 512 |
|
| 513 |
with gr.Column(scale=2):
|
| 514 |
# Outputs
|
|
|
|
| 516 |
output_frames = gr.Gallery(label="Hallucination Prediction Process", columns=5, rows=2)
|
| 517 |
|
| 518 |
# Examples section with integrated explanations
|
| 519 |
+
gr.Markdown("## Examples")
|
| 520 |
+
gr.Markdown("Select an example and click Load Parameters to apply its settings")
|
| 521 |
|
| 522 |
# For each example, create a row with the image and explanation side by side
|
| 523 |
for i, ex in enumerate(examples):
|