-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
157 lines (129 loc) · 5.45 KB
/
preprocess.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
import numpy as np
import cv2
from glob import glob
import fnmatch
from sklearn.model_selection import train_test_split
import random
from sklearn.decomposition import IncrementalPCA
import gc
import h5py
from tqdm import tqdm
import os
from utils import *
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from numpy.random import *
from numpy.linalg import *
data_dir = "C:/Users/Burak/PycharmProjects/mammography/data/mass_calc/"
def get_PCA_data():
print("Loading data...")
X = np.load("X.npy", mmap_mode='r')
#Y = np.load("Y.npy", mmap_mode='r')
n = X.shape[0] # how many rows we have in the dataset
X = X.flatten().reshape(n, 7500)
chunk_size = 50 # how many rows we feed to IPCA at a time, the divisor of n
ipca = IncrementalPCA(n_components=50, batch_size=16)
for i in range(0, n // chunk_size):
ipca.partial_fit(X[i * chunk_size: (i + 1) * chunk_size])
print("Fitted.")
X = ipca.transform(X)
print("Transformed.")
np.save('X.npy', X)
#fit but transform to images one by one and save them to folders!
def do_incremental_pca(batch=20, components=50, path="data/X.h5", target="data_batches"):
h5 = h5py.File(path, 'r')
data = h5['data']
i = 0
n = data.shape[0] # total size
batch_size = batch # batch size
ipca = IncrementalPCA(n_components=components, batch_size=batch)
print("Fitting initialized...")
for i in tqdm(range(0, n // batch_size)):
ipca.partial_fit(data[i * batch_size: (i + 1) * batch_size])
print("Trasformation initialized...")
for i in tqdm(range(0, n // batch_size)):
X_ipca = ipca.transform(data[i * batch_size: (i + 1) * batch_size])
np.save(target + "/X" + str(i) + ".npy", X_ipca)
#np.save('Y.npy', Y)
def do_incremental_pca_on_test(batch=20, components=50, path="data/X.h5", target="data_batches"):
h5 = h5py.File(path, 'r')
data = h5['data']
i = 0
n = data.shape[0] # total size
batch_size = batch # batch size
ipca = IncrementalPCA(n_components=components, batch_size=batch)
print("Fitting initialized...")
for i in tqdm(range(0, n // batch_size)):
ipca.partial_fit(data[i * batch_size: (i + 1) * batch_size])
del data
testdata = np.load(data_folder + "/X_test.npy")
testshape = testdata.shape
n = testdata.shape[0]
print(testshape)
testdata = testdata.flatten().reshape(testshape[0], testshape[1] * testshape[2] * testshape[3])
print("Test trasformation initialized...")
for i in tqdm(range(0, n // batch_size)):
X_ipca = ipca.transform(testdata[i * batch_size: (i + 1) * batch_size])
np.save(target + "/X_test" + str(i) + ".npy", X_ipca)
del testdata
valdata = np.load(data_folder + "/X_val.npy")
valshape = valdata.shape
n = valdata.shape[0]
print(valshape)
valdata = valdata.flatten().reshape(valshape[0], valshape[1] * valshape[2] * valshape[3])
print("Test trasformation initialized...")
for i in tqdm(range(0, n // batch_size)):
X_ipca = ipca.transform(valdata[i * batch_size: (i + 1) * batch_size])
np.save(target + "/X_val" + str(i) + ".npy", X_ipca)
def concat_data(folder="data_batches", target="data/X_train.npy", count=0):
arrays = []
i = 0
for i in tqdm(range(count)):
X_add = np.load(folder + "/X_val" + str(i) + ".npy")
arrays.append(X_add)
print(X_add.shape)
x = np.concatenate(arrays)
np.save(target, x)
print(x.shape)
def npy_to_npy_batches(size = 600):
data = np.load(data_folder + "/X_train.npy")
i = 0
n = data.shape[0] # total size
batch_size = 600 # batch size
for i in tqdm(range(0, n // batch_size)):
xpart = (data[i * batch_size: (i + 1) * batch_size])
xpart = xpart.flatten().reshape(batch_size, 128*128*3)
np.save("data_batches/X" + str(i) + ".npy", xpart)
def apply_lda(components=1, path="data/X_train.h5", target="data/lda/"):
X = np.load(data_dir + "X_train.npy", mmap_mode='r').astype('float32')
X = X.flatten().reshape(2860, 128*128*3)
# X, Y = get_data()
Y = np.load(data_dir + "Y_train.npy")
print(X.shape)
lda = LinearDiscriminantAnalysis(solver='svd', n_components=components)
lda.fit(X, Y)
X_train = lda.transform(X)
np.save(target + 'X_train.npy', X_train)
np.save(target + 'Y_train.npy', Y)
del X, Y
X_val = np.load(data_dir + "X_val.npy", mmap_mode='r').astype('float32')
Y_val = np.load(data_dir + "Y_val.npy")
Xvalshape = X_val.shape
X_val = X_val.flatten().reshape(Xvalshape[0], Xvalshape[1]*Xvalshape[2]*Xvalshape[3])
X_val = lda.transform(X_val)
np.save(target + 'X_val.npy', X_val)
np.save(target + 'Y_val.npy', Y_val)
del X_val, Y_val
X_test = np.load(data_dir + "X_test.npy", mmap_mode='r').astype('float32')
Y_test = np.load(data_dir + "Y_test.npy")
Xtestshape = X_test.shape
X_test = X_test.flatten().reshape(Xtestshape[0], Xtestshape[1]*Xtestshape[2]*Xtestshape[3])
X_test = lda.transform(X_test)
np.save(target + 'X_test.npy', X_test)
np.save(target + 'Y_test.npy', Y_test)
apply_lda()
# npy_to_npy_batches()
# npy_to_h5(path='/X_train.npy')
# do_incremental_pca(batch=100, components=100, path="data/X_train.h5", target="data_batches/pca100")
# concat_data(folder="data_batches/pca100")
#do_incremental_pca_on_test(batch=100, components=100, path="data/X_train.h5", target="data_batches/pca100/test")
#concat_data(folder="data_batches/pca100/test", target="data/X_test", count=5)