-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_eval.py
133 lines (113 loc) · 6.59 KB
/
run_eval.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
import torch
import numpy as np
import utils
from datetime import datetime
from ContactsNet import ContactsNet
from dataset import ProteinDataLoader
def run_eval(target_path, model_path, replica, out_dir, device):
''' Evaulate time needed to run examples and crops'''
config = utils.build_config(model_path, replica) #returns the configuration as a named tuple
dataloader = ProteinDataLoader(target_path, config) #loads data
model = ContactsNet(config.network_config).to(device)
print(f'Model parameters: {model.get_parameter_number()["Total"]}')
model_file = model_path / replica / 'model.pt'
if model_file.exists():
model.load_state_dict(torch.load(model_file, map_location=device))
else:
cost_time = utils.load_tf_ckpt(model, model_file)
model.to(device)
print(f'Load tf model cost time: {cost_time}')
num_examples = 0
num_crops = 0
num_bins = config.network_config.num_bins
torsion_bins = config.network_config.torsion_bins
crop_size_x = config.crop_size_x
crop_size_y = config.crop_size_y
prob_weights = 1
if config.eval_config.pyramid_weights > 0:
sx = np.expand_dims(np.linspace(1.0 / crop_size_x, 1, crop_size_x), 1)
sy = np.expand_dims(np.linspace(1.0 / crop_size_y, 1, crop_size_y), 0)
prob_weights = np.minimum(np.minimum(sx, np.flipud(sx)),
np.minimum(sy, np.fliplr(sy)))
prob_weights /= np.max(prob_weights)
prob_weights = np.minimum(prob_weights, config.eval_config.pyramid_weights) # crop_size_x x crop_size_y
start_t = datetime.now()
for protein, crops in dataloader:
L = protein.len
print('Data: ',protein.targets.domain_name, L)
# Cropping regions
contact_prob_accum = np.zeros((L, L, 2), dtype=np.float32)
distance_prob_accum = np.zeros((L, L, num_bins), dtype=np.float32)
sec_accum = np.zeros((L, 8), dtype=np.float32)
tor_accum = np.zeros((L, torsion_bins**2), dtype=np.float32)
asa_accum = np.zeros((L,), dtype=np.float32)
weights_1d_accum = np.zeros((L,), dtype=np.float32)
num_crops_local = 0
for x_2d, crop_x, crop_y in crops:
ic = max(0, crop_x[0])
jc = max(0, crop_y[0])
ic_to = min(L, crop_x[1])
jc_to = min(L, crop_y[1])
prepad_x = max(0, -crop_x[0])
prepad_y = max(0, -crop_y[0])
postpad_x = crop_x[1] - ic_to
postpad_y = crop_y[1] - jc_to
with torch.no_grad():
x_2d = np.transpose(x_2d, (2, 0, 1)) # to NCHW shape
x_2d = torch.tensor([x_2d]).float().to(device)
crop_x = torch.tensor([crop_x]).to(device)
crop_y = torch.tensor([crop_y]).to(device)
out = model(x_2d, crop_x, crop_y)
out = {k:t.cpu() for k,t in out.items()}
contact_probs = out['contact_probs'][0,
prepad_y:crop_size_y - postpad_y,
prepad_x:crop_size_x - postpad_x].numpy()
distance_probs = out['distance_probs'][0,
prepad_y:crop_size_y - postpad_y,
prepad_x:crop_size_x - postpad_x].numpy()
weight = prob_weights[prepad_y:crop_size_y - postpad_y,
prepad_x:crop_size_x - postpad_x]
contact_prob_accum[jc:jc_to, ic:ic_to, 0] += contact_probs * weight
contact_prob_accum[jc:jc_to, ic:ic_to, 1] += weight
distance_prob_accum[jc:jc_to, ic:ic_to, :] += distance_probs * np.expand_dims(weight, 2)
weights_1d_accum[jc:jc_to] += 1
weights_1d_accum[ic:ic_to] += 1
if 'secstruct_probs' in out:
sec_x = out['secstruct_probs'][0, prepad_x:crop_size_x - postpad_x].numpy()
sec_y = out['secstruct_probs'][0, crop_size_x + prepad_y:crop_size_x + crop_size_y - postpad_y].numpy()
sec_accum[ic:ic + sec_x.shape[0]] += sec_x
sec_accum[jc:jc + sec_y.shape[0]] += sec_y
if 'torsion_probs' in out:
tor_x = out['torsion_probs'][0, prepad_x:crop_size_x - postpad_x].numpy()
tor_y = out['torsion_probs'][0, crop_size_x + prepad_y:crop_size_x + crop_size_y - postpad_y].numpy()
tor_accum[ic:ic + tor_x.shape[0]] += tor_x
tor_accum[jc:jc + tor_y.shape[0]] += tor_y
if 'asa_output' in out:
asa_x = out['asa_output'][0, prepad_x:crop_size_x - postpad_x].numpy()
asa_y = out['asa_output'][0, crop_size_x + prepad_y:crop_size_x + crop_size_y - postpad_y].numpy()
asa_accum[ic:ic + asa_x.shape[0]] += np.squeeze(asa_x, 1)
asa_accum[jc:jc + asa_y.shape[0]] += np.squeeze(asa_y, 1)
num_crops_local += 1
assert (contact_prob_accum[:, :, 1] > 0.0).all()
contact_accum = contact_prob_accum[:, :, 0] / contact_prob_accum[:, :, 1]
distance_accum = distance_prob_accum[:, :, :] / contact_prob_accum[:, :, 1:2]
asa_accum /= weights_1d_accum
sec_accum /= np.expand_dims(weights_1d_accum, 1)
tor_accum /= np.expand_dims(weights_1d_accum, 1)
# The probs are symmetrical
contact_accum = (contact_accum + contact_accum.transpose()) / 2
distance_accum = (distance_accum + np.transpose(distance_accum, [1, 0, 2])) / 2
# Save the output files
distance_accum.dump(out_dir / f'{protein.targets.domain_name}.distance')
if config.network_config.torsion_multiplier > 0:
tor_accum.dump(out_dir / f'{protein.targets.domain_name}.torsion')
if config.network_config.secstruct_multiplier > 0:
utils.save_seq_prob(sec_accum, protein.seq, out_dir / f'{protein.targets.domain_name}.sec')
if config.network_config.asa_multiplier > 0:
utils.save_seq_prob(asa_accum, protein.seq, out_dir / f'{protein.targets.domain_name}.asa')
num_examples += 1
num_crops += num_crops_local
if num_examples >= config.eval_config.max_num_examples: break
time_spent = datetime.now() - start_t
print(f'Evaluate {num_examples} examples, {num_crops} crops, {num_crops/num_examples:.1f} crops/ex')
print(f'Cost time {time_spent}, {time_spent/num_examples} s/example, {time_spent/num_crops} s/crops\n')