huzey commited on
Commit
88c1ef1
1 Parent(s): 019cdc1

update ncut(legacy)

Browse files
Files changed (1) hide show
  1. app.py +211 -100
app.py CHANGED
@@ -2,8 +2,11 @@
2
  # %%
3
  USE_SPACES = True
4
 
5
- if USE_SPACES:
6
- import spaces
 
 
 
7
 
8
  import gradio as gr
9
 
@@ -14,10 +17,7 @@ import time
14
 
15
  import gradio as gr
16
 
17
- if USE_SPACES:
18
- from backbone import extract_features
19
- else:
20
- from draft_gradio_backbone import extract_features
21
  from ncut_pytorch import NCUT, eigenvector_to_rgb
22
 
23
 
@@ -88,9 +88,11 @@ def to_pil_images(images):
88
  Image.fromarray((image * 255).cpu().numpy().astype(np.uint8)).resize((256, 256), Image.Resampling.NEAREST)
89
  for image in images
90
  ]
 
91
 
92
  default_images = ['./images/image_0.jpg', './images/image_1.jpg', './images/image_2.jpg', './images/image_3.jpg', './images/image_5.jpg']
93
  default_outputs = ['./images/ncut_0.jpg', './images/ncut_1.jpg', './images/ncut_2.jpg', './images/ncut_3.jpg', './images/ncut_5.jpg']
 
94
 
95
  downscaled_images = ['./images/image_0_small.jpg', './images/image_1_small.jpg', './images/image_2_small.jpg', './images/image_3_small.jpg', './images/image_5_small.jpg']
96
  downscaled_outputs = ['./images/ncut_0_small.jpg', './images/ncut_1_small.jpg', './images/ncut_2_small.jpg', './images/ncut_3_small.jpg', './images/ncut_5_small.jpg']
@@ -113,6 +115,7 @@ def ncut_run(
113
  n_neighbors=500,
114
  min_dist=0.1,
115
  sampling_method="fps",
 
116
  ):
117
  logging_str = ""
118
  if perplexity >= num_sample_tsne or n_neighbors >= num_sample_tsne:
@@ -134,22 +137,44 @@ def ncut_run(
134
  # print(f"Feature extraction time (gpu): {time.time() - start:.2f}s")
135
  logging_str += f"Backbone time: {time.time() - start:.2f}s\n"
136
 
137
- rgb, _logging_str = compute_ncut(
138
- features,
139
- num_eig=num_eig,
140
- num_sample_ncut=num_sample_ncut,
141
- affinity_focal_gamma=affinity_focal_gamma,
142
- knn_ncut=knn_ncut,
143
- knn_tsne=knn_tsne,
144
- num_sample_tsne=num_sample_tsne,
145
- embedding_method=embedding_method,
146
- perplexity=perplexity,
147
- n_neighbors=n_neighbors,
148
- min_dist=min_dist,
149
- sampling_method=sampling_method,
150
- )
151
- logging_str += _logging_str
152
- rgb = dont_use_too_much_green(rgb)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  return to_pil_images(rgb), logging_str
154
 
155
  def _ncut_run(*args, **kwargs):
@@ -205,6 +230,7 @@ def run_fn(
205
  n_neighbors=500,
206
  min_dist=0.1,
207
  sampling_method="fps",
 
208
  ):
209
  if images is None:
210
  gr.Warning("No images selected.")
@@ -228,10 +254,13 @@ def run_fn(
228
  "n_neighbors": n_neighbors,
229
  "min_dist": min_dist,
230
  "sampling_method": sampling_method,
 
231
  }
232
  num_images = len(images)
233
  if num_images > 100:
234
  return super_duper_long_run(images, **kwargs)
 
 
235
  if num_images > 10:
236
  return long_run(images, **kwargs)
237
  if embedding_method == "UMAP":
@@ -244,73 +273,47 @@ def run_fn(
244
  return quick_run(images, **kwargs)
245
 
246
  return quick_run(images, **kwargs)
247
-
248
- with gr.Blocks() as demo:
249
 
250
- with gr.Row():
251
- with gr.Column(scale=5, min_width=200):
252
- gr.Markdown('### Input Images')
253
- input_gallery = gr.Gallery(value=[], label="Select images", show_label=False, elem_id="images", columns=[3], rows=[1], object_fit="contain", height="auto", type="pil", show_share_button=False)
254
- submit_button = gr.Button("🔴RUN", elem_id="submit_button")
255
- clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button')
256
-
257
- gr.Markdown('### Load from Cloud Dataset 👇')
258
- load_images_button = gr.Button("Load Example", elem_id="load-images-button")
259
- example_gallery = gr.Gallery(value=example_items, label="Example Set A", show_label=False, columns=[3], rows=[2], object_fit="scale-down", height="200px", show_share_button=False, elem_id="example-gallery")
260
- hide_button = gr.Button("Hide Example", elem_id="hide-button")
261
-
262
- hide_button.click(
263
- fn=lambda: gr.update(visible=False),
264
- outputs=example_gallery
265
- )
266
-
267
- with gr.Accordion("➜ Load from dataset", open=True):
268
- dataset_names = [
269
- 'UCSC-VLAA/Recap-COCO-30K',
270
- 'nateraw/pascal-voc-2012',
271
- 'johnowhitaker/imagenette2-320',
272
- 'jainr3/diffusiondb-pixelart',
273
- 'JapanDegitalMaterial/Places_in_Japan',
274
- 'Borismile/Anime-dataset',
275
- ]
276
- dataset_dropdown = gr.Dropdown(dataset_names, label="Dataset name", value="UCSC-VLAA/Recap-COCO-30K", elem_id="dataset")
277
- num_images_slider = gr.Slider(1, 200, step=1, label="Number of images", value=9, elem_id="num_images")
278
- random_seed_slider = gr.Number(0, label="Random seed", value=42, elem_id="random_seed")
279
- load_dataset_button = gr.Button("Load Dataset", elem_id="load-dataset-button")
280
-
281
- with gr.Column(scale=5, min_width=200):
282
- gr.Markdown('### Output Images')
283
- output_gallery = gr.Gallery(value=[], label="NCUT Embedding", show_label=False, elem_id="ncut", columns=[3], rows=[1], object_fit="contain", height="auto")
284
- model_dropdown = gr.Dropdown(["SAM(sam_vit_b)", "MobileSAM", "DiNO(dinov2_vitb14_reg)", "CLIP(openai/clip-vit-base-patch16)", "MAE(vit_base)"], label="Backbone", value="SAM(sam_vit_b)", elem_id="model_name")
285
- layer_slider = gr.Slider(0, 11, step=1, label="Backbone: Layer index", value=11, elem_id="layer")
286
- node_type_dropdown = gr.Dropdown(["attn: attention output", "mlp: mlp output", "block: sum of residual"], label="Backbone: Layer type", value="block: sum of residual", elem_id="node_type", info="which feature to take from each layer?")
287
- num_eig_slider = gr.Slider(1, 1000, step=1, label="NCUT: Number of eigenvectors", value=100, elem_id="num_eig", info='increase for more clusters')
288
- affinity_focal_gamma_slider = gr.Slider(0.01, 1, step=0.01, label="NCUT: Affinity focal gamma", value=0.5, elem_id="affinity_focal_gamma", info="decrease for shaper segmentation")
289
-
290
- with gr.Accordion("➜ Click to expand: more parameters", open=False):
291
- num_sample_ncut_slider = gr.Slider(100, 50000, step=100, label="NCUT: num_sample", value=10000, elem_id="num_sample_ncut", info="Nyström approximation")
292
- sampling_method_dropdown = gr.Dropdown(["fps", "random"], label="NCUT: Sampling method", value="fps", elem_id="sampling_method")
293
- knn_ncut_slider = gr.Slider(1, 100, step=1, label="NCUT: KNN", value=10, elem_id="knn_ncut", info="Nyström approximation")
294
- embedding_method_dropdown = gr.Dropdown(["tsne_3d", "umap_3d", "umap_shpere", "tsne_2d", "umap_2d"], label="Coloring method", value="tsne_3d", elem_id="embedding_method")
295
- num_sample_tsne_slider = gr.Slider(100, 1000, step=100, label="t-SNE/UMAP: num_sample", value=300, elem_id="num_sample_tsne", info="Nyström approximation")
296
- knn_tsne_slider = gr.Slider(1, 100, step=1, label="t-SNE/UMAP: KNN", value=10, elem_id="knn_tsne", info="Nyström approximation")
297
- perplexity_slider = gr.Slider(10, 500, step=10, label="t-SNE: Perplexity", value=150, elem_id="perplexity")
298
- n_neighbors_slider = gr.Slider(10, 500, step=10, label="UMAP: n_neighbors", value=150, elem_id="n_neighbors")
299
- min_dist_slider = gr.Slider(0.1, 1, step=0.1, label="UMAP: min_dist", value=0.1, elem_id="min_dist")
300
-
301
- # logging text box
302
- logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
303
-
304
- def load_default_images():
305
- return default_images, default_outputs
306
 
307
- def empty_input_and_output():
308
- return [], []
309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  def load_dataset_images(dataset_name, num_images=10, random_seed=42):
311
  from datasets import load_dataset
312
  try:
313
- dataset = load_dataset(dataset_name)['train']
 
 
314
  except Exception as e:
315
  gr.Error(f"Error loading dataset {dataset_name}: {e}")
316
  return None
@@ -319,27 +322,135 @@ with gr.Blocks() as demo:
319
  image_idx = np.random.RandomState(random_seed).choice(len(dataset), num_images, replace=False)
320
  image_idx = image_idx.tolist()
321
  images = [dataset[i]['image'] for i in image_idx]
322
- return images
323
-
324
-
325
- load_images_button.click(load_default_images, outputs=[input_gallery, output_gallery])
326
- clear_images_button.click(empty_input_and_output, outputs=[input_gallery, output_gallery])
327
  load_dataset_button.click(load_dataset_images, inputs=[dataset_dropdown, num_images_slider, random_seed_slider], outputs=[input_gallery])
328
- submit_button.click(
329
- run_fn,
330
- inputs=[
331
- input_gallery, model_dropdown, layer_slider, num_eig_slider, node_type_dropdown,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
333
  embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
334
- perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
335
- ],
336
- outputs=[output_gallery, logging_text]
337
- )
338
 
 
339
 
340
- if USE_SPACES:
341
- demo.launch()
342
- else:
343
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
 
345
  # %%
 
2
  # %%
3
  USE_SPACES = True
4
 
5
+ if USE_SPACES: # huggingface ZeroGPU
6
+ try:
7
+ import spaces
8
+ except ImportError:
9
+ USE_SPACES = False # run on standard GPU
10
 
11
  import gradio as gr
12
 
 
17
 
18
  import gradio as gr
19
 
20
+ from backbone import extract_features
 
 
 
21
  from ncut_pytorch import NCUT, eigenvector_to_rgb
22
 
23
 
 
88
  Image.fromarray((image * 255).cpu().numpy().astype(np.uint8)).resize((256, 256), Image.Resampling.NEAREST)
89
  for image in images
90
  ]
91
+
92
 
93
  default_images = ['./images/image_0.jpg', './images/image_1.jpg', './images/image_2.jpg', './images/image_3.jpg', './images/image_5.jpg']
94
  default_outputs = ['./images/ncut_0.jpg', './images/ncut_1.jpg', './images/ncut_2.jpg', './images/ncut_3.jpg', './images/ncut_5.jpg']
95
+ default_outputs_independent = ['./images/ncut_0_independent.jpg', './images/ncut_1_independent.jpg', './images/ncut_2_independent.jpg', './images/ncut_3_independent.jpg', './images/ncut_5_independent.jpg']
96
 
97
  downscaled_images = ['./images/image_0_small.jpg', './images/image_1_small.jpg', './images/image_2_small.jpg', './images/image_3_small.jpg', './images/image_5_small.jpg']
98
  downscaled_outputs = ['./images/ncut_0_small.jpg', './images/ncut_1_small.jpg', './images/ncut_2_small.jpg', './images/ncut_3_small.jpg', './images/ncut_5_small.jpg']
 
115
  n_neighbors=500,
116
  min_dist=0.1,
117
  sampling_method="fps",
118
+ old_school_ncut=False,
119
  ):
120
  logging_str = ""
121
  if perplexity >= num_sample_tsne or n_neighbors >= num_sample_tsne:
 
137
  # print(f"Feature extraction time (gpu): {time.time() - start:.2f}s")
138
  logging_str += f"Backbone time: {time.time() - start:.2f}s\n"
139
 
140
+ if not old_school_ncut: # joint across all images
141
+ rgb, _logging_str = compute_ncut(
142
+ features,
143
+ num_eig=num_eig,
144
+ num_sample_ncut=num_sample_ncut,
145
+ affinity_focal_gamma=affinity_focal_gamma,
146
+ knn_ncut=knn_ncut,
147
+ knn_tsne=knn_tsne,
148
+ num_sample_tsne=num_sample_tsne,
149
+ embedding_method=embedding_method,
150
+ perplexity=perplexity,
151
+ n_neighbors=n_neighbors,
152
+ min_dist=min_dist,
153
+ sampling_method=sampling_method,
154
+ )
155
+ logging_str += _logging_str
156
+ rgb = dont_use_too_much_green(rgb)
157
+ if old_school_ncut: # individual images
158
+ logging_str += "Running NCut for each image independently\n"
159
+ rgb = []
160
+ for i_image in range(features.shape[0]):
161
+ feature = features[i_image]
162
+ _rgb, _logging_str = compute_ncut(
163
+ feature[None],
164
+ num_eig=num_eig,
165
+ num_sample_ncut=num_sample_ncut,
166
+ affinity_focal_gamma=affinity_focal_gamma,
167
+ knn_ncut=knn_ncut,
168
+ knn_tsne=knn_tsne,
169
+ num_sample_tsne=num_sample_tsne,
170
+ embedding_method=embedding_method,
171
+ perplexity=perplexity,
172
+ n_neighbors=n_neighbors,
173
+ min_dist=min_dist,
174
+ sampling_method=sampling_method,
175
+ )
176
+ logging_str += _logging_str
177
+ rgb.append(_rgb[0])
178
  return to_pil_images(rgb), logging_str
179
 
180
  def _ncut_run(*args, **kwargs):
 
230
  n_neighbors=500,
231
  min_dist=0.1,
232
  sampling_method="fps",
233
+ old_school_ncut=False,
234
  ):
235
  if images is None:
236
  gr.Warning("No images selected.")
 
254
  "n_neighbors": n_neighbors,
255
  "min_dist": min_dist,
256
  "sampling_method": sampling_method,
257
+ "old_school_ncut": old_school_ncut,
258
  }
259
  num_images = len(images)
260
  if num_images > 100:
261
  return super_duper_long_run(images, **kwargs)
262
+ if num_images > 50:
263
+ return longer_run(images, **kwargs)
264
  if num_images > 10:
265
  return long_run(images, **kwargs)
266
  if embedding_method == "UMAP":
 
273
  return quick_run(images, **kwargs)
274
 
275
  return quick_run(images, **kwargs)
 
 
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
 
 
 
278
 
279
+ def make_input_images_section():
280
+ gr.Markdown('### Input Images')
281
+ input_gallery = gr.Gallery(value=[], label="Select images", show_label=False, elem_id="images", columns=[3], rows=[1], object_fit="contain", height="auto", type="pil", show_share_button=False)
282
+ submit_button = gr.Button("🔴RUN", elem_id="submit_button")
283
+ clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button')
284
+ return input_gallery, submit_button, clear_images_button
285
+
286
+ def make_example_images_section():
287
+ gr.Markdown('### Load from Cloud Dataset 👇')
288
+ load_images_button = gr.Button("Load Example", elem_id="load-images-button")
289
+ example_gallery = gr.Gallery(value=example_items, label="Example Set A", show_label=False, columns=[3], rows=[2], object_fit="scale-down", height="200px", show_share_button=False, elem_id="example-gallery")
290
+ hide_button = gr.Button("Hide Example", elem_id="hide-button")
291
+ hide_button.click(
292
+ fn=lambda: gr.update(visible=False),
293
+ outputs=example_gallery
294
+ )
295
+ return load_images_button, example_gallery, hide_button
296
+
297
+ def make_dataset_images_section():
298
+ with gr.Accordion("➜ Load from dataset", open=True):
299
+ dataset_names = [
300
+ 'UCSC-VLAA/Recap-COCO-30K',
301
+ 'nateraw/pascal-voc-2012',
302
+ 'johnowhitaker/imagenette2-320',
303
+ 'jainr3/diffusiondb-pixelart',
304
+ 'JapanDegitalMaterial/Places_in_Japan',
305
+ 'Borismile/Anime-dataset',
306
+ ]
307
+ dataset_dropdown = gr.Dropdown(dataset_names, label="Dataset name", value="UCSC-VLAA/Recap-COCO-30K", elem_id="dataset")
308
+ num_images_slider = gr.Slider(1, 200, step=1, label="Number of images", value=9, elem_id="num_images")
309
+ random_seed_slider = gr.Number(0, label="Random seed", value=42, elem_id="random_seed")
310
+ load_dataset_button = gr.Button("Load Dataset", elem_id="load-dataset-button")
311
  def load_dataset_images(dataset_name, num_images=10, random_seed=42):
312
  from datasets import load_dataset
313
  try:
314
+ dataset = load_dataset(dataset_name)
315
+ key = list(dataset.keys())[0]
316
+ dataset = dataset[key]
317
  except Exception as e:
318
  gr.Error(f"Error loading dataset {dataset_name}: {e}")
319
  return None
 
322
  image_idx = np.random.RandomState(random_seed).choice(len(dataset), num_images, replace=False)
323
  image_idx = image_idx.tolist()
324
  images = [dataset[i]['image'] for i in image_idx]
325
+ return images
 
 
 
 
326
  load_dataset_button.click(load_dataset_images, inputs=[dataset_dropdown, num_images_slider, random_seed_slider], outputs=[input_gallery])
327
+ return dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button
328
+
329
+ def make_output_images_section():
330
+ gr.Markdown('### Output Images')
331
+ output_gallery = gr.Gallery(value=[], label="NCUT Embedding", show_label=False, elem_id="ncut", columns=[3], rows=[1], object_fit="contain", height="auto")
332
+ return output_gallery
333
+
334
+ def make_parameters_section():
335
+ gr.Markdown('### Parameters')
336
+ model_dropdown = gr.Dropdown(["SAM(sam_vit_b)", "MobileSAM", "DiNO(dinov2_vitb14_reg)", "CLIP(openai/clip-vit-base-patch16)", "MAE(vit_base)"], label="Backbone", value="SAM(sam_vit_b)", elem_id="model_name")
337
+ layer_slider = gr.Slider(0, 11, step=1, label="Backbone: Layer index", value=11, elem_id="layer")
338
+ node_type_dropdown = gr.Dropdown(["attn: attention output", "mlp: mlp output", "block: sum of residual"], label="Backbone: Layer type", value="block: sum of residual", elem_id="node_type", info="which feature to take from each layer?")
339
+ num_eig_slider = gr.Slider(1, 1000, step=1, label="NCUT: Number of eigenvectors", value=100, elem_id="num_eig", info='increase for more clusters')
340
+ affinity_focal_gamma_slider = gr.Slider(0.01, 1, step=0.01, label="NCUT: Affinity focal gamma", value=0.5, elem_id="affinity_focal_gamma", info="decrease for shaper segmentation")
341
+
342
+ with gr.Accordion("➜ Click to expand: more parameters", open=False):
343
+ num_sample_ncut_slider = gr.Slider(100, 50000, step=100, label="NCUT: num_sample", value=10000, elem_id="num_sample_ncut", info="Nyström approximation")
344
+ sampling_method_dropdown = gr.Dropdown(["fps", "random"], label="NCUT: Sampling method", value="fps", elem_id="sampling_method")
345
+ knn_ncut_slider = gr.Slider(1, 100, step=1, label="NCUT: KNN", value=10, elem_id="knn_ncut", info="Nyström approximation")
346
+ embedding_method_dropdown = gr.Dropdown(["tsne_3d", "umap_3d", "umap_shpere", "tsne_2d", "umap_2d"], label="Coloring method", value="tsne_3d", elem_id="embedding_method")
347
+ num_sample_tsne_slider = gr.Slider(100, 1000, step=100, label="t-SNE/UMAP: num_sample", value=300, elem_id="num_sample_tsne", info="Nyström approximation")
348
+ knn_tsne_slider = gr.Slider(1, 100, step=1, label="t-SNE/UMAP: KNN", value=10, elem_id="knn_tsne", info="Nyström approximation")
349
+ perplexity_slider = gr.Slider(10, 500, step=10, label="t-SNE: Perplexity", value=150, elem_id="perplexity")
350
+ n_neighbors_slider = gr.Slider(10, 500, step=10, label="UMAP: n_neighbors", value=150, elem_id="n_neighbors")
351
+ min_dist_slider = gr.Slider(0.1, 1, step=0.1, label="UMAP: min_dist", value=0.1, elem_id="min_dist")
352
+ return [model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
353
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
354
  embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
355
+ perplexity_slider, n_neighbors_slider, min_dist_slider,
356
+ sampling_method_dropdown]
 
 
357
 
358
+ with gr.Blocks() as demo:
359
 
360
+ with gr.Tab('AlignedCut (Images)'):
361
+
362
+ with gr.Row():
363
+ with gr.Column(scale=5, min_width=200):
364
+ input_gallery, submit_button, clear_images_button = make_input_images_section()
365
+ load_images_button, example_gallery, hide_button = make_example_images_section()
366
+ dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button = make_dataset_images_section()
367
+
368
+ with gr.Column(scale=5, min_width=200):
369
+ output_gallery = make_output_images_section()
370
+ [
371
+ model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
372
+ affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
373
+ embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
374
+ perplexity_slider, n_neighbors_slider, min_dist_slider,
375
+ sampling_method_dropdown
376
+ ] = make_parameters_section()
377
+ # logging text box
378
+ logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
379
+ load_images_button.click(lambda x: (default_images, default_outputs), outputs=[input_gallery, output_gallery])
380
+
381
+ clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
382
+ submit_button.click(
383
+ run_fn,
384
+ inputs=[
385
+ input_gallery, model_dropdown, layer_slider, num_eig_slider, node_type_dropdown,
386
+ affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
387
+ embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
388
+ perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
389
+ ],
390
+ outputs=[output_gallery, logging_text]
391
+ )
392
+
393
+ with gr.Tab('NCut (Legacy)'):
394
+ gr.Markdown('#### Ncut, not aligned, no Nyström approximation')
395
+ gr.Markdown('1. Each image is solved independently, _color is not aligned across images_')
396
+ gr.Markdown('2. No Nyström approximation')
397
+
398
+ gr.Markdown('### NCut (Legacy) vs. AlignedCut:')
399
+ with gr.Row():
400
+ with gr.Column(scale=5, min_width=200):
401
+ gr.Markdown('#### Pros')
402
+ gr.Markdown('- Easy Solution. Use less eigenvectors.')
403
+ gr.Markdown('- Exact solution. No Nyström approximation.')
404
+ with gr.Column(scale=5, min_width=200):
405
+ gr.Markdown('#### Cons')
406
+ gr.Markdown('- Not aligned. Distance is not preserved across images. No pseudo-labeling or correspondence.')
407
+ gr.Markdown('- Poor complexity scaling. Unable to handle large number of pixels.')
408
+ gr.Markdown('---')
409
+ with gr.Row():
410
+ with gr.Column(scale=5, min_width=200):
411
+ gr.Markdown(' ')
412
+ with gr.Column(scale=5, min_width=200):
413
+ gr.Markdown('_color is not aligned across images_ 👇')
414
+
415
+
416
+ with gr.Row():
417
+ with gr.Column(scale=5, min_width=200):
418
+ input_gallery, submit_button, clear_images_button = make_input_images_section()
419
+ load_images_button, example_gallery, hide_button = make_example_images_section()
420
+ dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button = make_dataset_images_section()
421
+ example_gallery.visible = False
422
+ hide_button.visible = False
423
+
424
+ with gr.Column(scale=5, min_width=200):
425
+ output_gallery = make_output_images_section()
426
+ [
427
+ model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
428
+ affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
429
+ embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
430
+ perplexity_slider, n_neighbors_slider, min_dist_slider,
431
+ sampling_method_dropdown
432
+ ] = make_parameters_section()
433
+ old_school_ncut_checkbox = gr.Checkbox(label="Old school NCut", value=True, elem_id="old_school_ncut")
434
+ invisible_list = [old_school_ncut_checkbox, num_sample_ncut_slider, knn_ncut_slider,
435
+ num_sample_tsne_slider, knn_tsne_slider, sampling_method_dropdown]
436
+ for item in invisible_list:
437
+ item.visible = False
438
+ # logging text box
439
+ logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
440
+ load_images_button.click(lambda x: (default_images, default_outputs_independent), outputs=[input_gallery, output_gallery])
441
+
442
+ clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
443
+ submit_button.click(
444
+ run_fn,
445
+ inputs=[
446
+ input_gallery, model_dropdown, layer_slider, num_eig_slider, node_type_dropdown,
447
+ affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
448
+ embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
449
+ perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
450
+ old_school_ncut_checkbox
451
+ ],
452
+ outputs=[output_gallery, logging_text]
453
+ )
454
+ demo.launch(share=True)
455
 
456
  # %%