-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipca.py
195 lines (153 loc) · 6.78 KB
/
ipca.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
import os
import sys
import pandas as pd
import numpy as np
import json
import time
import seaborn as sns
import clip
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import pickle
from mpl_toolkits.mplot3d import Axes3D
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from PCAonGPU.gpu_pca import IncrementalPCAonGPU
def mean_iou(ground_truth, prediction, num_classes):
iou_list = []
for cls in range(num_classes):
# Create binary masks for the current class
gt_mask = (ground_truth == cls)
pred_mask = (prediction == cls)
# Calculate intersection and union
intersection = np.logical_and(gt_mask, pred_mask).sum()
union = np.logical_or(gt_mask, pred_mask).sum()
if union == 0:
# Avoid division by zero, consider IoU for this class as 1 if both are empty
iou = 1 if intersection == 0 else 0
else:
iou = intersection / union
iou_list.append(iou)
# Calculate mean IoU
mean_iou = np.mean(iou_list)
return mean_iou
def write_miou(ground_truth, prediction, num_classes, file_path):
with open(file_path, mode='a') as file:
miou = mean_iou(ground_truth, prediction, num_classes)
file.write(f"{miou}\n")
def get_sim_mat(batch_size, bp_data, label_features):
num_batches = (bp_data.size(0) + batch_size - 1) // batch_size
similarity_matrices = []
for i in range(num_batches):
start_idx = i * batch_size
end_idx = min(start_idx + batch_size, bp_data.size(0))
batch_bp_data = bp_data[start_idx:end_idx]
similarity_matrix = F.cosine_similarity(batch_bp_data.unsqueeze(1), label_features.unsqueeze(0), dim=2)
similarity_matrices.append(similarity_matrix)
similarity_matrix = torch.cat(similarity_matrices, dim=0)
print("similarity_matrix.shape: ", similarity_matrix.shape)
return similarity_matrix
def get_flattened_data(lseg_dir, npy_file, device):
data = np.load(os.path.join(lseg_dir, npy_file))
print("data.shape: ", data.shape)
data = data.squeeze(0)
print("data.shape: ", data.shape)
flattened_data = data.reshape(data.shape[0], -1)
print("flattened_data.shape: ", flattened_data.shape)
flattened_data = flattened_data.T
print("flattened_data.shape: ", flattened_data.shape)
flattened_data = torch.tensor(flattened_data).to(device)
return flattened_data
def main():
device = "cuda" if torch.cuda.is_available() else "cpu"
#device = "cpu"
print("device: ", device)
model, _ = clip.load("ViT-B/32", device)
# initialize ipca
target_dimension = 128
if len(sys.argv) > 1:
target_dimension = int(sys.argv[1])
print(f"Received argument: {target_dimension}")
else:
print("No arguments provided")
ipca = IncrementalPCAonGPU(n_components=target_dimension)
# path of data
data_dir = '/workspace/sdh1/vlmaps_data_dir/vlmaps_dataset'
data_dir2 = '/workspace/sdb1/vlmaps_data_dir/vlmaps_dataset'
sequence_name = 'gTV8FGcVJC9_1'
#sequence_dir = os.path.join(data_dir, sequence_name)
lseg_dir = os.path.join(data_dir2, f'{sequence_name}/lseg_feature')
#pred_dir = os.path.join(data_dir, f'{sequence_name}/lseg_pred')
npy_files = sorted([f for f in os.listdir(lseg_dir) if f.endswith('.npy')])
pca_feature_dir = os.path.join(data_dir, f'{sequence_name}/ipca_feature_{target_dimension}')
pca_pred_dir = os.path.join(data_dir, f'{sequence_name}/ipca_pred_{target_dimension}')
pca_miou_dir = os.path.join(data_dir, f'{sequence_name}/ipca_miou_{target_dimension}')
pca_save_dir = os.path.join(data_dir, f'{sequence_name}/ipca_{target_dimension}')
pca_save_path = os.path.join(pca_save_dir, 'ipca.pkl')
os.makedirs(pca_feature_dir, exist_ok=True)
os.makedirs(pca_pred_dir, exist_ok=True)
os.makedirs(pca_miou_dir, exist_ok=True)
os.makedirs(pca_save_dir, exist_ok=True)
# initialize labels
labels = ["void","wall","floor","chair",
"door","table","picture","cabinet",
"cushion","window","sofa","bed",
"curtain","chest_of_drawers","plant",
"sink","stairs","ceiling","toilet",
"stool","towel","mirror","tv_monitor",
"shower","column","bathtub","counter",
"fireplace","lighting","beam","railing",
"shelving","blinds","gym_equipment",
"seating","board_panel","furniture",
"appliances","clothes","objects",]
print("len(labels): ", len(labels))
label_token = clip.tokenize(labels).to(device)
print("label_token.shape: ", label_token.shape)
with torch.no_grad():
label_features = model.encode_text(label_token)
print("label_features.shape: ", label_features.shape)
h = 720
w = 1080
for i, npy_file in enumerate(npy_files):
print(i, " start")
flattened_data = get_flattened_data(lseg_dir, npy_file, device)
ipca.partial_fit(flattened_data)
print("partial fit ", i)
with open(pca_save_path, 'wb') as file:
pickle.dump(ipca, file)
print("IPCA model saved.")
for i, npy_file in enumerate(npy_files):
feature_path = os.path.join(pca_feature_dir, f'{i:06d}.pt')
pred_path = os.path.join(pca_pred_dir, f'{i:06d}.pt')
print(i, "start")
flattened_data = get_flattened_data(lseg_dir, npy_file, device)
pca_data = ipca.transform(flattened_data)
# back project to 512 dimension
bp_data = ipca.inverse_transform(pca_data)
print('bp_data.shape: ', bp_data.shape)
# save pca features
pca_data = pca_data.transpose(0, 1)
print('pca_data.shape: ', pca_data.shape)
pca_data = pca_data.reshape(pca_data.shape[0], h, w)
print('pca_data.shape: ', pca_data.shape)
pca_data = pca_data.unsqueeze(0)
print('pca_data.shape: ', pca_data.shape)
if not os.path.exists(feature_path):
torch.save(pca_data, feature_path)
print("save pca_data")
similarity_matrix = get_sim_mat(1000, bp_data, label_features)
print("similarity_matrix.shape: ", similarity_matrix.shape)
prediction_probs = F.softmax(similarity_matrix, dim=1)
print("prediction_probs.shape: ", prediction_probs.shape)
predictions = torch.argmax(prediction_probs, dim=1)
print("predictions.shape: ", predictions.shape)
predictions = predictions.reshape(h, w)
print("predictions.shape: ", predictions.shape)
# save pca predictions
if not os.path.exists(pred_path):
torch.save(predictions, pred_path)
print("save predictions")
print(i, "finished")
if __name__ == "__main__":
main()