-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprot_test.py
More file actions
147 lines (130 loc) · 4.79 KB
/
Copy pathprot_test.py
File metadata and controls
147 lines (130 loc) · 4.79 KB
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
from torch.utils.data import DataLoader
from models import ProtNet
from prot_util import *
from util import identity, to_device, init_from_dict
from diffusion import ProjectedSE3Diffusion, ProjectedEulerDiffusion
from itertools import count
from tqdm import tqdm, trange
import pickle
AUGMENT = True
SAMPLES = 4
if __name__ == "__main__":
import os
# Windows doesn't support process forking
if os.name != 'nt':
torch.multiprocessing.set_start_method("forkserver")
import wandb
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--batch",
type=int,
default=2,
help="batch",
)
parser.add_argument(
"--lr",
type=float,
default=1e-4,
help="learning rate",
)
parser.add_argument(
"--dim",
type=int,
default=1024,
help="transformer dimension",
)
parser.add_argument(
"--heads",
type=int,
default=8,
help="number of self-attention heads per layer",
)
parser.add_argument(
"--dim_head",
type=int,
default=32,
help="dimension of self-attention head",
)
parser.add_argument(
"--t_depth",
type=int,
default=12,
help="number of transformer layers",
)
parser.add_argument(
"--c_depth",
type=int,
default=8,
help="number of residue convolutional layers",
)
parser.add_argument(
"--se3",
action='store_true',
help="Use SE3 diffusion rather than euler angles",
)
args = parser.parse_args()
config = vars(args)
device = torch.device(f"cuda") if torch.cuda.is_available() else torch.device("cpu")
dataset = ProtDataset("data/BPTI_dock")
dl = DataLoader(dataset, batch_size=args.batch, shuffle=True,
num_workers=0, pin_memory=True,
collate_fn=identity,
# persistent_workers=True,
)
net, = init_from_dict(config, ProtNet)
net.to(device)
diff_type = "se3" if config['se3'] else "eul"
weight_path = f"weights/weights_protein_{diff_type}.pt"
net.load_state_dict(torch.load(weight_path, map_location=device))
net.eval()
if config['se3']:
process = ProjectedSE3Diffusion(net).to(device)
true_rot = torch.eye(3).unsqueeze(0).expand(args.batch, -1, -1).to(device)
true_shift = torch.zeros(args.batch, 3).to(device)
true_pos = AffineT(shift=true_shift, rot=true_rot)
else:
process = ProjectedEulerDiffusion(net).to(device)
true_pos = torch.zeros(args.batch, 6).to(device)
results = []
for i, data in enumerate(tqdm(dl, desc='batch')):
data = to_device(device, *data)
# Random transform.
if AUGMENT:
with torch.no_grad():
transl = torch.randn((len(data), 3)).to(device)
rot = torch.linalg.qr(torch.randn((len(data), 3, 3)))[0].to(device)
aff_ts = [AffineT(shift=t, rot=r) for t,r in zip(transl, rot)]
data = [move_prots(t, p)for t,p in zip(aff_ts, data)]
projection = ProtProjection(data, se3=config['se3']).to(device)
process.projection = projection
samples = []
for samp in trange(SAMPLES, leave=False, desc="sample number"):
with torch.no_grad():
# Initial Haar-Uniform random rotations from QR decomp of normal IID matrix
R, _ = torch.linalg.qr(torch.randn((args.batch, 3, 3)), "reduced")
T = torch.randn((args.batch, 3))
if not config['se3']:
R = torch.stack(rmat_to_euler(R),dim=-1)
transform = torch.cat((R,T), dim=-1)
else:
transform = AffineT(rot=R, shift=T)
transform = transform.to(device)
for i in tqdm(reversed(range(0, process.num_timesteps)),
desc='sampling loop time step',
total=process.num_timesteps,
leave=False,
):
transform = process.p_sample(transform, torch.full((args.batch,), i, device=device,
dtype=torch.long)).detach()
# TODO make this SE3 compatible
if not config['se3']:
eul = transform[..., :3]
rots = euler_to_rmat(*torch.unbind(eul,-1))
shift = transform[..., 3:]
aff_t = AffineT(rots, shift).to('cpu')
else:
aff_t = transform.to('cpu')
samples.append(aff_t)
results.append(samples)
pickle.dump(results, open(f'prot_samples_{diff_type}.pkl', 'wb'))