From a028d1316257c993dcef099b08b9c7adc1a0ac7b Mon Sep 17 00:00:00 2001 From: yilv <874448085@qq.com> Date: Sat, 23 Aug 2025 16:47:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NormWear/README.md | 247 + NormWear/__init__.py | 0 NormWear/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 158 bytes NormWear/__pycache__/__init__.cpython-311.pyc | Bin 0 -> 198 bytes .../downstream_main.cpython-310.pyc | Bin 0 -> 2131 bytes .../downstream_main.cpython-311.pyc | Bin 0 -> 4047 bytes .../__pycache__/main_model.cpython-311.pyc | Bin 0 -> 7052 bytes .../__pycache__/pretrain_main.cpython-310.pyc | Bin 0 -> 8235 bytes .../__pycache__/pretrain_main.cpython-311.pyc | Bin 0 -> 15981 bytes .../__pycache__/crossvit.cpython-311.pyc | Bin 0 -> 56744 bytes NormWear/baseline_models/crossvit/crossvit.py | 1211 + .../baseline_models/tfc/TFC/Data_split.py | 45 + NormWear/baseline_models/tfc/TFC/KNN.py | 42 + .../tfc/TFC/__pycache__/model.cpython-311.pyc | Bin 0 -> 3975 bytes .../baseline_models/tfc/TFC/augmentations.py | 133 + .../baseline_models/tfc/TFC/dataloader.py | 124 + NormWear/baseline_models/tfc/TFC/loss.py | 193 + NormWear/baseline_models/tfc/TFC/main.py | 119 + NormWear/baseline_models/tfc/TFC/model.py | 66 + NormWear/baseline_models/tfc/TFC/readme | 13 + NormWear/baseline_models/tfc/TFC/trainer.py | 309 + NormWear/baseline_models/tfc/TFC/utils.py | 95 + .../tfc/config_files/ECG_Configs.py | 56 + .../tfc/config_files/Epilepsy_Configs.py | 51 + .../tfc/config_files/FD_A_Configs.py | 62 + .../tfc/config_files/HAR_Configs.py | 56 + .../tfc/config_files/SleepEEG_Configs.py | 59 + .../baseline_models/tfc/config_files/readme | 1 + NormWear/downstream_main.py | 105 + .../__pycache__/analysis.cpython-310.pyc | Bin 0 -> 13980 bytes .../__pycache__/analysis.cpython-311.pyc | Bin 0 -> 25067 bytes .../__pycache__/config.cpython-310.pyc | Bin 0 -> 1422 bytes .../__pycache__/config.cpython-311.pyc | Bin 0 -> 2104 bytes .../corrected_linear_prob.cpython-311.pyc | Bin 0 -> 7008 bytes .../engine_finetune.cpython-310.pyc | Bin 0 -> 6801 bytes .../engine_finetune.cpython-311.pyc | Bin 0 -> 15014 bytes .../linear_prob_main.cpython-311.pyc | Bin 0 -> 10196 bytes .../__pycache__/losses.cpython-310.pyc | Bin 0 -> 2135 bytes .../__pycache__/main_finetune.cpython-310.pyc | Bin 0 -> 12314 bytes .../__pycache__/main_finetune.cpython-311.pyc | Bin 0 -> 25068 bytes .../__pycache__/model_apis.cpython-311.pyc | Bin 0 -> 25054 bytes .../prepare_embeds.cpython-310.pyc | Bin 0 -> 3052 bytes .../prepare_embeds.cpython-311.pyc | Bin 0 -> 7397 bytes .../task_specification.cpython-311.pyc | Bin 0 -> 4324 bytes NormWear/downstream_pipeline/analysis.py | 485 + NormWear/downstream_pipeline/config.py | 37 + .../corrected_linear_prob.py | 147 + NormWear/downstream_pipeline/data_clean.py | 301 + .../downstream_pipeline/engine_finetune.py | 233 + .../downstream_pipeline/linear_prob_main.py | 212 + NormWear/downstream_pipeline/losses.py | 64 + NormWear/downstream_pipeline/main_finetune.py | 324 + NormWear/downstream_pipeline/model_apis.py | 458 + .../downstream_pipeline/prepare_embeds.py | 149 + .../downstream_pipeline/task_specification.py | 177 + NormWear/imgs/overview-very-new.pdf | Bin 0 -> 729411 bytes NormWear/imgs/radar_plot.pdf | Bin 0 -> 107442 bytes NormWear/main_model.py | 131 + .../modules/__pycache__/head.cpython-310.pyc | Bin 0 -> 7716 bytes .../modules/__pycache__/head.cpython-311.pyc | Bin 0 -> 15237 bytes .../__pycache__/lr_sched.cpython-310.pyc | Bin 0 -> 618 bytes .../__pycache__/lr_sched.cpython-311.pyc | Bin 0 -> 1045 bytes .../__pycache__/normwear.cpython-310.pyc | Bin 0 -> 13224 bytes .../__pycache__/normwear.cpython-311.pyc | Bin 0 -> 29586 bytes .../__pycache__/patch_embed.cpython-310.pyc | Bin 0 -> 3040 bytes .../__pycache__/patch_embed.cpython-311.pyc | Bin 0 -> 5432 bytes .../__pycache__/pos_embed.cpython-310.pyc | Bin 0 -> 3956 bytes .../__pycache__/pos_embed.cpython-311.pyc | Bin 0 -> 7110 bytes NormWear/modules/head.py | 239 + NormWear/modules/lr_sched.py | 21 + NormWear/modules/normwear.py | 392 + NormWear/modules/patch_embed.py | 135 + NormWear/modules/pos_embed.py | 112 + NormWear/normwear - 快捷方式.lnk | Bin 0 -> 972 bytes NormWear/pretrain_main.py | 259 + NormWear/pretrain_pipeline/__init__.py | 0 .../__pycache__/__init__.cpython-310.pyc | Bin 0 -> 153 bytes .../__pycache__/__init__.cpython-311.pyc | Bin 0 -> 216 bytes .../__pycache__/dataset.cpython-310.pyc | Bin 0 -> 10298 bytes .../__pycache__/dataset.cpython-311.pyc | Bin 0 -> 22764 bytes .../engine_pretrain.cpython-310.pyc | Bin 0 -> 2530 bytes .../engine_pretrain.cpython-311.pyc | Bin 0 -> 6358 bytes NormWear/pretrain_pipeline/dataset.py | 664 + NormWear/pretrain_pipeline/engine_pretrain.py | 110 + NormWear/pretrain_pipeline/misc/__init__.py | 396 + .../misc/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 12348 bytes .../misc/__pycache__/__init__.cpython-311.pyc | Bin 0 -> 23921 bytes .../__pycache__/msitf_fusion.cpython-311.pyc | Bin 0 -> 19136 bytes .../sentence_template.cpython-311.pyc | Bin 0 -> 15669 bytes NormWear/zero_shot/engine_zeroshot.py | 249 + NormWear/zero_shot/main_zeroshot.py | 241 + NormWear/zero_shot/msitf_fusion.py | 341 + NormWear/zero_shot/sentence_template.py | 452 + NormWear/zero_shot/zero_shot_inference.py | 220 + __pycache__/demo.cpython-310.pyc | Bin 0 -> 8097 bytes check.py | 190 + combine.py | 233 + common1.txt | 441513 +++++++++++++++ cwt.py | 53 + data.py | 263 + data_prepare/batch.py | 98 + data_prepare/batch_resample.py | 87 + data_prepare/no_fuse.py | 185 + demo.py | 221 + dwt.py | 171 + evolution.py | 121 + file.py | 60 + mat.py | 179 + normalization.py | 140 + ppg+dalia.md | 164 + project.md | 0 ramdom.py | 135 + resample.py | 69 + see.py | 17 + split.py | 86 + temp.py | 168 + tool/data_convert.py | 189 + tool/dir.py | 36 + 118 files changed, 453914 insertions(+) create mode 100644 NormWear/README.md create mode 100644 NormWear/__init__.py create mode 100644 NormWear/__pycache__/__init__.cpython-310.pyc create mode 100644 NormWear/__pycache__/__init__.cpython-311.pyc create mode 100644 NormWear/__pycache__/downstream_main.cpython-310.pyc create mode 100644 NormWear/__pycache__/downstream_main.cpython-311.pyc create mode 100644 NormWear/__pycache__/main_model.cpython-311.pyc create mode 100644 NormWear/__pycache__/pretrain_main.cpython-310.pyc create mode 100644 NormWear/__pycache__/pretrain_main.cpython-311.pyc create mode 100644 NormWear/baseline_models/crossvit/__pycache__/crossvit.cpython-311.pyc create mode 100644 NormWear/baseline_models/crossvit/crossvit.py create mode 100644 NormWear/baseline_models/tfc/TFC/Data_split.py create mode 100644 NormWear/baseline_models/tfc/TFC/KNN.py create mode 100644 NormWear/baseline_models/tfc/TFC/__pycache__/model.cpython-311.pyc create mode 100644 NormWear/baseline_models/tfc/TFC/augmentations.py create mode 100644 NormWear/baseline_models/tfc/TFC/dataloader.py create mode 100644 NormWear/baseline_models/tfc/TFC/loss.py create mode 100644 NormWear/baseline_models/tfc/TFC/main.py create mode 100644 NormWear/baseline_models/tfc/TFC/model.py create mode 100644 NormWear/baseline_models/tfc/TFC/readme create mode 100644 NormWear/baseline_models/tfc/TFC/trainer.py create mode 100644 NormWear/baseline_models/tfc/TFC/utils.py create mode 100644 NormWear/baseline_models/tfc/config_files/ECG_Configs.py create mode 100644 NormWear/baseline_models/tfc/config_files/Epilepsy_Configs.py create mode 100644 NormWear/baseline_models/tfc/config_files/FD_A_Configs.py create mode 100644 NormWear/baseline_models/tfc/config_files/HAR_Configs.py create mode 100644 NormWear/baseline_models/tfc/config_files/SleepEEG_Configs.py create mode 100644 NormWear/baseline_models/tfc/config_files/readme create mode 100644 NormWear/downstream_main.py create mode 100644 NormWear/downstream_pipeline/__pycache__/analysis.cpython-310.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/analysis.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/config.cpython-310.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/config.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/corrected_linear_prob.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/engine_finetune.cpython-310.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/engine_finetune.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/linear_prob_main.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/losses.cpython-310.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/main_finetune.cpython-310.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/main_finetune.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/model_apis.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/prepare_embeds.cpython-310.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/prepare_embeds.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/__pycache__/task_specification.cpython-311.pyc create mode 100644 NormWear/downstream_pipeline/analysis.py create mode 100644 NormWear/downstream_pipeline/config.py create mode 100644 NormWear/downstream_pipeline/corrected_linear_prob.py create mode 100644 NormWear/downstream_pipeline/data_clean.py create mode 100644 NormWear/downstream_pipeline/engine_finetune.py create mode 100644 NormWear/downstream_pipeline/linear_prob_main.py create mode 100644 NormWear/downstream_pipeline/losses.py create mode 100644 NormWear/downstream_pipeline/main_finetune.py create mode 100644 NormWear/downstream_pipeline/model_apis.py create mode 100644 NormWear/downstream_pipeline/prepare_embeds.py create mode 100644 NormWear/downstream_pipeline/task_specification.py create mode 100644 NormWear/imgs/overview-very-new.pdf create mode 100644 NormWear/imgs/radar_plot.pdf create mode 100644 NormWear/main_model.py create mode 100644 NormWear/modules/__pycache__/head.cpython-310.pyc create mode 100644 NormWear/modules/__pycache__/head.cpython-311.pyc create mode 100644 NormWear/modules/__pycache__/lr_sched.cpython-310.pyc create mode 100644 NormWear/modules/__pycache__/lr_sched.cpython-311.pyc create mode 100644 NormWear/modules/__pycache__/normwear.cpython-310.pyc create mode 100644 NormWear/modules/__pycache__/normwear.cpython-311.pyc create mode 100644 NormWear/modules/__pycache__/patch_embed.cpython-310.pyc create mode 100644 NormWear/modules/__pycache__/patch_embed.cpython-311.pyc create mode 100644 NormWear/modules/__pycache__/pos_embed.cpython-310.pyc create mode 100644 NormWear/modules/__pycache__/pos_embed.cpython-311.pyc create mode 100644 NormWear/modules/head.py create mode 100644 NormWear/modules/lr_sched.py create mode 100644 NormWear/modules/normwear.py create mode 100644 NormWear/modules/patch_embed.py create mode 100644 NormWear/modules/pos_embed.py create mode 100644 NormWear/normwear - 快捷方式.lnk create mode 100644 NormWear/pretrain_main.py create mode 100644 NormWear/pretrain_pipeline/__init__.py create mode 100644 NormWear/pretrain_pipeline/__pycache__/__init__.cpython-310.pyc create mode 100644 NormWear/pretrain_pipeline/__pycache__/__init__.cpython-311.pyc create mode 100644 NormWear/pretrain_pipeline/__pycache__/dataset.cpython-310.pyc create mode 100644 NormWear/pretrain_pipeline/__pycache__/dataset.cpython-311.pyc create mode 100644 NormWear/pretrain_pipeline/__pycache__/engine_pretrain.cpython-310.pyc create mode 100644 NormWear/pretrain_pipeline/__pycache__/engine_pretrain.cpython-311.pyc create mode 100644 NormWear/pretrain_pipeline/dataset.py create mode 100644 NormWear/pretrain_pipeline/engine_pretrain.py create mode 100644 NormWear/pretrain_pipeline/misc/__init__.py create mode 100644 NormWear/pretrain_pipeline/misc/__pycache__/__init__.cpython-310.pyc create mode 100644 NormWear/pretrain_pipeline/misc/__pycache__/__init__.cpython-311.pyc create mode 100644 NormWear/zero_shot/__pycache__/msitf_fusion.cpython-311.pyc create mode 100644 NormWear/zero_shot/__pycache__/sentence_template.cpython-311.pyc create mode 100644 NormWear/zero_shot/engine_zeroshot.py create mode 100644 NormWear/zero_shot/main_zeroshot.py create mode 100644 NormWear/zero_shot/msitf_fusion.py create mode 100644 NormWear/zero_shot/sentence_template.py create mode 100644 NormWear/zero_shot/zero_shot_inference.py create mode 100644 __pycache__/demo.cpython-310.pyc create mode 100644 check.py create mode 100644 combine.py create mode 100644 common1.txt create mode 100644 cwt.py create mode 100644 data.py create mode 100644 data_prepare/batch.py create mode 100644 data_prepare/batch_resample.py create mode 100644 data_prepare/no_fuse.py create mode 100644 demo.py create mode 100644 dwt.py create mode 100644 evolution.py create mode 100644 file.py create mode 100644 mat.py create mode 100644 normalization.py create mode 100644 ppg+dalia.md create mode 100644 project.md create mode 100644 ramdom.py create mode 100644 resample.py create mode 100644 see.py create mode 100644 split.py create mode 100644 temp.py create mode 100644 tool/data_convert.py create mode 100644 tool/dir.py diff --git a/NormWear/README.md b/NormWear/README.md new file mode 100644 index 0000000..8bb9623 --- /dev/null +++ b/NormWear/README.md @@ -0,0 +1,247 @@ +
+
+
+
jEHVNIqeOT{8ZdFpt)f{1G(D#t59
zVwYxTQhb2xAEjHvIR^QxTj>xAOB_ZsTC~z=gn={+t14!_)g`hD`849>=BndLR^wlkGSMf
z*L7o(p7oy};{P`^Hvd-u3By3ZH*~# P+d0pblZjr2(u|KI3__vwbIoJ6>#kGuF1Dg`#oINnOYMxcy<9UzfU
zA_>s<@;_uGfsue`kVzx_v}(^z<)v)E$hrE*%C)2(E3@B=^6!(;AW(W$T!kwN+f4X$
zMU9eUVW=?fXvl~GgBGM$cjWYBHJI-7@lWSX`KNYDZR^GQ^*425UC+&3w|dyeFC2XO
zmjBkyPoEO&uu1QTWIys3&z_K;eNph776#5Fzk?&f(1-vyFd}(I#b?h+&z=+Q=aO5D
z=Xa-bw|IPZeA+A4JuTV~OZLNmarAlVs9SjPlyG_=`8_)*oE@U?=^^RGVe#mQbaX_t
zkAzH^!Z9OncymM47#$PsU6Q>kWFoT@8BytU#gng!_Vto|eL9^giYOLG_~O-isBW6x
z?GSCtr&df?eGEnJ9->{!b6rBdirPs3Rd=ZHPN{R!^u6Nm6kjjBQF`;a|8Vk8PX5&W
zsXN}fe4dfB#=P32pcs?T%8t!Pbu#;0jfqLnOBnS2&mb_)V4nm3jpxaGdSDnqg=f
o8*%b7YW$)}K8 zxd<{4mJcDO3KViHR~aTHMJ|F&YtM^DEDIoOu~jS!;Zv9-WT=(q1PCtYkXDQ!gEBWG zWagb~yhvl)B3-YdU_sV;X{}_X)0=XnFG)T}R`dX}FFQd7ry=Y)=ACO?Yx6B#uhJat zYXf;|BjStyn2>>y|0({mQdVUTp{%}78UHPTPYC>gz`rB#LjpuR@zZqlBLcLclMzk) zAK}PfhO6oUFCVL@C1q2dJnECAk{R#U|1VGgBb6Yu8-9H_ayWV_G%S{MNF^Q2a{JuK zS?d#;CKmJ#V9`e1yO|1P&7T7Qh3&ZT}?}s9*zgkJI1wWsSdWoGCarn2$3BbE!8ZX~`j^ zLcQb=EX0{Y;+*1erjRY{Oj$Owv%*w9c9x<+x!<(E?aL|)u3AwM>TgZe(Q80D ${p9IjyUc?{ }5p zXnqW3w4}<=JoAE7?3eSnjk+yNmGGF$UG!@zxAoUlt}^CxwL_*ifb145=iuJO53BE& zEVn&txl2;9bLz1)bw%@hv^Hg7PI}TSVSz1YExr6ToE_nR7m)upfp-D=FxM-AR$ENI zwi5@B$D=m?dnk(keF7f>FiuTUT7MnqV{J@YuT0>|a#bxelO1V$FmLkTp@K+cBnx 4^F%Ps{_&vy>u&Jqx9I%b1iEHfG8SVWdNMIX}&~pW_G{bLL0Nyb0-H6X=eVBW%W1 zf!Ih{-SdBn=H&l`z{6!n&;?>d-=Cp^Z3VKhk>zK_O(8bz lAH}J$_SI_oi@NWyV{n#s>kjB{J@eDxr@`4G>KEkX_!(@- zOCDDqrDH&_l%6;`e&XmDn1KwCJk;4Pxn#~qVivM*K~S49d5E*!CytJjEs2d3;3_`E z$@CLP$E>xUI66*t8VJZUSKLn=9V?803^nq3x>bJS=s4NAv_0#Iqhq;Q&L|uc#lf>I zW18dq`q%I1nEz|kiT{kiJ%C5^H!F)L)6{-aQt=+G?uuyPM60_e-W !mEbHq?V&~ko)zXorPd@+s8asrhA{wmxxwUp2NmxYqeNS3q|njY)2$3Zhh z9()Ux#fG{ff<>8T{D26!`+o_MQ^t;SFsD-XY@axygCn}Je?pGv=$$6R8G6So P`PyU0Y?|TdQi2zBjp-`|_n~ z9cHCVCHHb)zErJyxpb*obrgjW3cCt(-OHuK#Oj9Pu)NH4FZbnZX$~kNF=*-LaxbS& zGk^DT1!@Zw8uxPAE5BLya_Kjb%d$bnD1bA@FW cKUm~~KE!?BuTkRI^(R0(@3Po`u)i(66xP>!ox4~s~3zvPCu9_b{feW`Uk9V{# z+|oGjH*#<0!c8@2JfaIXs6VDzKXY8TX|F!qg iqv;x2T`vD%iGbR4{$Mggr(i5CWJpq4`j$R J{+Lkv`rP)Oc77osCtLa1!U@1g=Sw?+Mo(hyjS2KWCJ zl0XqO^#Nh?Pu5&Kr!pO{I%ciO9ynog!;v&xg}7m+XdyKNfzK|X2|nuqXls_6k~$*u z#*r`uM) N*!ho8Ctq5dV4l;#QrN z63+#ptylKkqxjF$7Z>*w=-?(HI}C4~KnA+@xjMzpk_B{wxgh-X#Ibu6|9Sf2L0t_c zo(m#P*HCrDf1bX$;h>J%FFOqL6R+H3*FIOLIGwkRWy%iaN1$t;bE*^v^UfJ{ 1 and n_dims < len(s) + return x.view(*s[:-n_dims], -1) + +class FeedForward(nn.Module): + def __init__(self, emb_size, hidden_size, dropout=0.1, add_norm=True, vae_out=False): + super().__init__() + self.add_norm = add_norm + + last_out = nn.Linear + + self.fc_liner = nn.Sequential( + nn.Linear(emb_size, hidden_size), + nn.GELU(), + # nn.Dropout(p=dropout), + # nn.Linear(hidden_size, emb_size), + last_out(hidden_size, emb_size), + nn.Dropout(p=dropout), + ) + + self.LayerNorm = nn.LayerNorm(emb_size, eps=1e-6) + + def forward(self, x): + out = self.fc_liner(x) + if self.add_norm: + return self.LayerNorm(x + out) + return out + +class tAPE(nn.Module): + def __init__(self, d_model, dropout=0.1, max_len=1024, scale_factor=1.0): + super(tAPE, self).__init__() + self.dropout = nn.Dropout(p=dropout) + pe = torch.zeros(max_len, d_model) # positional encoding + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)) + + pe[:, 0::2] = torch.sin((position * div_term)*(d_model/max_len)) + pe[:, 1::2] = torch.cos((position * div_term)*(d_model/max_len)) + pe = scale_factor * pe.unsqueeze(0) + self.register_buffer('pe', pe) # this stores the variable in the state_dict (used for non-trainable variables) + + def forward(self, x): # N, L, C + # print(torch.min(self.pe[:, :, :]), torch.max(self.pe[:, :, :])) # [-1, 1] + # exit() + x = x + self.pe[:, -x.shape[1]:, :] + return self.dropout(x) + +class PositionWiseFeedForward(nn.Module): + """FeedForward Neural Networks for each position""" + def __init__(self, dim, ff_dim): + super().__init__() + self.fc1 = nn.Linear(dim, ff_dim) + self.fc2 = nn.Linear(ff_dim, dim) + + def forward(self, x): + # (B, S, D) -> (B, S, D_ff) -> (B, S, D) + return self.fc2(F.gelu(self.fc1(x))) + +class MultiHeadedSelfAttentionRaw(nn.Module): + """Multi-Headed Dot Product Attention""" + def __init__(self, dim, num_heads, dropout): + super().__init__() + self.proj_q = nn.Linear(dim, dim) + self.proj_k = nn.Linear(dim, dim) + self.proj_v = nn.Linear(dim, dim) + self.drop = nn.Dropout(dropout) + self.n_heads = num_heads + self.scores = None # for visualization + + def forward(self, x, mask): + """ + x, q(query), k(key), v(value) : (B(batch_size), S(seq_len), D(dim)) + mask : (B(batch_size) x S(seq_len)) + * split D(dim) into (H(n_heads), W(width of head)) ; D = H * W + """ + # (B, S, D) -proj-> (B, S, D) -split-> (B, S, H, W) -trans-> (B, H, S, W) + q, k, v = self.proj_q(x), self.proj_k(x), self.proj_v(x) + q, k, v = (split_last(x, (self.n_heads, -1)).transpose(1, 2) for x in [q, k, v]) + # (B, H, S, W) @ (B, H, W, S) -> (B, H, S, S) -softmax-> (B, H, S, S) + scores = q @ k.transpose(-2, -1) / np.sqrt(k.size(-1)) + if mask is not None: + mask = mask[:, None, None, :].float() + scores -= 10000.0 * (1.0 - mask) + scores = self.drop(F.softmax(scores, dim=-1)) + # (B, H, S, S) @ (B, H, S, W) -> (B, H, S, W) -trans-> (B, S, H, W) + h = (scores @ v).transpose(1, 2).contiguous() + # -merge-> (B, S, D) + h = merge_last(h, 2) + self.scores = scores + return h + +class Block(nn.Module): + """Transformer Block""" + def __init__(self, dim, num_heads, ff_dim, dropout): + super().__init__() + self.attn = MultiHeadedSelfAttentionRaw(dim, num_heads, dropout) + self.proj = nn.Linear(dim, dim) + self.norm1 = nn.LayerNorm(dim, eps=1e-6) + self.pwff = PositionWiseFeedForward(dim, ff_dim) + self.norm2 = nn.LayerNorm(dim, eps=1e-6) + self.drop = nn.Dropout(dropout) + + def forward(self, x, mask): + h = self.drop(self.proj(self.attn(self.norm1(x), mask))) + x = x + h + h = self.drop(self.pwff(self.norm2(x))) + x = x + h + return x + +# Instead of masking the original cwt, let's try masking on the latents, +# therefore forcing the model use the alternate sequence to learn the missing feature + +class CrossViTModel(nn.Module): + def __init__( + self, + embed_dim=768, + num_heads=12, + mlp_ratio=4, + norm_layer = nn.LayerNorm, + device='cuda', + max_length = 387, + decoder_depth=2, + depth = 12, + num_sensor = 11, + is_test = False, # for visulization + is_pretrain = True, # for linear prob + num_class = 1 + ): + super().__init__() + + self.device = device + self.is_pretrain = is_pretrain + self.max_length = max_length + # for statistics track + self.stats = dict() + + # for visulization + self.is_test = is_test + self.cross_attn1 = [] + self.self_attn1 = [] + self.cross_attn2 = [] + self.self_attn2 = [] + # -------------------------------------------------------------------------- + # encoder specifics + # ViT default patch embeddings + + self.vit = ViT('B_16_imagenet1k', pretrained=True).to(torch.bfloat16).to(self.device) # construct and load + self.vit.patch_embedding.stride = (4, 16) + freeze_model(self.vit) + + self.input_transforms = transforms.Compose([ + # transforms.Resize((L+16, 65)), + transforms.Normalize(0.5, 0.5), + lambda im: nn.functional.pad(im, (0, 0, 16, 0), value=0), + ]) # N, 3, L, 384 + + self.cls_token_1 = nn.Parameter(torch.zeros(1, 1, embed_dim,dtype=torch.bfloat16)) + self.cls_token_2 = nn.Parameter(torch.zeros(1, 1, embed_dim,dtype=torch.bfloat16)) + #self.mask_token = nn.Parameter(torch.zeros(1,1,embed_dim,dtype=torch.bfloat16)) + self.pos_embed_1 = tAPE(embed_dim, dropout=0.1, max_len=max_length+1) + self.pos_embed_2 = tAPE(embed_dim, dropout=0.1, max_len=max_length+1) + + # # encoder + # self.signal1_encoders = nn.ModuleList([ + # Block(dim=embed_dim, num_heads=num_heads, + # ff_dim=embed_dim*mlp_ratio, + # dropout=0.1) + # for i in range(2)]) + + # self.signal2_encoders = nn.ModuleList([ + # Block(dim=embed_dim, num_heads=num_heads, + # ff_dim=embed_dim*mlp_ratio, + # dropout=0.1) + # for i in range(2)]) + + # multiSignal blocks (self-attn -> cross-attn ) + # self.pos_embed_encode = tAPE(embed_dim, dropout=0.1, max_len=max_length+1) + + self.blocks = nn.ModuleList([ + MultiSignalBlock(embed_dim = embed_dim, depth=1, + num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=False, + qk_scale=None, drop=0., attn_drop=0., + norm_layer=norm_layer) for _ in range(depth) + ]) + + self.x1_norm = norm_layer(embed_dim).bfloat16() + self.x2_norm = norm_layer(embed_dim).bfloat16() + + # Decoder Blocks (light weight) + # To reconstruct points from the encoder features + # self.decoder_blocks = nn.ModuleList([ + # nn.Sequential( + # Block(dim=embed_dim, num_heads=num_heads, + # ff_dim=embed_dim*mlp_ratio, + # dropout=0.1)) + # for i in range(decoder_depth)]) + + # self.decoder_norm = nn.LayerNorm(embed_dim).bfloat16() + + #TODO: Head + self.decoder_pred_1 = nn.Linear(embed_dim, 1).bfloat16() # decoder to series1 + self.decoder_pred_2 = nn.Linear(embed_dim, 1).bfloat16() # decoder to series1 + + # if not self.is_pretrain: + # self.decoder_pred_1 = nn.ModuleList([nn.Linear(768, num_class).bfloat16() for i in range(1)]) + # self.decoder_pred_2 = nn.ModuleList([nn.Linear(768, num_class).bfloat16() for i in range(1)]) + + + self.ar_pred_1 = nn.Linear(embed_dim,1).bfloat16() + self.ar_pred_2 = nn.Linear(embed_dim,1).bfloat16() + + self.cls_1 = nn.Linear(embed_dim,num_sensor).bfloat16() + self.cls_2 = nn.Linear(embed_dim,num_sensor).bfloat16() + + # loss + self.l1_loss = nn.L1Loss() + self.ce_loss = nn.CrossEntropyLoss() + + def forward(self,x_1,x_2, + x1_mask,x2_mask,): + ''' + @param x1,x2: (N, 3, 387, 65) + @param padding_mask: (N, 388) + ''' + N, _, L, F = x_1.shape + #print('input shape: ',x_1.shape) + x_1 = self.input_transforms(x_1) + x_1 = self.vit.patch_embedding(x_1) + # print('shape of x_1 patch_embedding,',x_1.shape) + # print('shape of input padding mask,',x1_mask.shape) + x_1 = x_1.flatten(2).transpose(1, 2) # b,gh*gw,d + x_1 = x_1[:, -L:, :] # b,L,d + + cls_tokens = self.cls_token_1.expand(x_1.shape[0], -1, -1) + x_1_input = torch.cat((cls_tokens, x_1), dim=1) + + x_1_input = self.pos_embed_1(x_1_input) # N, length * mask_ratio+1, E + + # helper signals + x_2 = self.input_transforms(x_2) + x_2 = self.vit.patch_embedding(x_2) + x_2 = x_2.flatten(2).transpose(1, 2) # b,gh*gw,d + x_2 = x_2[:, -L:, :] # b,L,d + + cls_tokens_2 = self.cls_token_2.expand(x_2.shape[0], -1, -1) + x_2_input = torch.cat((cls_tokens_2, x_2), dim=1) + x_2_input = self.pos_embed_2(x_2_input) # N, num_patches+1,E + + # print('shape of x_1_input',x_1_input.shape) # N, 388, 768 + # print('shape of x_2 input: ',x_2_input.shape) + + #print('data type of x_1_input',x_1_input.dtype) + # Self-Attention Encoder, Freeze this two + # with torch.no_grad(): + # for b_i in range(4): + # x_1_input = self.vit.transformer.blocks[b_i](x_1_input,mask=x1_mask) + # x_2_input = self.vit.transformer.blocks[b_i](x_2_input,mask=x2_mask) + + # print('vit feature shape:',x_1_input.shape) + + # Multi-modal Encoder + if not self.is_test: + for blk in self.blocks: + x_1_input,x_2_input = blk(x_1 = x_1_input, + x_2 = x_2_input, + x1_attn=x1_mask, + x2_attn=x2_mask, + is_test = self.is_test) + # For Visulization ########################################### + if self.is_test: + for blk in self.blocks: + x_1_input,x_2_input,corss_attn_matrix1,\ + cross_attn_matrix2,self_attn_matrix1,\ + self_attn_matrix2 = blk(x_1 = x_1_input, + x_2 = x_2_input, + x1_attn=x1_mask, + x2_attn=x2_mask, + is_test = self.is_test) + self.cross_attn1.append(corss_attn_matrix1) + self.cross_attn2.append(cross_attn_matrix2) + self.self_attn1.append(self_attn_matrix1) + self.self_attn2.append(self_attn_matrix2) + ############################################################### + + x1_out = self.x1_norm(x_1_input) # N, num_patches+1,E + x2_out = self.x2_norm(x_2_input) + + + # # Decoder + # for blk in self.decoder_blocks: + # x_out = blk(x_out,mask=x1_mask) + + # x_out = self.decoder_norm(x_out) + # cls_out = x_out[:,0,:] + + + if self.is_pretrain:# Reconstruct + reconstruct_x1 = self.decoder_pred_1(x1_out[:,1:,:]).squeeze(2) # N, 387 + reconstruct_x2 = self.decoder_pred_2(x2_out[:,1:,:]).squeeze(2) # N, 387 + + # TODO: Next_value Prediction + ar_x1 = self.ar_pred_1(x1_out[:,0,:]).squeeze(1) + ar_x2 = self.ar_pred_2(x1_out[:,0,:]).squeeze(1) + + sensor_cls_x1 = self.cls_1(x1_out[:,0,:]) + sensor_cls_x2 = self.cls_2(x2_out[:,0,:]) + + return x1_out,x2_out,reconstruct_x1,reconstruct_x2,ar_x1,ar_x2,sensor_cls_x1,sensor_cls_x2 + + else: + #hack: we will replace decoder_pred_1, and decoder_pred_2 with a module_list for linear probing + result_list = [] + for linear1,linear2 in zip(self.decoder_pred_1,self.decoder_pred_2): + x1_out = linear1(torch.mean(x1_out[:,1:,:],dim=1)) # N x num_classes + x2_out = linear2(torch.mean(x2_out[:,1:,:],dim=1)) # N x num_classes + result = (x1_out+x2_out)/2 # N x num_classes + result_list.append(result) # Address the situation where there are multiple task + + return result_list # num_tasks x tensor(N x num_classes) + + + def loss_f(self, pred_ts1, pred_ts2, + ar_x1, ar_x2, + target_ts1, target_ts2, + sensor_cls_x1, sensor_cls_x2, + target_cls1, target_cls2, + x1_mask, x2_mask): + + ''' + pred_ts1, pred_ts2: N, 387 + ''' + + # Extract the last timestep for autoregression loss + target_ar1 = target_ts1[:, -1] + target_ar2 = target_ts2[:, -1] + + # Calculate reconstruction loss + re_loss1 = torch.sum((pred_ts1 - target_ts1[:, :-1]) ** 2 * x1_mask[:, :-1]) / torch.sum(x1_mask[:, :-1]) + re_loss2 = torch.sum((pred_ts2 - target_ts2[:, :-1]) ** 2 * x2_mask[:, 1:]) / torch.sum(x2_mask[:, 1:]) + + # Calculate L1 loss for each branch + l1_loss1 = self.l1_loss(ar_x1, target_ar1) + l1_loss2 = self.l1_loss(ar_x2, target_ar2) + + # Calculate cross-entropy loss for each branch + ce_loss1 = self.ce_loss(sensor_cls_x1, target_cls1) + ce_loss2 = self.ce_loss(sensor_cls_x2, target_cls2) + + # Calculate total losses for each branch + loss_1 = 0.4 * re_loss1 + 0.3 * l1_loss1 + 0.3 * ce_loss1 + loss_2 = 0.4 * re_loss2 + 0.3 * l1_loss2 + 0.3 * ce_loss2 + + # Total losses + total_loss = loss_1 + loss_2 + + return total_loss, {'re_loss1': re_loss1.item(), 're_loss2': re_loss2.item(), + 'l1_loss1': l1_loss1.item(), 'l1_loss2': l1_loss2.item(), + 'ce_loss1': ce_loss1.item(), 'ce_loss2': ce_loss2.item()} + + + @torch.no_grad() + def forward_all(self,all_cwt): + # Input: all_cwt shape: tensor(N x ch x 3 x L x 65) + # Output: all_cwt embedding: tensor(N x ch x L x 768) + + N,ch,_,L,F = all_cwt.shape + #print('**cwt ch: ',ch) + all_signal_pairs = torch.empty(N,0,2,3,self.max_length,F).to(self.device) + all_mask_pairs = torch.empty(N,0,2,388).to(self.device) + pair_index_record = torch.empty(N,0,2) + if ch == 1: + x_1 = all_cwt.squeeze(1) # N x 3 x L x 65 + x_1,padding_mask_1 = self.padding(x_1) + x_2 = x_1.clone() + padding_mask_2 = padding_mask_1.clone() # N x L+1 + + print('shape of inference input sample: ',x_1.shape) # N x 3 x L x 65 + pair_signal = torch.cat((x_1.unsqueeze(1), x_2.unsqueeze(1)), dim=1) # N x 2 x 3 x L x 65 + pair_mask = torch.cat((padding_mask_1.unsqueeze(1), padding_mask_2.unsqueeze(1)), dim=1) # N x 2 x 388 + + all_signal_pairs = torch.cat((all_signal_pairs,pair_signal.unsqueeze(1)),dim=1) # N x 1 x 2 x 3 x 387 x 65 + all_mask_pairs = torch.cat((all_mask_pairs,pair_mask.unsqueeze(1)),dim=1) # N x 1 x 2 x 388 + pair_index_record = torch.cat((pair_index_record,torch.tensor([0,0]).repeat(N, 1).unsqueeze(1)),dim=1) # N x 1 x 2 + + + #print('**CH: ',ch) + for i in range(ch): + for j in range(i+1,ch): + x_1 = all_cwt[:,i,...] # N x 3 x L x 65 + x_2 = all_cwt[:,j,...] # N x 3 x L x 65 + + x_1,padding_mask_1 = self.padding(x_1) + x_2,padding_mask_2 = self.padding(x_2) + + pair_signal = torch.cat((x_1.unsqueeze(1), x_2.unsqueeze(1)), dim=1) # N x 2 x 3 x 387 x 65 + pair_mask = torch.cat((padding_mask_1.unsqueeze(1), padding_mask_2.unsqueeze(1)), dim=1) # N x 2 x 388 + + all_signal_pairs = torch.cat((all_signal_pairs,pair_signal.unsqueeze(1)),dim=1) # N x num_pairs x 2 x 3 x 387 x 65 + all_mask_pairs = torch.cat((all_mask_pairs,pair_mask.unsqueeze(1)),dim=1) # N x num_pairs x 2 x 388 + pair_index_record = torch.cat((pair_index_record,torch.tensor([i,j]).repeat(N, 1).unsqueeze(1)),dim=1) # N x num_pairs x 2 + + #print('all_signal_pairs shape: ',all_signal_pairs.shape) + N, num_pairs,_,_,L,F = all_signal_pairs.shape + print('all signals pair shape:',all_signal_pairs.shape) + + reshaped_signal_pairs = all_signal_pairs.reshape(N * num_pairs, 2, 3, L, F) + reshaped_mask_pairs = all_mask_pairs.reshape(N * num_pairs, 2, -1) + reshaped_pair_record = pair_index_record.reshape(N * num_pairs, 2).to(self.device) + + # print('shape of reshaped_signal_pairs',reshaped_signal_pairs.shape) + # print('shape of reshaped_mask_pairs',reshaped_mask_pairs.shape) + # print('shape of reshaped_pair_record',reshaped_pair_record.shape) + + print('Start Inference: ') + + x_1 = reshaped_signal_pairs[:,0,...].bfloat16() # (N x # of pairs) x 3 x 387 x 65 + x_2 = reshaped_signal_pairs[:,1,...].bfloat16() # (N x # of pairs) x 3 x 387 x 65 + + x_1_attn_mask = reshaped_mask_pairs[:,0,...].bfloat16() # (N x # of pairs) x 388 + x_2_attn_mask = reshaped_mask_pairs[:,1,...].bfloat16() # (N x # of pairs) x 388 + + x1_out,x2_out,_,_,_,_,_,_ = self.forward(x_1,x_2, + x1_mask=x_1_attn_mask, + x2_mask=x_2_attn_mask) + + # x1_out shape: (Nxnum_pairs) x (L+1) x E + # x2_out shape: (Nxnum_pairs) x (L+1) x E + + print('x1_out shape: ',x1_out.shape) + print('x2_out shape: ',x2_out.shape) + + combine_tensor = torch.cat((x1_out.unsqueeze(1),x2_out.unsqueeze(1)),dim=1) # (Nxnum_pairs) x 2 x (L+1) x E + combine_tensor = torch.cat((x1_out.unsqueeze(1),x2_out.unsqueeze(1)),dim=1) # (Nxnum_pairs) x 2 x (L+1) x E + + embed_out = [] + for i in range(0,N*num_pairs,num_pairs): + curr_batch = combine_tensor[i:i+num_pairs,...] + curr_idx_record = reshaped_pair_record[i:i+num_pairs,...] + + batch_embed = torch.empty(0,L+1,768).to(self.device) + for i in range(ch): + selected_tensor = curr_batch[curr_idx_record==i] # num x (L+1) x E + #print('shape of selected_tensor: ',selected_tensor.shape) + if selected_tensor.numel() != 0: + mean_tensor = selected_tensor.mean(dim=0) # L+1 x E + batch_embed = torch.cat((batch_embed,mean_tensor.unsqueeze(0)),dim=0) # num_ch x L+1 x E + + embed_out.append(batch_embed.unsqueeze(0)) # # N x num_ch x L+1 x 768 + + embed_out = torch.cat(embed_out, dim=0).to(self.device) # N x num_ch x L+1 x 768 + + print('Embedding out shape',embed_out.shape) + + return embed_out + + + def padding(self,in_cwt): + N,_,L,F = in_cwt.shape + # reshape to C,L,F + max_length = self.max_length+1 + + cwt_length = L + ts_length = cwt_length+1 + + padding_mask = torch.ones(N,self.max_length+1).to(self.device) # N, 388 + + if ts_length > max_length: + in_cwt = in_cwt[:, :,-max_length+1:, :] #3,max_length,65 + + elif ts_length < max_length: + in_cwt = torch.cat((torch.zeros(N,3,max_length-1-L, F).to(self.device), in_cwt), dim=2) # 3, max_len, 65 + padding_mask[:,:self.max_length-L+1] = 0 + + return in_cwt, padding_mask + + + + def num_params(self): + total_num = sum(p.numel() for p in self.parameters()) + train_num = sum(p.numel() for p in self.parameters() if p.requires_grad) + print("Total of Parameters: {}M".format(round(total_num / 1e6, 2))) + print("Train Parameters: {}M".format(round(train_num / 1e6, 2))) + + + + def fit( + self, + dataloader, + epochs, + lr=1e-3, + weight_decay=1e-2 + ): + optimizer = optim.AdamW( + self.parameters(), + lr=lr, + weight_decay=weight_decay + ) + + # train + train_losses = list() + print("Start training") + for e in tqdm(range(epochs)): + if len(train_losses) > 0: + print("Last Train Loss:", train_losses[-1]) + self.train() + for data in tqdm(dataloader): + # forward + x1_out,x2_out,reconstruct_x1,reconstruct_x2,ar_x1,ar_x2,sensor_cls_x1,sensor_cls_x2 = self( + x_1=data["sample_in_1"], + x_2=data['sample_in_2'], + x1_mask = data['padding_mask_1'], + x2_mask = data['padding_mask_2'], + ) + + loss,loss_dict = self.loss_f(pred_ts1=reconstruct_x1, + pred_ts2=reconstruct_x2, + ar_x1 = ar_x1, + ar_x2 = ar_x2, + target_ts1=data['sample_out_1'], + target_ts2=data['sample_out_2'], + sensor_cls_x1 = sensor_cls_x1, + sensor_cls_x2 = sensor_cls_x2, + target_cls1 = data['cls_1'], + target_cls2 = data['cls_2'], + x1_mask=data['padding_mask_1'], + x2_mask = data['padding_mask_2']) + + + # backprop + optimizer.zero_grad() # clear cache + loss.backward() # calculate gradient + + # gradient clipping + for p in self.parameters(): # addressing gradient vanishing + if p.requires_grad and p.grad is not None: + p.grad = torch.nan_to_num(p.grad, nan=0.0) + clip_grad_norm_(self.parameters(), 5) + + optimizer.step() # update parameters + + # update record + train_losses.append(loss.detach().cpu().item()) + for key,value in loss_dict.items(): + print(key,value) + + # for i in range(len(data["in_tk"])): + # in_tk, out_tk = data["in_tk"][i], data["out_tk"][i] + # comb_tk = "{}-{}".format(in_tk, out_tk) + # if self.stats.get(in_tk) is None: + # self.stats[in_tk] = 0 + # if self.stats.get(out_tk) is None: + # self.stats[out_tk] = 0 + # if self.stats.get(comb_tk) is None: + # self.stats[comb_tk] = 0 + # self.stats[in_tk] += 1 + # self.stats[out_tk] += 1 + # self.stats[comb_tk] += 1 + + + self.eval() + + return train_losses + +class CrossAttention(nn.Module): + def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.): + super().__init__() + # cross attention of q: cls token and kv: whole sequence + + self.num_heads = num_heads + head_dim = dim // num_heads + + self.scale = qk_scale or head_dim ** -0.5 + + self.wq = nn.Linear(dim, dim, bias=qkv_bias).bfloat16() + self.wk = nn.Linear(dim, dim, bias=qkv_bias).bfloat16() + self.wv = nn.Linear(dim, dim, bias=qkv_bias).bfloat16() + self.attn_drop = nn.Dropout(attn_drop) + self.proj = nn.Linear(dim, dim).bfloat16() + self.proj_drop = nn.Dropout(proj_drop) + + def forward(self, x, mask=None,is_test=False): + # x = (x_1-cls token, x_2 tokens) + B, N, C = x.shape #bs, num_patches+1, E, + q = self.wq(x[:, 0:1, ...]).reshape(B, 1, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) # B1C -> B1H(C/H) -> BH1(C/H) + k = self.wk(x).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) # BNC -> BNH(C/H) -> BHN(C/H) + v = self.wv(x).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) # BNC -> BNH(C/H) -> BHN(C/H) + + attn = (q @ k.transpose(-2, -1)) * self.scale # BH1(C/H) @ BH(C/H)N -> BH1N + if mask is not None: + mask = mask[:,None,None,:].bfloat16() + attn -= 1000.0*(1.0-mask) + attn = attn.softmax(dim=-1) + attn = self.attn_drop(attn) + + x = (attn @ v).transpose(1, 2).reshape(B, 1, C) # (BH1N @ BHN(C/H)) -> BH1(C/H) -> B1H(C/H) -> B1C + x = self.proj(x) + x = self.proj_drop(x) + + if is_test: return x,attn + + return x + + +class CrossAttentionBlock(nn.Module): + def __init__(self, dim, num_heads=12, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0., + norm_layer=nn.LayerNorm, has_mlp=False): + super().__init__() + self.norm1 = norm_layer(dim).bfloat16() + self.attn = CrossAttention( + dim, num_heads=num_heads, qkv_bias=qkv_bias, + qk_scale=qk_scale, attn_drop=attn_drop, + proj_drop=drop) + + self.has_mlp = has_mlp + if has_mlp: + self.norm2 = norm_layer(dim).bfloat16() + mlp_hidden_dim = int(dim * mlp_ratio) + self.mlp = FeedForward(emb_size=dim, hidden_size=mlp_hidden_dim) + + def forward(self, x,mask,is_test=False): + if is_test: tmp,attn_matrix = self.attn(self.norm1(x),mask,is_test) + else: tmp = self.attn(self.norm1(x),mask,is_test) + + x = x[:, 0:1, ...] + tmp + + if self.has_mlp: + x = x + self.mlp(self.norm2(x)) + + if is_test: return x, attn_matrix + + return x # N, 1, E + + +class MultiSignalBlock(nn.Module): + def __init__(self,embed_dim = 768, depth=1, + num_heads=12, mlp_ratio=2.0, qkv_bias=False, + qk_scale=None, drop=0., attn_drop=0., + norm_layer=nn.LayerNorm): + super().__init__() + + self.signal1_blocks = nn.ModuleList([ + Block(dim=embed_dim, num_heads=num_heads, + ff_dim=embed_dim*mlp_ratio, + dropout=0.1) + for i in range(depth)]) + + + self.signal2_blocks = nn.ModuleList([ + Block(dim=embed_dim, num_heads=num_heads, + ff_dim=embed_dim*mlp_ratio, + dropout=0.1) + for i in range(depth)]) + + + # self.cls_proj = FeedForward(emb_size=embed_dim,hidden_size=embed_dim*mlp_ratio) + # self.revert_cls_proj = FeedForward(emb_size=embed_dim,hidden_size=embed_dim*mlp_ratio) + + self.x_1fusion = CrossAttentionBlock(dim=embed_dim,num_heads=num_heads, + mlp_ratio=mlp_ratio,qkv_bias=qkv_bias, + qk_scale=qk_scale,drop=drop,attn_drop=attn_drop, + norm_layer=norm_layer, + has_mlp=False) + + self.x_2fusion = CrossAttentionBlock(dim=embed_dim,num_heads=num_heads, + mlp_ratio=mlp_ratio,qkv_bias=qkv_bias, + qk_scale=qk_scale,drop=drop,attn_drop=attn_drop, + norm_layer=norm_layer, + has_mlp=False) + + def forward(self,x_1,x_2, + x1_attn=None,x2_attn=None,is_test=False): + + # Self-Attention Block + for blk in self.signal1_blocks: + x_1 = blk(x_1,mask=x1_attn) # N, num_patches+1, E + + for blk in self.signal2_blocks: + x_2 = blk(x_2,mask=x2_attn) # N, num_patches+1, E + + if is_test: + self_attn_matrix1 = self.signal1_blocks[0].attn.scores + self_attn_matrix2 = self.signal2_blocks[0].attn.scores + + ################################################################################## + x1_cls = x_1[:, 0:1,:] + x1_ts = x_1[:, 1:,:] + x2_cls = x_2[:, 0:1,:] + x2_ts = x_2[:, 1:,:] + ################################################################################## + # x_1 branch + # take x_1 cls_token out and concat with x_2 tokens + + # x_in = torch.cat((self.cls_proj(cls_1),x_2[:,1:,:]),dim=1) # N, num_patches+1, E + x1_kqv = torch.cat((x1_cls,x2_ts),dim=1) + if is_test: tmp1,corss_attn_matrix1 = self.x_1fusion(x1_kqv,x1_attn,is_test) # N, 1, E + else: tmp1=self.x_1fusion(x1_kqv,x1_attn,is_test) + + #print('dimension of cross-attention token: ',tmp.shape) + #cls_out = self.revert_cls_proj(tmp) + + # concat the fuse-clse token with the original x_1 tokens + x_1_out = torch.cat((tmp1,x1_ts),dim=1) # N, num_patches+1, E + + ################################################################################## + # TODO: x_2 branch + x2_kqv = torch.cat((x2_cls,x1_ts),dim=1) + if is_test: tmp2,cross_attn_matrix2 = self.x_2fusion(x2_kqv,x2_attn,is_test) + else: tmp2 = self.x_2fusion(x2_kqv,x2_attn,is_test) + x_2_out = torch.cat((tmp2,x2_ts),dim=1) + ################################################################################## + + if is_test: return x_1_out,x_2_out,corss_attn_matrix1,cross_attn_matrix2,self_attn_matrix1,self_attn_matrix2 + + return x_1_out,x_2_out + + + +class CrossViTBlock(nn.Module): + def __init__(self,embed_dim = 768, depth=1,self_attn_model = None, + num_heads=12, mlp_ratio=2.0, qkv_bias=False, + qk_scale=None, drop=0., attn_drop=0., + curr_layer = 0, + norm_layer=nn.LayerNorm): + super().__init__() + + self.self_attn = self_attn_model.transformer.blocks[curr_layer].eval() + + # self.cls_proj = FeedForward(emb_size=embed_dim,hidden_size=embed_dim*mlp_ratio) + # self.revert_cls_proj = FeedForward(emb_size=embed_dim,hidden_size=embed_dim*mlp_ratio) + + self.x_1fusion = CrossAttentionBlock(dim=embed_dim,num_heads=num_heads, + mlp_ratio=mlp_ratio,qkv_bias=qkv_bias, + qk_scale=qk_scale,drop=drop,attn_drop=attn_drop, + norm_layer=norm_layer, + has_mlp=False) + + self.x_2fusion = CrossAttentionBlock(dim=embed_dim,num_heads=num_heads, + mlp_ratio=mlp_ratio,qkv_bias=qkv_bias, + qk_scale=qk_scale,drop=drop,attn_drop=attn_drop, + norm_layer=norm_layer, + has_mlp=False) + + def forward(self,x_1,x_2, + x1_attn=None,x2_attn=None,is_test=False): + + # Self-Attention Block + x_1 = self.self_attn(x_1,mask=x1_attn) # N, num_patches+1, E + x_2 = self.self_attn(x_2,mask=x2_attn) # N, num_patches+1, E + + + # if is_test: + # self_attn_matrix1 = self.signal1_blocks[0].attn.scores + # self_attn_matrix2 = self.signal2_blocks[0].attn.scores + + ################################################################################## + x1_cls = x_1[:, 0:1,:] + x1_ts = x_1[:, 1:,:] + x2_cls = x_2[:, 0:1,:] + x2_ts = x_2[:, 1:,:] + ################################################################################## + # x_1 branch + # take x_1 cls_token out and concat with x_2 tokens + + # x_in = torch.cat((self.cls_proj(cls_1),x_2[:,1:,:]),dim=1) # N, num_patches+1, E + x1_kqv = torch.cat((x1_cls,x2_ts),dim=1) + if is_test: tmp1,corss_attn_matrix1 = self.x_1fusion(x1_kqv,x1_attn,is_test) # N, 1, E + else: tmp1=self.x_1fusion(x1_kqv,x1_attn,is_test) + + #print('dimension of cross-attention token: ',tmp.shape) + #cls_out = self.revert_cls_proj(tmp) + + # concat the fuse-clse token with the original x_1 tokens + x_1_out = torch.cat((tmp1,x1_ts),dim=1) # N, num_patches+1, E + + ################################################################################## + # TODO: x_2 branch + x2_kqv = torch.cat((x2_cls,x1_ts),dim=1) + if is_test: tmp2,cross_attn_matrix2 = self.x_2fusion(x2_kqv,x2_attn,is_test) + else: tmp2 = self.x_2fusion(x2_kqv,x2_attn,is_test) + x_2_out = torch.cat((tmp2,x2_ts),dim=1) + ################################################################################## + + #if is_test: return x_1_out,x_2_out,corss_attn_matrix1,cross_attn_matrix2,self_attn_matrix1,self_attn_matrix2 + + return x_1_out,x_2_out + + + + +class CrossSignalViT(nn.Module): + def __init__( + self, + embed_dim=768, + num_heads=12, + mlp_ratio=4, + norm_layer = nn.LayerNorm, + device='cuda', + max_length = 387, + decoder_depth=2, + depth = 12, + num_sensor = 11, + is_test = False, # for visulization + is_pretrain = True, # for linear prob + num_class = 1 + ): + super().__init__() + + self.device = device + self.is_pretrain = is_pretrain + self.max_length = max_length + # for statistics track + self.stats = dict() + + # for visulization + self.is_test = is_test + self.cross_attn1 = [] + self.self_attn1 = [] + self.cross_attn2 = [] + self.self_attn2 = [] + # -------------------------------------------------------------------------- + # encoder specifics + # ViT default patch embeddings + + self.vit = ViT('B_16_imagenet1k', pretrained=True).to(torch.bfloat16).to(self.device) # construct and load + self.vit.patch_embedding.stride = (4, 16) + freeze_model(self.vit) + + self.input_transforms = transforms.Compose([ + # transforms.Resize((L+16, 65)), + transforms.Normalize(0.5, 0.5), + lambda im: nn.functional.pad(im, (0, 0, 16, 0), value=0), + ]) # N, 3, L, 384 + + self.cls_token_1 = nn.Parameter(torch.zeros(1, 1, embed_dim,dtype=torch.bfloat16)) + self.cls_token_2 = nn.Parameter(torch.zeros(1, 1, embed_dim,dtype=torch.bfloat16)) + #self.mask_token = nn.Parameter(torch.zeros(1,1,embed_dim,dtype=torch.bfloat16)) + self.pos_embed_1 = tAPE(embed_dim, dropout=0.1, max_len=max_length+1) + self.pos_embed_2 = tAPE(embed_dim, dropout=0.1, max_len=max_length+1) + + # # encoder + # self.signal1_encoders = nn.ModuleList([ + # Block(dim=embed_dim, num_heads=num_heads, + # ff_dim=embed_dim*mlp_ratio, + # dropout=0.1) + # for i in range(2)]) + + # self.signal2_encoders = nn.ModuleList([ + # Block(dim=embed_dim, num_heads=num_heads, + # ff_dim=embed_dim*mlp_ratio, + # dropout=0.1) + # for i in range(2)]) + + # multiSignal blocks (self-attn -> cross-attn ) + # self.pos_embed_encode = tAPE(embed_dim, dropout=0.1, max_len=max_length+1) + + self.blocks = nn.ModuleList([ + CrossViTBlock(embed_dim = embed_dim, depth=1, + curr_layer = curr_layer,self_attn_model = self.vit, + num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=False, + qk_scale=None, drop=0., attn_drop=0., + norm_layer=norm_layer) for curr_layer in range(depth)]) + + self.x1_norm = norm_layer(embed_dim).bfloat16() + self.x2_norm = norm_layer(embed_dim).bfloat16() + + # Decoder Blocks (light weight) + # To reconstruct points from the encoder features + # self.decoder_blocks = nn.ModuleList([ + # nn.Sequential( + # Block(dim=embed_dim, num_heads=num_heads, + # ff_dim=embed_dim*mlp_ratio, + # dropout=0.1)) + # for i in range(decoder_depth)]) + + # self.decoder_norm = nn.LayerNorm(embed_dim).bfloat16() + + #TODO: Head + self.decoder_pred_1 = nn.Linear(embed_dim, 1).bfloat16() # decoder to series1 + self.decoder_pred_2 = nn.Linear(embed_dim, 1).bfloat16() # decoder to series1 + + # if not self.is_pretrain: + # self.decoder_pred_1 = nn.ModuleList([nn.Linear(768, num_class).bfloat16() for i in range(1)]) + # self.decoder_pred_2 = nn.ModuleList([nn.Linear(768, num_class).bfloat16() for i in range(1)]) + + + self.ar_pred_1 = nn.Linear(embed_dim,1).bfloat16() + self.ar_pred_2 = nn.Linear(embed_dim,1).bfloat16() + + self.cls_1 = nn.Linear(embed_dim,num_sensor).bfloat16() + self.cls_2 = nn.Linear(embed_dim,num_sensor).bfloat16() + + # loss + self.l1_loss = nn.L1Loss() + self.ce_loss = nn.CrossEntropyLoss() + + def forward(self,x_1,x_2, + x1_mask,x2_mask,): + ''' + @param x1,x2: (N, 3, 387, 65) + @param padding_mask: (N, 388) + ''' + N, _, L, F = x_1.shape + #print('input shape: ',x_1.shape) + x_1 = self.input_transforms(x_1) + x_1 = self.vit.patch_embedding(x_1) + # print('shape of x_1 patch_embedding,',x_1.shape) + # print('shape of input padding mask,',x1_mask.shape) + x_1 = x_1.flatten(2).transpose(1, 2) # b,gh*gw,d + x_1 = x_1[:, -L:, :] # b,L,d + + cls_tokens = self.cls_token_1.expand(x_1.shape[0], -1, -1) + x_1_input = torch.cat((cls_tokens, x_1), dim=1) + + x_1_input = self.pos_embed_1(x_1_input) # N, length * mask_ratio+1, E + + # helper signals + x_2 = self.input_transforms(x_2) + x_2 = self.vit.patch_embedding(x_2) + x_2 = x_2.flatten(2).transpose(1, 2) # b,gh*gw,d + x_2 = x_2[:, -L:, :] # b,L,d + + cls_tokens_2 = self.cls_token_2.expand(x_2.shape[0], -1, -1) + x_2_input = torch.cat((cls_tokens_2, x_2), dim=1) + x_2_input = self.pos_embed_2(x_2_input) # N, num_patches+1,E + + # print('shape of x_1_input',x_1_input.shape) # N, 388, 768 + # print('shape of x_2 input: ',x_2_input.shape) + + #print('data type of x_1_input',x_1_input.dtype) + # Self-Attention Encoder, Freeze this two + # with torch.no_grad(): + # for b_i in range(4): + # x_1_input = self.vit.transformer.blocks[b_i](x_1_input,mask=x1_mask) + # x_2_input = self.vit.transformer.blocks[b_i](x_2_input,mask=x2_mask) + + # print('vit feature shape:',x_1_input.shape) + + # Multi-modal Encoder + if not self.is_test: + for blk in self.blocks: + x_1_input,x_2_input = blk(x_1 = x_1_input, + x_2 = x_2_input, + x1_attn=x1_mask, + x2_attn=x2_mask, + is_test = self.is_test) + # For Visulization ########################################### + if self.is_test: + for blk in self.blocks: + x_1_input,x_2_input,corss_attn_matrix1,\ + cross_attn_matrix2,self_attn_matrix1,\ + self_attn_matrix2 = blk(x_1 = x_1_input, + x_2 = x_2_input, + x1_attn=x1_mask, + x2_attn=x2_mask, + is_test = self.is_test) + self.cross_attn1.append(corss_attn_matrix1) + self.cross_attn2.append(cross_attn_matrix2) + self.self_attn1.append(self_attn_matrix1) + self.self_attn2.append(self_attn_matrix2) + ############################################################### + + x1_out = self.x1_norm(x_1_input) # N, num_patches+1,E + x2_out = self.x2_norm(x_2_input) + + + # # Decoder + # for blk in self.decoder_blocks: + # x_out = blk(x_out,mask=x1_mask) + + # x_out = self.decoder_norm(x_out) + # cls_out = x_out[:,0,:] + + + if self.is_pretrain:# Reconstruct + reconstruct_x1 = self.decoder_pred_1(x1_out[:,1:,:]).squeeze(2) # N, 387 + reconstruct_x2 = self.decoder_pred_2(x2_out[:,1:,:]).squeeze(2) # N, 387 + + # TODO: Next_value Prediction + ar_x1 = self.ar_pred_1(x1_out[:,0,:]).squeeze(1) + ar_x2 = self.ar_pred_2(x1_out[:,0,:]).squeeze(1) + + sensor_cls_x1 = self.cls_1(x1_out[:,0,:]) + sensor_cls_x2 = self.cls_2(x2_out[:,0,:]) + + return x1_out,x2_out,reconstruct_x1,reconstruct_x2,ar_x1,ar_x2,sensor_cls_x1,sensor_cls_x2 + + else: + #hack: we will replace decoder_pred_1, and decoder_pred_2 with a module_list for linear probing + result_list = [] + for linear1,linear2 in zip(self.decoder_pred_1,self.decoder_pred_2): + x1_out = linear1(torch.mean(x1_out[:,1:,:],dim=1)) # N x num_classes + x2_out = linear2(torch.mean(x2_out[:,1:,:],dim=1)) # N x num_classes + result = (x1_out+x2_out)/2 # N x num_classes + result_list.append(result) # Address the situation where there are multiple task + + return result_list # num_tasks x tensor(N x num_classes) + + + def loss_f(self, pred_ts1, pred_ts2, + ar_x1, ar_x2, + target_ts1, target_ts2, + sensor_cls_x1, sensor_cls_x2, + target_cls1, target_cls2, + x1_mask, x2_mask): + + ''' + pred_ts1, pred_ts2: N, 387 + ''' + + # Extract the last timestep for autoregression loss + target_ar1 = target_ts1[:, -1] + target_ar2 = target_ts2[:, -1] + + # Calculate reconstruction loss + re_loss1 = torch.sum((pred_ts1 - target_ts1[:, :-1]) ** 2 * x1_mask[:, :-1]) / torch.sum(x1_mask[:, :-1]) + re_loss2 = torch.sum((pred_ts2 - target_ts2[:, :-1]) ** 2 * x2_mask[:, 1:]) / torch.sum(x2_mask[:, 1:]) + + # Calculate L1 loss for each branch + l1_loss1 = self.l1_loss(ar_x1, target_ar1) + l1_loss2 = self.l1_loss(ar_x2, target_ar2) + + # Calculate cross-entropy loss for each branch + ce_loss1 = self.ce_loss(sensor_cls_x1, target_cls1) + ce_loss2 = self.ce_loss(sensor_cls_x2, target_cls2) + + # Calculate total losses for each branch + loss_1 = 0.4 * re_loss1 + 0.3 * l1_loss1 + 0.3 * ce_loss1 + loss_2 = 0.4 * re_loss2 + 0.3 * l1_loss2 + 0.3 * ce_loss2 + + # Total losses + total_loss = loss_1 + loss_2 + + return total_loss, {'re_loss1': re_loss1.item(), 're_loss2': re_loss2.item(), + 'l1_loss1': l1_loss1.item(), 'l1_loss2': l1_loss2.item(), + 'ce_loss1': ce_loss1.item(), 'ce_loss2': ce_loss2.item()} + + + @torch.no_grad() + def forward_all(self,all_cwt): + # Input: all_cwt shape: tensor(N x ch x 3 x L x 65) + # Output: all_cwt embedding: tensor(N x ch x L x 768) + + N,ch,_,L,F = all_cwt.shape + #print('**cwt ch: ',ch) + all_signal_pairs = torch.empty(N,0,2,3,self.max_length,F).to(self.device) + all_mask_pairs = torch.empty(N,0,2,388).to(self.device) + pair_index_record = torch.empty(N,0,2) + if ch == 1: + x_1 = all_cwt.squeeze(1) # N x 3 x L x 65 + x_1,padding_mask_1 = self.padding(x_1) + x_2 = x_1.clone() + padding_mask_2 = padding_mask_1.clone() # N x L+1 + + # print('shape of inference input sample: ',x_1.shape) # N x 3 x L x 65 + pair_signal = torch.cat((x_1.unsqueeze(1), x_2.unsqueeze(1)), dim=1) # N x 2 x 3 x L x 65 + pair_mask = torch.cat((padding_mask_1.unsqueeze(1), padding_mask_2.unsqueeze(1)), dim=1) # N x 2 x 388 + + all_signal_pairs = torch.cat((all_signal_pairs,pair_signal.unsqueeze(1)),dim=1) # N x 1 x 2 x 3 x 387 x 65 + all_mask_pairs = torch.cat((all_mask_pairs,pair_mask.unsqueeze(1)),dim=1) # N x 1 x 2 x 388 + pair_index_record = torch.cat((pair_index_record,torch.tensor([0,0]).repeat(N, 1).unsqueeze(1)),dim=1) # N x 1 x 2 + + + #print('**CH: ',ch) + for i in range(ch): + for j in range(i+1,ch): + x_1 = all_cwt[:,i,...] # N x 3 x L x 65 + x_2 = all_cwt[:,j,...] # N x 3 x L x 65 + + x_1,padding_mask_1 = self.padding(x_1) + x_2,padding_mask_2 = self.padding(x_2) + + pair_signal = torch.cat((x_1.unsqueeze(1), x_2.unsqueeze(1)), dim=1) # N x 2 x 3 x 387 x 65 + pair_mask = torch.cat((padding_mask_1.unsqueeze(1), padding_mask_2.unsqueeze(1)), dim=1) # N x 2 x 388 + + all_signal_pairs = torch.cat((all_signal_pairs,pair_signal.unsqueeze(1)),dim=1) # N x num_pairs x 2 x 3 x 387 x 65 + all_mask_pairs = torch.cat((all_mask_pairs,pair_mask.unsqueeze(1)),dim=1) # N x num_pairs x 2 x 388 + pair_index_record = torch.cat((pair_index_record,torch.tensor([i,j]).repeat(N, 1).unsqueeze(1)),dim=1) # N x num_pairs x 2 + + #print('all_signal_pairs shape: ',all_signal_pairs.shape) + N, num_pairs,_,_,L,F = all_signal_pairs.shape + # print('all signals pair shape:',all_signal_pairs.shape) + + reshaped_signal_pairs = all_signal_pairs.reshape(N * num_pairs, 2, 3, L, F) + reshaped_mask_pairs = all_mask_pairs.reshape(N * num_pairs, 2, -1) + reshaped_pair_record = pair_index_record.reshape(N * num_pairs, 2).to(self.device) + + # print('shape of reshaped_signal_pairs',reshaped_signal_pairs.shape) + # print('shape of reshaped_mask_pairs',reshaped_mask_pairs.shape) + # print('shape of reshaped_pair_record',reshaped_pair_record.shape) + + # print('Start Inference: ') + + x_1 = reshaped_signal_pairs[:,0,...].bfloat16() # (N x # of pairs) x 3 x 387 x 65 + x_2 = reshaped_signal_pairs[:,1,...].bfloat16() # (N x # of pairs) x 3 x 387 x 65 + + x_1_attn_mask = reshaped_mask_pairs[:,0,...].bfloat16() # (N x # of pairs) x 388 + x_2_attn_mask = reshaped_mask_pairs[:,1,...].bfloat16() # (N x # of pairs) x 388 + + x1_out,x2_out,_,_,_,_,_,_ = self.forward(x_1,x_2, + x1_mask=x_1_attn_mask, + x2_mask=x_2_attn_mask) + + # x1_out shape: (Nxnum_pairs) x (L+1) x E + # x2_out shape: (Nxnum_pairs) x (L+1) x E + + # print('x1_out shape: ',x1_out.shape) + # print('x2_out shape: ',x2_out.shape) + + combine_tensor = torch.cat((x1_out.unsqueeze(1),x2_out.unsqueeze(1)),dim=1) # (Nxnum_pairs) x 2 x (L+1) x E + combine_tensor = torch.cat((x1_out.unsqueeze(1),x2_out.unsqueeze(1)),dim=1) # (Nxnum_pairs) x 2 x (L+1) x E + + embed_out = [] + for i in range(0,N*num_pairs,num_pairs): + curr_batch = combine_tensor[i:i+num_pairs,...] + curr_idx_record = reshaped_pair_record[i:i+num_pairs,...] + + batch_embed = torch.empty(0,L+1,768).to(self.device) + for i in range(ch): + selected_tensor = curr_batch[curr_idx_record==i] # num x (L+1) x E + #print('shape of selected_tensor: ',selected_tensor.shape) + if selected_tensor.numel() != 0: + mean_tensor = selected_tensor.mean(dim=0) # L+1 x E + batch_embed = torch.cat((batch_embed,mean_tensor.unsqueeze(0)),dim=0) # num_ch x L+1 x E + + embed_out.append(batch_embed.unsqueeze(0)) # # N x num_ch x L+1 x 768 + + embed_out = torch.cat(embed_out, dim=0).to(self.device) # N x num_ch x L+1 x 768 + + # print('Embedding out shape',embed_out.shape) + + return embed_out + + + def padding(self,in_cwt): + N,_,L,F = in_cwt.shape + # reshape to C,L,F + max_length = self.max_length+1 + + cwt_length = L + ts_length = cwt_length+1 + + padding_mask = torch.ones(N,self.max_length+1).to(self.device) # N, 388 + + if ts_length > max_length: + in_cwt = in_cwt[:, :,-max_length+1:, :] #3,max_length,65 + + elif ts_length < max_length: + in_cwt = torch.cat((torch.zeros(N,3,max_length-1-L, F).to(self.device), in_cwt), dim=2) # 3, max_len, 65 + padding_mask[:,:self.max_length-L+1] = 0 + + return in_cwt, padding_mask + + + + def num_params(self): + total_num = sum(p.numel() for p in self.parameters()) + train_num = sum(p.numel() for p in self.parameters() if p.requires_grad) + print("Total of Parameters: {}M".format(round(total_num / 1e6, 2))) + print("Train Parameters: {}M".format(round(train_num / 1e6, 2))) + + + + +if __name__ == '__main__': + fake_x = torch.rand((3, 3, 128, 65)) + + # model = PhysioModel( + # num_layers=6, + # upper_layer=2, + # emb_size=384, + # # data related + # in_channel=65 + # ) + # out = model(fake_x, input_mask=None, exchange_tks=None) + # print("out shape:", out["embed"].shape) # (N, L+1, E = 3, 129, 384) + # print("cls shape:", out["cls"].shape) # (N, E = 3, 384) + # print("reconstruct shape:", out["rec"].shape) # (N, L+1, 1 = 3, 384, 1) \ No newline at end of file diff --git a/NormWear/baseline_models/tfc/TFC/Data_split.py b/NormWear/baseline_models/tfc/TFC/Data_split.py new file mode 100644 index 0000000..f4efccd --- /dev/null +++ b/NormWear/baseline_models/tfc/TFC/Data_split.py @@ -0,0 +1,45 @@ +"""This file aims to generate a different data split to evaluate the stability of models. +In each split, we select 30 positive and 30 negative samples to form a training set (60 samples in total). +This is an example on Epilepsy dataset. -- Xiang Zhang, Jan 16, 2023""" + +import torch +import os +import numpy as np + +targetdata_path = f"../../datasets/Epilepsy/" + +finetune_dataset = torch.load(os.path.join(targetdata_path, "test.pt")) +train_data = torch.load(os.path.join(targetdata_path, "train.pt")) +Samples = torch.cat((finetune_dataset['samples'], train_data['samples']), dim=0) +Labels = torch.cat((finetune_dataset['labels'], train_data['labels']), dim=0) + +train_size = 30 + +"""Generate balanced training set""" +id0 = Labels==0 +id1 = Labels==1 + +Samples_0, Samples_1 = Samples[id0], Samples[id1] +Labels_0, Labels_1 = Labels[id0], Labels[id1] + +Samples_train = torch.cat((Samples_0[:train_size], Samples_1[:train_size])) +Samples_test = torch.cat((Samples_0[train_size:], Samples_1[train_size:])) + +Labels_train = torch.cat((Labels_0[:train_size], Labels_1[:train_size])) +Labels_test = torch.cat((Labels_0[train_size:], Labels_1[train_size:])) + +# """Generate imbalanced training set""" +# data = list(zip(Samples, Labels)) +# np.random.shuffle(data) +# X, y = zip(*data) +# X = torch.stack(list(X), dim=0) +# y = torch.stack(list(y), dim=0) +# Samples_train, Samples_test = X[:train_size], X[train_size:] +# Labels_train, Labels_test = y[:train_size], y[train_size:] + +train_dic = {'samples':Samples_train, 'labels':Labels_train} +test_dic = {'samples':Samples_test, 'labels':Labels_test} +torch.save(train_dic, os.path.join(targetdata_path,'train.pt')) +torch.save(test_dic, os.path.join(targetdata_path,'test.pt')) + +print('Re-split finished. Dataset saved to folder:', targetdata_path) diff --git a/NormWear/baseline_models/tfc/TFC/KNN.py b/NormWear/baseline_models/tfc/TFC/KNN.py new file mode 100644 index 0000000..2acbf53 --- /dev/null +++ b/NormWear/baseline_models/tfc/TFC/KNN.py @@ -0,0 +1,42 @@ +"""It's a KNN baseline for prompt sanity check of the dataset. -- Xiang Zhang Jan 16, 2013""" + +from sklearn.metrics import roc_auc_score, classification_report, confusion_matrix, \ + average_precision_score, accuracy_score, precision_score,f1_score,recall_score +from sklearn.neighbors import KNeighborsClassifier +import torch,os +import numpy as np + + +def one_hot_encoding(X): + X = [int(x) for x in X] + n_values = np.max(X) + 1 + b = np.eye(n_values)[X] + return b + +traindata = torch.load(os.path.join('../../datasets/Epilepsy', "test.pt")) +valdata = torch.load(os.path.join('../../datasets/Epilepsy', "val.pt")) + +train_fea, train_label = traindata['samples'].detach().cpu().numpy().squeeze(1), traindata['labels'].detach().cpu().numpy() +val_fea, val_label = valdata['samples'].detach().cpu().numpy().squeeze(1), valdata['labels'].detach().cpu().numpy() + + +# train classifier: KNN +neigh = KNeighborsClassifier(n_neighbors=1) +neigh.fit(train_fea, train_label) # .detach().cpu().numpy() +knn_acc_train = neigh.score(train_fea, train_label) # .detach().cpu().numpy() +print('KNN finetune acc:', knn_acc_train) +# test the downstream classifier +# representation_test = emb_test.detach().cpu().numpy() +knn_result = neigh.predict(val_fea) +knn_result_score = neigh.predict_proba(val_fea) +one_hot_label_test = one_hot_encoding(val_label) +print(classification_report(val_label, knn_result, digits=4)) +print(confusion_matrix(val_label, knn_result)) +knn_acc = accuracy_score(val_label, knn_result) +precision = precision_score(val_label, knn_result, average='macro', ) +recall = recall_score(val_label, knn_result, average='macro', ) +F1 = f1_score(val_label, knn_result, average='macro') +auc = roc_auc_score(knn_result_score, one_hot_label_test, average="macro", multi_class="ovr") +prc = average_precision_score(knn_result_score, one_hot_label_test, average="macro") +print("KNN Train Acc:{}. '\n' Test: acc {}, precision {}, Recall {}, F1 {}, AUROC {}, AUPRC {}" + "".format(knn_acc_train, knn_acc, precision, recall, F1, auc, prc)) \ No newline at end of file diff --git a/NormWear/baseline_models/tfc/TFC/__pycache__/model.cpython-311.pyc b/NormWear/baseline_models/tfc/TFC/__pycache__/model.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0e14343f24d9105c9d4bde7051b3828cae74b692 GIT binary patch literal 3975 zcmdT{Z)h7w7N6N&t=6_|S#jdpwcW&ZVyZMaQR0$Z+AE=HlMD5k()=kEf<@7;WVtKJ zo?Z76vjKkyR~@)eN*kNQaDw)74(W${I=JhX``C}WW{b^&A<(AaZwg7^xG#6_?MfT1 z?S_`Sez@`K&CHuO@6EiK_j}{Nbau8AD8Hxo3Rl_)`3^hH5@|FZ%|l~}C`6$eDbX~g z*yc2n#+ui-5})QvLRz4tjd0{VQG}aBiKukCse#<)3AqbDzof;6oCtE!XUIhr?jlJ_ zTR1IAU9Mc7Omgm?ODtQ~^A%Q7*<0mYMNygi+)my^_PWY)4U=BQH9C(DLgORYl1kE4 zA!)8k@>HQWr8KW_H%VHkl4~>_0n@z8UpjX-*PMv+!Ak^p^5_ =bc1|rD#jNIv z6UDNcWp2kCStBC9yvW@WAywGT6I zs`>{;WhV2(t$Po?{`(KNKKlOazdrc#ZwFpFp3LC#--F#`u4Z8&urav=K%?myBcICv zCS<(s*vxen2fgz#V2w3>hu2yk0T2Ikfow{hALl+9x1~Nu>RXqF*Q8-v8gZl%OB$(5 zWblY3^*tCkZk;%5UA|(ySFkS^tjdfvpxXnwGoZuh=Ao0;so#7vY#*9%4oy@AM|$zW zz|hLCH)CCk7p&gVRo;#rabicXKe>Fuj_r41`>}KW4;L0Mcv7F_n*4`tk-_%I1PChX zd(uS)PlRljE7riQJuvGG%>Kf*K|Oqrz*i&}qz35Vo$~-z0S_(_lhlB*vzRmtJ~w!i zuLV%lDsS@B0rV=gKNwQDFy!12+R$3-S^`;9*k&!_$p!!w2tvc;w#ka7_#-P5f8xrb zp0xnXL)N0(tVtn=4WQnPm{Oio_^-ABEyBfo9HQbtOA2M^u&sYX*+Q2>?&SeUk`n8N zo*QMVK9!wO0XeW@`+&)MkvxaP5eW0#lq=>c $v;k%F9T; dpy$41v;qqPt1 z*q9R=tJ00co~8az`<4dSNpe9m~D@etf2A`YAf0sz$fs|)>+_Bz>xoa&Ce zQYX(M-g-UFIZzsuW>o#0_f*2jvVv;(_*~2v;fdB%gL*Nbyx{)^8vpy(RqNYf8f|$+ zq1&M#^l}P92wqN4=Wr&SMn8Kc3?wi+nB(PRev;u+W9tkM2%;{i*hwUqJG?~7_CU)O zwMwCA=