Upload downstream.py
Browse files- downstream.py +146 -145
downstream.py
CHANGED
@@ -1,146 +1,147 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""
|
3 |
-
Created on Fri Jan 10 11:11:58 2025
|
4 |
-
|
5 |
-
This script evaluates downstream task performance by comparing models trained
|
6 |
-
on raw channel representations versus those trained on LWM embeddings.
|
7 |
-
|
8 |
-
@author: Sadjad Alikhani
|
9 |
-
"""
|
10 |
-
#%% IMPORT PACKAGES & MODULES
|
11 |
-
from input_preprocess import tokenizer, scenarios_list
|
12 |
-
from inference import lwm_inference
|
13 |
-
from utils import prepare_loaders
|
14 |
-
from train import finetune
|
15 |
-
import lwm_model
|
16 |
-
import matplotlib.pyplot as plt
|
17 |
-
import numpy as np
|
18 |
-
import torch
|
19 |
-
import torch.nn as nn
|
20 |
-
import warnings
|
21 |
-
warnings.filterwarnings("ignore", category=UserWarning)
|
22 |
-
#%% DOWNSTERAM DATA GENERATION
|
23 |
-
n_beams = 16
|
24 |
-
task = ['Beam Prediction', 'LoS/NLoS Classification'][1]
|
25 |
-
task_type = ["classification", "regression"][0]
|
26 |
-
visualization_method = ["pca", "umap", "tsne"][2]
|
27 |
-
input_types = ["cls_emb", "channel_emb", "raw"]
|
28 |
-
train_ratios = [.001, .01, .05, .1, .25, .5, .8]
|
29 |
-
fine_tuning_status = [None, ["layers.8", "layers.9", "layers.10", "layers.11"], "full"]
|
30 |
-
selected_scenario_names = [scenarios_list()[6]]
|
31 |
-
preprocessed_data, labels, raw_chs = tokenizer(
|
32 |
-
selected_scenario_names,
|
33 |
-
bs_idxs=[3],
|
34 |
-
load_data=False,
|
35 |
-
task=task,
|
36 |
-
n_beams=n_beams
|
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 |
-
print(f"
|
72 |
-
print(f"
|
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 |
-
plt.
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
plt.
|
130 |
-
plt.
|
131 |
-
plt.
|
132 |
-
plt.
|
133 |
-
plt.
|
134 |
-
plt.
|
135 |
-
plt.
|
136 |
-
plt.
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
146 |
visualization_method=visualization_method)
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Fri Jan 10 11:11:58 2025
|
4 |
+
|
5 |
+
This script evaluates downstream task performance by comparing models trained
|
6 |
+
on raw channel representations versus those trained on LWM embeddings.
|
7 |
+
|
8 |
+
@author: Sadjad Alikhani
|
9 |
+
"""
|
10 |
+
#%% IMPORT PACKAGES & MODULES
|
11 |
+
from input_preprocess import tokenizer, scenarios_list
|
12 |
+
from inference import lwm_inference
|
13 |
+
from utils import prepare_loaders
|
14 |
+
from train import finetune
|
15 |
+
import lwm_model
|
16 |
+
import matplotlib.pyplot as plt
|
17 |
+
import numpy as np
|
18 |
+
import torch
|
19 |
+
import torch.nn as nn
|
20 |
+
import warnings
|
21 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
22 |
+
#%% DOWNSTERAM DATA GENERATION
|
23 |
+
n_beams = 16
|
24 |
+
task = ['Beam Prediction', 'LoS/NLoS Classification'][1]
|
25 |
+
task_type = ["classification", "regression"][0]
|
26 |
+
visualization_method = ["pca", "umap", "tsne"][2]
|
27 |
+
input_types = ["cls_emb", "channel_emb", "raw"]
|
28 |
+
train_ratios = [.001, .01, .05, .1, .25, .5, .8]
|
29 |
+
fine_tuning_status = [None, ["layers.8", "layers.9", "layers.10", "layers.11"], "full"]
|
30 |
+
selected_scenario_names = [scenarios_list()[6]]
|
31 |
+
preprocessed_data, labels, raw_chs = tokenizer(
|
32 |
+
selected_scenario_names,
|
33 |
+
bs_idxs=[3],
|
34 |
+
load_data=False,
|
35 |
+
task=task,
|
36 |
+
n_beams=n_beams,
|
37 |
+
manual_data=None)
|
38 |
+
#%% LOAD THE MODEL
|
39 |
+
gpu_ids = [0]
|
40 |
+
device = torch.device("cuda:0")
|
41 |
+
model = lwm_model.lwm().to(device)
|
42 |
+
|
43 |
+
model_name = "model.pth"
|
44 |
+
state_dict = torch.load(f"models/{model_name}", map_location=device)
|
45 |
+
new_state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()}
|
46 |
+
model.load_state_dict(new_state_dict)
|
47 |
+
|
48 |
+
model = nn.DataParallel(model, gpu_ids)
|
49 |
+
print(f"Model loaded successfully on GPU {device.index}")
|
50 |
+
#%% 2D EMBEDDING SPACE VISUALIZATIONN BEFORE FINE-TUNING
|
51 |
+
chs = lwm_inference(
|
52 |
+
model,
|
53 |
+
preprocessed_data,
|
54 |
+
input_type="cls_emb",
|
55 |
+
device=device,
|
56 |
+
batch_size=64,
|
57 |
+
visualization=False,
|
58 |
+
labels=labels,
|
59 |
+
visualization_method=visualization_method)
|
60 |
+
#%% FINE-TUNE
|
61 |
+
results = np.zeros((len(fine_tuning_status), len(input_types), len(train_ratios)))
|
62 |
+
for fine_tuning_stat_idx, fine_tuning_stat in enumerate(fine_tuning_status):
|
63 |
+
for input_type_idx, input_type in enumerate(input_types):
|
64 |
+
|
65 |
+
if input_type == "raw" and fine_tuning_stat is not None:
|
66 |
+
continue
|
67 |
+
|
68 |
+
selected_patches_idxs = None
|
69 |
+
for train_ratio_idx, train_ratio in enumerate(train_ratios):
|
70 |
+
|
71 |
+
print(f"\nfine-tuning status: {fine_tuning_stat}")
|
72 |
+
print(f"input type: {input_type}")
|
73 |
+
print(f"train ratio: {train_ratio}\n")
|
74 |
+
|
75 |
+
# PREPARE LOADERS
|
76 |
+
train_loader, val_loader, samples, target = prepare_loaders(
|
77 |
+
preprocessed_data=preprocessed_data,
|
78 |
+
labels=labels,
|
79 |
+
selected_patches_idxs=selected_patches_idxs,
|
80 |
+
input_type=input_type,
|
81 |
+
task_type=task_type,
|
82 |
+
train_ratio=train_ratio,
|
83 |
+
batch_size=128,
|
84 |
+
seed=42
|
85 |
+
)
|
86 |
+
|
87 |
+
# FINE-TUNE LWM
|
88 |
+
fine_tuned_model, best_model_path, train_losses, val_losses, f1_scores, attn_maps_ft = finetune(
|
89 |
+
base_model=model,
|
90 |
+
train_loader=train_loader,
|
91 |
+
val_loader=val_loader,
|
92 |
+
task_type=task_type,
|
93 |
+
input_type=input_type,
|
94 |
+
num_classes=n_beams if task=='Beam Prediction' else 2 if task=='LoS/NLoS Classification' else None,
|
95 |
+
output_dim=target.shape[-1] if task_type =='regression' else None,
|
96 |
+
use_custom_head=True,
|
97 |
+
fine_tune_layers=fine_tuning_stat,
|
98 |
+
optimizer_config={"lr": 1e-3},
|
99 |
+
epochs=15,
|
100 |
+
device=device,
|
101 |
+
task=task
|
102 |
+
)
|
103 |
+
|
104 |
+
results[fine_tuning_stat_idx][input_type_idx][train_ratio_idx] = f1_scores[-1]
|
105 |
+
|
106 |
+
markers = ['o', 's', 'D']
|
107 |
+
labels = ['CLS Emb', 'CHS Emb', 'Raw']
|
108 |
+
fine_tuning_status_labels = ['No FT', 'Partial FT', 'Full FT']
|
109 |
+
line_styles = ['-', '--', ':']
|
110 |
+
colors = plt.cm.viridis(np.linspace(0, 0.8, len(labels)))
|
111 |
+
plt.figure(figsize=(12, 8), dpi=500)
|
112 |
+
for ft_idx, (ft_status_label, line_style) in enumerate(zip(fine_tuning_status_labels, line_styles)):
|
113 |
+
for idx, (marker, label, color) in enumerate(zip(markers, labels, colors)):
|
114 |
+
# For "Raw Channels," only plot "No Fine-Tuning" case
|
115 |
+
if label == "Raw" and ft_status_label != "No FT":
|
116 |
+
continue
|
117 |
+
# Simplify label for "Raw Channels" without fine-tuning
|
118 |
+
plot_label = label if label != "Raw Channels" or ft_status_label != "No Fine-Tuning" else "Raw Channels"
|
119 |
+
plt.plot(
|
120 |
+
train_ratios,
|
121 |
+
results[ft_idx, idx],
|
122 |
+
marker=marker,
|
123 |
+
linestyle=line_style,
|
124 |
+
label=f"{plot_label} ({ft_status_label})" if label != "Raw Channels" else plot_label,
|
125 |
+
color=color,
|
126 |
+
linewidth=3,
|
127 |
+
markersize=9
|
128 |
+
)
|
129 |
+
plt.xscale('log')
|
130 |
+
plt.xlabel("Train Ratio", fontsize=20)
|
131 |
+
plt.ylabel("F1-Score", fontsize=20)
|
132 |
+
plt.legend(fontsize=17, loc="best")
|
133 |
+
plt.grid(True, linestyle="--", alpha=0.7)
|
134 |
+
plt.xticks(fontsize=17)
|
135 |
+
plt.yticks(fontsize=17)
|
136 |
+
plt.tight_layout()
|
137 |
+
plt.show()
|
138 |
+
#%% 2D EMBEDDING SPACE VISUALIZATIONN AFTER FINE-TUNING
|
139 |
+
chs = lwm_inference(
|
140 |
+
fine_tuned_model.model,
|
141 |
+
preprocessed_data,
|
142 |
+
input_type="cls_emb",
|
143 |
+
device=device,
|
144 |
+
batch_size=64,
|
145 |
+
visualization=False,
|
146 |
+
labels=labels,
|
147 |
visualization_method=visualization_method)
|