-
Notifications
You must be signed in to change notification settings - Fork 6
/
create_pca_dl_feats.py
175 lines (149 loc) · 5.84 KB
/
create_pca_dl_feats.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
import argparse
import pandas as pd
import numpy as np
from tqdm import tqdm
import os
import pickle
from sklearn.decomposition import IncrementalPCA, MiniBatchDictionaryLearning
import gc
def load_subject(subject_filename):
with open(subject_filename, 'rb') as f:
subject_data = pickle.load(f)
return subject_data
class ImageLoader():
def __init__(self, transforms=None):
self.transforms = transforms
pass
def transform(self, X, y=None):
X = load_subject(X)
if self.transforms is not None:
X = self.transforms(image=X)['image']
return X
def main():
parser = argparse.ArgumentParser(description='train pca and dl features')
parser.add_argument('--data-path', default='./data/raw',
help='path to original data, default ./data/raw')
parser.add_argument('--imgs-path', default='./data/imgs',
help='path to resaved images, default ./data/imgs')
parser.add_argument('--path-to-save', default='./data/features',
help='path to save features, default ./data/features')
parser.add_argument('--path-to-save-model', default='./models/pca',
help='path to save models, default ./models/pca')
args = parser.parse_args()
data_path = args.data_path
imgs_path = args.imgs_path
path_to_save = args.path_to_save
path_to_save_model = args.path_to_save_model
for _path in [path_to_save, path_to_save_model,
os.path.join(path_to_save, '100dl_feats'),
os.path.join(path_to_save, '200pca_feats')]:
if not os.path.exists(_path):
os.makedirs(_path)
loading = pd.read_csv(os.path.join(data_path, 'loading.csv'), index_col = ['Id'])
# creates pathes to all images
img_path = pd.DataFrame(index=loading.index, columns=['path'])
for index in img_path.index:
path = str(index) + '.npy'
img_path.loc[index, 'path'] = os.path.join(imgs_path, path)
# start train and inference of pca feats
print('PCA started. ~13 hours')
for k in range(0, 6):
##fit
pca = IncrementalPCA(n_components=200)
batch = []
for n, i in enumerate(tqdm(img_path.values)):
f = ImageLoader().transform(i[0])
f = f[k*10:(k+1)*10].flatten()
batch.append(f)
if (n + 1) % 200 == 0:
batch = np.array(batch)
pca.partial_fit(batch)
del batch
gc.collect()
batch = []
##save pca
_p = os.path.join(path_to_save_model, f'200pca_3d_k{k}.pickle')
with open(_p, 'wb') as f:
pickle.dump(pca, f)
##transform
res = []
batch = []
for n, i in enumerate(tqdm(img_path.values)):
f = ImageLoader().transform(i[0])
f = f[k*10:(k+1)*10].flatten()
batch.append(f)
if (n + 1) % 200 == 0:
batch = np.array(batch)
res.append(pca.transform(batch))
del batch
gc.collect()
batch = []
lb = len(batch)
if lb > 0:
batch = np.array(batch)
if lb == 1:
res.append(pca.transform(batch.reshape(1, -1)))
else:
res.append(pca.transform(batch))
##save df
res = np.array(res)
df_res = pd.DataFrame(np.vstack(res), index=loading.index, columns=[f'200PCA_k{k}_' + str(i) for i in range(200)])
_p = os.path.join(path_to_save, f'200pca_feats/200pca_3d_k{k}.csv')
df_res.to_csv(_p)
print('Dictionary learning started. ~47 hours')
n_k = 100
for k in range(0, 6):
##fit
pca = MiniBatchDictionaryLearning(n_components=n_k, random_state=0, n_iter=10, batch_size=n_k)
batch = []
for n, i in enumerate(tqdm(img_path.values)):
f = ImageLoader().transform(i[0])
f = f[k*10:(k+1)*10].flatten()
batch.append(f)
if (n + 1) % 100 == 0:
batch = np.array(batch)
pca.partial_fit(batch)
del batch
gc.collect()
batch = []
##save pca
_p = os.path.join(path_to_save_model, f'dl_3d_k{k}.pickle')
with open(_p, 'wb') as f:
pickle.dump(pca, f)
##transform
res = []
batch = []
for n, i in enumerate(tqdm(img_path.values)):
f = ImageLoader().transform(i[0])
f = f[k*10:(k+1)*10].flatten()
batch.append(f)
if (n + 1) % 100 == 0:
batch = np.array(batch)
res.append(pca.transform(batch))
del batch
gc.collect()
batch = []
lb = len(batch)
if lb > 0:
batch = np.array(batch)
if lb == 1:
res.append(pca.transform(batch.reshape(1, -1)))
else:
res.append(pca.transform(batch))
##save df
res = np.array(res)
df_res = pd.DataFrame(np.vstack(res), index=loading.index, columns=[f'dl_k{k}_' + str(i) for i in range(n_k)])
_p = os.path.join(path_to_save, f'100dl_feats/dl_3d_k{k}.csv')
df_res.to_csv(_p)
#resave results
_p = os.path.join(path_to_save, '100dl_feats/dl_3d_k0.csv')
data_pca = pd.read_csv(_p)
for i in range(1, 6):
_p = os.path.join(path_to_save, '100dl_feats/dl_3d_k{}.csv'.format(i))
part = pd.read_csv(_p)
del part['Id']
data_pca = pd.concat((data_pca, part), axis=1)
data_pca.to_csv(os.path.join(path_to_save, 'dl_feats.csv'), index=None)
print('All saved')
if __name__ == '__main__':
main()