-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrategy.py
1097 lines (908 loc) · 50.5 KB
/
strategy.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
# TODO fix loading net, sys.append problem
from abc import ABC, abstractmethod
import torch
import tensorflow as tf
import sys
import os
import time
import numpy as np
from torch.utils.data import DataLoader
from torch.utils.data import WeightedRandomSampler
from umap.umap_ import find_ab_params
from singleVis.custom_weighted_random_sampler import CustomWeightedRandomSampler
from singleVis.SingleVisualizationModel import VisModel, tfModel
from singleVis.losses import HybridLoss, SmoothnessLoss, UmapLoss, ReconstructionLoss, TemporalLoss, DVILoss, SingleVisLoss, umap_loss, reconstruction_loss, regularize_loss
from singleVis.edge_dataset import HybridDataHandler, DVIDataHandler, DataHandler, construct_edge_dataset
from singleVis.trainer import HybridVisTrainer, DVITrainer, SingleVisTrainer
from singleVis.data import DataProviderAbstractClass, NormalDataProvider, ActiveLearningDataProvider, DenseActiveLearningDataProvider
from singleVis.spatial_edge_constructor import kcHybridSpatialEdgeConstructor, SingleEpochSpatialEdgeConstructor, kcSpatialEdgeConstructor, tfEdgeConstructor
from singleVis.temporal_edge_constructor import GlobalTemporalEdgeConstructor
from singleVis.projector import DeepDebuggerProjector, DVIProjector, ProjectorAbstractClass, TimeVisProjector, ALProjector, tfDVIProjector, tfDVIDenseALProjector, TimeVisDenseALProjector
from singleVis.segmenter import Segmenter
from singleVis.eval.evaluator import Evaluator, ALEvaluator, EvaluatorAbstractClass, DenseALEvaluator
from singleVis.visualizer import VisualizerAbstractClass, visualizer, DenseALvisualizer
from singleVis.utils import find_neighbor_preserving_rate
class StrategyAbstractClass(ABC):
def __init__(self, CONTENT_PATH, config):
self._config = config
self.CONTENT_PATH = CONTENT_PATH
@property
def config(self)->dict:
return self._config
@property
def projector(self)->ProjectorAbstractClass:
return self._projector
@projector.setter
def projector(self, projector:ProjectorAbstractClass)->None:
self._projector = projector
@property
def data_provider(self)->DataProviderAbstractClass:
return self._data_provider
@data_provider.setter
def data_provider(self, data_provider:DataProviderAbstractClass)->None:
self._data_provider = data_provider
@property
def evaluator(self)->EvaluatorAbstractClass:
return self._evaluator
@evaluator.setter
def evaluator(self, evaluator:EvaluatorAbstractClass)->None:
self._evaluator = evaluator
@property
def vis(self)->VisualizerAbstractClass:
return self._vis
@vis.setter
def vis(self, visualizer:VisualizerAbstractClass)->None:
self._vis = visualizer
@abstractmethod
def _init(self):
pass
@abstractmethod
def _preprocess(self):
pass
@abstractmethod
def _train(self):
pass
@abstractmethod
def _evaluate(self):
pass
@abstractmethod
def _visualize(self):
pass
def visualize_embedding(self):
self._init()
self._preprocess()
self._train()
self._evaluate()
self._visualize()
class DeepVisualInsight(StrategyAbstractClass):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self._init()
self.VIS_METHOD = "DVI"
def _init(self):
sys.path.append(self.CONTENT_PATH)
# # record output information
# now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# sys.stdout = open(os.path.join(self.CONTENT_PATH, now+".txt"), "w")
CLASSES = self.config["CLASSES"]
GPU_ID = self.config["GPU"]
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
EPOCH_NAME = self.config["EPOCH_NAME"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
NET = TRAINING_PARAMETER["NET"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
ENCODER_DIMS = VISUALIZATION_PARAMETER["ENCODER_DIMS"]
DECODER_DIMS = VISUALIZATION_PARAMETER["DECODER_DIMS"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
# define hyperparameters
self.DEVICE = torch.device("cuda:{}".format(GPU_ID) if torch.cuda.is_available() else "cpu")
import Model.model as subject_model
net = eval("subject_model.{}()".format(NET))
self._data_provider = NormalDataProvider(self.CONTENT_PATH, net, EPOCH_START, EPOCH_END, EPOCH_PERIOD, device=self.DEVICE, classes=CLASSES, epoch_name=EPOCH_NAME, verbose=1)
self.model = VisModel(ENCODER_DIMS, DECODER_DIMS)
negative_sample_rate = 5
min_dist = .1
_a, _b = find_ab_params(1.0, min_dist)
umap_loss_fn = UmapLoss(negative_sample_rate, self.DEVICE, _a, _b, repulsion_strength=1.0)
recon_loss_fn = ReconstructionLoss(beta=1.0)
temporal_loss_fn = TemporalLoss()
self.umap_fn = umap_loss_fn
self.recon_fn = recon_loss_fn
self.temporal_fn = temporal_loss_fn
self.projector(DVIProjector(vis_model=self.model, content_path=self.CONTENT_PATH, vis_model_name=VIS_MODEL_NAME, device=self.DEVICE))
self.vis(visualizer(self.data_provider, self.projector, 200, "tab10"))
self.evaluator(Evaluator(self.data_provider, self.projector))
def _preprocess(self):
PREPROCESS = self.config["VISUALIZATION"]["PREPROCESS"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
LEN = TRAINING_PARAMETER["train_num"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
L_BOUND = VISUALIZATION_PARAMETER["BOUNDARY"]["L_BOUND"]
if PREPROCESS:
self.data_provider._meta_data()
if B_N_EPOCHS >0:
self.data_provider._estimate_boundary(LEN//10, l_bound=L_BOUND)
def _train(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
LAMBDA1 = VISUALIZATION_PARAMETER["LAMBDA1"]
LAMBDA2 = VISUALIZATION_PARAMETER["LAMBDA2"]
ENCODER_DIMS = VISUALIZATION_PARAMETER["ENCODER_DIMS"]
DECODER_DIMS = VISUALIZATION_PARAMETER["DECODER_DIMS"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
S_N_EPOCHS = VISUALIZATION_PARAMETER["S_N_EPOCHS"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
PATIENT = VISUALIZATION_PARAMETER["PATIENT"]
MAX_EPOCH = VISUALIZATION_PARAMETER["MAX_EPOCH"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
start_flag = 1
prev_model = VisModel(ENCODER_DIMS, DECODER_DIMS)
prev_model.load_state_dict(self.model.state_dict())
for param in prev_model.parameters():
param.requires_grad = False
w_prev = dict(self.model.named_parameters())
for iteration in range(EPOCH_START, EPOCH_END+EPOCH_PERIOD, EPOCH_PERIOD):
# Define DVI Loss
if start_flag:
criterion = DVILoss(self.umap_fn, self.recon_fn, self.temporal_fn, lambd1=LAMBDA1, lambd2=0.0)
start_flag = 0
else:
# TODO AL mode, redefine train_representation
prev_data = self.data_provider.train_representation(iteration-EPOCH_PERIOD)
curr_data = self.data_provider.train_representation(iteration)
npr = find_neighbor_preserving_rate(prev_data, curr_data, N_NEIGHBORS)
criterion = DVILoss(self.umap_fn, self.recon_fn, self.temporal_fn, lambd1=LAMBDA1, lambd2=LAMBDA2*npr)
# Define training parameters
optimizer = torch.optim.Adam(self.model.parameters(), lr=.01, weight_decay=1e-5)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=4, gamma=.1)
# Define Edge dataset
t0 = time.time()
spatial_cons = SingleEpochSpatialEdgeConstructor(self.data_provider, iteration, S_N_EPOCHS, B_N_EPOCHS, N_NEIGHBORS)
edge_to, edge_from, probs, feature_vectors, attention = spatial_cons.construct()
t1 = time.time()
probs = probs / (probs.max()+1e-3)
eliminate_zeros = probs>1e-3
edge_to = edge_to[eliminate_zeros]
edge_from = edge_from[eliminate_zeros]
probs = probs[eliminate_zeros]
dataset = DVIDataHandler(edge_to, edge_from, feature_vectors, attention, w_prev)
n_samples = int(np.sum(S_N_EPOCHS * probs) // 1)
# chose sampler based on the number of dataset
if len(edge_to) > 2^24:
sampler = CustomWeightedRandomSampler(probs, n_samples, replacement=True)
else:
sampler = WeightedRandomSampler(probs, n_samples, replacement=True)
edge_loader = DataLoader(dataset, batch_size=1000, sampler=sampler)
########################################################################################################################
# TRAIN #
########################################################################################################################
trainer = DVITrainer(self.model, criterion, optimizer, lr_scheduler,edge_loader=edge_loader, DEVICE=self.DEVICE)
t2=time.time()
trainer.train(PATIENT, MAX_EPOCH)
t3 = time.time()
# save result
save_dir = self.data_provider.model_path
trainer.record_time(save_dir, "time_{}.json".format(VIS_MODEL_NAME), "complex_construction", str(iteration), t1-t0)
trainer.record_time(save_dir, "time_{}.json".format(VIS_MODEL_NAME), "training", str(iteration), t3-t2)
save_dir = os.path.join(self.data_provider.model_path, "Epoch_{}".format(iteration))
trainer.save(save_dir=save_dir, file_name="{}".format(VIS_MODEL_NAME))
prev_model.load_state_dict(self.model.state_dict())
for param in prev_model.parameters():
param.requires_grad = False
w_prev = dict(prev_model.named_parameters())
def _visualize(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
save_dir = os.path.join(self.data_provider.content_path, "img")
if not os.path.exists(save_dir):
os.mkdir(save_dir)
for i in range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD):
self.vis.savefig(i, path=os.path.join(save_dir, "{}_{}.png".format(self.VIS_METHOD, i)))
def _evaluate(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
EVALUATION_NAME = VISUALIZATION_PARAMETER["EVALUATION_NAME"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
eval_epochs = list(range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD))
for eval_epoch in eval_epochs:
self.evaluator.save_epoch_eval(eval_epoch, N_NEIGHBORS, temporal_k=5, file_name="{}".format(EVALUATION_NAME))
def visualize_embedding(self):
self._preprocess()
self._train()
self._visualize()
self._evaluate()
class tfDeepVisualInsight(StrategyAbstractClass):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self._init()
self.VIS_METHOD = "tfDVI"
def _init(self):
sys.path.append(self.CONTENT_PATH)
CLASSES = self.config["CLASSES"]
GPU_ID = self.config["GPU"]
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
EPOCH_NAME = self.config["EPOCH_NAME"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
NET = TRAINING_PARAMETER["NET"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
# define hyperparameters
self.DEVICE = torch.device("cuda:{}".format(GPU_ID) if torch.cuda.is_available() else "cpu")
import Model.model as subject_model
net = eval("subject_model.{}()".format(NET))
self.data_provider = NormalDataProvider(self.CONTENT_PATH, net, EPOCH_START, EPOCH_END, EPOCH_PERIOD, device=self.DEVICE, classes=CLASSES, epoch_name=EPOCH_NAME, verbose=1)
# Define Projector
self.flag = "_temporal_id{}".format("_withoutB" if B_N_EPOCHS==0 else "")
self.projector = tfDVIProjector(self.CONTENT_PATH, flag=self.flag)
self.vis = visualizer(self.data_provider, self.projector, 200, "tab10")
self.evaluator = Evaluator(self.data_provider, self.projector)
def _preprocess(self):
PREPROCESS = self.config["VISUALIZATION"]["PREPROCESS"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
LEN = TRAINING_PARAMETER["train_num"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
L_BOUND = VISUALIZATION_PARAMETER["BOUNDARY"]["L_BOUND"]
if PREPROCESS:
self.data_provider._meta_data()
if B_N_EPOCHS >0:
self.data_provider._estimate_boundary(LEN//10, l_bound=L_BOUND)
def _train(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
LAMBDA1 = VISUALIZATION_PARAMETER["LAMBDA1"]
LAMBDA2 = VISUALIZATION_PARAMETER["LAMBDA2"]
ENCODER_DIMS = VISUALIZATION_PARAMETER["ENCODER_DIMS"]
DECODER_DIMS = VISUALIZATION_PARAMETER["DECODER_DIMS"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
S_N_EPOCHS = VISUALIZATION_PARAMETER["S_N_EPOCHS"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
PATIENT = VISUALIZATION_PARAMETER["PATIENT"]
MAX_EPOCH = VISUALIZATION_PARAMETER["MAX_EPOCH"]
BATCH_SIZE = VISUALIZATION_PARAMETER["BATCH_SIZE"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
# Define Losses
losses = {}
loss_weights = {}
negative_sample_rate = 5
min_dist = .1
_a, _b = find_ab_params(1.0, min_dist)
# umap loss
umap_loss_fn = umap_loss(
BATCH_SIZE,
negative_sample_rate,
_a,
_b,
)
losses["umap"] = umap_loss_fn
loss_weights["umap"] = 1.0
recon_loss_fn = reconstruction_loss(beta=1)
losses["reconstruction"] = recon_loss_fn
loss_weights["reconstruction"] = LAMBDA1
regularize_loss_fn = regularize_loss()
losses["regularization"] = regularize_loss_fn
loss_weights["regularization"] = LAMBDA2 # TODO: change this weight
# define training
optimizer = tf.keras.optimizers.Adam()
# Define visualization models
weights_dict = {}
self.model = tfModel(optimizer=optimizer, encoder_dims=ENCODER_DIMS, decoder_dims=DECODER_DIMS, loss=losses, loss_weights=loss_weights, batch_size=BATCH_SIZE, prev_trainable_variables=None)
callbacks = [
tf.keras.callbacks.EarlyStopping(
monitor='loss',
min_delta=10 ** -2,
patience=8,
verbose=1,
),
tf.keras.callbacks.LearningRateScheduler(lambda epoch: 1e-3 if epoch < 8 else 1e-4),
tf.keras.callbacks.LambdaCallback(on_train_end=lambda logs: weights_dict.update(
{'prev': [tf.identity(tf.stop_gradient(x)) for x in self.model.trainable_weights]})),
]
# edge constructor
spatial_cons = tfEdgeConstructor(self.data_provider, S_N_EPOCHS, B_N_EPOCHS, N_NEIGHBORS)
for iteration in range(EPOCH_START, EPOCH_END+EPOCH_PERIOD, EPOCH_PERIOD):
self.model.compile(
optimizer=optimizer, loss=losses, loss_weights=loss_weights,
)
t0 = time.time()
edge_to, edge_from, probs, feature_vectors, attention, n_rate = spatial_cons.construct(iteration-EPOCH_PERIOD, iteration)
t1 = time.time()
edge_dataset = construct_edge_dataset(edge_to, edge_from, probs, feature_vectors, attention, n_rate, BATCH_SIZE)
steps_per_epoch = int(
len(edge_to) / BATCH_SIZE / 10
)
t2 = time.time()
# create embedding
self.model.fit(
edge_dataset,
epochs=200, # a large value, because we have early stop callback
steps_per_epoch=steps_per_epoch,
callbacks=callbacks,
max_queue_size=100,
)
t3 = time.time()
# save for later use
self.model.prev_trainable_variables = weights_dict["prev"]
# save
self.model.encoder.save(os.path.join(CONTENT_PATH, "Model", "Epoch_{:d}".format(iteration), "encoder" + self.flag))
self.model.decoder.save(os.path.join(CONTENT_PATH, "Model", "Epoch_{:d}".format(iteration), "decoder" + self.flag))
print("save visualized model for Epoch {:d}".format(iteration))
# save time result
# TODO
# save_dir = self.data_provider.model_path
# trainer.record_time(save_dir, "time_{}.json".format(VIS_MODEL_NAME), "complex_construction", str(iteration), t1-t0)
# trainer.record_time(save_dir, "time_{}.json".format(VIS_MODEL_NAME), "training", str(iteration), t3-t2)
def _visualize(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
save_dir = os.path.join(self.data_provider.content_path, "img")
if not os.path.exists(save_dir):
os.mkdir(save_dir)
for i in range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD):
self.vis.savefig(i, path=os.path.join(save_dir, "{}_{}.png".format(self.VIS_METHOD, i)))
def _evaluate(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
EVALUATION_NAME = VISUALIZATION_PARAMETER["EVALUATION_NAME"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
eval_epochs = list(range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD))
for eval_epoch in eval_epochs:
self.evaluator.save_epoch_eval(eval_epoch, N_NEIGHBORS, temporal_k=5, file_name="{}".format(EVALUATION_NAME))
def visualize_embedding(self):
self._preprocess()
self._train()
self._visualize()
self._evaluate()
class TimeVis(StrategyAbstractClass):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self._init()
self.VIS_METHOD = "TimeVis"
def _init(self):
sys.path.append(self.CONTENT_PATH)
# record output information
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
sys.stdout = open(os.path.join(self.CONTENT_PATH, now+".txt"), "w")
CLASSES = self.config["CLASSES"]
GPU_ID = self.config["GPU"]
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
EPOCH_NAME = self.config["EPOCH_NAME"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
NET = TRAINING_PARAMETER["NET"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
LAMBDA = VISUALIZATION_PARAMETER["LAMBDA"]
ENCODER_DIMS = VISUALIZATION_PARAMETER["ENCODER_DIMS"]
DECODER_DIMS = VISUALIZATION_PARAMETER["DECODER_DIMS"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
# define hyperparameters
self.DEVICE = torch.device("cuda:{}".format(GPU_ID) if torch.cuda.is_available() else "cpu")
import Model.model as subject_model
net = eval("subject_model.{}()".format(NET))
self.data_provider = NormalDataProvider(self.CONTENT_PATH, net, EPOCH_START, EPOCH_END, EPOCH_PERIOD, device=self.DEVICE, classes=CLASSES, epoch_name=EPOCH_NAME, verbose=1)
self.model = VisModel(ENCODER_DIMS, DECODER_DIMS)
negative_sample_rate = 5
min_dist = .1
_a, _b = find_ab_params(1.0, min_dist)
umap_loss_fn = UmapLoss(negative_sample_rate, self.DEVICE, _a, _b, repulsion_strength=1.0)
recon_loss_fn = ReconstructionLoss(beta=1.0)
self.criterion = SingleVisLoss(umap_loss_fn, recon_loss_fn, lambd=LAMBDA)
self.projector = TimeVisProjector(vis_model=self.model, content_path=self.CONTENT_PATH, vis_model_name=VIS_MODEL_NAME, device=self.DEVICE)
self.vis = visualizer(self.data_provider, self.projector, 200, "tab10")
self.evaluator = Evaluator(self.data_provider, self.projector)
def _preprocess(self):
PREPROCESS = self.config["VISUALIZATION"]["PREPROCESS"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
LEN = TRAINING_PARAMETER["train_num"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
L_BOUND = VISUALIZATION_PARAMETER["BOUNDARY"]["L_BOUND"]
if PREPROCESS:
self.data_provider._meta_data()
if B_N_EPOCHS >0:
self.data_provider._estimate_boundary(LEN//10, l_bound=L_BOUND)
def _train(self):
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
S_N_EPOCHS = VISUALIZATION_PARAMETER["S_N_EPOCHS"]
T_N_EPOCHS = VISUALIZATION_PARAMETER["T_N_EPOCHS"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
PATIENT = VISUALIZATION_PARAMETER["PATIENT"]
MAX_EPOCH = VISUALIZATION_PARAMETER["MAX_EPOCH"]
INIT_NUM = VISUALIZATION_PARAMETER["INIT_NUM"]
ALPHA = VISUALIZATION_PARAMETER["ALPHA"]
BETA = VISUALIZATION_PARAMETER["BETA"]
MAX_HAUSDORFF = VISUALIZATION_PARAMETER["MAX_HAUSDORFF"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
optimizer = torch.optim.Adam(self.model.parameters(), lr=.01, weight_decay=1e-5)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=4, gamma=.1)
t0 = time.time()
spatial_cons = kcSpatialEdgeConstructor(data_provider=self.data_provider, init_num=INIT_NUM, s_n_epochs=S_N_EPOCHS, b_n_epochs=B_N_EPOCHS, n_neighbors=N_NEIGHBORS, MAX_HAUSDORFF=MAX_HAUSDORFF, ALPHA=ALPHA, BETA=BETA)
s_edge_to, s_edge_from, s_probs, feature_vectors, time_step_nums, time_step_idxs_list, knn_indices, sigmas, rhos, attention = spatial_cons.construct()
temporal_cons = GlobalTemporalEdgeConstructor(X=feature_vectors, time_step_nums=time_step_nums, sigmas=sigmas, rhos=rhos, n_neighbors=N_NEIGHBORS, n_epochs=T_N_EPOCHS)
t_edge_to, t_edge_from, t_probs = temporal_cons.construct()
t1 = time.time()
edge_to = np.concatenate((s_edge_to, t_edge_to),axis=0)
edge_from = np.concatenate((s_edge_from, t_edge_from), axis=0)
probs = np.concatenate((s_probs, t_probs), axis=0)
probs = probs / (probs.max()+1e-3)
eliminate_zeros = probs>1e-3
edge_to = edge_to[eliminate_zeros]
edge_from = edge_from[eliminate_zeros]
probs = probs[eliminate_zeros]
dataset = DataHandler(edge_to, edge_from, feature_vectors, attention)
n_samples = int(np.sum(S_N_EPOCHS * probs) // 1)
# chose sampler based on the number of dataset
if len(edge_to) > 2^24:
sampler = CustomWeightedRandomSampler(probs, n_samples, replacement=True)
else:
sampler = WeightedRandomSampler(probs, n_samples, replacement=True)
edge_loader = DataLoader(dataset, batch_size=1000, sampler=sampler)
########################################################################################################################
# TRAIN #
########################################################################################################################
trainer = SingleVisTrainer(self.model, self.criterion, optimizer, lr_scheduler, edge_loader=edge_loader, DEVICE=self.DEVICE)
t2=time.time()
trainer.train(PATIENT, MAX_EPOCH)
t3 = time.time()
save_dir = self.data_provider.model_path
trainer.record_time(save_dir, "time_{}.json".format(VIS_MODEL_NAME), "complex_construction", t1-t0)
trainer.record_time(save_dir, "time_{}.json".format(VIS_MODEL_NAME), "training", t3-t2)
trainer.save(save_dir=save_dir, file_name="{}".format(VIS_MODEL_NAME))
def _visualize(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
self._vis = visualizer(self.data_provider, self.projector, 200, "plasma")
save_dir = os.path.join(self.data_provider.content_path, "img")
if not os.path.exists(save_dir):
os.mkdir(save_dir)
for i in range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD):
self.vis.savefig(i, path=os.path.join(save_dir, "{}_{}.png".format(self.VIS_METHOD, i)))
def _evaluate(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
EVALUATION_NAME = VISUALIZATION_PARAMETER["EVALUATION_NAME"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
eval_epochs = list(range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD))
self.evaluator = Evaluator(self.data_provider, self.projector)
for eval_epoch in eval_epochs:
self.evaluator.save_epoch_eval(eval_epoch, N_NEIGHBORS, temporal_k=5, file_name="{}".format(EVALUATION_NAME))
def visualize_embedding(self):
self._preprocess()
self._train()
self._visualize()
self._evaluate()
class DeepDebugger(StrategyAbstractClass):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self._init()
self.VIS_METHOD = "DeepDebugger"
def _init(self):
sys.path.append(self.CONTENT_PATH)
# record output information
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
sys.stdout = open(os.path.join(self.CONTENT_PATH, now+".txt"), "w")
CLASSES = self.config["CLASSES"]
GPU_ID = self.config["GPU"]
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
EPOCH_NAME = self.config["EPOCH_NAME"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
NET = TRAINING_PARAMETER["NET"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
LAMBDA = VISUALIZATION_PARAMETER["LAMBDA"]
S_LAMBDA = VISUALIZATION_PARAMETER["S_LAMBDA"]
ENCODER_DIMS = VISUALIZATION_PARAMETER["ENCODER_DIMS"]
DECODER_DIMS = VISUALIZATION_PARAMETER["DECODER_DIMS"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
# define hyperparameters
self.DEVICE = torch.device("cuda:{}".format(GPU_ID) if torch.cuda.is_available() else "cpu")
import Model.model as subject_model
net = eval("subject_model.{}()".format(NET))
self.data_provider = NormalDataProvider(self.CONTENT_PATH, net, EPOCH_START, EPOCH_END, EPOCH_PERIOD, device=self.DEVICE, classes=CLASSES, epoch_name=EPOCH_NAME, verbose=1)
self.model = VisModel(ENCODER_DIMS, DECODER_DIMS)
negative_sample_rate = 5
min_dist = .1
_a, _b = find_ab_params(1.0, min_dist)
umap_loss_fn = UmapLoss(negative_sample_rate, self.DEVICE, _a, _b, repulsion_strength=1.0)
recon_loss_fn = ReconstructionLoss(beta=1.0)
smooth_loss_fn = SmoothnessLoss(margin=0.5)
self.criterion = HybridLoss(umap_loss_fn, recon_loss_fn, smooth_loss_fn, lambd1=LAMBDA, lambd2=S_LAMBDA)
self.segmenter = Segmenter(data_provider=self.data_provider, threshold=78.5, range_s=EPOCH_START, range_e=EPOCH_END, range_p=EPOCH_PERIOD)
self.projector = DeepDebuggerProjector(vis_model=self.model, content_path=self.CONTENT_PATH,vis_model_name=VIS_MODEL_NAME, segments=None, device=self.DEVICE)
self.vis = visualizer(self.data_provider, self.projector, 200, "tab10")
self.evaluator = Evaluator(self.data_provider, self.projector)
def _preprocess(self):
PREPROCESS = self.config["VISUALIZATION"]["PREPROCESS"]
# Training parameter (subject model)
TRAINING_PARAMETER = self.config["TRAINING"]
LEN = TRAINING_PARAMETER["train_num"]
# Training parameter (visualization model)
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
L_BOUND = VISUALIZATION_PARAMETER["BOUNDARY"]["L_BOUND"]
if PREPROCESS:
self.data_provider._meta_data()
if B_N_EPOCHS >0:
self.data_provider._estimate_boundary(LEN//10, l_bound=L_BOUND)
def _segment(self):
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
t0 = time.time()
SEGMENTS = self.segmenter.segment()
t1 = time.time()
self.projector.segments = SEGMENTS
self.segmenter.record_time(self.data_provider.model_path, "time_{}.json".format(VIS_MODEL_NAME), t1-t0)
print("Segmentation takes {:.1f} seconds.".format(round(t1-t0, 3)))
def _train(self):
TRAINING_PARAMETER = self.config["TRAINING"]
LEN = TRAINING_PARAMETER["train_num"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
SEGMENTS = self.segmenter.segments
B_N_EPOCHS = VISUALIZATION_PARAMETER["BOUNDARY"]["B_N_EPOCHS"]
INIT_NUM = VISUALIZATION_PARAMETER["INIT_NUM"]
ALPHA = VISUALIZATION_PARAMETER["ALPHA"]
BETA = VISUALIZATION_PARAMETER["BETA"]
MAX_HAUSDORFF = VISUALIZATION_PARAMETER["MAX_HAUSDORFF"]
S_N_EPOCHS = VISUALIZATION_PARAMETER["S_N_EPOCHS"]
T_N_EPOCHS = VISUALIZATION_PARAMETER["T_N_EPOCHS"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
PATIENT = VISUALIZATION_PARAMETER["PATIENT"]
MAX_EPOCH = VISUALIZATION_PARAMETER["MAX_EPOCH"]
VIS_MODEL_NAME = VISUALIZATION_PARAMETER["VIS_MODEL_NAME"]
prev_selected = np.random.choice(np.arange(LEN), size=INIT_NUM, replace=False)
prev_embedding = None
start_point = len(SEGMENTS)-1
c0=None
d0=None
for seg in range(start_point,-1,-1):
epoch_start, epoch_end = SEGMENTS[seg]
self.data_provider.update_interval(epoch_s=epoch_start, epoch_e=epoch_end)
optimizer = torch.optim.Adam(self.model.parameters(), lr=.01, weight_decay=1e-5)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=4, gamma=.1)
t0 = time.time()
spatial_cons = kcHybridSpatialEdgeConstructor(data_provider=self.data_provider, init_num=INIT_NUM, s_n_epochs=S_N_EPOCHS, b_n_epochs=B_N_EPOCHS, n_neighbors=N_NEIGHBORS, MAX_HAUSDORFF=MAX_HAUSDORFF, ALPHA=ALPHA, BETA=BETA, init_idxs=prev_selected, init_embeddings=prev_embedding, c0=c0, d0=d0)
s_edge_to, s_edge_from, s_probs, feature_vectors, embedded, coefficient, time_step_nums, time_step_idxs_list, knn_indices, sigmas, rhos, attention, (c0,d0) = spatial_cons.construct()
temporal_cons = GlobalTemporalEdgeConstructor(X=feature_vectors, time_step_nums=time_step_nums, sigmas=sigmas, rhos=rhos, n_neighbors=N_NEIGHBORS, n_epochs=T_N_EPOCHS)
t_edge_to, t_edge_from, t_probs = temporal_cons.construct()
t1 = time.time()
edge_to = np.concatenate((s_edge_to, t_edge_to),axis=0)
edge_from = np.concatenate((s_edge_from, t_edge_from), axis=0)
probs = np.concatenate((s_probs, t_probs), axis=0)
probs = probs / (probs.max()+1e-3)
eliminate_zeros = probs>1e-3
edge_to = edge_to[eliminate_zeros]
edge_from = edge_from[eliminate_zeros]
probs = probs[eliminate_zeros]
dataset = HybridDataHandler(edge_to, edge_from, feature_vectors, attention, embedded, coefficient)
n_samples = int(np.sum(S_N_EPOCHS * probs) // 1)
# chose sampler based on the number of dataset
if len(edge_to) > 2^24:
sampler = CustomWeightedRandomSampler(probs, n_samples, replacement=True)
else:
sampler = WeightedRandomSampler(probs, n_samples, replacement=True)
edge_loader = DataLoader(dataset, batch_size=1000, sampler=sampler)
########################################################################################################################
# TRAIN #
########################################################################################################################
trainer = HybridVisTrainer(self.model, self.criterion, optimizer, lr_scheduler, edge_loader=edge_loader, DEVICE=self.DEVICE)
t2=time.time()
trainer.train(PATIENT, MAX_EPOCH)
t3 = time.time()
file_name = "time_{}".format(VIS_MODEL_NAME)
trainer.record_time(self.data_provider.model_path, file_name, "complex_construction", seg, t1-t0)
trainer.record_time(self.data_provider.model_path, file_name, "training", seg, t3-t2)
trainer.save(save_dir=self.data_provider.model_path, file_name="{}_{}".format(VIS_MODEL_NAME, seg))
self.model = trainer.model
# update prev_idxs and prev_embedding
prev_selected = time_step_idxs_list[0]
prev_data = torch.from_numpy(feature_vectors[:len(prev_selected)]).to(dtype=torch.float32, device=self.DEVICE)
self.model = self.model.to(device=self.DEVICE)
prev_embedding = self.model.encoder(prev_data).cpu().detach().numpy()
def _evaluate(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
VISUALIZATION_PARAMETER = self.config["VISUALIZATION"]
EVALUATION_NAME = VISUALIZATION_PARAMETER["EVALUATION_NAME"]
N_NEIGHBORS = VISUALIZATION_PARAMETER["N_NEIGHBORS"]
eval_epochs = list(range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD))
for eval_epoch in eval_epochs:
self.evaluator.save_epoch_eval(eval_epoch, N_NEIGHBORS, temporal_k=5, file_name="{}".format(EVALUATION_NAME))
def _visualize(self):
EPOCH_START = self.config["EPOCH_START"]
EPOCH_END = self.config["EPOCH_END"]
EPOCH_PERIOD = self.config["EPOCH_PERIOD"]
save_dir = os.path.join(self.data_provider.content_path, "img")
os.makedirs(save_dir, exist_ok=True)
for i in range(EPOCH_START, EPOCH_END+1, EPOCH_PERIOD):
self.vis.savefig(i, path=os.path.join(save_dir, "{}_{}.png".format(self.VIS_METHOD, i)))
def visualize_embedding(self):
self._preprocess()
self._segment()
self._train()
self._visualize()
self._evaluate()
class DVIAL(StrategyAbstractClass):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
resume_iter = config["BASE_ITERATION"]
self.VIS_METHOD = "DVIAL"
self._init(resume_iteration=resume_iter)
def _init(self, resume_iteration=-1):
sys.path.append(self.CONTENT_PATH)
# record output information
# now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# sys.stdout = open(os.path.join(self.CONTENT_PATH, now+".txt"), "w")
CLASSES = self.config["CLASSES"]
BASE_ITERATION = self.config["BASE_ITERATION"]
GPU_ID = self.config["GPU"]
self.DEVICE = torch.device("cuda:{}".format(GPU_ID) if torch.cuda.is_available() else "cpu")
################################################# VISUALIZATION PARAMETERS ########################################
ENCODER_DIMS = self.config["VISUALIZATION"]["ENCODER_DIMS"]
DECODER_DIMS = self.config["VISUALIZATION"]["DECODER_DIMS"]
VIS_MODEL_NAME = self.config["VISUALIZATION"]["VIS_MODEL_NAME"]
############################################ ACTIVE LEARNING MODEL PARAMETERS ######################################
TRAINING_PARAMETERS = self.config["TRAINING"]
NET = TRAINING_PARAMETERS["NET"]
import Model.model as subject_model
net = eval("subject_model.{}()".format(NET))
self.data_provider = ActiveLearningDataProvider(self.CONTENT_PATH, net, BASE_ITERATION, device=self.DEVICE, classes=CLASSES, iteration_name="Iteration", verbose=1)
self.model = VisModel(ENCODER_DIMS, DECODER_DIMS)
self.projector = ALProjector(vis_model=self.model, content_path=self.CONTENT_PATH, vis_model_name=VIS_MODEL_NAME, device=self.DEVICE)
if resume_iteration > 0:
self.projector.load(resume_iteration)
self.evaluator = ALEvaluator(self.data_provider, self.projector)
self.vis = visualizer(self.data_provider, self.projector, 200)
def _preprocess(self, iteration):
PREPROCESS = self.config["VISUALIZATION"]["PREPROCESS"]
B_N_EPOCHS = self.config["VISUALIZATION"]["BOUNDARY"]["B_N_EPOCHS"]
L_BOUND = self.config["VISUALIZATION"]["BOUNDARY"]["L_BOUND"]
if PREPROCESS:
self.data_provider._meta_data(iteration)
LEN = len(self.data_provider.train_labels(iteration))
if B_N_EPOCHS >0:
self.data_provider._estimate_boundary(iteration, LEN//10, l_bound=L_BOUND)
def _train(self, iteration):
S_N_EPOCHS = self.config["VISUALIZATION"]["S_N_EPOCHS"]
LAMBDA = self.config["VISUALIZATION"]["LAMBDA"]
MAX_EPOCH = self.config["VISUALIZATION"]["MAX_EPOCH"]
PATIENT = self.config["VISUALIZATION"]["PATIENT"]
VIS_MODEL_NAME = self.config["VISUALIZATION"]["VIS_MODEL_NAME"]
t0 = time.time()
spatial_cons = SingleEpochSpatialEdgeConstructor(self.data_provider, iteration, 5, 0, 15)
edge_to, edge_from, probs, feature_vectors, attention = spatial_cons.construct()
t1 = time.time()
probs = probs / (probs.max()+1e-3)
eliminate_zeros = probs>1e-3
edge_to = edge_to[eliminate_zeros]
edge_from = edge_from[eliminate_zeros]
probs = probs[eliminate_zeros]
spatial_cons.record_time(self.data_provider.model_path, "time_{}".format(VIS_MODEL_NAME), "complex_construction", t1-t0)
dataset = DataHandler(edge_to, edge_from, feature_vectors, attention)
n_samples = int(np.sum(S_N_EPOCHS * probs) // 1)
# chosse sampler based on the number of dataset
if len(edge_to) > 2^24:
sampler = CustomWeightedRandomSampler(probs, n_samples, replacement=True)
else:
sampler = WeightedRandomSampler(probs, n_samples, replacement=True)
edge_loader = DataLoader(dataset, batch_size=1024, sampler=sampler)
negative_sample_rate = 5
min_dist = .1
_a, _b = find_ab_params(1.0, min_dist)
umap_loss_fn = UmapLoss(negative_sample_rate, self.DEVICE, _a, _b, repulsion_strength=1.0)
recon_loss_fn = ReconstructionLoss(beta=1.0)
criterion = SingleVisLoss(umap_loss_fn, recon_loss_fn, lambd=LAMBDA)
optimizer = torch.optim.Adam(self.model.parameters(), lr=.01, weight_decay=1e-5)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=4, gamma=.1)
trainer = SingleVisTrainer(self.model, criterion, optimizer, lr_scheduler,edge_loader=edge_loader, DEVICE=self.DEVICE)
t2=time.time()
trainer.train(PATIENT, MAX_EPOCH)
t3 = time.time()
# save result
save_dir = os.path.join(self.data_provider.model_path, "time_{}.json".format(VIS_MODEL_NAME))
if not os.path.exists(save_dir):
evaluation = dict()
else:
f = open(save_dir, "r")
evaluation = json.load(f)
f.close()
if "training" not in evaluation.keys():
evaluation["training"] = dict()
evaluation["training"][str(iteration)] = round(t3-t2, 3)
with open(save_dir, 'w') as f:
json.dump(evaluation, f)
save_dir = os.path.join(self.data_provider.model_path, "Iteration_{}".format(iteration))
os.makedirs(save_dir, exist_ok=True)
trainer.save(save_dir=save_dir, file_name=VIS_MODEL_NAME)
def _evaluate(self, iteration):
EVALUATION_NAME = self.config["VISUALIZATION"]["EVALUATION_NAME"]
self.evaluator.save_epoch_eval(iteration, file_name=EVALUATION_NAME)
def _visualize(self, iteration):
save_dir = os.path.join(self.data_provider.content_path, "img")
os.makedirs(save_dir, exist_ok=True)
data = self.data_provider.train_representation(iteration)
pred = self.data_provider.get_pred(iteration, data).argmax(1)
labels = self.data_provider.train_labels(iteration)
self.vis.savefig_cus(iteration, data, pred, labels, path=os.path.join(save_dir, "{}_al.png".format(iteration)))
def visualize_embedding(self, iteration, resume_iter=-1):
self._init(resume_iter)
self._preprocess(iteration)
self._train(iteration)
self._evaluate(iteration)
self._visualize(iteration)
class DenseAL(StrategyAbstractClass):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self.VIS_METHOD = "DenseAL"
class tfDVIDenseAL(DenseAL):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self.VIS_METHOD = "tfDVIDenseAL"
self._init(resume_iteration=config["BASE_ITERATION"])
def _init(self, resume_iteration=-1):
sys.path.append(self.CONTENT_PATH)
CLASSES = self.config["CLASSES"]
BASE_ITERATION = self.config["BASE_ITERATION"]
GPU_ID = self.config["GPU"]
self.DEVICE = torch.device("cuda:{}".format(GPU_ID) if torch.cuda.is_available() else "cpu")
################################################# VISUALIZATION PARAMETERS ########################################
EPOCH_NUM = self.config["TRAINING"]["total_epoch"]
FLAG = self.config["VISUALIZATION"]["FLAG"]
############################################ ACTIVE LEARNING MODEL PARAMETERS ######################################
TRAINING_PARAMETERS = self.config["TRAINING"]
NET = TRAINING_PARAMETERS["NET"]
import Model.model as subject_model
net = eval("subject_model.{}()".format(NET))
self.data_provider = DenseActiveLearningDataProvider(self.CONTENT_PATH, net, BASE_ITERATION, EPOCH_NUM, device=self.DEVICE, classes=CLASSES, iteration_name="Iteration", epoch_name="Epoch", verbose=1)
self.projector = tfDVIDenseALProjector(content_path=self.CONTENT_PATH, flag=FLAG)
if resume_iteration > 0:
self.projector.load(resume_iteration, EPOCH_NUM)
self.evaluator = DenseALEvaluator(self.data_provider, self.projector)
self.vis = DenseALvisualizer(self.data_provider, self.projector, 200)
def _preprocess(self, iteration):
PREPROCESS = self.config["VISUALIZATION"]["PREPROCESS"]
B_N_EPOCHS = self.config["VISUALIZATION"]["BOUNDARY"]["B_N_EPOCHS"]
L_BOUND = self.config["VISUALIZATION"]["BOUNDARY"]["L_BOUND"]
if PREPROCESS:
self.data_provider._meta_data(iteration)
LEN = len(self.data_provider.train_labels(iteration))
if B_N_EPOCHS >0:
self.data_provider._estimate_boundary(iteration, LEN//10, l_bound=L_BOUND)
def _train(self, iteration):
# TODO
pass
def _evaluate(self, iteration):
EVALUATION_NAME = self.config["VISUALIZATION"]["EVALUATION_NAME"]
self.evaluator.save_epoch_eval(iteration, file_name=EVALUATION_NAME)
def _visualize(self, iteration):
save_dir = os.path.join(self.data_provider.content_path, "img")
os.makedirs(save_dir, exist_ok=True)
data = self.data_provider.train_representation(iteration)
pred = self.data_provider.get_pred(iteration, data).argmax(1)
labels = self.data_provider.train_labels(iteration)
self.vis.savefig_cus(iteration, data, pred, labels, path=os.path.join(save_dir, "{}_al.png".format(iteration)))
def visualize_embedding(self, iteration, resume_iter=-1):
self._init(resume_iter)
self._preprocess(iteration)
self._train(iteration)
self._evaluate(iteration)
self._visualize(iteration)
class TimeVisDenseAL(DenseAL):
def __init__(self, CONTENT_PATH, config):
super().__init__(CONTENT_PATH, config)
self.VIS_METHOD = "TimeVisDenseAL"
self._init(resume_iteration=config["BASE_ITERATION"])
def _init(self, resume_iteration=-1):
sys.path.append(self.CONTENT_PATH)