-
Notifications
You must be signed in to change notification settings - Fork 17
/
extractFeatures.py
178 lines (96 loc) · 4.73 KB
/
extractFeatures.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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 24 08:28:05 2021
@author: Narmin Ghaffari Laleh
"""
##############################################################################
from utils.utils import Collate_features
from dataGenerator.dataSetGenerator_FeatEx import Whole_Slide_Bag
from models.resnet_custom import Resnet50_baseline
from torch.utils.data import DataLoader
import h5py
import glob
import torch
import os
##############################################################################
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
##############################################################################
def Save_hdf5(output_dir, asset_dict, mode='a'):
file = h5py.File(output_dir, mode)
for key, val in asset_dict.items():
data_shape = val.shape
if key not in file:
data_type = val.dtype
chunk_shape = (1, ) + data_shape[1:]
maxshape = (None, ) + data_shape[1:]
dset = file.create_dataset(key, shape = data_shape, maxshape = maxshape, chunks = chunk_shape, dtype = data_type)
dset[:] = val
else:
dset = file[key]
dset.resize(len(dset) + data_shape[0], axis = 0)
dset[-data_shape[0]:] = val
file.close()
return output_dir
##############################################################################
def Compute_w_loader(file_path, output_path, model, batch_size = 8, verbose = 0, print_every = 20, pretrained = True, target_patch_size = -1):
dataset = Whole_Slide_Bag(file_path = file_path, pretrained = pretrained, target_patch_size = target_patch_size)
kwargs = {'num_workers': 0, 'pin_memory': True} if device.type == "cuda" else {}
loader = DataLoader(dataset = dataset, batch_size = batch_size, **kwargs, collate_fn = Collate_features)
if verbose > 0:
print('processing {}: total of {} batches'.format(file_path, len(loader)))
mode = 'w'
for count, (batch, coords) in enumerate(loader):
with torch.no_grad():
if count % print_every == 0:
print('batch {}/{}, {} files processed'.format(count, len(loader), count * batch_size))
batch = batch.to(device, non_blocking=True)
features = model(batch)
features = features.cpu().detach().numpy()
asset_dict = {'features': features, 'coords': coords}
Save_hdf5(output_path, asset_dict, mode=mode)
mode = 'a'
return output_path
##############################################################################
def load_model_weights(model, weights):
model_dict = model.state_dict()
weights = {k: v for k, v in weights.items() if k in model_dict}
if weights == {}:
print("No weight could be loaded..")
model_dict.update(weights)
model.load_state_dict(model_dict)
return model
##############################################################################
def ExtractFeatures(data_dir, feat_dir, batch_size, target_patch_size = -1, filterData = True):
print('initializing dataset')
if filterData:
bags_dataset = data_dir
else:
bags_dataset = glob.glob(data_dir + '/*')
os.makedirs(feat_dir, exist_ok = True)
print('loading model checkpoint')
model = Resnet50_baseline(pretrained = True)
model = model.to(device)
model.eval()
total = len(bags_dataset)
for bag_candidate_idx in range(total):
bag_candidate = bags_dataset[bag_candidate_idx]
bag_name = os.path.basename(os.path.normpath(bag_candidate))
print('\nprogress: {}/{}'.format(bag_candidate_idx, total))
bag_base = bag_name.split('\\')[-1]
if not os.path.exists(os.path.join(feat_dir, bag_base + '.pt')):
print(bag_name)
output_path = os.path.join(feat_dir, bag_name)
file_path = bag_candidate
output_file_path = Compute_w_loader(file_path, output_path,
model = model, batch_size = batch_size,
verbose = 1, print_every = 20,
target_patch_size = target_patch_size)
if os.path.exists (output_file_path):
file = h5py.File(output_file_path, "r")
features = file['features'][:]
print('features size: ', features.shape)
print('coordinates size: ', file['coords'].shape)
features = torch.from_numpy(features)
torch.save(features, os.path.join(feat_dir, bag_base+'.pt'))
file.close()
##############################################################################