huzey commited on
Commit
47ba996
1 Parent(s): 9413eda

update color and tsne metric

Browse files
Files changed (1) hide show
  1. app.py +109 -33
app.py CHANGED
@@ -30,7 +30,8 @@ import threading
30
 
31
  from ncut_pytorch.backbone import extract_features, load_model
32
  from ncut_pytorch.backbone import MODEL_DICT, LAYER_DICT, RES_DICT
33
- from ncut_pytorch import NCUT, eigenvector_to_rgb
 
34
 
35
  DATASET_TUPS = [
36
  # (name, num_classes)
@@ -66,6 +67,7 @@ def compute_ncut(
66
  knn_ncut=10,
67
  knn_tsne=10,
68
  embedding_method="UMAP",
 
69
  num_sample_tsne=300,
70
  perplexity=150,
71
  n_neighbors=150,
@@ -100,6 +102,7 @@ def compute_ncut(
100
  _, rgb = eigenvector_to_rgb(
101
  eigvecs,
102
  method=embedding_method,
 
103
  num_sample=num_sample_tsne,
104
  perplexity=perplexity,
105
  n_neighbors=n_neighbors,
@@ -123,14 +126,20 @@ def dont_use_too_much_green(image_rgb):
123
  return image_rgb
124
 
125
 
126
- def to_pil_images(images, target_size=256):
127
  size = images[0].shape[1]
128
  multiplier = target_size // size
129
  res = int(size * multiplier)
130
- return [
131
- Image.fromarray((image * 255).cpu().numpy().astype(np.uint8)).resize((res, res), Image.Resampling.NEAREST)
132
- for image in images
133
- ]
 
 
 
 
 
 
134
 
135
 
136
 
@@ -221,6 +230,7 @@ def ncut_run(
221
  num_sample_ncut=10000,
222
  knn_ncut=10,
223
  embedding_method="UMAP",
 
224
  num_sample_tsne=1000,
225
  knn_tsne=10,
226
  perplexity=500,
@@ -304,6 +314,7 @@ def ncut_run(
304
  knn_tsne=knn_tsne,
305
  num_sample_tsne=num_sample_tsne,
306
  embedding_method=embedding_method,
 
307
  perplexity=perplexity,
308
  n_neighbors=n_neighbors,
309
  min_dist=min_dist,
@@ -333,6 +344,7 @@ def ncut_run(
333
  knn_tsne=10,
334
  num_sample_tsne=300,
335
  embedding_method=embedding_method,
 
336
  perplexity=perplexity,
337
  n_neighbors=n_neighbors,
338
  min_dist=min_dist,
@@ -351,6 +363,7 @@ def ncut_run(
351
  knn_tsne=knn_tsne,
352
  num_sample_tsne=num_sample_tsne,
353
  embedding_method=embedding_method,
 
354
  perplexity=perplexity,
355
  n_neighbors=n_neighbors,
356
  min_dist=min_dist,
@@ -378,7 +391,7 @@ def ncut_run(
378
  galleries = []
379
  for i_prompt in range(len(lisa_prompts)):
380
  _rgb = rgb[i_prompt]
381
- galleries.append(to_pil_images(_rgb, target_size=256))
382
  return *galleries, logging_str
383
 
384
  rgb = dont_use_too_much_green(rgb)
@@ -584,6 +597,7 @@ def run_fn(
584
  num_sample_ncut=10000,
585
  knn_ncut=10,
586
  embedding_method="UMAP",
 
587
  num_sample_tsne=1000,
588
  knn_tsne=10,
589
  perplexity=500,
@@ -699,6 +713,7 @@ def run_fn(
699
  "num_sample_ncut": num_sample_ncut,
700
  "knn_ncut": knn_ncut,
701
  "embedding_method": embedding_method,
 
702
  "num_sample_tsne": num_sample_tsne,
703
  "knn_tsne": knn_tsne,
704
  "perplexity": perplexity,
@@ -882,9 +897,61 @@ def make_dataset_images_section(advanced=False, is_random=False):
882
 
883
  return dataset_dropdown, num_images_slider, random_seed_slider, load_images_button
884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
885
  def make_output_images_section():
886
  gr.Markdown('### Output Images')
887
- output_gallery = gr.Gallery(value=[], label="NCUT Embedding", show_label=False, elem_id="ncut", columns=[3], rows=[1], object_fit="contain", height="auto", show_share_button=True)
 
888
  return output_gallery
889
 
890
  def make_parameters_section(is_lisa=False):
@@ -951,6 +1018,7 @@ def make_parameters_section(is_lisa=False):
951
  sampling_method_dropdown = gr.Dropdown(["fps", "random"], label="NCUT: Sampling method", value="fps", elem_id="sampling_method", info="Nyström approximation")
952
  knn_ncut_slider = gr.Slider(1, 100, step=1, label="NCUT: KNN", value=10, elem_id="knn_ncut", info="Nyström approximation")
953
  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")
 
954
  num_sample_tsne_slider = gr.Slider(100, 10000, step=100, label="t-SNE/UMAP: num_sample", value=300, elem_id="num_sample_tsne", info="Nyström approximation")
955
  knn_tsne_slider = gr.Slider(1, 100, step=1, label="t-SNE/UMAP: KNN", value=10, elem_id="knn_tsne", info="Nyström approximation")
956
  perplexity_slider = gr.Slider(10, 1000, step=10, label="t-SNE: perplexity", value=150, elem_id="perplexity")
@@ -958,7 +1026,7 @@ def make_parameters_section(is_lisa=False):
958
  min_dist_slider = gr.Slider(0.1, 1, step=0.1, label="UMAP: min_dist", value=0.1, elem_id="min_dist")
959
  return [model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
960
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
961
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
962
  perplexity_slider, n_neighbors_slider, min_dist_slider,
963
  sampling_method_dropdown, positive_prompt, negative_prompt]
964
 
@@ -980,7 +1048,7 @@ with demo:
980
  [
981
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
982
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
983
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
984
  perplexity_slider, n_neighbors_slider, min_dist_slider,
985
  sampling_method_dropdown, positive_prompt, negative_prompt
986
  ] = make_parameters_section()
@@ -999,7 +1067,7 @@ with demo:
999
  positive_prompt, negative_prompt,
1000
  false_placeholder, no_prompt, no_prompt, no_prompt,
1001
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1002
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1003
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1004
  ],
1005
  outputs=[output_gallery, logging_text],
@@ -1039,7 +1107,7 @@ with demo:
1039
  [
1040
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1041
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1042
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1043
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1044
  sampling_method_dropdown, positive_prompt, negative_prompt
1045
  ] = make_parameters_section()
@@ -1062,7 +1130,7 @@ with demo:
1062
  positive_prompt, negative_prompt,
1063
  false_placeholder, no_prompt, no_prompt, no_prompt,
1064
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1065
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1066
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
1067
  old_school_ncut_checkbox
1068
  ],
@@ -1079,13 +1147,16 @@ with demo:
1079
  with gr.Row():
1080
  with gr.Column(scale=5, min_width=200):
1081
  gr.Markdown('### Output (Recursion #1)')
1082
- l1_gallery = gr.Gallery(value=[], label="Recursion #1", show_label=False, elem_id="ncut_l1", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
 
1083
  with gr.Column(scale=5, min_width=200):
1084
  gr.Markdown('### Output (Recursion #2)')
1085
- l2_gallery = gr.Gallery(value=[], label="Recursion #2", show_label=False, elem_id="ncut_l2", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
 
1086
  with gr.Column(scale=5, min_width=200):
1087
  gr.Markdown('### Output (Recursion #3)')
1088
- l3_gallery = gr.Gallery(value=[], label="Recursion #3", show_label=False, elem_id="ncut_l3", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
 
1089
  with gr.Row():
1090
  with gr.Column(scale=5, min_width=200):
1091
  input_gallery, submit_button, clear_images_button = make_input_images_section()
@@ -1105,7 +1176,7 @@ with demo:
1105
  [
1106
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1107
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1108
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1109
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1110
  sampling_method_dropdown, positive_prompt, negative_prompt
1111
  ] = make_parameters_section()
@@ -1136,7 +1207,7 @@ with demo:
1136
  positive_prompt, negative_prompt,
1137
  false_placeholder, no_prompt, no_prompt, no_prompt,
1138
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1139
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1140
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
1141
  false_placeholder, number_placeholder, true_placeholder,
1142
  l2_num_eig_slider, l3_num_eig_slider, metric_dropdown,
@@ -1156,7 +1227,7 @@ with demo:
1156
  [
1157
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1158
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1159
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1160
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1161
  sampling_method_dropdown, positive_prompt, negative_prompt
1162
  ] = make_parameters_section()
@@ -1179,7 +1250,7 @@ with demo:
1179
  positive_prompt, negative_prompt,
1180
  false_placeholder, no_prompt, no_prompt, no_prompt,
1181
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1182
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1183
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
1184
  place_holder_false, max_frame_number
1185
  ],
@@ -1204,15 +1275,15 @@ with demo:
1204
  with gr.Row():
1205
  with gr.Column(scale=5, min_width=200):
1206
  gr.Markdown('### Output (Prompt #1)')
1207
- l1_gallery = gr.Gallery(value=[], label="Prompt #1", show_label=False, elem_id="ncut_p1", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1208
  prompt1 = gr.Textbox(label="Input Prompt #1", elem_id="prompt1", value="where is the person, include the clothes, don't include the guitar and chair", lines=3)
1209
  with gr.Column(scale=5, min_width=200):
1210
  gr.Markdown('### Output (Prompt #2)')
1211
- l2_gallery = gr.Gallery(value=[], label="Prompt #2", show_label=False, elem_id="ncut_p2", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1212
  prompt2 = gr.Textbox(label="Input Prompt #2", elem_id="prompt2", value="where is the Gibson Les Pual guitar", lines=3)
1213
  with gr.Column(scale=5, min_width=200):
1214
  gr.Markdown('### Output (Prompt #3)')
1215
- l3_gallery = gr.Gallery(value=[], label="Prompt #3", show_label=False, elem_id="ncut_p3", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1216
  prompt3 = gr.Textbox(label="Input Prompt #3", elem_id="prompt3", value="where is the floor", lines=3)
1217
 
1218
  with gr.Row():
@@ -1225,7 +1296,7 @@ with demo:
1225
  [
1226
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1227
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1228
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1229
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1230
  sampling_method_dropdown, positive_prompt, negative_prompt
1231
  ] = make_parameters_section(is_lisa=True)
@@ -1240,7 +1311,7 @@ with demo:
1240
  positive_prompt, negative_prompt,
1241
  true_placeholder, prompt1, prompt2, prompt3,
1242
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1243
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1244
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1245
  ],
1246
  outputs=galleries + [logging_text],
@@ -1273,7 +1344,7 @@ with demo:
1273
  [
1274
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1275
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1276
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1277
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1278
  sampling_method_dropdown, positive_prompt, negative_prompt
1279
  ] = make_parameters_section()
@@ -1309,7 +1380,7 @@ with demo:
1309
  positive_prompt, negative_prompt,
1310
  false_placeholder, no_prompt, no_prompt, no_prompt,
1311
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1312
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1313
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1314
  ],
1315
  # outputs=galleries + [logging_text],
@@ -1320,12 +1391,13 @@ with demo:
1320
  def add_one_model(i_model=1):
1321
  with gr.Column(scale=5, min_width=200) as col:
1322
  gr.Markdown(f'### Output Images')
1323
- output_gallery = gr.Gallery(value=[], label="NCUT Embedding", show_label=False, elem_id=f"ncut{i_model}", columns=[3], rows=[1], object_fit="contain", height="auto", show_fullscreen_button=True)
1324
  submit_button = gr.Button("🔴 RUN", elem_id=f"submit_button{i_model}", variant='primary')
 
1325
  [
1326
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1327
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1328
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1329
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1330
  sampling_method_dropdown, positive_prompt, negative_prompt
1331
  ] = make_parameters_section()
@@ -1341,7 +1413,7 @@ with demo:
1341
  positive_prompt, negative_prompt,
1342
  false_placeholder, no_prompt, no_prompt, no_prompt,
1343
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1344
- embedding_method_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1345
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1346
  ],
1347
  outputs=[output_gallery, logging_text]
@@ -1390,14 +1462,18 @@ with demo:
1390
 
1391
 
1392
  with gr.Tab('📄About'):
1393
- gr.Markdown("##### This demo is for python package `ncut-pytorch`, please visit the [Documentation](https://ncut-pytorch.readthedocs.io/) ")
 
1394
  gr.Markdown("---")
1395
  gr.Markdown("**Normalized Cuts**, aka. spectral clustering, is a graphical method to analyze data grouping in the affinity eigenvector space. It has been widely used for unsupervised segmentation in the 2000s.")
 
1396
  gr.Markdown("---")
1397
- gr.Markdown("##### We have implemented NCut, with some advanced features:")
1398
  gr.Markdown("- **Nyström** Normalized Cut, is a new approximation algorithm developed for large-scale graph cuts, a large-graph of million nodes can be processed in under 10s (cpu) or 2s (gpu).")
1399
  gr.Markdown("- **spectral-tSNE** visualization, a new method to visualize the high-dimensional eigenvector space with 3D RGB cube. Color is aligned across images, color infers distance in representation.")
1400
-
 
 
1401
 
1402
  with gr.Row():
1403
  with gr.Column():
 
30
 
31
  from ncut_pytorch.backbone import extract_features, load_model
32
  from ncut_pytorch.backbone import MODEL_DICT, LAYER_DICT, RES_DICT
33
+ from ncut_pytorch import NCUT
34
+ from ncut_pytorch import eigenvector_to_rgb, rotate_rgb_cube
35
 
36
  DATASET_TUPS = [
37
  # (name, num_classes)
 
67
  knn_ncut=10,
68
  knn_tsne=10,
69
  embedding_method="UMAP",
70
+ embedding_metric='euclidean',
71
  num_sample_tsne=300,
72
  perplexity=150,
73
  n_neighbors=150,
 
102
  _, rgb = eigenvector_to_rgb(
103
  eigvecs,
104
  method=embedding_method,
105
+ metric=embedding_metric,
106
  num_sample=num_sample_tsne,
107
  perplexity=perplexity,
108
  n_neighbors=n_neighbors,
 
126
  return image_rgb
127
 
128
 
129
+ def to_pil_images(images, target_size=512, resize=True):
130
  size = images[0].shape[1]
131
  multiplier = target_size // size
132
  res = int(size * multiplier)
133
+ pil_images = [
134
+ Image.fromarray((image * 255).cpu().numpy().astype(np.uint8))
135
+ for image in images
136
+ ]
137
+ if resize:
138
+ pil_images = [
139
+ image.resize((res, res), Image.Resampling.NEAREST)
140
+ for image in pil_images
141
+ ]
142
+ return pil_images
143
 
144
 
145
 
 
230
  num_sample_ncut=10000,
231
  knn_ncut=10,
232
  embedding_method="UMAP",
233
+ embedding_metric='euclidean',
234
  num_sample_tsne=1000,
235
  knn_tsne=10,
236
  perplexity=500,
 
314
  knn_tsne=knn_tsne,
315
  num_sample_tsne=num_sample_tsne,
316
  embedding_method=embedding_method,
317
+ embedding_metric=embedding_metric,
318
  perplexity=perplexity,
319
  n_neighbors=n_neighbors,
320
  min_dist=min_dist,
 
344
  knn_tsne=10,
345
  num_sample_tsne=300,
346
  embedding_method=embedding_method,
347
+ embedding_metric=embedding_metric,
348
  perplexity=perplexity,
349
  n_neighbors=n_neighbors,
350
  min_dist=min_dist,
 
363
  knn_tsne=knn_tsne,
364
  num_sample_tsne=num_sample_tsne,
365
  embedding_method=embedding_method,
366
+ embedding_metric=embedding_metric,
367
  perplexity=perplexity,
368
  n_neighbors=n_neighbors,
369
  min_dist=min_dist,
 
391
  galleries = []
392
  for i_prompt in range(len(lisa_prompts)):
393
  _rgb = rgb[i_prompt]
394
+ galleries.append(to_pil_images(_rgb))
395
  return *galleries, logging_str
396
 
397
  rgb = dont_use_too_much_green(rgb)
 
597
  num_sample_ncut=10000,
598
  knn_ncut=10,
599
  embedding_method="UMAP",
600
+ embedding_metric='euclidean',
601
  num_sample_tsne=1000,
602
  knn_tsne=10,
603
  perplexity=500,
 
713
  "num_sample_ncut": num_sample_ncut,
714
  "knn_ncut": knn_ncut,
715
  "embedding_method": embedding_method,
716
+ "embedding_metric": embedding_metric,
717
  "num_sample_tsne": num_sample_tsne,
718
  "knn_tsne": knn_tsne,
719
  "perplexity": perplexity,
 
897
 
898
  return dataset_dropdown, num_images_slider, random_seed_slider, load_images_button
899
 
900
+
901
+ def random_rotate_rgb_gallery(images):
902
+ if images is None or len(images) == 0:
903
+ gr.Warning("No images selected.")
904
+ return []
905
+ # read webp images
906
+ images = [Image.open(image[0]).convert("RGB") for image in images]
907
+ images = [np.array(image).astype(np.float32) for image in images]
908
+ images = np.stack(images)
909
+ images = torch.tensor(images) / 255
910
+ position = np.random.choice([1, 2, 4, 5, 6])
911
+ images = rotate_rgb_cube(images, position)
912
+ images = to_pil_images(images, resize=False)
913
+ return images
914
+
915
+ def sequence_rotate_rgb_gallery(images):
916
+ if images is None or len(images) == 0:
917
+ gr.Warning("No images selected.")
918
+ return []
919
+ # read webp images
920
+ images = [Image.open(image[0]).convert("RGB") for image in images]
921
+ images = [np.array(image).astype(np.float32) for image in images]
922
+ images = np.stack(images)
923
+ images = torch.tensor(images) / 255
924
+ rotation_matrix = torch.tensor([[0, 1, 0], [0, 0, 1], [1, 0, 0]]).float()
925
+ images = images @ rotation_matrix
926
+ images = to_pil_images(images, resize=False)
927
+ return images
928
+
929
+ def flip_rgb_gallery(images, axis=0):
930
+ if images is None or len(images) == 0:
931
+ gr.Warning("No images selected.")
932
+ return []
933
+ # read webp images
934
+ images = [Image.open(image[0]).convert("RGB") for image in images]
935
+ images = [np.array(image).astype(np.float32) for image in images]
936
+ images = np.stack(images)
937
+ images = torch.tensor(images) / 255
938
+ images = 1 - images
939
+ images = to_pil_images(images, resize=False)
940
+ return images
941
+
942
+ def add_output_images_buttons(output_gallery):
943
+ with gr.Row():
944
+ rotate_button = gr.Button("🔄 Rotate", elem_id="rotate_button", variant='secondary')
945
+ rotate_button.click(sequence_rotate_rgb_gallery, inputs=[output_gallery], outputs=[output_gallery])
946
+ flip_button = gr.Button("🔃 Flip", elem_id="flip_button", variant='secondary')
947
+ flip_button.click(flip_rgb_gallery, inputs=[output_gallery], outputs=[output_gallery])
948
+ return rotate_button, flip_button
949
+
950
+
951
  def make_output_images_section():
952
  gr.Markdown('### Output Images')
953
+ output_gallery = gr.Gallery(format='png', value=[], label="NCUT Embedding", show_label=False, elem_id="ncut", columns=[3], rows=[1], object_fit="contain", height="auto", show_share_button=True)
954
+ add_output_images_buttons(output_gallery)
955
  return output_gallery
956
 
957
  def make_parameters_section(is_lisa=False):
 
1018
  sampling_method_dropdown = gr.Dropdown(["fps", "random"], label="NCUT: Sampling method", value="fps", elem_id="sampling_method", info="Nyström approximation")
1019
  knn_ncut_slider = gr.Slider(1, 100, step=1, label="NCUT: KNN", value=10, elem_id="knn_ncut", info="Nyström approximation")
1020
  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")
1021
+ embedding_metric_dropdown = gr.Dropdown(["euclidean", "cosine"], label="t-SNE/UMAP metric", value="euclidean", elem_id="embedding_metric")
1022
  num_sample_tsne_slider = gr.Slider(100, 10000, step=100, label="t-SNE/UMAP: num_sample", value=300, elem_id="num_sample_tsne", info="Nyström approximation")
1023
  knn_tsne_slider = gr.Slider(1, 100, step=1, label="t-SNE/UMAP: KNN", value=10, elem_id="knn_tsne", info="Nyström approximation")
1024
  perplexity_slider = gr.Slider(10, 1000, step=10, label="t-SNE: perplexity", value=150, elem_id="perplexity")
 
1026
  min_dist_slider = gr.Slider(0.1, 1, step=0.1, label="UMAP: min_dist", value=0.1, elem_id="min_dist")
1027
  return [model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1028
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1029
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1030
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1031
  sampling_method_dropdown, positive_prompt, negative_prompt]
1032
 
 
1048
  [
1049
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1050
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1051
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1052
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1053
  sampling_method_dropdown, positive_prompt, negative_prompt
1054
  ] = make_parameters_section()
 
1067
  positive_prompt, negative_prompt,
1068
  false_placeholder, no_prompt, no_prompt, no_prompt,
1069
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1070
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1071
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1072
  ],
1073
  outputs=[output_gallery, logging_text],
 
1107
  [
1108
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1109
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1110
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1111
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1112
  sampling_method_dropdown, positive_prompt, negative_prompt
1113
  ] = make_parameters_section()
 
1130
  positive_prompt, negative_prompt,
1131
  false_placeholder, no_prompt, no_prompt, no_prompt,
1132
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1133
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1134
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
1135
  old_school_ncut_checkbox
1136
  ],
 
1147
  with gr.Row():
1148
  with gr.Column(scale=5, min_width=200):
1149
  gr.Markdown('### Output (Recursion #1)')
1150
+ l1_gallery = gr.Gallery(format='png', value=[], label="Recursion #1", show_label=False, elem_id="ncut_l1", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1151
+ add_output_images_buttons(l1_gallery)
1152
  with gr.Column(scale=5, min_width=200):
1153
  gr.Markdown('### Output (Recursion #2)')
1154
+ l2_gallery = gr.Gallery(format='png', value=[], label="Recursion #2", show_label=False, elem_id="ncut_l2", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1155
+ add_output_images_buttons(l2_gallery)
1156
  with gr.Column(scale=5, min_width=200):
1157
  gr.Markdown('### Output (Recursion #3)')
1158
+ l3_gallery = gr.Gallery(format='png', value=[], label="Recursion #3", show_label=False, elem_id="ncut_l3", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1159
+ add_output_images_buttons(l3_gallery)
1160
  with gr.Row():
1161
  with gr.Column(scale=5, min_width=200):
1162
  input_gallery, submit_button, clear_images_button = make_input_images_section()
 
1176
  [
1177
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1178
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1179
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1180
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1181
  sampling_method_dropdown, positive_prompt, negative_prompt
1182
  ] = make_parameters_section()
 
1207
  positive_prompt, negative_prompt,
1208
  false_placeholder, no_prompt, no_prompt, no_prompt,
1209
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1210
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1211
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
1212
  false_placeholder, number_placeholder, true_placeholder,
1213
  l2_num_eig_slider, l3_num_eig_slider, metric_dropdown,
 
1227
  [
1228
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1229
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1230
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1231
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1232
  sampling_method_dropdown, positive_prompt, negative_prompt
1233
  ] = make_parameters_section()
 
1250
  positive_prompt, negative_prompt,
1251
  false_placeholder, no_prompt, no_prompt, no_prompt,
1252
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1253
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1254
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown,
1255
  place_holder_false, max_frame_number
1256
  ],
 
1275
  with gr.Row():
1276
  with gr.Column(scale=5, min_width=200):
1277
  gr.Markdown('### Output (Prompt #1)')
1278
+ l1_gallery = gr.Gallery(format='png', value=[], label="Prompt #1", show_label=False, elem_id="ncut_p1", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1279
  prompt1 = gr.Textbox(label="Input Prompt #1", elem_id="prompt1", value="where is the person, include the clothes, don't include the guitar and chair", lines=3)
1280
  with gr.Column(scale=5, min_width=200):
1281
  gr.Markdown('### Output (Prompt #2)')
1282
+ l2_gallery = gr.Gallery(format='png', value=[], label="Prompt #2", show_label=False, elem_id="ncut_p2", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1283
  prompt2 = gr.Textbox(label="Input Prompt #2", elem_id="prompt2", value="where is the Gibson Les Pual guitar", lines=3)
1284
  with gr.Column(scale=5, min_width=200):
1285
  gr.Markdown('### Output (Prompt #3)')
1286
+ l3_gallery = gr.Gallery(format='png', value=[], label="Prompt #3", show_label=False, elem_id="ncut_p3", columns=[3], rows=[5], object_fit="contain", height="auto", show_fullscreen_button=True)
1287
  prompt3 = gr.Textbox(label="Input Prompt #3", elem_id="prompt3", value="where is the floor", lines=3)
1288
 
1289
  with gr.Row():
 
1296
  [
1297
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1298
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1299
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1300
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1301
  sampling_method_dropdown, positive_prompt, negative_prompt
1302
  ] = make_parameters_section(is_lisa=True)
 
1311
  positive_prompt, negative_prompt,
1312
  true_placeholder, prompt1, prompt2, prompt3,
1313
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1314
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1315
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1316
  ],
1317
  outputs=galleries + [logging_text],
 
1344
  [
1345
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1346
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1347
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1348
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1349
  sampling_method_dropdown, positive_prompt, negative_prompt
1350
  ] = make_parameters_section()
 
1380
  positive_prompt, negative_prompt,
1381
  false_placeholder, no_prompt, no_prompt, no_prompt,
1382
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1383
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1384
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1385
  ],
1386
  # outputs=galleries + [logging_text],
 
1391
  def add_one_model(i_model=1):
1392
  with gr.Column(scale=5, min_width=200) as col:
1393
  gr.Markdown(f'### Output Images')
1394
+ output_gallery = gr.Gallery(format='png', value=[], label="NCUT Embedding", show_label=False, elem_id=f"ncut{i_model}", columns=[3], rows=[1], object_fit="contain", height="auto", show_fullscreen_button=True)
1395
  submit_button = gr.Button("🔴 RUN", elem_id=f"submit_button{i_model}", variant='primary')
1396
+ add_output_images_buttons(output_gallery)
1397
  [
1398
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
1399
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1400
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1401
  perplexity_slider, n_neighbors_slider, min_dist_slider,
1402
  sampling_method_dropdown, positive_prompt, negative_prompt
1403
  ] = make_parameters_section()
 
1413
  positive_prompt, negative_prompt,
1414
  false_placeholder, no_prompt, no_prompt, no_prompt,
1415
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
1416
+ embedding_method_dropdown, embedding_metric_dropdown, num_sample_tsne_slider, knn_tsne_slider,
1417
  perplexity_slider, n_neighbors_slider, min_dist_slider, sampling_method_dropdown
1418
  ],
1419
  outputs=[output_gallery, logging_text]
 
1462
 
1463
 
1464
  with gr.Tab('📄About'):
1465
+ gr.Markdown("**This demo is for the Python package `ncut-pytorch`, please visit the [Documentation](https://ncut-pytorch.readthedocs.io/)**")
1466
+ gr.Markdown("**All the models and functions used for this demo are in the Python package `ncut-pytorch`**")
1467
  gr.Markdown("---")
1468
  gr.Markdown("**Normalized Cuts**, aka. spectral clustering, is a graphical method to analyze data grouping in the affinity eigenvector space. It has been widely used for unsupervised segmentation in the 2000s.")
1469
+ gr.Markdown("*Normalized Cuts and Image Segmentation, Jianbo Shi and Jitendra Malik, 2000*")
1470
  gr.Markdown("---")
1471
+ gr.Markdown("**We have improved NCut, with some advanced features:**")
1472
  gr.Markdown("- **Nyström** Normalized Cut, is a new approximation algorithm developed for large-scale graph cuts, a large-graph of million nodes can be processed in under 10s (cpu) or 2s (gpu).")
1473
  gr.Markdown("- **spectral-tSNE** visualization, a new method to visualize the high-dimensional eigenvector space with 3D RGB cube. Color is aligned across images, color infers distance in representation.")
1474
+ gr.Markdown("*paper in prep, Yang 2024*")
1475
+ gr.Markdown("*AlignedCut: Visual Concepts Discovery on Brain-Guided Universal Feature Space, Huzheng Yang, James Gee\*, and Jianbo Shi\*, 2024*")
1476
+
1477
 
1478
  with gr.Row():
1479
  with gr.Column():