-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtli.py
1593 lines (1305 loc) · 51.7 KB
/
tli.py
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
# MUSIC: https://www.youtube.com/watch?v=F3OFIuIXcSo
# https://www.youtube.com/watch?v=m_ysN9BQm8s
# FIXME: https://arxiv.org/pdf/2006.12986.pdf
# -----> depth/width/kernel level --> (podzial kodu)
# https://github.com/JaminFong/FNA/blob/master/fna_det/tools/apis/param_remap.py
# (paper) Karate Club
# https://arxiv.org/pdf/2003.04819.pdf
"""
weryfikacja:
- [ ] zrobic wizualizacje (reset -> applyied --> GT) dla KD
--> SCORE/LOSS roznicy rozkladu --> SUMA/MEAN
--> histogramy jako wrzuta do "folderu" dla warstwy
- [ ] wizualizacja dopasowania [1. matching 2. injection]
- [ ] zrobic exp__tli --> to samo co MNIST (1k)
tylko modele 2flops moze 2 rozne od siebie
jeden przeuczony drugi nie --> transfer --> patrzymy jaki score (ACC)
[train/test mean]
technikalia:
- [ ] do kazdej warstwy dac "prawdopodobienstwo przypisania trans."
te co maja najwyzsze prawd. (kilka) vs. (one) big boss
to sa: rescale(X) + (wiekszawaga)*centercrop(X) + iter. mixing(zbioru)
- [ ] drzewiasty algo? similarity hashing? [[LHS]]
jako dopasowanie!!!!!!!!!!!!!!!!!
- [ ] poczatkowe warstwy maja "wieksza wage"/"wieksze warstwy"
--> zrobic jakas uczciwa krzywa z palca 100 -> 75 na ostatnich warstwach
- [ ] mixowanie wiele sieci z `results-imagenet`
-> az ***nasycimy*** wszystkie wagi
- [ ] uzywanie `trace_graph` --> a nie "modulow" (uwzglednienie relacji)
- [ ] !!! UZYC graph cluster-ingu // zamiast DP
dodatki:
- [ ] FIXME: a co z reszta? np. ._bn0.num_batches_tracked
----------> model.state_dict().keys()
zrobic cos w stylu --> with_meta = True
- [ ] FIXME: zrobic "szybkie" szukanie najlepszych modeli z ImageNet
jesli ktos zdefiniuje [[auto=True]]
- [ ] analiza: https://github.com/KamilPiechowiak/weights-transfer/pull/17/files
- [ ] sprawdzic czy dziala [WS]/aug/grid tutaj?
- [ ] analiza: https://github.com/mortezamg63/Accessing-and-modifying-different-layers-of-a-pretrained-model-in-pytorch/blob/master/README.md
- [ ] jakas porzadna nazwa np.
yak shaving (use urban dictionary) // sponge function
---> still from crypto name / Unsponge ducktape
ducktransfer
- [ ] analiza: https://github.com/MSeal/agglom_cluster
- [ ] wielopoziomowe dopasowanie/clustry (a nie standaryzacja):
(zagniezdzone clustry)
- in/mid/out -> block -> branch -> grupa tensorow -> tensor -> itp.
"""
# commit: dark tensor rises
import collections
import os
import random
import sys
# FIXME: repair config "reinit" case
from copy import copy
from typing import Dict, List
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import torch
import torch.nn as nn
from graphviz import Digraph
from karateclub import FeatherNode, NetMF
from networkx.drawing.nx_agraph import graphviz_layout
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
from torch.autograd import Variable
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
################################################################################
# API
################################################################################
def apply_tli(model, teacher=None):
# print(f"[TLI] model={model}")
# print(f"[TLI] teacher={teacher}")
model_teacher = str_to_model(teacher)
transfer(model_teacher, model)
return model
def get_tli_score(model_from, model_to):
model_a = str_to_model(model_from)
model_b = str_to_model(model_to)
sim, _, _, _ = transfer(model_a, model_b)
return sim
def get_model_timm(name="dla46x_c"):
try:
import timm
except:
raise Exception("timm package is not installed! try `pip install timm`")
# FIXME: `channels`!!! and `classes`!!! as param (debug)
model = timm.create_model(name, num_classes=10, in_chans=3, pretrained=True)
return model
# FIXME: move to class ModelObj
def str_to_model(name):
if isinstance(name, str):
print(f"loading `{name}` from pytorch-image-models...")
model = get_model_timm(name)
else: # FIXME: check if "pytorch" model
model = name
return model
# def get_tli_score(model_from, model_to):
# model_a = str_to_model(model_from)
# model_b = str_to_model(model_to)
# score_ab = transfer(model_a, model_b)
# score_ba = transfer(model_b, model_a)
# sim = (score_ab + score_ba) / 2
# print(
# f"[score_ab={round(score_ab, 2):6} score_ba={round(score_ba, 2):6} | sim={round(sim, 2):6}]"
# )
# return sim
################################################################################
# Utils
################################################################################
def apply_hard_reset(model):
for layer in model.modules():
if hasattr(layer, "reset_parameters"):
nn.init.zeros_(layer.weight)
if layer.bias is not None:
nn.init.zeros_(layer.bias)
return model
def fn_inject(from_tensor, to_tensor):
# FIXME: debug -> vis. -> rescale
from_slices, to_slices = [], []
for a, b in zip(from_tensor.shape, to_tensor.shape):
if a < b:
from_slices.append(slice(0, a))
to_slices.append(slice((b - a) // 2, -((b - a + 1) // 2)))
elif a > b:
from_slices.append(slice((a - b) // 2, -((a - b + 1) // 2)))
to_slices.append(slice(0, b))
else:
from_slices.append(slice(0, a))
to_slices.append(slice(0, b))
to_tensor[tuple(to_slices)] = from_tensor[tuple(from_slices)]
################################################################################
################################################################################
################################################################################
# FIXME: ladnie podzielic na "matching" / "injection"
##########################################################
# --> fn_stats() -> [abs.mean(), rozklad()]
# --> fn_kullbeck(stats1, stats2) -> [0, 1]
# FIXME: pretty list of modules? --> fn_stats
# if GT -> fn_kullbeck
##########################################################
# DIST: https://github.com/timtadh/zhang-shasha
# "graph matching" https://arxiv.org/pdf/1904.12787.pdf
# https://github.com/deepmind/deepmind-research/tree/master/graph_matching_networks
# Graph / Node [Embedding???] / Graph2Vec
# https://github.com/Jacobe2169/GMatch4py
###############
# https://github.com/benedekrozemberczki/awesome-graph-classification
# WeisfeilerLehman ??????
################# BEST ###################
## https://karateclub.readthedocs.io/en/latest/notes/introduction.html
## SELF-LEARN? ---> weak-estimators???
# https://github.com/topics/graph2vec
# [FIXME] konstytucja rewolucjonisty
# kazdy blok ma: [[ a) pozycje b) strukture c) rozmiar ]]
# taki musi byc tez *score*
# FIXME: matching blokow to prekalkulacja scorow
# --> rozwazania beda tensor vs tensor
# STEPS:
# (1) (s-match) cluster vs cluster (top k/percentile)
# (2) (d-match) cluster (iterate -> tensor // double in-tree/out-tree)
# (3) (w-inject)/(k-inject) combo-inject
# pozycja -> level
# struktura -> treedist(edges)
# meta-learning?
# a) pozycje (normalized)
# b) strukture (graph features)
# c) rozmiar (tensor)
# AS DIFF (a -> b)
# [a] (position, structure) -
# [b] (position, structure)
# SELF-LEARN / [[[SELF-ENCODER?]]]
# AS UNSUPERVISED? --> data augmentation / [permutation]??????
# --> rozne tensory z tego modulu sa?????????
# [a] [(position, structure), (tensor)] --> 0/1
# label: y = [1 -> to ten tensor], [0 -> to nie ten tensor]
# GENIUS =========
# self-learn? permutacje? graph2vec na samym sobie?
# uczy sie rozpoznawac jaki to tensor xd
# https://arxiv.org/pdf/2010.12878.pdf
# ================
# ??????????????
# http://ryanrossi.com/pubs/KDD18-graph-attention-model.pdf
# FIXME: "faster and naive alternative"
# after scoring [size] -->> [maximum weight bipartite b-matching]
########################## DRAFT ###############################################
# FIXME: [ALWAYS FIT ALL?] model.fit()
# FIXME: [ALWAYS in-tree/out-tree split]
# FIXME: graph embeddings? from all graphs? (EVEN student+teacher)
# or per `cluster` --> graph embedding? (1)
# and per `graph` --> graph embedding? (2)
# so i have then 2 different embeddings space...
# N basic --> (nodeE, shape)
# N advanced --> (nodeE, graphE in-tree, graphE out-tree, shape)
# FIXME: there is a problem with "NODE Embedding"?
# --> change to split in/out graph embedding?
# [S + G + N], [N_i] ---> 0/1 [smooth labeling 0.5/0.25 itc.]
# 1) structure (graph embedding)
# 2) graph (graph embedding)
# 3) node (node embedding)
# ---> laczy na chwile graf [student-teacher] --> cos na tym robi?
# ---> duzo prostsze featury // --> l / max(l) --> c / max(c)
# [XXX] READ THIS: https://markheimann.github.io/projects.html
# https://sci-hub.se/https://link.springer.com/chapter/10.1007/978-3-319-93040-4_57
# print(clf.predict(predictionData),'\n')
# model = LinearRegression().fit(X_train, y_train)
# print(model)
# y_hat = downstream_model.predict_proba(X_test)[:, 1]
# auc = roc_auc_score(y_test, y_hat)
# print('AUC: {:.4f}'.format(auc))
############################################################################
# FIXME: BIAS / WEIGHT (wildcard)
# FIXME: split_d for `student` then ensemble for encoder?
# >>> FOR FLOW
# split_map = split_flow_level(graph_teacher)
# pprint(split_map)
# encoded_split_map = encoder_graph(split_map)
# pprint(encoded_split_map)
# >>> FOR CLUSTERS
# for cluster_idx in graph_teacher.cluster_map.keys():
# split_map = split_cluster_level(graph_teacher, cluster_idx)
# pprint(split_map)
# print(f"cluster_idx={cluster_idx}")
# break
# >>> ALL FOR NODES
# edges = []
# for a, dst in graph_teacher.edges.items():
# for b in dst:
# edges.append([a, b])
# obj = encoder_nodes(edges)
# pprint(obj)
# sys.exit(1)
# >>> FOR NODES IN CLUSTER
# for cluster_idx in graph_teacher.cluster_map.keys():
# obj = encoder_nodes(graph_teacher.cluster_map[cluster_idx].edges)
# pprint(obj)
# print("="*30)
# FIXME: KD-tree?
# FIXME: zrobic wizualizacje matchingu!!!!!!!!!!!!!!!!!!!!!!!
# (przetestowac laczac ze soba 2 tensory)
# (dodatek - wizualizacja dodatkowych `edges` do debugu)
#### [[[[[Fast Network Alignment]]]]]]] / xNetMF
# XXX XXX XXX XXX XXX [READ THIS] #######################
# https://gemslab.github.io/papers/heimann-2018-regal.pdf
# https://github.com/GemsLab/REGAL
#########################################################
# class NodeFeatures
# [a] structures_info
# [b] graph_info
# [c] ???? shape
# for multiple matches [[ SparseMAP ]]
# ---> https://arxiv.org/pdf/1802.04223.pdf
# KD-tree? for representations?
# ----> MATRIX???
# matching if provided map
################################################################################
################################################################################
################################################################################
# XXX XXX XXX XXX XXX [READ THIS] #######################
# https://gemslab.github.io/papers/heimann-2018-regal.pdf
# https://github.com/GemsLab/REGAL
#########################################################
def get_networkx(edges, dag=True):
if dag:
G = nx.DiGraph()
else:
G = nx.Graph()
G.add_edges_from(edges)
return G
def show_networkx(graph):
if isinstance(graph, list):
graph = get_networkx(edges=graph)
pos = graphviz_layout(graph, prog="dot")
nx.draw(graph, pos, with_labels=True, arrows=True)
plt.show()
def dag_split(edges, token, root=None):
graph = {}
for a, b in edges:
if a not in graph:
graph[a] = []
if b not in graph:
graph[b] = []
graph[a].append(b)
graph[b].append(a)
edges_split = []
visited, queue = set(), collections.deque([root])
while queue:
stop = False
node_root = queue.popleft()
if node_root not in graph:
continue
if node_root == token:
break
for node in graph[node_root]:
if node not in visited:
if node == token:
stop = True
edges_split.append([node_root, node])
visited.add(node)
queue.append(node)
if stop:
break
# FIXME: empty graphs?
if not edges_split:
edges_split.append([token, token])
return edges_split
def graph_splits(edges, nodes=False):
G = get_networkx(edges)
order = list(nx.topological_sort(G))
if len(order) == 0:
return {}
idx_src, idx_dst = order[0], order[-1]
if not nodes:
nodes = set()
for a, b in edges:
nodes.add(a)
nodes.add(b)
split_map = {}
for idx in nodes:
in_tree = dag_split(edges, idx, root=idx_src)
out_tree = dag_split(edges, idx, root=idx_dst)
split_map[idx] = {"in-tree": in_tree, "out-tree": out_tree}
return split_map
def graph_norm(edges, attr=None):
normal_id_map = {}
normal_id_iter = [0]
rev_mask = {}
def __for_single(idx):
if not idx in normal_id_map:
normal_id_map[idx] = normal_id_iter[0]
rev_mask[normal_id_iter[0]] = idx
normal_id_iter[0] += 1
# random.shuffle(edges)
for a, b in edges:
__for_single(a)
__for_single(b)
norm_edges = []
for a, b in edges:
norm_edges.append([normal_id_map[a], normal_id_map[b]])
# norm_edges = sorted(norm_edges)
norm_attr = []
if attr:
for i in range(len(normal_id_map.keys())):
norm_attr.append(attr[rev_mask[i]])
return norm_edges, rev_mask, norm_attr
def utils_map_to_mask(split_map):
mask, graphs = [], []
for key, split_dict in split_map.items():
for dict_key in split_dict.keys():
_g, rev_mask, _ = graph_norm(split_dict[dict_key])
g = get_networkx(_g, dag=False)
mask.append([key, dict_key])
graphs.append(g)
return mask, graphs
def utils_mask_to_map(mask, X):
split_map = {}
for i, (key, dict_key) in enumerate(mask):
if key not in split_map:
split_map[key] = {}
split_map[key][dict_key] = X[i]
return split_map
################################################################################
def split_flow_level(graph):
edges = []
for edge in graph.cluster_links:
cluster_idx_1 = graph.nodes[edge[0]].cluster_idx
cluster_idx_2 = graph.nodes[edge[1]].cluster_idx
edges.append([cluster_idx_1, cluster_idx_2])
return graph_splits(edges)
def split_cluster_level(graph, cluster_idx):
edges = graph.cluster_map[cluster_idx].edges
return graph_splits(edges)
def encode_graph(split_map):
mask, graphs = utils_map_to_mask(split_map)
# FIXME: move to settings
from karateclub import GL2Vec
model = GL2Vec(dimensions=16) #FeatherGraph(eval_points=2, order=2)
print("FIT")
model.fit(graphs)
print("EMBEDDING")
X = model.get_embedding()
print("-------------------->", X.shape)
return utils_mask_to_map(mask, X)
################################################################################
# TLI
################################################################################
class TLIConfig(object):
def __init__(self, adict):
self.__dict__.update(adict)
from karateclub import Diff2Vec
embedding_dim = 5 # best 4, 6, 5 / FIXME: was 9, how to find?
CONFIG = TLIConfig(
{
# FIXME: move outsite? --> lazy_load?
"node_embedding_attributed": FeatherNode( # 2, 4
eval_points=4, order=4, svd_iterations=100, reduction_dimensions=32
),
"node_embedding_neighbourhood": NetMF(
dimensions=embedding_dim
), # FIXME: use xNetMF
# Diff2Vec(diffusion_number=5, diffusion_cover=5, dimensions=embedding_dim),
"autoencoder": MLPRegressor(
max_iter=100, # 100 // 3, # FIXME: best 50
early_stopping=False,
activation="relu",
solver="adam",
tol=0.0001,
##############################################
# n_iter_no_change=100, # FIXME: is that good?
##############################################
hidden_layer_sizes=(200, 50, 25,), # 125, 25
warm_start=True,
learning_rate_init=0.0005,
alpha=0.001,
verbose=True,
),
"test_size": 0.05, # FIXME: this is important!
"samples_per_tensor": 10,
}
)
def E_nodes(edges, attr=None):
norm_graph, rev_mask, norm_attr = graph_norm(edges, attr=attr)
if len(rev_mask) == 0:
return []
model = (
CONFIG.node_embedding_attributed
if attr
else CONFIG.node_embedding_neighbourhood
)
graph = get_networkx(norm_graph, dag=False)
if attr:
model.fit(graph, np.array(norm_attr))
X = model.get_embedding()
else:
model.fit(graph)
X = model.get_embedding()
print(f"[E_nodes {X.shape}]", end="")
encoded_nodes = {}
for i in range(X.shape[0]):
encoded_nodes[rev_mask[i]] = X[i]
return encoded_nodes
def F_architecture(graph, mlb=None, mfa=None):
### POSITION ENCODING ###
edges = []
cluster_feature = {}
for cluster_idx, cluster in graph.cluster_map.items():
cluster_feature[cluster_idx] = [len(cluster.nodes) / (1 + len(cluster.edges))]
for edge in graph.cluster_links:
cluster_idx_1 = graph.nodes[edge[0]].cluster_idx
cluster_idx_2 = graph.nodes[edge[1]].cluster_idx
edges.append([cluster_idx_1, cluster_idx_2])
P = E_nodes(edges, attr=cluster_feature)
### STRUCTURE ENCODING ###
S = {}
for cluster_idx in graph.cluster_map.keys():
edges = graph.cluster_map[cluster_idx].edges
## obj = E_nodes(edges)
if len(edges) > embedding_dim:
obj = E_nodes(edges)
else:
obj = {}
for idx in graph.cluster_map[cluster_idx].nodes:
obj[idx] = np.array([0.0] * embedding_dim) # FIXME: config
S.update(obj)
### NODE ENCODING ###
N = {} # FIXME: move to fn_node_encoder?
vec = []
for idx, node in graph.nodes.items():
vec.append(__encode(node.name))
# vec.append(list(node.name.replace(".weight", "").replace(".bias", "")))
# vec.append(node.name.split("."))
vec = mlb.transform(vec)
vec = mfa.transform(vec)
vec_final = []
for i, (idx, node) in enumerate(
graph.nodes.items()
): # FIXME: better way? [pad len 4]
_shape4 = nn.ConstantPad1d((0, 4 - len(node.size)), 0.0)(
torch.tensor(node.size)
)
#shape_ab = __shape_score(_shape4.type(torch.FloatTensor), (100, 1, 1, 1))
#shape_ba = __shape_score(_shape4.type(torch.FloatTensor), (1, 100, 1, 1))
shape4 = _shape4.type(torch.FloatTensor) / torch.max(1 + _shape4)
if shape4[0] > shape4[1]:
rot = 1
else:
rot = 0
_idx_rev = (graph.max_idx - node.idx) / graph.max_idx
_idx_rev2 = (node.idx) / graph.max_idx
_level_rev = (graph.max_level - node.level) / graph.max_level
_level_rev2 = (node.level) / graph.max_level
_cluster_rev = (graph.max_idx - node.cluster_idx) / graph.max_idx
_cluster_rev2 = (node.cluster_idx) / graph.max_idx
_type = 0 if ".bias" in node.name else 1
# dotcount = node.name.count('.')
# N[idx] = np.array(
vec_final.append(np.array(
[rot]
+ shape4.tolist()
+ [(_idx_rev + _cluster_rev+_level_rev)/3,
(_idx_rev2+_cluster_rev2+_level_rev2)/3, _type]
))
# vec_final.append(np.array(
# # [shape_ab, shape_ba]
# [rot]
# + shape4.tolist()
# ))
from sklearn import preprocessing
# _pp = preprocessing.QuantileTransformer() # BEST
# _pp = preprocessing.QuantileTransformer() # 83 / 158
# _pp = preprocessing.Normalizer(norm='l2') # 77 / 158
# _pp = preprocessing.Normalizer(norm='l1') # 76 / 158
# _pp = preprocessing.Normalizer(norm='max') # [78] 79 / 158
# _pp = preprocessing.PowerTransformer() # 80 / 158
# _pp = preprocessing.MaxAbsScaler() #XXX 20 77 / 158
# _pp = preprocessing.RobustScaler() # 78 / 158
_pp = preprocessing.StandardScaler() #XXX 85 / 158
# _pp = preprocessing.KBinsDiscretizer(n_bins=10, encode='ordinal',
# strategy='quantile') # 75
vec_final = _pp.fit_transform(vec_final)
for i, (idx, node) in enumerate(
graph.nodes.items()
):
# FIXME???????? without vec_final?
# print(vec_final[i])
N[idx] = np.array(vec_final[i].tolist() + vec[i].tolist())
print("(encode_graph ended)")
return P, S, N
def __q(a, b):
return np.array(a) + np.array(b)
# return np.array(a) * np.array(b) # 60 / 158
# return np.concatenate((a, b), axis=0) # 65 / 158
def __shape_score(s1, s2):
if len(s1) != len(s2):
return 0
score = 1
for x, y in zip(s1, s2):
score *= min(x / y, y / x)
return score
# gen_dataset / `self-learn`
def gen_dataset(graph, P, S, N, EG, prefix=""):
X, y = [], []
# FIXME: move to encoder settings? / encoder definition
for idx, node in graph.nodes.items():
if node.type != "W": # FIXME: is it good?
continue
cluster_idx = node.cluster_idx
# FIXME: make it pretty
# FIXME: encoder score for [N]
# === CASE 1: [self to self] (q_src, q_dst) -> 1
for _ in range(CONFIG.samples_per_tensor):
# FIXME: move to `augmentation`
p_src = np.array(P[cluster_idx])
r = np.random.uniform(low=-0.05, high=0.05, size=p_src.shape)
p_src += r
s_src = np.array(S[idx])
r = np.random.uniform(low=-0.05, high=0.05, size=s_src.shape)
s_src += r
q_src = p_src.tolist() + s_src.tolist() + list(N[idx]) + \
EG[f"{prefix}_{idx}"]["in-tree"].tolist()
X.append(__q(q_src, q_src))
# FIXME: verify 0.05, 0.05? maybe add as std/var
y.append(1 + np.random.uniform(low=-0.05, high=0.05))
q_src = list(P[cluster_idx]) + list(S[idx]) + list(N[idx]) + \
EG[f"{prefix}_{idx}"]["in-tree"].tolist()
X.append(__q(q_src, q_src))
y.append(1)
def __get_node(cluster_idx=None, type=None):
r_idx = None
if cluster_idx is not None:
nodes = list(graph.cluster_map[cluster_idx].nodes)
else:
nodes = list(graph.nodes.keys())
for _ in range(len(N)):
r_idx = random.choice(nodes)
if graph.nodes[r_idx].type == type or not type:
break
return r_idx
# === CASE 2: same cluster, W
for _ in range(CONFIG.samples_per_tensor):
r_idx = __get_node(cluster_idx=cluster_idx, type="W")
r_cluster_idx = cluster_idx
if idx == r_idx:
continue
q_dst = list(P[r_cluster_idx]) + list(S[r_idx]) + list(N[r_idx]) + \
EG[f"{prefix}_{r_idx}"]["in-tree"].tolist()
N_bonus = 0
N_dist = np.linalg.norm(N[idx] - N[r_idx])
if N_dist <= 1:
N_bonus = (1 - N_dist) / 4
X.append(__q(q_src, q_dst))
y.append(
N_bonus
+ 0.25
+ 0.5 * __shape_score(graph.nodes[idx].size, graph.nodes[r_idx].size)
)
# === CASE 3: other cluster, W
for _ in range(CONFIG.samples_per_tensor):
r_idx = __get_node(cluster_idx=None, type="W")
r_cluster_idx = graph.nodes[r_idx].cluster_idx
if r_cluster_idx == cluster_idx:
continue
if idx == r_idx:
continue
q_dst = list(P[r_cluster_idx]) + list(S[r_idx]) + list(N[r_idx]) + \
EG[f"{prefix}_{r_idx}"]["in-tree"].tolist()
N_bonus = 0
N_dist = np.linalg.norm(N[idx] - N[r_idx])
if N_dist <= 1:
N_bonus = (1 - N_dist) / 4
S_bonus = 0
S_dist = np.linalg.norm(S[idx] - S[r_idx])
if S_dist <= 1:
S_bonus = (1 - S_dist) / 4
X.append(__q(q_src, q_dst))
y.append(
N_bonus / 2
+ S_bonus / 2
+ 0.25 * __shape_score(graph.nodes[idx].size, graph.nodes[r_idx].size)
)
# === CASE 4: ?, F
# for _ in range(CONFIG.samples_per_tensor):
# r_idx = __get_node(cluster_idx=None, type="F")
# r_cluster_idx = graph.nodes[r_idx].cluster_idx
# if idx == r_idx:
# continue
# q_dst = list(P[r_cluster_idx]) + list(S[r_idx]) + list(N[r_idx])
# X.append(__q(q_src, q_dst))
# y.append(0)
print("DATASET", np.array(X).shape)#len(y))
return X, y
# _vec = list(x.replace(".weight", "").replace(".bias", ""))
# # print(_vec)
# _lvl = [s for s in _vec if s.isdigit()]
# _lvl = "".join(_lvl)
# _vec = list(set(_vec))
# if _lvl:
# _vec.append(_lvl)
def __encode(x):
x = x.replace(".weight", "").replace(".bias", "")
x = x.replace("blocks", "")
if "Backward" in x:
x = ""
# print(x)
_vec = list(x) # + [x]
# minl, maxl = 1, 2
# t = x
# _vec = [t[i:i+j] for i in range(len(t)-minl) for j in range(minl,maxl+1)]
# print(_vec)
_lvl = [s for s in _vec if s.isdigit()]
_lvl = "".join(_lvl)
_vec = list(set(_vec))
if _lvl:
_vec.append(_lvl)
# for i in range(2, len(_lvl)+1):
# _vec.append(_lvl[0:i])
return _vec
def score_autoencoder(graph_src, graph_dst):
# src_ids_to_layers_mapping = get_idx_to_layers_mapping(model_src,
# graph_src)
# dst_ids_to_layers_mapping = get_idx_to_layers_mapping(model_dst,
# graph_dst)
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.manifold import Isomap
mlb = MultiLabelBinarizer()
vec = []
# FIXME: mutual
for idx, node in graph_dst.nodes.items():
# if node.type != "W":
# continue
vec.append(__encode(node.name))
# for idx, node in graph_src.nodes.items():
# vec.append(__encode(node.name))
# vec.append(node.name.split("."))
mlb.fit(vec) # FIXME: 50
_l1 = len(graph_dst.nodes.keys())
_l2 = len(graph_dst.cluster_map.keys())
# print(_l2, _l1)
mfa = Isomap(n_components=min(_l1//2, 30), n_neighbors=min(_l1//10, 50), p=3) # 30 best
_vec = mlb.transform(vec)
mfa.fit(_vec)
P_src, S_src, N_src = F_architecture(graph_src, mlb=mlb, mfa=mfa)
P_dst, S_dst, N_dst = F_architecture(graph_dst, mlb=mlb, mfa=mfa)
from pprint import pprint
split_map = {}
for cluster_idx in graph_src.cluster_map.keys():
_split_map = split_cluster_level(graph_src, cluster_idx)
for key in _split_map:
split_map[f"src_{key}"] = _split_map[key]
print("(graph_src ended)")
for cluster_idx in graph_dst.cluster_map.keys():
_split_map = split_cluster_level(graph_dst, cluster_idx)
for key in _split_map:
split_map[f"dst_{key}"] = _split_map[key]
print("(graph_dst ended)")
EG = encode_graph(split_map)
# for key in EG:
# print("-------->", key, EG[key]["in-tree"].shape)
X1, y1 = gen_dataset(graph_src, P_src, S_src, N_src, EG, prefix="src")
X2, y2 = gen_dataset(graph_dst, P_dst, S_dst, N_dst, EG, prefix="dst")
X = X1 + X2
y = y1 + y2
print("DATASET FULL", np.array(X).shape)
# for x in range(len(X)):
# print(np.array(X[x]).shape)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=CONFIG.test_size, random_state=42
)
### AUTOENCODER ###
# https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.SelfTrainingClassifier.html#sklearn.semi_supervised.SelfTrainingClassifier
model = copy(CONFIG.autoencoder)
### model.fit(X1, y1)
### model.fit(X2, y2)
model.fit(X_train, y_train)
#########################
y_hat = model.predict(X_test)
loss = mean_squared_error(y_test, y_hat)
print(f" LOSS --> {loss}")
#################################################################
## FIXME: bipartie_matching between top-k #######################
## FIXME: match by clusters --> if best in cluster / eliminate ##
## FIXME: try connection? # FIXME: elimination? greedy {top 3} ##
#################################################################
### MATCHING ###
# FIXME: move to [fn_matcher, fn_scorer]
def __norm_weights(graph):
arr, imap, i = [], {}, 0
for _, (idx, node) in enumerate(graph.nodes.items()):
if node.type != "W":
continue
arr.append(idx)
imap[idx] = i
i += 1
return arr, imap
src_arr, src_map = __norm_weights(graph_src)
dst_arr, dst_map = __norm_weights(graph_dst)
n, m = len(src_arr), len(dst_arr)
scores = np.zeros((n, m))
# classes = [
# nn.Conv1d, nn.Conv2d, nn.Conv3d, nn.ConvTranspose1d, nn.ConvTranspose2d, nn.ConvTranspose3d,
# nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d,
# nn.Linear
# ]
for dst_j, idx_dst in enumerate(dst_arr):
node_dst = graph_dst.nodes[idx_dst]
dst_type = node_dst.name.split(".")[-1]
q_dst = (
list(P_dst[node_dst.cluster_idx])
+ list(S_dst[idx_dst])
+ list(N_dst[idx_dst])
+ list(EG[f"dst_{idx_dst}"]["in-tree"].tolist())
)
q_arr = []
for src_i, idx_src in enumerate(src_arr):
node_src = graph_src.nodes[idx_src]
src_type = node_src.name.split(".")[-1]
q_src = (
list(P_src[node_src.cluster_idx])
+ list(S_src[idx_src])
+ list(N_src[idx_src])
+ list(EG[f"src_{idx_src}"]["in-tree"].tolist())
)
q_arr.append(__q(q_src, q_dst))
scores[src_i, dst_j] = __shape_score(node_dst.size, node_src.size)
# src_layer = src_ids_to_layers_mapping[idx_src]
# dst_layer = dst_ids_to_layers_mapping[idx_dst]
# not_same_class = True
# for classname in classes:
# if isinstance(src_layer, classname) and \
# isinstance(dst_layer, classname):
# not_same_class = False
# break
if dst_type != src_type: # or not_same_class:
scores[src_i, dst_j] = 0
y_hat = model.predict(q_arr)
scores[:, dst_j] *= y_hat
return scores, src_arr, dst_arr
def transfer(model_src, model_dst=None, teacher=None, inject=True, debug=False):
# FIXME: replace str to model if needed
if model_src and model_dst:
# API: v2
print("API: V2")
pass
elif not model_dst and teacher:
# API: v1
print("API: V1")
model_src, model_dst = teacher, model_src
else:
raise Exception("where is teacher?! is this a joke?")
graph_src = get_graph(model_src)
graph_dst = get_graph(model_dst)
if debug:
show_graph(graph_src, ver=3, path="__tli_src")
show_graph(graph_dst, ver=3, path="__tli_dst")
scores, src_arr, dst_arr = score_autoencoder(graph_src, graph_dst)
remap = {}
n, m = len(src_arr), len(dst_arr)
##############################################
# for size in np.arange(0.10, 0.50, 0.10):
# window_size = size
# for _dst_j, idx_dst in enumerate(dst_arr[::-1]):
# dst_j = m - _dst_j - 1
# ith = dst_j / m
# shift = max(int(ith*n - window_size*n), 0)
# i = np.argmax(scores[shift:shift+int(window_size*n), dst_j])+shift
# if idx_dst not in remap and scores[i, dst_j] > 1 - size:
# remap[idx_dst] = src_arr[i]
beta = 0.5
smap = copy(scores)
for _ in range(n*m):