-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
57 lines (52 loc) · 2.32 KB
/
predict.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
from model.define_model import get_model_for_feature_extraction
import os
import numpy as np
from keras import backend as K
import utils
import skimage.io
import gc
def extract_features(basenet,data_path):
"""
extract features from all fold of all sets for the CFCMC classifier
:param basenet:
:param data_path:
:return:
"""
sets = ['train', 'valid', 'test']
classes = ['01_TUMOR', '02_STROMA', '03_COMPLEX', '04_LYMPHO',
'05_DEBRIS', '06_MUCOSA', '07_ADIPOSE', '08_EMPTY']
# create folders for features
for k in range(1, 11):
for set_name in sets:
for cl in classes:
path = os.path.join(data_path,
f"{basenet}_features",
f"{k}_fold",
set_name,
cl)
if not os.path.exists(path):
os.makedirs(path)
for k in range(1, 11):
model = get_model_for_feature_extraction(kfold=k, basenet=basenet, pathbase=pathbase)
# par_model = multi_gpu_model(model, gpus=2)
for set_name in sets:
for cl in classes:
print(f"Extracting from {k} fold from {set_name} set from class {cl}")
save_path = os.path.join(data_path,
f"{basenet}_features",
f"{k}_fold",
set_name,
cl)
batches = utils.batch_data(os.path.join(data_path, f"{k}_fold", set_name, cl), 16)
images = []
for id, files in enumerate(batches):
print("Detecting batches... " + str(round((id * 100) / len(batches), 2)) + " %", end="\r")
images = [skimage.io.imread(x) for x in files]
features = model.predict(np.array(images)/255, batch_size=16)
for _idx, feat in enumerate(features):
file_name = os.path.splitext(os.path.basename(files[_idx]))[0]
save_file_path = os.path.join(save_path, file_name + ".csv")
np.savetxt(save_file_path, features[_idx], delimiter=",")
K.clear_session()
del model
gc.collect()