ttoosi commited on
Commit
feeaf45
·
verified ·
1 Parent(s): d59c855

added more sliders

Browse files
Files changed (1) hide show
  1. app.py +125 -47
app.py CHANGED
@@ -1,54 +1,62 @@
1
- import os
2
-
3
- # If running on Hugging Face Spaces, import the GPU decorator
4
- if "SPACE_ID" in os.environ:
5
- from spaces.zero.decorator import GPU
6
- else:
7
- # Define a dummy GPU decorator when running locally
8
- def GPU(func):
9
- return func
10
-
11
- # Now you can safely import torch and other libraries
12
- import torch
13
  import gradio as gr
14
-
15
  import numpy as np
16
  from PIL import Image
 
 
 
 
 
 
 
17
  import os
18
  import argparse
19
  from inference import GenerativeInferenceModel, get_inference_configs
20
 
21
- # Check if running on Hugging Face Spaces (using 'SPACE_ID' as an example environment variable)
22
- if "SPACE_ID" in os.environ:
23
- default_port = int(os.environ.get("PORT", 8860)) # Use provided PORT or fallback to 7860
24
- else:
25
- default_port = 7860 # Local default port
26
-
27
  # Parse command line arguments
28
  parser = argparse.ArgumentParser(description='Run Generative Inference Demo')
29
- parser.add_argument('--port', type=int, default=default_port, help='Port to run the server on')
30
  args = parser.parse_args()
31
 
32
-
33
-
34
-
35
  # Create model directories if they don't exist
36
  os.makedirs("models", exist_ok=True)
37
  os.makedirs("stimuli", exist_ok=True)
38
 
 
 
 
 
 
 
39
  # Initialize model
40
  model = GenerativeInferenceModel()
41
 
42
  @GPU
43
- def run_inference(image, model_type, illusion_type, eps_value, num_iterations):
 
44
  # Convert eps to float
45
  eps = float(eps_value)
46
 
47
- # Load inference configuration
48
- config = get_inference_configs(eps=eps, n_itr=int(num_iterations))
 
 
 
 
 
 
49
 
50
  # Run generative inference
51
- output_image, all_steps = model.inference(image, model_type, config)
 
 
 
 
 
 
 
 
 
52
 
53
  # Create animation frames
54
  frames = []
@@ -80,15 +88,39 @@ with gr.Blocks(title="Generative Inference Demo") as demo:
80
  label="Model"
81
  )
82
 
83
- illusion_type = gr.Dropdown(
84
- choices=["Kanizsa", "Face-Vase", "Neon-Color", "Figure-Ground"],
85
- value="Kanizsa",
86
- label="Illusion Type"
87
  )
88
 
89
  with gr.Row():
90
- eps_slider = gr.Slider(minimum=0.01, maximum=3.0, value=0.5, step=0.01, label="Epsilon (Perturbation Size)")
91
- iterations_slider = gr.Slider(minimum=10, maximum=200, value=50, step=10, label="Number of Iterations")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  run_button = gr.Button("Run Inference")
94
 
@@ -97,20 +129,39 @@ with gr.Blocks(title="Generative Inference Demo") as demo:
97
  output_image = gr.Image(label="Final Inferred Image")
98
  output_frames = gr.Gallery(label="Inference Steps", columns=4, rows=2)
99
 
100
- # Set up example images
101
  examples = [
102
- [os.path.join("stimuli", "Kanizsa_square.jpg"), "robust_resnet50", "Kanizsa", 0.5, 50],
103
- [os.path.join("stimuli", "face_vase.png"), "robust_resnet50", "Face-Vase", 0.5, 50],
104
- [os.path.join("stimuli", "figure_ground.png"), "robust_resnet50", "Figure-Ground", 0.7, 100],
105
- [os.path.join("stimuli", "Neon_Color_Circle.jpg"), "robust_resnet50", "Neon-Color", 0.3, 80]
 
 
 
 
 
 
 
 
 
 
 
106
  ]
107
 
108
- gr.Examples(examples=examples, inputs=[image_input, model_choice, illusion_type, eps_slider, iterations_slider])
 
 
 
 
109
 
110
  # Set up event handler
111
  run_button.click(
112
  fn=run_inference,
113
- inputs=[image_input, model_choice, illusion_type, eps_slider, iterations_slider],
 
 
 
 
114
  outputs=[output_image, output_frames]
115
  )
116
 
@@ -118,20 +169,47 @@ with gr.Blocks(title="Generative Inference Demo") as demo:
118
  gr.Markdown("""
119
  ## About Generative Inference
120
 
121
- Generative inference is a technique that reveals how neural networks perceive visual stimuli by optimizing the input
122
- to increase the network's confidence in its predictions. This process can reveal emergent perception of contours,
123
- figure-ground separation, and other visual phenomena similar to human perception.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  This demo allows you to:
126
- 1. Upload your own images or select from example illusions
127
- 2. Choose between robust or standard models
128
- 3. Adjust parameters like perturbation size (epsilon) and number of iterations
129
- 4. Visualize how the perception emerges over time
 
 
130
  """)
131
 
132
  # Launch the demo with specific settings
133
  if __name__ == "__main__":
134
  print(f"Starting server on port {args.port}")
 
135
  demo.launch(
136
  server_name="0.0.0.0", # Listen on all interfaces
137
  server_port=args.port, # Use the port from command line arguments
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
  import numpy as np
4
  from PIL import Image
5
+ try:
6
+ from spaces import GPU
7
+ except ImportError:
8
+ # Define a no-op decorator if running locally
9
+ def GPU(func):
10
+ return func
11
+
12
  import os
13
  import argparse
14
  from inference import GenerativeInferenceModel, get_inference_configs
15
 
 
 
 
 
 
 
16
  # Parse command line arguments
17
  parser = argparse.ArgumentParser(description='Run Generative Inference Demo')
18
+ parser.add_argument('--port', type=int, default=7860, help='Port to run the server on')
19
  args = parser.parse_args()
20
 
 
 
 
21
  # Create model directories if they don't exist
22
  os.makedirs("models", exist_ok=True)
23
  os.makedirs("stimuli", exist_ok=True)
24
 
25
+ # Check if running on Hugging Face Spaces (using 'SPACE_ID' as an example environment variable)
26
+ if "SPACE_ID" in os.environ:
27
+ default_port = int(os.environ.get("PORT", 7860)) # Use provided PORT or fallback to 7860
28
+ else:
29
+ default_port = 8861 # Local default port
30
+
31
  # Initialize model
32
  model = GenerativeInferenceModel()
33
 
34
  @GPU
35
+ def run_inference(image, model_type, inference_type, eps_value, num_iterations,
36
+ step_size, initial_noise=0.05, step_noise=0.01, model_layer="all"):
37
  # Convert eps to float
38
  eps = float(eps_value)
39
 
40
+ # Load inference configuration based on the selected type
41
+ config = get_inference_configs(inference_type=inference_type, eps=eps, n_itr=int(num_iterations), step_size=float(step_size))
42
+
43
+ # Handle ReverseDiffusion specific parameters
44
+ if inference_type == "ReverseDiffusion":
45
+ config['initial_inference_noise_ratio'] = float(initial_noise)
46
+ config['diffusion_noise_ratio'] = float(step_noise)
47
+ config['top_layer'] = model_layer
48
 
49
  # Run generative inference
50
+ result = model.inference(image, model_type, config)
51
+
52
+ # Extract results based on return type
53
+ if isinstance(result, tuple):
54
+ # Old format returning (output_image, all_steps)
55
+ output_image, all_steps = result
56
+ else:
57
+ # New format returning dictionary
58
+ output_image = result['final_image']
59
+ all_steps = result['steps']
60
 
61
  # Create animation frames
62
  frames = []
 
88
  label="Model"
89
  )
90
 
91
+ inference_type = gr.Dropdown(
92
+ choices=["IncreaseConfidence", "ReverseDiffusion"],
93
+ value="IncreaseConfidence",
94
+ label="Inference Method"
95
  )
96
 
97
  with gr.Row():
98
+ eps_slider = gr.Slider(minimum=0.0, maximum=50.0, value=0.5, step=0.1, label="Epsilon (Perturbation Size)")
99
+ iterations_slider = gr.Slider(minimum=1, maximum=500, value=50, step=1, label="Number of Iterations")
100
+ step_size_slider = gr.Slider(minimum=0.0, maximum=10.0, value=1.0, step=0.1, label="Step Size")
101
+
102
+ # Additional parameters for ReverseDiffusion that appear conditionally
103
+ with gr.Row(visible=False) as diffusion_params:
104
+ initial_noise_slider = gr.Slider(minimum=0.0, maximum=0.5, value=0.05, step=0.01,
105
+ label="Initial Noise Ratio")
106
+ step_noise_slider = gr.Slider(minimum=0.0, maximum=0.2, value=0.01, step=0.01,
107
+ label="Per-Step Noise Ratio")
108
+
109
+ with gr.Row(visible=False) as layer_params:
110
+ layer_choice = gr.Dropdown(
111
+ choices=["all", "conv1", "bn1", "relu", "maxpool", "layer1", "layer2", "layer3", "layer4", "avgpool"],
112
+ value="all",
113
+ label="Model Layer"
114
+ )
115
+
116
+ # Show/hide parameters based on inference type
117
+ def toggle_params(inference):
118
+ if inference == "ReverseDiffusion":
119
+ return gr.update(visible=True), gr.update(visible=True)
120
+ else:
121
+ return gr.update(visible=False), gr.update(visible=False)
122
+
123
+ inference_type.change(toggle_params, [inference_type], [diffusion_params, layer_params])
124
 
125
  run_button = gr.Button("Run Inference")
126
 
 
129
  output_image = gr.Image(label="Final Inferred Image")
130
  output_frames = gr.Gallery(label="Inference Steps", columns=4, rows=2)
131
 
132
+ # Set up example images with default parameters for all inputs
133
  examples = [
134
+ # IncreaseConfidence examples
135
+ [os.path.join("stimuli", "Kanizsa_square.jpg"), "robust_resnet50", "IncreaseConfidence",
136
+ 0.5, 50, 1.0, 0.05, 0.01, "all"],
137
+ [os.path.join("stimuli", "face_vase.png"), "robust_resnet50", "IncreaseConfidence",
138
+ 0.5, 50, 1.0, 0.05, 0.01, "all"],
139
+ [os.path.join("stimuli", "figure_ground.png"), "robust_resnet50", "IncreaseConfidence",
140
+ 0.7, 100, 1.0, 0.05, 0.01, "all"],
141
+
142
+ # ReverseDiffusion examples with different layers and noise values
143
+ [os.path.join("stimuli", "Neon_Color_Circle.jpg"), "robust_resnet50", "ReverseDiffusion",
144
+ 0.3, 80, 0.8, 0.05, 0.01, "all"],
145
+ [os.path.join("stimuli", "Kanizsa_square.jpg"), "robust_resnet50", "ReverseDiffusion",
146
+ 0.5, 50, 0.8, 0.1, 0.02, "layer4"], # Using layer4 (high-level features)
147
+ [os.path.join("stimuli", "face_vase.png"), "robust_resnet50", "ReverseDiffusion",
148
+ 0.4, 60, 0.8, 0.15, 0.03, "layer1"] # Using layer1 (lower-level features)
149
  ]
150
 
151
+ gr.Examples(examples=examples, inputs=[
152
+ image_input, model_choice, inference_type,
153
+ eps_slider, iterations_slider, step_size_slider,
154
+ initial_noise_slider, step_noise_slider, layer_choice
155
+ ])
156
 
157
  # Set up event handler
158
  run_button.click(
159
  fn=run_inference,
160
+ inputs=[
161
+ image_input, model_choice, inference_type,
162
+ eps_slider, iterations_slider, step_size_slider,
163
+ initial_noise_slider, step_noise_slider, layer_choice
164
+ ],
165
  outputs=[output_image, output_frames]
166
  )
167
 
 
169
  gr.Markdown("""
170
  ## About Generative Inference
171
 
172
+ Generative inference is a technique that reveals how neural networks perceive visual stimuli. This demo offers two methods:
173
+
174
+ ### 1. IncreaseConfidence
175
+ Optimizes the input to increase the network's confidence in its least confident predictions. This reveals how the
176
+ network perceives contours, figure-ground separation, and other visual phenomena similar to human perception.
177
+
178
+ ### 2. ReverseDiffusion
179
+ Starts with a noisy version of the image and guides the optimization to match features of the noisy image.
180
+ This approach can reveal different aspects of visual processing and is inspired by diffusion models.
181
+
182
+ When using ReverseDiffusion, additional parameters become available:
183
+ - **Initial Noise Ratio**: Controls the amount of noise added to the image at the beginning
184
+ - **Per-Step Noise Ratio**: Controls the amount of noise added at each optimization step
185
+ - **Model Layer**: Select a specific layer of the ResNet50 model to extract features from:
186
+ - `all`: Use the full model (default)
187
+ - `conv1`: First convolutional layer
188
+ - `bn1`: First batch normalization layer
189
+ - `relu`: First ReLU activation
190
+ - `maxpool`: Max pooling layer
191
+ - `layer1`: First residual block
192
+ - `layer2`: Second residual block
193
+ - `layer3`: Third residual block
194
+ - `layer4`: Fourth residual block
195
+ - `avgpool`: Average pooling layer
196
+
197
+ Different layers capture different levels of abstraction - earlier layers represent low-level features
198
+ like edges and textures, while later layers represent higher-level features and object parts.
199
 
200
  This demo allows you to:
201
+ 1. Upload your own images or select from example images
202
+ 2. Choose between inference methods (IncreaseConfidence or ReverseDiffusion)
203
+ 3. Select between robust or standard ResNet50 models
204
+ 4. Adjust parameters like perturbation size (epsilon) and number of iterations
205
+ 5. For ReverseDiffusion, fine-tune noise levels and select specific model layers
206
+ 6. Visualize how the perception emerges over time
207
  """)
208
 
209
  # Launch the demo with specific settings
210
  if __name__ == "__main__":
211
  print(f"Starting server on port {args.port}")
212
+ # Simplified launch parameters
213
  demo.launch(
214
  server_name="0.0.0.0", # Listen on all interfaces
215
  server_port=args.port, # Use the port from command line arguments