huzey commited on
Commit
9e43ce3
1 Parent(s): 4d58f0c
Files changed (2) hide show
  1. app.py +51 -61
  2. images/guitar_ego.jpg +0 -0
app.py CHANGED
@@ -116,8 +116,12 @@ def dont_use_too_much_green(image_rgb):
116
 
117
 
118
  def to_pil_images(images):
 
 
 
 
119
  return [
120
- Image.fromarray((image * 255).cpu().numpy().astype(np.uint8)).resize((256, 256), Image.Resampling.NEAREST)
121
  for image in images
122
  ]
123
 
@@ -164,9 +168,10 @@ def get_random_path(length=10):
164
  path = f'/tmp/{name}.mp4'
165
  return path
166
 
167
- default_images = ['./images/image_0.jpg', './images/image_1.jpg', './images/image_2.jpg', './images/image_3.jpg', './images/image_5.jpg']
168
  default_outputs = ['./images/image-1.webp', './images/image-2.webp', './images/image-3.webp', './images/image-4.webp', './images/image-5.webp']
169
- default_outputs_independent = ['./images/image-6.webp', './images/image-7.webp', './images/image-8.webp', './images/image-9.webp', './images/image-10.webp']
 
170
 
171
  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']
172
  downscaled_outputs = default_outputs
@@ -455,52 +460,50 @@ def run_fn(
455
  def make_input_images_section():
456
  gr.Markdown('### Input Images')
457
  input_gallery = gr.Gallery(value=None, label="Select images", show_label=False, elem_id="images", columns=[3], rows=[1], object_fit="contain", height="auto", type="pil", show_share_button=False)
458
- submit_button = gr.Button("🔴 RUN", elem_id="submit_button")
459
- clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button')
460
  return input_gallery, submit_button, clear_images_button
461
 
462
  def make_input_video_section():
463
- gr.Markdown('### Input Video')
464
  input_gallery = gr.Video(value=None, label="Select video", elem_id="video-input", height="auto", show_share_button=False)
465
  gr.Markdown('_image backbone model is used to extract features from each frame, NCUT is computed on all frames_')
466
  # max_frames_number = gr.Number(100, label="Max frames", elem_id="max_frames")
467
  max_frames_number = gr.Slider(1, 200, step=1, label="Max frames", value=100, elem_id="max_frames")
468
- submit_button = gr.Button("🔴 RUN", elem_id="submit_button")
469
- clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button')
470
  return input_gallery, submit_button, clear_images_button, max_frames_number
471
 
472
- def make_example_images_section():
473
- gr.Markdown('### Load Images 👇')
474
- load_images_button = gr.Button("Load Example", elem_id="load-images-button")
475
- 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")
476
- hide_button = gr.Button("Hide Example", elem_id="hide-button")
477
- hide_button.click(
478
- fn=lambda: gr.update(visible=False),
479
- outputs=example_gallery
480
- )
481
- hide_button.click(
482
- fn=lambda: gr.update(visible=False),
483
- outputs=hide_button
484
- )
485
- return load_images_button, example_gallery, hide_button
486
 
487
- def make_example_video_section():
488
- gr.Markdown('### Load Video 👇')
489
- load_video_button = gr.Button("Load Example", elem_id="load-video-button")
490
- return load_video_button
491
-
492
- def make_dataset_images_section(open=False):
493
-
494
- with gr.Accordion("➡️ Click to expand: Load from dataset", open=open):
495
  dataset_names = DATASET_NAMES
496
  dataset_classes = DATASET_CLASSES
497
  dataset_dropdown = gr.Dropdown(dataset_names, label="Dataset name", value="mrm8488/ImageNet1K-val", elem_id="dataset")
498
  num_images_slider = gr.Number(10, label="Number of images", elem_id="num_images")
 
499
  filter_by_class_checkbox = gr.Checkbox(label="Filter by class", value=True, elem_id="filter_by_class_checkbox")
500
  filter_by_class_text = gr.Textbox(label="Class to select", value="0,33,99", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. (1000 classes)", visible=True)
 
501
  is_random_checkbox = gr.Checkbox(label="Random shuffle", value=False, elem_id="random_seed_checkbox")
502
  random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=1, elem_id="random_seed", visible=False)
503
- load_dataset_button = gr.Button("Load Dataset", elem_id="load-dataset-button")
 
 
 
 
 
 
 
 
 
 
 
504
 
505
  def change_filter_options(dataset_name):
506
  idx = dataset_names.index(dataset_name)
@@ -523,9 +526,12 @@ def make_dataset_images_section(open=False):
523
  is_random_checkbox.change(fn=change_random_seed, inputs=is_random_checkbox, outputs=random_seed_slider)
524
 
525
 
526
- def load_dataset_images(dataset_name, num_images=10,
527
  is_filter=True, filter_by_class_text="0,1,2",
528
  is_random=False, seed=1):
 
 
 
529
  try:
530
  dataset = load_dataset(dataset_name, trust_remote_code=True)
531
  key = list(dataset.keys())[0]
@@ -538,7 +544,6 @@ def make_dataset_images_section(open=False):
538
 
539
  if is_filter:
540
  classes = [int(i) for i in filter_by_class_text.split(",")]
541
- print(filter_by_class_text, len(classes), classes)
542
  labels = np.array(dataset['label'])
543
  unique_labels = np.unique(labels)
544
  valid_classes = [i for i in classes if i in unique_labels]
@@ -564,15 +569,16 @@ def make_dataset_images_section(open=False):
564
  else:
565
  image_idx = list(range(num_images))
566
  images = [dataset[i]['image'] for i in image_idx]
 
567
  return images
568
 
569
- load_dataset_button.click(load_dataset_images,
570
- inputs=[dataset_dropdown, num_images_slider,
571
  filter_by_class_checkbox, filter_by_class_text,
572
  is_random_checkbox, random_seed_slider],
573
  outputs=[input_gallery])
574
 
575
- return dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button
576
 
577
  def make_output_images_section():
578
  gr.Markdown('### Output Images')
@@ -628,8 +634,7 @@ with demo:
628
  with gr.Row():
629
  with gr.Column(scale=5, min_width=200):
630
  input_gallery, submit_button, clear_images_button = make_input_images_section()
631
- load_images_button, example_gallery, hide_button = make_example_images_section()
632
- dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button = make_dataset_images_section()
633
 
634
  with gr.Column(scale=5, min_width=200):
635
  output_gallery = make_output_images_section()
@@ -642,7 +647,6 @@ with demo:
642
  ] = make_parameters_section()
643
  # logging text box
644
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
645
- load_images_button.click(lambda x: default_images, outputs=input_gallery)
646
 
647
  clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
648
  submit_button.click(
@@ -682,10 +686,7 @@ with demo:
682
  with gr.Row():
683
  with gr.Column(scale=5, min_width=200):
684
  input_gallery, submit_button, clear_images_button = make_input_images_section()
685
- load_images_button, example_gallery, hide_button = make_example_images_section()
686
- dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button = make_dataset_images_section()
687
- example_gallery.visible = False
688
- hide_button.visible = False
689
 
690
  with gr.Column(scale=5, min_width=200):
691
  output_gallery = make_output_images_section()
@@ -703,7 +704,6 @@ with demo:
703
  item.visible = False
704
  # logging text box
705
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
706
- load_images_button.click(lambda x: (default_images, default_outputs_independent), outputs=[input_gallery, output_gallery])
707
 
708
  clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
709
  submit_button.click(
@@ -727,11 +727,7 @@ with demo:
727
  with gr.Row():
728
  with gr.Column(scale=5, min_width=200):
729
  input_gallery, submit_button, clear_images_button = make_input_images_section()
730
- load_images_button, example_gallery, hide_button = make_example_images_section()
731
- load_images_button.click(lambda x: default_images, outputs=[input_gallery])
732
- example_gallery.visible = False
733
- hide_button.visible = False
734
- dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button = make_dataset_images_section()
735
  num_images_slider.value = 100
736
 
737
  with gr.Column(scale=5, min_width=200):
@@ -791,8 +787,7 @@ with demo:
791
  with gr.Tab('Video'):
792
  with gr.Row():
793
  with gr.Column(scale=5, min_width=200):
794
- video_input_gallery, submit_button, clear_images_button, max_frame_number = make_input_video_section()
795
- # load_video_button = make_example_video_section()
796
  with gr.Column(scale=5, min_width=200):
797
  video_output_gallery = gr.Video(value=None, label="NCUT Embedding", elem_id="ncut", height="auto", show_share_button=False)
798
  [
@@ -808,8 +803,7 @@ with demo:
808
  knn_tsne_slider.value = 20
809
  # logging text box
810
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
811
- load_images_button.click(lambda x: (default_images, default_outputs), outputs=[input_gallery, output_gallery])
812
- clear_images_button.click(lambda x: (None, []), outputs=[video_input_gallery, video_output_gallery])
813
  place_holder_false = gr.Checkbox(label="Place holder", value=False, elem_id="place_holder_false")
814
  place_holder_false.visible = False
815
  submit_button.click(
@@ -830,12 +824,12 @@ with demo:
830
  gr.Markdown('---')
831
  gr.Markdown('![ncut](https://ncut-pytorch.readthedocs.io/en/latest/images/gallery/llama3/llama3_layer_31.jpg)')
832
 
833
- with gr.Tab('Compare'):
834
  def add_one_model(i_model=1):
835
  with gr.Column(scale=5, min_width=200) as col:
836
  gr.Markdown(f'### Output Images')
837
  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")
838
- submit_button = gr.Button("🔴 RUN", elem_id=f"submit_button{i_model}")
839
  [
840
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
841
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
@@ -863,11 +857,7 @@ with demo:
863
  input_gallery, submit_button, clear_images_button = make_input_images_section()
864
  clear_images_button.click(lambda x: ([],), outputs=[input_gallery])
865
  submit_button.visible = False
866
- load_images_button, example_gallery, hide_button = make_example_images_section()
867
- example_gallery.visible = False
868
- hide_button.visible = False
869
- dataset_dropdown, num_images_slider, random_seed_slider, load_dataset_button = make_dataset_images_section(open=True)
870
- load_images_button.click(lambda x: default_images, outputs=input_gallery)
871
 
872
 
873
  for i in range(2):
@@ -886,7 +876,7 @@ with demo:
886
  with gr.Column(scale=5, min_width=200):
887
  add_one_model()
888
 
889
- button = gr.Button("Add Compare", elem_id=f"add_button_{i}", visible=False if i > 0 else True)
890
  buttons.append(button)
891
 
892
  if i > 0:
 
116
 
117
 
118
  def to_pil_images(images):
119
+ size = images[0].shape[1]
120
+ target = 256
121
+ multiplier = target // size
122
+ res = int(size * multiplier)
123
  return [
124
+ Image.fromarray((image * 255).cpu().numpy().astype(np.uint8)).resize((res, res), Image.Resampling.NEAREST)
125
  for image in images
126
  ]
127
 
 
168
  path = f'/tmp/{name}.mp4'
169
  return path
170
 
171
+ default_images = ['./images/image_0.jpg', './images/image_1.jpg', './images/image_2.jpg', './images/image_3.jpg', './images/guitar_ego.jpg', './images/image_5.jpg']
172
  default_outputs = ['./images/image-1.webp', './images/image-2.webp', './images/image-3.webp', './images/image-4.webp', './images/image-5.webp']
173
+ # default_outputs_independent = ['./images/image-6.webp', './images/image-7.webp', './images/image-8.webp', './images/image-9.webp', './images/image-10.webp']
174
+ default_outputs_independent = []
175
 
176
  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']
177
  downscaled_outputs = default_outputs
 
460
  def make_input_images_section():
461
  gr.Markdown('### Input Images')
462
  input_gallery = gr.Gallery(value=None, label="Select images", show_label=False, elem_id="images", columns=[3], rows=[1], object_fit="contain", height="auto", type="pil", show_share_button=False)
463
+ submit_button = gr.Button("🔴 RUN", elem_id="submit_button", variant='primary')
464
+ clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
465
  return input_gallery, submit_button, clear_images_button
466
 
467
  def make_input_video_section():
468
+ # gr.Markdown('### Input Video')
469
  input_gallery = gr.Video(value=None, label="Select video", elem_id="video-input", height="auto", show_share_button=False)
470
  gr.Markdown('_image backbone model is used to extract features from each frame, NCUT is computed on all frames_')
471
  # max_frames_number = gr.Number(100, label="Max frames", elem_id="max_frames")
472
  max_frames_number = gr.Slider(1, 200, step=1, label="Max frames", value=100, elem_id="max_frames")
473
+ submit_button = gr.Button("🔴 RUN", elem_id="submit_button", variant='primary')
474
+ clear_images_button = gr.Button("🗑️Clear", elem_id='clear_button', variant='stop')
475
  return input_gallery, submit_button, clear_images_button, max_frames_number
476
 
477
+ def make_dataset_images_section(advanced=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
478
 
479
+ gr.Markdown('### Load Datasets')
480
+ load_images_button = gr.Button("Load", elem_id="load-images-button", variant='secondary')
481
+ advanced_radio = gr.Radio(["Basic", "Advanced"], label="Datasets", value="Advanced" if advanced else "Basic", elem_id="advanced-radio")
482
+ with gr.Column() as basic_block:
483
+ 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")
484
+ with gr.Column() as advanced_block:
 
 
485
  dataset_names = DATASET_NAMES
486
  dataset_classes = DATASET_CLASSES
487
  dataset_dropdown = gr.Dropdown(dataset_names, label="Dataset name", value="mrm8488/ImageNet1K-val", elem_id="dataset")
488
  num_images_slider = gr.Number(10, label="Number of images", elem_id="num_images")
489
+ # with gr.Row():
490
  filter_by_class_checkbox = gr.Checkbox(label="Filter by class", value=True, elem_id="filter_by_class_checkbox")
491
  filter_by_class_text = gr.Textbox(label="Class to select", value="0,33,99", elem_id="filter_by_class_text", info=f"e.g. `0,1,2`. (1000 classes)", visible=True)
492
+ # with gr.Row():
493
  is_random_checkbox = gr.Checkbox(label="Random shuffle", value=False, elem_id="random_seed_checkbox")
494
  random_seed_slider = gr.Slider(0, 1000, step=1, label="Random seed", value=1, elem_id="random_seed", visible=False)
495
+
496
+ if advanced:
497
+ advanced_block.visible = True
498
+ basic_block.visible = False
499
+ else:
500
+ advanced_block.visible = False
501
+ basic_block.visible = True
502
+
503
+ # change visibility
504
+ advanced_radio.change(fn=lambda x: gr.update(visible=x=="Advanced"), inputs=advanced_radio, outputs=[advanced_block])
505
+ advanced_radio.change(fn=lambda x: gr.update(visible=x=="Basic"), inputs=advanced_radio, outputs=[basic_block])
506
+
507
 
508
  def change_filter_options(dataset_name):
509
  idx = dataset_names.index(dataset_name)
 
526
  is_random_checkbox.change(fn=change_random_seed, inputs=is_random_checkbox, outputs=random_seed_slider)
527
 
528
 
529
+ def load_dataset_images(is_advanced, dataset_name, num_images=10,
530
  is_filter=True, filter_by_class_text="0,1,2",
531
  is_random=False, seed=1):
532
+ if is_advanced == "Basic":
533
+ gr.Info("Loaded images from Ego-Exo4D")
534
+ return default_images
535
  try:
536
  dataset = load_dataset(dataset_name, trust_remote_code=True)
537
  key = list(dataset.keys())[0]
 
544
 
545
  if is_filter:
546
  classes = [int(i) for i in filter_by_class_text.split(",")]
 
547
  labels = np.array(dataset['label'])
548
  unique_labels = np.unique(labels)
549
  valid_classes = [i for i in classes if i in unique_labels]
 
569
  else:
570
  image_idx = list(range(num_images))
571
  images = [dataset[i]['image'] for i in image_idx]
572
+ gr.Info(f"Loaded {len(images)} images from {dataset_name}")
573
  return images
574
 
575
+ load_images_button.click(load_dataset_images,
576
+ inputs=[advanced_radio, dataset_dropdown, num_images_slider,
577
  filter_by_class_checkbox, filter_by_class_text,
578
  is_random_checkbox, random_seed_slider],
579
  outputs=[input_gallery])
580
 
581
+ return dataset_dropdown, num_images_slider, random_seed_slider, load_images_button
582
 
583
  def make_output_images_section():
584
  gr.Markdown('### Output Images')
 
634
  with gr.Row():
635
  with gr.Column(scale=5, min_width=200):
636
  input_gallery, submit_button, clear_images_button = make_input_images_section()
637
+ dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section()
 
638
 
639
  with gr.Column(scale=5, min_width=200):
640
  output_gallery = make_output_images_section()
 
647
  ] = make_parameters_section()
648
  # logging text box
649
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
 
650
 
651
  clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
652
  submit_button.click(
 
686
  with gr.Row():
687
  with gr.Column(scale=5, min_width=200):
688
  input_gallery, submit_button, clear_images_button = make_input_images_section()
689
+ dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section()
 
 
 
690
 
691
  with gr.Column(scale=5, min_width=200):
692
  output_gallery = make_output_images_section()
 
704
  item.visible = False
705
  # logging text box
706
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
 
707
 
708
  clear_images_button.click(lambda x: ([], []), outputs=[input_gallery, output_gallery])
709
  submit_button.click(
 
727
  with gr.Row():
728
  with gr.Column(scale=5, min_width=200):
729
  input_gallery, submit_button, clear_images_button = make_input_images_section()
730
+ dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section()
 
 
 
 
731
  num_images_slider.value = 100
732
 
733
  with gr.Column(scale=5, min_width=200):
 
787
  with gr.Tab('Video'):
788
  with gr.Row():
789
  with gr.Column(scale=5, min_width=200):
790
+ video_input_gallery, submit_button, clear_video_button, max_frame_number = make_input_video_section()
 
791
  with gr.Column(scale=5, min_width=200):
792
  video_output_gallery = gr.Video(value=None, label="NCUT Embedding", elem_id="ncut", height="auto", show_share_button=False)
793
  [
 
803
  knn_tsne_slider.value = 20
804
  # logging text box
805
  logging_text = gr.Textbox("Logging information", label="Logging", elem_id="logging", type="text", placeholder="Logging information")
806
+ clear_video_button.click(lambda x: (None, None), outputs=[video_input_gallery, video_output_gallery])
 
807
  place_holder_false = gr.Checkbox(label="Place holder", value=False, elem_id="place_holder_false")
808
  place_holder_false.visible = False
809
  submit_button.click(
 
824
  gr.Markdown('---')
825
  gr.Markdown('![ncut](https://ncut-pytorch.readthedocs.io/en/latest/images/gallery/llama3/llama3_layer_31.jpg)')
826
 
827
+ with gr.Tab('Compare Models'):
828
  def add_one_model(i_model=1):
829
  with gr.Column(scale=5, min_width=200) as col:
830
  gr.Markdown(f'### Output Images')
831
  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")
832
+ submit_button = gr.Button("🔴 RUN", elem_id=f"submit_button{i_model}", variant='primary')
833
  [
834
  model_dropdown, layer_slider, node_type_dropdown, num_eig_slider,
835
  affinity_focal_gamma_slider, num_sample_ncut_slider, knn_ncut_slider,
 
857
  input_gallery, submit_button, clear_images_button = make_input_images_section()
858
  clear_images_button.click(lambda x: ([],), outputs=[input_gallery])
859
  submit_button.visible = False
860
+ dataset_dropdown, num_images_slider, random_seed_slider, load_images_button = make_dataset_images_section(advanced=True)
 
 
 
 
861
 
862
 
863
  for i in range(2):
 
876
  with gr.Column(scale=5, min_width=200):
877
  add_one_model()
878
 
879
+ button = gr.Button("Add Compare", elem_id=f"add_button_{i}", visible=False if i > 0 else True, scale=3)
880
  buttons.append(button)
881
 
882
  if i > 0:
images/guitar_ego.jpg ADDED