File size: 37,514 Bytes
a16f7a6
 
 
 
aeb8017
a16f7a6
 
aeb8017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a16f7a6
 
 
 
 
 
 
 
 
70dfa79
 
 
a16f7a6
 
 
3f788ef
a16f7a6
 
 
3f788ef
a16f7a6
 
 
 
 
70dfa79
3f788ef
a16f7a6
 
 
 
ed37c6c
3f788ef
a16f7a6
ed37c6c
a16f7a6
3f788ef
 
 
 
 
 
ed37c6c
3f788ef
 
ed37c6c
3f788ef
 
a16f7a6
 
 
 
ed37c6c
3f788ef
 
 
 
 
 
 
 
 
ed37c6c
3f788ef
a16f7a6
ed37c6c
a16f7a6
3f788ef
a16f7a6
 
 
 
ed37c6c
3f788ef
a16f7a6
ed37c6c
a16f7a6
3f788ef
 
 
a16f7a6
 
 
 
 
ed37c6c
3f788ef
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
8a2c29c
 
ed37c6c
8a2c29c
 
ed37c6c
8a2c29c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeb8017
ed37c6c
 
aeb8017
 
 
 
 
 
 
 
 
 
ed37c6c
 
 
 
 
 
 
 
 
 
aeb8017
ed37c6c
 
 
 
 
 
 
 
 
 
aeb8017
ed37c6c
 
aeb8017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeb8017
8a2c29c
 
 
 
 
 
 
 
 
aeb8017
ed37c6c
 
 
 
 
 
 
 
 
aeb8017
ed37c6c
 
 
 
 
 
 
 
 
 
 
 
8a2c29c
 
 
 
 
 
 
 
 
a16f7a6
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
70dfa79
a16f7a6
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
70dfa79
a16f7a6
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
70dfa79
a16f7a6
70dfa79
a16f7a6
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
70dfa79
a16f7a6
70dfa79
 
 
 
 
a16f7a6
70dfa79
 
 
 
 
 
 
 
 
 
 
 
 
 
a16f7a6
70dfa79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a16f7a6
70dfa79
 
a16f7a6
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
a16f7a6
 
 
 
 
 
ed37c6c
a16f7a6
 
 
 
 
 
 
 
 
70dfa79
a16f7a6
 
70dfa79
a16f7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
 
 
70dfa79
 
 
a16f7a6
 
 
 
ed37c6c
a16f7a6
 
 
 
70dfa79
a16f7a6
 
 
 
 
ed37c6c
a16f7a6
 
70dfa79
 
 
 
 
 
 
 
 
ed37c6c
70dfa79
 
 
 
 
 
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
a16f7a6
70dfa79
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
70dfa79
 
 
 
 
 
ed37c6c
70dfa79
 
 
 
 
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
70dfa79
 
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
70dfa79
 
 
 
 
 
ed37c6c
70dfa79
 
 
 
 
 
 
 
a16f7a6
70dfa79
 
a16f7a6
 
 
 
ed37c6c
70dfa79
 
ed37c6c
70dfa79
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
70dfa79
 
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
70dfa79
 
 
 
 
 
ed37c6c
a16f7a6
 
ed37c6c
70dfa79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
70dfa79
 
ed37c6c
625d699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
625d699
 
ed37c6c
625d699
 
 
 
 
 
ed37c6c
625d699
 
ed37c6c
625d699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
625d699
 
ed37c6c
625d699
 
 
 
 
 
 
 
ed37c6c
625d699
 
ed37c6c
625d699
 
 
 
 
 
 
 
ed37c6c
625d699
 
ed37c6c
625d699
 
 
 
 
 
ed37c6c
625d699
 
 
 
 
 
 
 
 
ed37c6c
625d699
 
ed37c6c
625d699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
625d699
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
625d699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a16f7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed37c6c
a16f7a6
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fe7acfaf-dc61-4211-9c78-8e4433bc9deb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n",
      "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n"
     ]
    },
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'sklearn'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 8\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mtorch\u001b[39;00m\n\u001b[1;32m      7\u001b[0m \u001b[39m#import ninja\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mbackend\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdisentangle_concepts\u001b[39;00m \u001b[39mimport\u001b[39;00m \u001b[39m*\u001b[39m\n\u001b[1;32m      9\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mdnnlib\u001b[39;00m \n\u001b[1;32m     10\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mlegacy\u001b[39;00m\n",
      "File \u001b[0;32m~/Desktop/latent-space-theories/backend/disentangle_concepts.py:2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnumpy\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mnp\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39msvm\u001b[39;00m \u001b[39mimport\u001b[39;00m SVC\n\u001b[1;32m      3\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mlinear_model\u001b[39;00m \u001b[39mimport\u001b[39;00m LogisticRegression\n\u001b[1;32m      4\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mmodel_selection\u001b[39;00m \u001b[39mimport\u001b[39;00m train_test_split\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'sklearn'"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import pickle\n",
    "import random\n",
    "\n",
    "import numpy as np\n",
    "import torch\n",
    "#import ninja\n",
    "from backend.disentangle_concepts import *\n",
    "import dnnlib \n",
    "import legacy\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "annotations_file = './data/annotated_files/seeds0000-50000.pkl'\n",
    "with open(annotations_file, 'rb') as f:\n",
    "    annotations = pickle.load(f)\n",
    "\n",
    "ann_df = pd.read_csv('./data/annotated_files/sim_seeds0000-50000.csv')\n",
    "concepts = './data/concepts.txt'\n",
    "\n",
    "with open(concepts) as f:\n",
    "    labels = [line.strip() for line in f.readlines()]\n",
    "\n",
    "with dnnlib.util.open_url('./data/model_files/network-snapshot-010600.pkl') as f:\n",
    "    model = legacy.load_network_pkl(f)['G_ema'].to('cpu') # type: ignore\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecba0e89",
   "metadata": {},
   "outputs": [],
   "source": [
    "concept_id = 'Abstract'\n",
    "separation_vector, number_important_features, important_nodes, accuracy_sep = get_separation_space(concept_id, annotations, ann_df, method='LR', samples=150, C=0.1, latent_space='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7475624",
   "metadata": {},
   "outputs": [],
   "source": [
    "separation_vector.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf4b31c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get_verification_score(concept_id, separation_vector, model, annotations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac437ea3",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(number_important_features, important_nodes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a63345",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = random.randint(0,10000)\n",
    "original_image_vec = annotations['w_vectors'][seed]\n",
    "img = generate_original_image(original_image_vec, model, latent_space='W')\n",
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09f13e6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "images, lambdas = regenerate_images(model, original_image_vec, separation_vector, min_epsilon=-(int(10)), max_epsilon=int(10), latent_space='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cf9f95d",
   "metadata": {},
   "outputs": [],
   "source": [
    "images[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48834be4",
   "metadata": {},
   "outputs": [],
   "source": [
    "images[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5390d8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import open_clip\n",
    "import os\n",
    "import random\n",
    "from tqdm import tqdm\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\"\n",
    "\n",
    "model_clip, _, preprocess = open_clip.create_model_and_transforms('ViT-L-14', pretrained='laion2b_s32b_b82k')\n",
    "tokenizer = open_clip.get_tokenizer('ViT-L-14')\n",
    "\n",
    "pre_prompt = \"Artwork, \" #@param {type:\"string\"}\n",
    "text_descriptions = [f\"{pre_prompt}{label}\" for label in labels]\n",
    "text_tokens = tokenizer(text_descriptions)\n",
    "\n",
    "with torch.no_grad(), torch.cuda.amp.autocast():\n",
    "    text_features = model_clip.encode_text(text_tokens).float()\n",
    "    text_features /= text_features.norm(dim=-1, keepdim=True)\n",
    "    \n",
    "text_features = text_features.cpu().numpy()\n",
    "print(text_features.shape)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0eae840f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ludovicaschaerf/anaconda3/envs/torch_arm/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "import open_clip\n",
    "import os\n",
    "import random\n",
    "from tqdm import tqdm\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4d776015",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e3f917a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Provenance ADRIA', 'Provenance AEGINA', 'Provenance AL MINA', 'Provenance ALICANTE', 'Provenance AMATHUS', 'Provenance AMPURIAS', 'Provenance APOLLONIA PONTICA', 'Provenance APULIA', 'Provenance ARGOLIS', 'Provenance ARGOS', 'Provenance ATHENS', 'Provenance ATHENS (?)', 'Provenance ATTICA', 'Provenance BARI', 'Provenance BENGHAZI', 'Provenance BEREZAN', 'Provenance BLACK SEA', 'Provenance BOEOTIA', 'Provenance BOLOGNA', 'Provenance CAPUA', 'Provenance CARIA', 'Provenance CARTHAGE', 'Provenance CHIUSI', 'Provenance CIVITAVECCHIA', 'Provenance CORINTH', 'Provenance CORSICA', 'Provenance CRETE', 'Provenance CRIMEA', 'Provenance CUMAE', 'Provenance CYCLADES', 'Provenance CYPRUS', 'Provenance CYRENAICA', 'Provenance CYRENE', 'Provenance EGYPT', 'Provenance ELIS', 'Provenance ENSERUNE', 'Provenance ETRURIA', 'Provenance EUBOEA', 'Provenance FALERII', 'Provenance GRANADA', 'Provenance GREECE', 'Provenance GREECE (?)', 'Provenance HISTRIA', 'Provenance ITALY', 'Provenance JAEN', 'Provenance KITION', 'Provenance LESBOS', 'Provenance LOCRI', 'Provenance LOCRIS', 'Provenance LYCIA', 'Provenance MACEDONIA', 'Provenance MARION', 'Provenance MARSEILLES', 'Provenance MARZABOTTO', 'Provenance METAPONTUM', 'Provenance MILETUS', 'Provenance NAPLES', 'Provenance NAUCRATIS', 'Provenance NOLA', 'Provenance NUMANA', 'Provenance OLD SMYRNA', 'Provenance ORVIETO', 'Provenance PAESTUM', 'Provenance PHOCIS', 'Provenance PITANE', 'Provenance POPULONIA', 'Provenance RHODES', 'Provenance ROME', 'Provenance RUVO', 'Provenance SALERNO', 'Provenance SAMOS', 'Provenance SARDINIA', 'Provenance SARDIS', 'Provenance SICILY', 'Provenance SMYRNA', 'Provenance SOUTH', 'Provenance SPARTA', 'Provenance SPINA', 'Provenance SUESSULA', 'Provenance TARANTO', 'Provenance TELL DEFENNEH', 'Provenance THASOS', 'Provenance THERA', 'Provenance THESSALY', 'Provenance THRACE', 'Provenance TOCRA', 'Provenance TODI', 'Provenance ULLASTRET', 'Provenance VALENCIA', 'Shape Name ALABASTRON', 'Shape Name ALABASTRON FRAGMENT', 'Shape Name AMPHORA', 'Shape Name AMPHORA (?) FRAGMENT', 'Shape Name AMPHORISKOS', 'Shape Name ARYBALLOS', 'Shape Name ASKOS', 'Shape Name ASKOS FRAGMENT', 'Shape Name BOTTLE', 'Shape Name BOWL', 'Shape Name BOWL FRAGMENT', 'Shape Name CHALICE', 'Shape Name CHALICE FRAGMENT', 'Shape Name CHOUS', 'Shape Name CHOUS FRAGMENT', 'Shape Name CUP', 'Shape Name CUP (?) FRAGMENT', 'Shape Name CUP A', 'Shape Name CUP A FRAGMENT', 'Shape Name CUP A FRAGMENTS', 'Shape Name CUP B', 'Shape Name CUP B FRAGMENT', 'Shape Name CUP B FRAGMENTS', 'Shape Name CUP C', 'Shape Name CUP C FRAGMENT', 'Shape Name CUP C FRAGMENTS', 'Shape Name CUP DROOP', 'Shape Name CUP DROOP FRAGMENT', 'Shape Name CUP FRAGMENT', 'Shape Name CUP FRAGMENTS', 'Shape Name CUP KASSEL', 'Shape Name CUP KASSEL FRAGMENT', 'Shape Name CUP LITTLE MASTER BAND', 'Shape Name CUP LITTLE MASTER BAND (?) FRAGMENT', 'Shape Name CUP LITTLE MASTER BAND FRAGMENT', 'Shape Name CUP LITTLE MASTER BAND FRAGMENTS', 'Shape Name CUP LITTLE MASTER FRAGMENT', 'Shape Name CUP LITTLE MASTER LIP', 'Shape Name CUP LITTLE MASTER LIP FRAGMENT', 'Shape Name CUP LITTLE MASTER LIP FRAGMENTS', 'Shape Name CUP SIANA', 'Shape Name CUP SIANA FRAGMENT', 'Shape Name CUP SIANA FRAGMENTS', 'Shape Name CUP SKYPHOS', 'Shape Name CUP SKYPHOS FRAGMENT', 'Shape Name CUP STEMLESS', 'Shape Name CUP STEMLESS FRAGMENT', 'Shape Name CUP STEMLESS FRAGMENTS', 'Shape Name DINOS', 'Shape Name DINOS FRAGMENT', 'Shape Name DISH', 'Shape Name EPICHYSIS', 'Shape Name EPINETRON FRAGMENT', 'Shape Name FEEDER', 'Shape Name FIGURE VASE', 'Shape Name FIGURE VASE ARYBALLOS', 'Shape Name FIGURE VASE ASKOS', 'Shape Name FIGURE VASE FRAGMENT', 'Shape Name FIGURE VASE KANTHAROS', 'Shape Name FIGURE VASE LEKYTHOS', 'Shape Name FIGURE VASE OINOCHOE', 'Shape Name FISH-PLATE', 'Shape Name FLASK', 'Shape Name FRAGMENT', 'Shape Name FRAGMENTS', 'Shape Name GUTTUS', 'Shape Name HYDRIA', 'Shape Name HYDRIA (?) FRAGMENT', 'Shape Name HYDRIA FRAGMENT', 'Shape Name HYDRIA FRAGMENTS', 'Shape Name INCENSE BURNER', 'Shape Name JAR', 'Shape Name JUG', 'Shape Name KALATHOS', 'Shape Name KANTHAROS', 'Shape Name KANTHAROS FRAGMENT', 'Shape Name KERNOS', 'Shape Name KOTYLE', 'Shape Name KOTYLE FRAGMENT', 'Shape Name KRATER', 'Shape Name KRATER (?) FRAGMENT', 'Shape Name KRATER FRAGMENT', 'Shape Name KRATER FRAGMENTS', 'Shape Name KYATHOS', 'Shape Name KYATHOS FRAGMENT', 'Shape Name LEBES', 'Shape Name LEBES FRAGMENT', 'Shape Name LEKANIS', 'Shape Name LEKANIS FRAGMENT', 'Shape Name LEKANIS FRAGMENTS', 'Shape Name LEKANIS LID', 'Shape Name LEKANIS LID FRAGMENT', 'Shape Name LEKYTHOS', 'Shape Name LEKYTHOS FRAGMENT', 'Shape Name LEKYTHOS FRAGMENTS', 'Shape Name LID', 'Shape Name LID FRAGMENT', 'Shape Name LOUTROPHOROS', 'Shape Name LOUTROPHOROS FRAGMENT', 'Shape Name LOUTROPHOROS FRAGMENTS', 'Shape Name LYDION', 'Shape Name MASTOID', 'Shape Name MINIATURE PANATHENAIC AMPHORA', 'Shape Name MUG', 'Shape Name MUG FRAGMENT', 'Shape Name NESTORIS', 'Shape Name OINOCHOE', 'Shape Name OINOCHOE (?) FRAGMENT', 'Shape Name OINOCHOE FRAGMENT', 'Shape Name OINOCHOE FRAGMENTS', 'Shape Name OLLA', 'Shape Name OLPE', 'Shape Name OLPE FRAGMENT', 'Shape Name OLPE FRAGMENTS', 'Shape Name PELIKE', 'Shape Name PELIKE FRAGMENT', 'Shape Name PELIKE FRAGMENTS', 'Shape Name PHIALE', 'Shape Name PHIALE FRAGMENT', 'Shape Name PITCHER', 'Shape Name PLAQUE FRAGMENT', 'Shape Name PLAQUE FRAGMENTS', 'Shape Name PLATE', 'Shape Name PLATE FRAGMENT', 'Shape Name PLATE FRAGMENTS', 'Shape Name PLEMOCHOE', 'Shape Name PSEUDO-PANATHENAIC AMPHORA', 'Shape Name PSEUDO-PANATHENAIC AMPHORA FRAGMENT', 'Shape Name PSYKTER', 'Shape Name PYXIS', 'Shape Name PYXIS FRAGMENT', 'Shape Name PYXIS FRAGMENTS', 'Shape Name PYXIS LID', 'Shape Name PYXIS LID FRAGMENT', 'Shape Name RHYTON', 'Shape Name SKYPHOS', 'Shape Name SKYPHOS (?) FRAGMENT', 'Shape Name SKYPHOS FRAGMENT', 'Shape Name SKYPHOS FRAGMENTS', 'Shape Name STAMNOS', 'Shape Name STAMNOS FRAGMENT', 'Shape Name STAMNOS FRAGMENTS', 'Shape Name STAND', 'Shape Name STAND FRAGMENT', 'Shape Name STEMLESS CUP FRAGMENT', 'Shape Name STIRRUP JAR', 'Shape Name TANKARD', 'Shape Name UNKNOWN', 'Shape Name URN', 'Shape Name VARIOUS', 'Shape Name VASE', 'Fabric ARGIVE GEOMETRIC', 'Fabric ARRETINE', 'Fabric ATHENIAN', 'Fabric ATHENIAN (?)', 'Fabric ATHENIAN GEOMETRIC', 'Fabric ATHENIAN PROTOGEOMETRIC', 'Fabric BOEOTIAN', 'Fabric BOEOTIAN GEOMETRIC', 'Fabric CALENIAN', 'Fabric CAMPANA', 'Fabric CANOSAN', 'Fabric CHALCIDIAN', 'Fabric CORINTHIAN', 'Fabric CRETAN', 'Fabric CYCLADIC', 'Fabric CYPRIOT', 'Fabric CYPRIOT, BRONZE AGE', 'Fabric CYPRIOT, IRON AGE', 'Fabric CYPRIOT, MYCENAEAN STYLE', 'Fabric DAUNIAN', 'Fabric EAST GREEK', 'Fabric EAST GREEK GEOMETRIC', 'Fabric EAST GREEK, CLAZOMENIAN', 'Fabric EAST GREEK, FIKELLURA', 'Fabric EAST GREEK, NAUCRATITE', 'Fabric EGYPTIAN', 'Fabric ETRUSCAN', 'Fabric ETRUSCO-CORINTHIAN', 'Fabric FALISCAN', 'Fabric GALLIC', 'Fabric GALLO-ROMAN', 'Fabric GREEK', 'Fabric HELLADIC', 'Fabric HELLENISTIC', 'Fabric IBERIAN', 'Fabric IONIAN', 'Fabric ITALIOTE', 'Fabric ITALO-CORINTHIAN', 'Fabric ITALO-GEOMETRIC', 'Fabric LACONIAN', 'Fabric LACONIAN GEOMETRIC', 'Fabric MESSAPIAN', 'Fabric MINOAN', 'Fabric MYCENEAN', 'Fabric PONTIC', 'Fabric PROTO-ELAMITE', 'Fabric PROTOATTIC', 'Fabric PROTOCORINTHIAN', 'Fabric PROTOCORINTHIAN, TRANSITIONAL', 'Fabric ROMAN', 'Fabric SOUTH ITALIAN', 'Fabric SOUTH ITALIAN, APULIAN', 'Fabric SOUTH ITALIAN, CAMPANIAN', 'Fabric SOUTH ITALIAN, GNATHIAN', 'Fabric SOUTH ITALIAN, LUCANIAN', 'Fabric SOUTH ITALIAN, PAESTAN', 'Fabric SOUTH ITALIAN, SICILIAN', 'Fabric TERRA SIGILLATA', 'Fabric UNCERTAIN', 'Fabric VEIAN', 'Fabric VILLA NOVA', 'Technique ADDED COLOUR', 'Technique BLACK GLAZE', 'Technique BLACK PATTERN', 'Technique BLACK-FIGURE', 'Technique BROWN GLAZE', 'Technique BUCCHERO', 'Technique IMPASTO', 'Technique OUTLINE', 'Technique PATTERN', 'Technique PLAIN', 'Technique PSEUDO RED-FIGURE', 'Technique RED POLISHED WARE', 'Technique RED-FIGURE', 'Technique RELIEF', 'Technique RESERVING', 'Technique SILHOUETTE', 'Technique WHITE PAINTED WARE']\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ludovicaschaerf/anaconda3/envs/torch_arm/lib/python3.11/site-packages/torch/amp/autocast_mode.py:221: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling\n",
      "  warnings.warn('User provided device_type of \\'cuda\\', but CUDA is not available. Disabling')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(318, 768)\n"
     ]
    }
   ],
   "source": [
    "model_clip, _, preprocess = open_clip.create_model_and_transforms('ViT-L-14', pretrained='laion2b_s32b_b82k')\n",
    "tokenizer = open_clip.get_tokenizer('ViT-L-14')\n",
    "\n",
    "labels = []\n",
    "for type_col in ['Provenance', 'Shape Name', 'Fabric', 'Technique']:\n",
    "    ann_df = pd.read_csv(f'./data/vase_annotated_files/sim_{type_col}_seeds0000-20000.csv').drop(['Unnamed: 0', 'ID'], axis=1)\n",
    "    \n",
    "    labels += [type_col + ' ' + lbl for lbl in list(ann_df.columns)]\n",
    "        \n",
    "print(labels)\n",
    "pre_prompt = \"Vase art, \" #@param {type:\"string\"}\n",
    "text_descriptions = [f\"{pre_prompt}{label}\" for label in labels]\n",
    "text_tokens = tokenizer(text_descriptions)\n",
    "\n",
    "with torch.no_grad(), torch.cuda.amp.autocast():\n",
    "    text_features = model_clip.encode_text(text_tokens).float()\n",
    "    text_features /= text_features.norm(dim=-1, keepdim=True)\n",
    "    \n",
    "text_features = text_features.cpu().numpy()\n",
    "print(text_features.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f7858bbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic_clip_vecs = {l:v for l,v in zip(labels, text_features)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de6bd428",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d19c8e4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic_clip_vecs['Provenance ADRIA'].shape\n",
    "with open('data/CLIP_vecs_vases.pkl', 'wb') as f:\n",
    "    pickle.dump(dic_clip_vecs, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89b4a6fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "dic_clip_vecs['Abstract'].shape\n",
    "with open('data/CLIP_vecs.pkl', 'wb') as f:\n",
    "    pickle.dump(dic_clip_vecs, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ac84c8c-e916-4719-a7cd-610cea768d7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(annotations_file, 'rb') as f:\n",
    "        annotations = pickle.load(f)\n",
    "annotations['fname'][:10], annotations['z_vectors'][:1],# annotations['annotations'][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bcfdb5c-0ab9-462f-98ce-78bddbb96710",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = random.randint(0,10000)\n",
    "original_image_vec = annotations['z_vectors'][seed]\n",
    "img = generate_original_image(original_image_vec, model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8916281",
   "metadata": {},
   "outputs": [],
   "source": [
    "concept_id = \"Readymade\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55b47c11-9f42-4ff0-9098-f32b1e826ccb",
   "metadata": {},
   "outputs": [],
   "source": [
    "samples = 500 \n",
    "abstracts = np.array([float(ann) for ann in ann_df[concept_id]])\n",
    "\n",
    "repr_idxs = list(np.argsort(abstracts))[:samples]\n",
    "abstract_idxs = list(np.argsort(abstracts))[-samples:]\n",
    "    \n",
    "z = annotations['z_vectors'][abstract_idxs[0]]\n",
    "img = generate_original_image(z, model)\n",
    "if 'ID' in ann_df.columns:\n",
    "    ann_df = ann_df.drop('ID', axis=1)\n",
    "print(ann_df.loc[abstract_idxs[0], :].sort_values()[-20:])\n",
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb7ca184",
   "metadata": {},
   "outputs": [],
   "source": [
    "ann_df['Abstract'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1a4d9e3-656c-41c1-a433-1c1a83fa33de",
   "metadata": {},
   "outputs": [],
   "source": [
    "z = annotations['z_vectors'][repr_idxs[0]]\n",
    "img = generate_original_image(z, model)\n",
    "print(ann_df.loc[repr_idxs[0], :].sort_values())\n",
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65218a37-76e6-4df6-929e-fc672ff5f37b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for col in ann_df.columns:\n",
    "    if '1' in col:\n",
    "        print(col)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72fa8929-40a0-4376-97d2-5d6ac4a4eda6",
   "metadata": {},
   "outputs": [],
   "source": [
    "concepts = ['Op Art', 'Minimalism', 'Surrealism', 'Baroque', 'Lithography', 'Woodcut', 'Etching', 'Intaglio',]\n",
    "\n",
    "vectors, nodes_in_common = get_concepts_vectors(concepts, annotations, ann_df, method='LR', samples=150, C=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43c5fdd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(nodes_in_common)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dbf83d5-e33e-4401-8541-1dbd0aa4b10a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import plotly.graph_objects as go\n",
    "\n",
    "# # Generate example data\n",
    "# x = projection[:, 0]  # x-coordinates of vectors\n",
    "# y = projection[:, 1]  # y-coordinates of vectors\n",
    "# z = projection[:, 2]  # z-coordinates of vectors\n",
    "# data = []\n",
    "\n",
    "# for i in range(len(x)):\n",
    "#     # Create a 3D scatter plot\n",
    "#     data.append(go.Scatter3d(\n",
    "#         x=[0, x[i]],  # Start at x = 0\n",
    "#         y=[0, y[i]],  # Start at y = 0\n",
    "#         z=[0, z[i]],  # Start at z = 0\n",
    "#         mode='lines',\n",
    "#         marker=dict(size=5),\n",
    "#         line=dict(width=2),\n",
    "#         hovertext=concepts[i],\n",
    "#         text=concepts[i],\n",
    "#         name=concepts[i],\n",
    "#         showlegend=True,\n",
    "#     ))\n",
    "\n",
    "# # layout = go.Layout(margin = dict( l = 0,\n",
    "# #                                   r = 0,\n",
    "# #                                   b = 0,\n",
    "# #                                   t = 0)\n",
    "# #                   )\n",
    "# fig = go.Figure(data=data)\n",
    "# # plot(fig,filename=\"vector.html\",auto_open=False,image='png',image_height=800,image_width=1500)\n",
    "# # # Set the title\n",
    "# fig.update_layout(title='3D Vectors')\n",
    "# # Set the axis labels\n",
    "# fig.update_layout(scene=dict(\n",
    "#     xaxis=dict(title='X', range=[-10, 10],),\n",
    "#     yaxis=dict(title='Y', range=[-10, 10],),\n",
    "#     zaxis=dict(title='Z', range=[-10, 10],),\n",
    "# ))\n",
    "\n",
    "# # Show the plot\n",
    "# fig.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30a395c4-9d42-4790-af08-62b474eb1df3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "\n",
    "edges = []\n",
    "for i in range(len(concepts)):\n",
    "    for j in range(len(concepts)):\n",
    "        if i != j:\n",
    "            print(f'Similarity between {concepts[i]} and {concepts[j]}')\n",
    "            similarity = cosine_similarity(vectors[i,:].reshape(1, -1), vectors[j,:].reshape(1, -1))\n",
    "            print(np.round(similarity[0][0], 3))\n",
    "            edges.append((concepts[i], concepts[j], np.round(similarity[0][0], 3)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16e81bd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01d31c45",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create an empty graph\n",
    "G = nx.Graph()\n",
    "\n",
    "# Add edges with weights to the graph\n",
    "for edge in edges:\n",
    "    node1, node2, weight = edge\n",
    "    G.add_edge(node1, node2, weight=weight)\n",
    "\n",
    "# Draw the graph\n",
    "pos = nx.spring_layout(G)  # Position nodes using the spring layout algorithm\n",
    "nx.draw_networkx(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=12)\n",
    "\n",
    "# Draw edge labels with weights\n",
    "edge_labels = nx.get_edge_attributes(G, 'weight')\n",
    "nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)\n",
    "\n",
    "# Display the graph interactively\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8fd3469",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nxviz as nv\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f0925e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyvis.network import Network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f89bd374",
   "metadata": {},
   "outputs": [],
   "source": [
    "net = Network(notebook=True, height=\"750px\", width=\"100%\",)\n",
    "for e in edges:\n",
    "    src = e[0]\n",
    "    dst = e[1]\n",
    "    w = e[2]\n",
    "\n",
    "    net.add_node(src, src, title=src)\n",
    "    net.add_node(dst, dst, title=dst)\n",
    "    net.add_edge(src, dst, value=w, title=src + ' to ' + dst + ' similarity ' +str(w))\n",
    "print(net)\n",
    "\n",
    "# neighbor_map = net.get_adj_list()\n",
    "\n",
    "# # add neighbor data to node hover data\n",
    "# for node in net.nodes:\n",
    "#     node[\"title\"] += \" Neighbors:<br>\" + \"<br>\".join(neighbor_map[node[\"id\"]])\n",
    "#     node[\"value\"] = len(neighbor_map[node[\"id\"]])\n",
    "\n",
    "\n",
    "# net.toggle_physics(True)\n",
    "net.show('nx.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fce19b9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# seed = random.randint(0,10000)\n",
    "# original_image_vec = annotations['z_vectors'][seed]\n",
    "# img = generate_original_image(original_image_vec, model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "184687a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# z_0 = original_image_vec + 2 * vectors[0] + 2 * vectors[1]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52bc2a0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# img = model(z_0.to(), None, truncation_psi=0.7, noise_mode='const')\n",
    "# img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5afdc77f",
   "metadata": {},
   "outputs": [],
   "source": [
    "with dnnlib.util.open_url('./data/model_files/network-snapshot-010600.pkl') as f:\n",
    "    G = legacy.load_network_pkl(f)['G_ema'].to('cpu') # type: ignore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad9865af",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c771ba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(G.mapping)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eec0534d",
   "metadata": {},
   "outputs": [],
   "source": [
    "W = G.mapping(z, None, truncation_psi=0.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97057ae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(G.mapping(z, None, truncation_psi=1).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15cbbc5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(G.synthesis(W, noise_mode='const').shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f02410a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = random.randint(0, 1000000)\n",
    "W_0 = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to('cpu')\n",
    "W = W_0.repeat([1, G.num_ws, 1])\n",
    "img = G.synthesis(W, noise_mode='random')\n",
    "img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)\n",
    "im = PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')\n",
    "im.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "046ffb56",
   "metadata": {},
   "outputs": [],
   "source": [
    "z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to('cpu')\n",
    "W = G.mapping(z, None, truncation_psi=1)\n",
    "print(W[0, 0] == W[0, 1])\n",
    "img = G.synthesis(W, noise_mode='random')\n",
    "img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)\n",
    "im = PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')\n",
    "im.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ea5676d",
   "metadata": {},
   "outputs": [],
   "source": [
    "W[:,0,:,].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0687e87",
   "metadata": {},
   "outputs": [],
   "source": [
    "z.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a01b9c7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "z_vals = []\n",
    "w_vals = []\n",
    "fnames = []\n",
    "# Generate images.\n",
    "seeds = range(100)\n",
    "for seed_idx, seed in enumerate(seeds):\n",
    "    print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))\n",
    "    z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim))\n",
    "    z_vals.append(z)\n",
    "    z = z.to('cpu')\n",
    "    fnames.append(f'data/images/seed{seed:04d}.png')\n",
    "    # Construct an inverse rotation/translation matrix and pass to the generator.  The\n",
    "    # generator expects this matrix as an inverse to avoid potentially failing numerical\n",
    "    # operations in the network.\n",
    "    # if hasattr(G.synthesis, 'input'):\n",
    "    #     m = make_transform(translate, rotate)\n",
    "    #     m = np.linalg.inv(m)\n",
    "    #     G.synthesis.input.transform.copy_(torch.from_numpy(m))\n",
    "    W = G.mapping(z, None, truncation_psi=1)\n",
    "    w_vals.append(W[:,0,:])\n",
    "    img = G.synthesis(W, noise_mode='const')\n",
    "\n",
    "    #img = G(z, None, truncation_psi=0.7, noise_mode='const')\n",
    "    img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)\n",
    "    PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save(f'data/images/seed{seed:04d}.png')\n",
    "    \n",
    "info = {'fname': fnames, 'z_vectors': z_vals, 'w_vectors': w_vals}\n",
    "with open(f'data/images/seeds{seeds[0]:04d}-{seeds[-1]:04d}.pkl', 'wb') as f:\n",
    "    pickle.dump(info, f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e44701e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import open_clip\n",
    "# from open_clip import tokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e85c85c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model, _, preprocess = open_clip.create_model_and_transforms('ViT-g-14', pretrained='laion2b_s34b_b88k')\n",
    "# print(model.eval())\n",
    "# context_length = model.context_length\n",
    "# vocab_size = model.vocab_size\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1990c21",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rdflib\n",
    "import csv\n",
    "\n",
    "# Load the RDF file\n",
    "rdf_file = '/Users/ludovicaschaerf/Desktop/Vase data/Getty vases.rdf'\n",
    "graph = rdflib.Graph()\n",
    "graph.parse(rdf_file)\n",
    "\n",
    "# Specify the desired CSV file path\n",
    "csv_file = '/Users/ludovicaschaerf/Desktop/Vase data/Getty vases.csv'\n",
    "print(len(graph))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92d94cf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(graph))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c47800b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Open the CSV file in write mode\n",
    "with open(csv_file, 'w', newline='', encoding='utf-8') as file:\n",
    "\n",
    "    # Create a CSV writer object\n",
    "    writer = csv.writer(file)\n",
    "\n",
    "    # Write the header row\n",
    "    writer.writerow(['Subject', 'Predicate', 'Object'])\n",
    "\n",
    "    # Iterate over each triple in the RDF graph\n",
    "    for subject, predicate, obj in graph:\n",
    "\n",
    "        # Convert the triple components to strings\n",
    "        subject_str = str(subject)\n",
    "        predicate_str = str(predicate)\n",
    "        object_str = str(obj)\n",
    "\n",
    "        # Write the triple as a row in the CSV file\n",
    "        writer.writerow([subject_str, predicate_str, object_str])\n",
    "\n",
    "print('Conversion completed successfully!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e7d4b21",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "final_file = pd.read_csv(csv_file)\n",
    "final_file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c337915d",
   "metadata": {},
   "outputs": [],
   "source": [
    "vases = pd.read_csv('/Users/ludovicaschaerf/Desktop/Vase data/oxford_vases_info.csv')\n",
    "vases_links = vases['URI']\n",
    "vases.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdd909e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "vases['Vase Number'].nunique(), vases.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "883531df",
   "metadata": {},
   "outputs": [],
   "source": [
    "vases = vases.groupby('Vase Number').first().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee27fdbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import requests\n",
    "response = requests.get(vases_links[0])\n",
    "html_content = response.text\n",
    "soup = BeautifulSoup(html_content, 'html.parser')\n",
    "div = soup.find('div', class_='recordImage')\n",
    "if div is not None:\n",
    "    img = div.find('img')\n",
    "    if img is not None:\n",
    "        img_src = img['src']\n",
    "        print(\"Image source:\", img_src)\n",
    "    else:\n",
    "        print(\"No image found inside the div.\")\n",
    "else:\n",
    "    print(\"Div not found.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2edc40d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# Specify the folder path to save the images\n",
    "folder_path = '/Users/ludovicaschaerf/Desktop/Vase data/images'\n",
    "\n",
    "# Create the folder if it doesn't exist\n",
    "os.makedirs(folder_path, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7bfcfb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f06e4e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "for uri,i in tqdm(zip(vases['URI'], vases['Vase Number'])):\n",
    "    if i > 473:\n",
    "        response = requests.get(uri)\n",
    "        html_content = response.text\n",
    "        soup = BeautifulSoup(html_content, 'html.parser')\n",
    "        div = soup.find('div', class_='recordImage')\n",
    "        if div is not None:\n",
    "            img = div.find('img')\n",
    "            if img is not None:\n",
    "                img_src = 'https://www.carc.ox.ac.uk/' + img['src']\n",
    "                print(\"Image source:\", img_src)\n",
    "            else:\n",
    "                print(\"No image found inside the div.\", i)\n",
    "                continue\n",
    "        else:\n",
    "            print(\"Div not found.\", i)\n",
    "            continue\n",
    "        # Specify the path to save the image\n",
    "        save_path = os.path.join(folder_path, str(i) + '.jpg')\n",
    "\n",
    "        # Download the image and save it to the specified path\n",
    "        response = requests.get(img_src)\n",
    "        if response.status_code == 200:\n",
    "            with open(save_path, 'wb') as file:\n",
    "                file.write(response.content)\n",
    "            print(\"Image downloaded successfully.\")\n",
    "        else:\n",
    "            print(\"Failed to download the image.\")\n",
    "            continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1420a58",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}