forked from jadie1/BVIB-DeepSSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SS_loaders.py
291 lines (278 loc) · 10.1 KB
/
SS_loaders.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
# Jadie Adams
import os
import argparse
import nrrd
import json
import random
import numpy as np
from sklearn.decomposition import PCA
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
'''
Main function - creates train, validation, and test loaders
Input:
data_dir - directory containing "points" and "images" folders
loader_dir - directory to save the loaders
eval_size - size for validation and test sets
batch_size - size of training batches
'''
def get_loaders(data_dir, loader_dir="loaders", size=1000, eval_size=100, batch_size=6):
# Create loader directory
if not os.path.exists(loader_dir):
os.makedirs(loader_dir)
# Split data
point_files = sorted(os.listdir(data_dir + 'points/'))
train_point_files = point_files[:-2*eval_size]
train_point_files = train_point_files[:size] # resize
val_point_files = point_files[-2*eval_size:-eval_size]
test_point_files = point_files[-eval_size:]
# Train set
print("Creating training set:")
img_dir = data_dir + 'images/'
train_points = get_points(train_point_files, point_dir=data_dir + 'points/')
train_images = get_images(train_point_files, img_dir, loader_dir)
train_pca, train_embedder = get_pca(train_points, loader_dir)
train_data = DeepSSMdataset(train_images, train_points, train_pca, train_point_files)
train_loader = DataLoader(
train_data,
batch_size=batch_size,
shuffle=True ) #,
# num_workers=4,
# persistent_workers=True,
# pin_memory=torch.cuda.is_available()
# )
torch.save(train_loader, loader_dir + 'train')
print("Saved train loader of size " + str(len(train_data)) + ".\n")
# Val set
print("Creating validation set:")
val_points = get_points(val_point_files, point_dir=data_dir + 'points/')
val_images = get_images(val_point_files, img_dir, loader_dir)
val_pca, _ = get_pca(val_points, loader_dir, train_embedder)
val_data = DeepSSMdataset(val_images, val_points, val_pca, val_point_files)
val_loader = DataLoader(
val_data,
batch_size=1,
shuffle=False) #,
# num_workers=4,
# persistent_workers=True,
# pin_memory=torch.cuda.is_available()
# )
torch.save(val_loader, loader_dir + 'validation')
print("Saved validation loader of size " + str(len(val_data)) + ".\n")
# Test set
print("Creating test set:")
test_points = get_points(test_point_files, point_dir=data_dir + 'points/')
test_images = get_images(test_point_files, img_dir, loader_dir)
test_pca, _ = get_pca(test_points, loader_dir, train_embedder)
test_data = DeepSSMdataset(test_images, test_points, test_pca, test_point_files)
test_loader = DataLoader(
test_data,
batch_size=1,
shuffle=False) #,
# num_workers=4,
# persistent_workers=True,
# pin_memory=torch.cuda.is_available()
# )
torch.save(test_loader, loader_dir + 'test')
print("Saved test loader of size " + str(len(test_data)) + ".\n")
'''
Reads .particle files and returns numpy array
'''
def get_points(point_files, point_dir=''):
points = []
for point_file in point_files:
f = open(point_dir + point_file, "r")
data = []
for line in f.readlines():
pts = line.replace(' \n','').split(" ")
pts = [float(i) for i in pts]
data.append(pts)
points.append(data)
return np.array(points)
'''
Reads .nrrd files and returns numpy array
Whitens/normalizes images
'''
def get_images(point_files, img_dir, loader_dir):
# get all images
all_images = []
for point_file in point_files:
image_file = img_dir + point_file.replace(".particles", ".nrrd")
img, header = nrrd.read(image_file)
all_images.append(img)
all_images = np.array(all_images)
# get mean and std
mean_path = loader_dir + 'mean_img.npy'
std_path = loader_dir + 'std_img.npy'
if not os.path.exists(mean_path) or not os.path.exists(std_path):
mean_image = np.mean(all_images)
std_image = np.std(all_images)
np.save(mean_path, mean_image)
np.save(std_path, std_image)
else:
mean_image = np.load(mean_path)
std_image = np.load(std_path)
# normalize
norm_images = []
for image in all_images:
norm_images.append([(image-mean_image)/std_image])
return np.array(norm_images)
'''
Performs PCA on point matrix and returns embedded matrix
Embedding prexerves 99% of the variability
'''
def get_pca(point_matrix, loader_dir, point_embedder=None):
if not point_embedder:
point_embedder = PCA_Embbeder(point_matrix)
embedded_matrix = point_embedder.run_PCA()
point_embedder.write_PCA(loader_dir + "/PCA_Particle_Info/", "particles")
else:
embedded_matrix = point_embedder.getEmbeddedMatrix(point_matrix)
reconstructed = point_embedder.project(embedded_matrix)
print("Reconstruction MSE: " + str(np.mean((point_matrix-reconstructed)**2)))
return embedded_matrix, point_embedder
'''
Class for PCA Embedding
'''
class PCA_Embbeder():
# overriding abstract methods
def __init__(self, data_matrix, num_dim=0, percent_variability=0.999):
self.data_matrix = data_matrix
self.num_dim=num_dim
self.percent_variability = percent_variability
# run PCA on data_matrix for PCA_Embedder
def run_PCA(self):
# get covariance matrix (uses compact trick)
N = self.data_matrix.shape[0]
data_matrix_2d = self.data_matrix.reshape(self.data_matrix.shape[0], -1).T # flatten data instances and transpose
mean = np.mean(data_matrix_2d, axis=1)
centered_data_matrix_2d = (data_matrix_2d.T - mean).T
trick_cov_matrix = np.dot(centered_data_matrix_2d.T,centered_data_matrix_2d) * 1.0/np.sqrt(N-1)
# get eignevectors and eigenvalues
eigen_values, eigen_vectors = np.linalg.eigh(trick_cov_matrix)
eigen_vectors = np.dot(centered_data_matrix_2d, eigen_vectors)
for i in range(N):
eigen_vectors[:,i] = eigen_vectors[:,i]/np.linalg.norm(eigen_vectors[:,i])
eigen_values = np.flip(eigen_values)
eigen_vectors = np.flip(eigen_vectors, 1)
if self.num_dim == 0:
cumDst = np.cumsum(eigen_values) / np.sum(eigen_values)
num_dim = np.where(cumDst > float(self.percent_variability))[0][0] + 1
W = eigen_vectors[:, :num_dim]
PCA_scores = np.matmul(centered_data_matrix_2d.T, W)
print("The PCA modes of particles being retained : " + str(num_dim))
print("Variablity preserved: " + str(float(cumDst[num_dim-1])))
self.mean=mean
self.num_dim = num_dim
self.PCA_scores = PCA_scores
self.cumDst = np.cumsum(eigen_values) / np.sum(eigen_values)
self.eigen_vectors = eigen_vectors
self.eigen_values = eigen_values
return PCA_scores
# write PCA info to files
def write_PCA(self, out_dir, suffix):
if not os.path.exists(out_dir):
os.makedirs(out_dir)
np.save(out_dir + 'original_PCA_scores.npy', self.PCA_scores)
mean = np.mean(self.data_matrix, axis=0)
np.savetxt(out_dir + 'mean.' + suffix, mean)
np.savetxt(out_dir + 'eigenvalues.txt', self.eigen_values)
for i in range(self.data_matrix.shape[0]):
nm = out_dir + 'pcamode' + str(i) + '.' + suffix
data = self.eigen_vectors[:, i]
data = data.reshape(self.data_matrix.shape[1:])
np.savetxt(nm, data)
np.savetxt(out_dir + 'cummulative_variance.txt', self.cumDst)
# returns embedded form of dtat_matrix
def getEmbeddedMatrix(self, data_matrix):
N = data_matrix.shape[0]
data_matrix_2d = data_matrix.reshape(data_matrix.shape[0], -1).T # flatten data instances and transpose
centered_data_matrix_2d = (data_matrix_2d.T - self.mean).T
W = self.eigen_vectors[:, :self.num_dim]
PCA_scores = np.matmul(centered_data_matrix_2d.T, W)
return PCA_scores
# projects embbed array into data
def project(self, PCA_instance):
W = self.eigen_vectors[:, :self.num_dim].T
mean = np.mean(self.data_matrix, axis=0)
data_instance = np.matmul(PCA_instance, W) + mean.reshape(-1)
data_instance = data_instance.reshape((-1,self.data_matrix.shape[1], self.data_matrix.shape[2]))
return data_instance
'''
Class for DeepSSM datasets that works with Pytorch DataLoader
'''
class DeepSSMdataset():
def __init__(self, img, mdl_target, pca_target, filenames):
# img = img[:100]
# mdl_target = mdl_target[:100]
# pca_target = pca_target[:100]
# filenames = filenames[:100]
self.img = torch.FloatTensor(img)
self.pca_target = torch.FloatTensor(pca_target)
self.mdl_target = torch.FloatTensor(mdl_target)
self.names = [filename.split(".")[0] for filename in filenames]
def __getitem__(self, index):
x = self.img[index]
y1 = self.pca_target[index]
y2 = self.mdl_target[index]
name = self.names[index]
return x, y1, y2, name
def __len__(self):
return len(self.names)
'''
Creates a JSON files of image names and outlier degrees
'''
def get_image_outlier_degrees(data_dir, loader_dir):
# Create loader directory
if not os.path.exists(loader_dir):
os.makedirs(loader_dir)
out_file = loader_dir + 'image_outlier_degrees.json'
img_dir = data_dir + 'images/'
imgs = []
print("Getting images...")
names = []
for image_file in sorted(os.listdir(img_dir)):
img, header = nrrd.read(img_dir + image_file)
imgs.append(img.flatten())
names.append(image_file.replace(".nrrd", ""))
imgs = np.array(imgs)
print("Running PCA...")
imgs_pca = PCA(0.95)
imgs_pca.fit(imgs[:800])
scores = imgs_pca.transform(imgs)
reconstructed = imgs_pca.inverse_transform(scores)
print("Getting values...")
dists = []
for index in range(imgs.shape[0]):
dists.append(getMahalanobisDist(scores[index], scores))
recons = ((imgs - reconstructed)**2).sum(axis=1)
recons = (recons - np.mean(recons))/np.std(recons)
dists = (dists - np.mean(dists))/np.std(dists)
values = np.abs(recons) + np.abs(dists)
values_dict={}
for i in range(len(names)):
values_dict[names[i]] = values[i]
write_json(values_dict, out_file)
print("Done.")
'''
Returns Mahalanobis distance
'''
def getMahalanobisDist(score, scores):
nearest_neighbor_dists = []
cov = np.cov(scores.T)
temp = score - np.mean(scores)
dist = np.dot(np.dot(temp, np.linalg.inv(cov)), temp.T)
return dist
'''
Write json file
'''
def write_json(data_dict, out_file):
with open(out_file, "w") as outfile:
json.dump(data_dict, outfile, indent=2)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create data loaders')
parser.add_argument('-s', '--size', help='Size of training dataset (1000 or less)', default=1000, type=int)
parser.add_argument('-bs', '--batch_size', help='Training batch size', default=4, type=int)
arg = parser.parse_args()
get_loaders('SS_data/', 'SS_loaders/', size=arg.size, batch_size=arg.batch_size)