codeShare commited on
Commit
0c8190e
1 Parent(s): 552b812

Upload fusion_t2i_CLIP_interrogator.ipynb

Browse files
Google Colab Notebooks/fusion_t2i_CLIP_interrogator.ipynb CHANGED
@@ -412,7 +412,7 @@
412
  "\n",
413
  "#image_index = 0 # @param {type:'number'}\n",
414
  "# @markdown 📥 Load the data (only required one time)\n",
415
- "load_the_data = False # @param {type:\"boolean\"}\n",
416
  "\n",
417
  "# @markdown 🖼️ Choose a pre-encoded reference\n",
418
  "index = 708 # @param {type:\"slider\", min:0, max:1666, step:1}\n",
@@ -429,7 +429,7 @@
429
  "strength = 1 # @param {type:\"slider\", min:-5, max:5, step:0.1}\n",
430
  "\n",
431
  "# @markdown Calculate most similiar items using above settings?\n",
432
- "enable = True # @param {type:\"boolean\"}\n",
433
  "\n",
434
  "if (load_the_data):\n",
435
  " target_prompts , target_text_encodings , urls , target_image_encodings , NUM_ITEMS = getPromptsAndLinks('/content/text-to-image-prompts/fusion')\n",
@@ -465,10 +465,10 @@
465
  " # Get text features for user input\n",
466
  " inputs = tokenizer(text = NEG, padding=True, return_tensors=\"pt\")\n",
467
  " text_features_NEG = model.get_text_features(**inputs)\n",
468
- " text_features_NEG = text_features_A/text_features_A.norm(p=2, dim=-1, keepdim=True)\n",
469
  "\n",
470
  " # text-similarity\n",
471
- " neg_sims = strength*torch.matmul(text_tensor, text_features_NEG.t())\n",
472
  " #------#\n",
473
  "\n",
474
  " # plus image-similarity\n",
@@ -476,7 +476,7 @@
476
  "\n",
477
  "\n",
478
  " # minus NEG-similarity\n",
479
- " sims = sims - neg_sims\n",
480
  "\n",
481
  " # Sort the items\n",
482
  " sorted , indices = torch.sort(sims,dim=0 , descending=True)\n",
@@ -553,6 +553,77 @@
553
  "execution_count": null,
554
  "outputs": []
555
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556
  {
557
  "cell_type": "markdown",
558
  "source": [
@@ -798,77 +869,6 @@
798
  "execution_count": null,
799
  "outputs": []
800
  },
801
- {
802
- "cell_type": "code",
803
- "source": [
804
- "# @title ⚙️📝 Print the results (Advanced)\n",
805
- "list_size = 1000 # @param {type:'number'}\n",
806
- "start_at_index = 0 # @param {type:'number'}\n",
807
- "print_Similarity = True # @param {type:\"boolean\"}\n",
808
- "print_Prompts = True # @param {type:\"boolean\"}\n",
809
- "print_Descriptions = True # @param {type:\"boolean\"}\n",
810
- "compact_Output = True # @param {type:\"boolean\"}\n",
811
- "newline_Separator = False # @param {type:\"boolean\"}\n",
812
- "\n",
813
- "import random\n",
814
- "# @markdown -----------\n",
815
- "# @markdown Mix with...\n",
816
- "list_size2 = 1000 # @param {type:'number'}\n",
817
- "start_at_index2 = 10000 # @param {type:'number'}\n",
818
- "rate_percent = 0 # @param {type:\"slider\", min:0, max:100, step:1}\n",
819
- "\n",
820
- "# @markdown -----------\n",
821
- "# @markdown Repeat output N times\n",
822
- "N = 6 # @param {type:\"slider\", min:0, max:10, step:1}\n",
823
- "\n",
824
- "# title Show the 100 most similiar suffix and prefix text-encodings to the text encoding\n",
825
- "RANGE = list_size\n",
826
- "separator = '|'\n",
827
- "if newline_Separator : separator = separator + '\\n'\n",
828
- "\n",
829
- "_prompts = ''\n",
830
- "_sims = ''\n",
831
- "for _index in range(start_at_index + RANGE):\n",
832
- " if _index < start_at_index : continue\n",
833
- " index = indices[_index].item()\n",
834
- "\n",
835
- " prompt = prompts[f'{index}']\n",
836
- " if rate_percent >= random.randint(0,100) : prompt = prompts[f'{random.randint(start_at_index2 , start_at_index2 + list_size2)}']\n",
837
- "\n",
838
- " #Remove duplicates\n",
839
- " if _prompts.find(prompt + separator)<=-1:\n",
840
- " _sims = _sims + f'{round(100*sims[index].item(), 2)} %' + separator\n",
841
- " #-------#\n",
842
- " _prompts = _prompts.replace(prompt + separator,'')\n",
843
- " _prompts = _prompts + prompt + separator\n",
844
- " #------#\n",
845
- "#------#\n",
846
- "__prompts = fix_bad_symbols(__prompts)\n",
847
- "__prompts = ('{' + _prompts + '}').replace(separator + '}', '}')\n",
848
- "__sims = ('{' + _sims + '}').replace(separator + '}', '}')\n",
849
- "#------#\n",
850
- "\n",
851
- "if(not print_Prompts): __prompts = ''\n",
852
- "if(not print_Similarity): __sims = ''\n",
853
- "\n",
854
- "if(not compact_Output):\n",
855
- " if(print_Descriptions):\n",
856
- " print(f'The {start_at_index}-{start_at_index + RANGE} most similiar items to prompt : \\n\\n ')\n",
857
- " for i in range(N) : print(__prompts)\n",
858
- " print(f'The {start_at_index}-{start_at_index + RANGE} similarity % for items : \\n\\n' + __sims)\n",
859
- " print('')\n",
860
- " else:\n",
861
- " for i in range(N) : print(__prompts)\n",
862
- "else:\n",
863
- " for i in range(N) : print(__prompts)\n",
864
- "#-------#"
865
- ],
866
- "metadata": {
867
- "id": "EdBiAguJO9aX"
868
- },
869
- "execution_count": null,
870
- "outputs": []
871
- },
872
  {
873
  "cell_type": "code",
874
  "source": [
 
412
  "\n",
413
  "#image_index = 0 # @param {type:'number'}\n",
414
  "# @markdown 📥 Load the data (only required one time)\n",
415
+ "load_the_data = True # @param {type:\"boolean\"}\n",
416
  "\n",
417
  "# @markdown 🖼️ Choose a pre-encoded reference\n",
418
  "index = 708 # @param {type:\"slider\", min:0, max:1666, step:1}\n",
 
429
  "strength = 1 # @param {type:\"slider\", min:-5, max:5, step:0.1}\n",
430
  "\n",
431
  "# @markdown Calculate most similiar items using above settings?\n",
432
+ "enable = False # @param {type:\"boolean\"}\n",
433
  "\n",
434
  "if (load_the_data):\n",
435
  " target_prompts , target_text_encodings , urls , target_image_encodings , NUM_ITEMS = getPromptsAndLinks('/content/text-to-image-prompts/fusion')\n",
 
465
  " # Get text features for user input\n",
466
  " inputs = tokenizer(text = NEG, padding=True, return_tensors=\"pt\")\n",
467
  " text_features_NEG = model.get_text_features(**inputs)\n",
468
+ " text_features_NEG = text_features_NEG/text_features_NEG.norm(p=2, dim=-1, keepdim=True)\n",
469
  "\n",
470
  " # text-similarity\n",
471
+ " neg_sims = torch.matmul(text_tensor, text_features_NEG.t())\n",
472
  " #------#\n",
473
  "\n",
474
  " # plus image-similarity\n",
 
476
  "\n",
477
  "\n",
478
  " # minus NEG-similarity\n",
479
+ " sims = sims - strength*neg_sims\n",
480
  "\n",
481
  " # Sort the items\n",
482
  " sorted , indices = torch.sort(sims,dim=0 , descending=True)\n",
 
553
  "execution_count": null,
554
  "outputs": []
555
  },
556
+ {
557
+ "cell_type": "code",
558
+ "source": [
559
+ "# @title ⚙️📝 Print the results (Advanced)\n",
560
+ "list_size = 1000 # @param {type:'number'}\n",
561
+ "start_at_index = 0 # @param {type:'number'}\n",
562
+ "print_Similarity = True # @param {type:\"boolean\"}\n",
563
+ "print_Prompts = True # @param {type:\"boolean\"}\n",
564
+ "print_Descriptions = True # @param {type:\"boolean\"}\n",
565
+ "compact_Output = True # @param {type:\"boolean\"}\n",
566
+ "newline_Separator = False # @param {type:\"boolean\"}\n",
567
+ "\n",
568
+ "import random\n",
569
+ "# @markdown -----------\n",
570
+ "# @markdown Mix with...\n",
571
+ "list_size2 = 1000 # @param {type:'number'}\n",
572
+ "start_at_index2 = 10000 # @param {type:'number'}\n",
573
+ "rate_percent = 0 # @param {type:\"slider\", min:0, max:100, step:1}\n",
574
+ "\n",
575
+ "# @markdown -----------\n",
576
+ "# @markdown Repeat output N times\n",
577
+ "N = 6 # @param {type:\"slider\", min:0, max:10, step:1}\n",
578
+ "\n",
579
+ "# title Show the 100 most similiar suffix and prefix text-encodings to the text encoding\n",
580
+ "RANGE = list_size\n",
581
+ "separator = '|'\n",
582
+ "if newline_Separator : separator = separator + '\\n'\n",
583
+ "\n",
584
+ "_prompts = ''\n",
585
+ "_sims = ''\n",
586
+ "for _index in range(start_at_index + RANGE):\n",
587
+ " if _index < start_at_index : continue\n",
588
+ " index = indices[_index].item()\n",
589
+ "\n",
590
+ " prompt = prompts[f'{index}']\n",
591
+ " if rate_percent >= random.randint(0,100) : prompt = prompts[f'{random.randint(start_at_index2 , start_at_index2 + list_size2)}']\n",
592
+ "\n",
593
+ " #Remove duplicates\n",
594
+ " if _prompts.find(prompt + separator)<=-1:\n",
595
+ " _sims = _sims + f'{round(100*sims[index].item(), 2)} %' + separator\n",
596
+ " #-------#\n",
597
+ " _prompts = _prompts.replace(prompt + separator,'')\n",
598
+ " _prompts = _prompts + prompt + separator\n",
599
+ " #------#\n",
600
+ "#------#\n",
601
+ "__prompts = fix_bad_symbols(__prompts)\n",
602
+ "__prompts = ('{' + _prompts + '}').replace(separator + '}', '}')\n",
603
+ "__sims = ('{' + _sims + '}').replace(separator + '}', '}')\n",
604
+ "#------#\n",
605
+ "\n",
606
+ "if(not print_Prompts): __prompts = ''\n",
607
+ "if(not print_Similarity): __sims = ''\n",
608
+ "\n",
609
+ "if(not compact_Output):\n",
610
+ " if(print_Descriptions):\n",
611
+ " print(f'The {start_at_index}-{start_at_index + RANGE} most similiar items to prompt : \\n\\n ')\n",
612
+ " for i in range(N) : print(__prompts)\n",
613
+ " print(f'The {start_at_index}-{start_at_index + RANGE} similarity % for items : \\n\\n' + __sims)\n",
614
+ " print('')\n",
615
+ " else:\n",
616
+ " for i in range(N) : print(__prompts)\n",
617
+ "else:\n",
618
+ " for i in range(N) : print(__prompts)\n",
619
+ "#-------#"
620
+ ],
621
+ "metadata": {
622
+ "id": "EdBiAguJO9aX"
623
+ },
624
+ "execution_count": null,
625
+ "outputs": []
626
+ },
627
  {
628
  "cell_type": "markdown",
629
  "source": [
 
869
  "execution_count": null,
870
  "outputs": []
871
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
872
  {
873
  "cell_type": "code",
874
  "source": [