-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_train.py
141 lines (101 loc) · 4.16 KB
/
eval_train.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
import torch
from torch_geometric.data import DataLoader
import torch.optim as optim
from model.model import DeeperGCN
from tqdm import tqdm
from args import ArgsInit
from utils.ckpt_util import save_ckpt
import logging
import time
import statistics
from ogb.graphproppred import PygGraphPropPredDataset, Evaluator
from dataset.dataset import load_dataset, AMPsDataset
import torch.nn.functional as F
from utils import metrics_pharma
import copy
import numpy as np
import datetime
import os
import csv
@torch.no_grad()
def eval(model, device, loader, num_classes, cross_val, args):
model.eval()
y_true = []
y_pred = []
print('------Copying model {}---------'.format(cross_val))
prop_predictor = copy.deepcopy(model)
test_model_path = './log/'+args.save
test_model_path = test_model_path+'/Fold{}/model_ckpt/Checkpoint__valid_best.pth'.format(cross_val)
#LOAD MODELS
print('------- Loading weights----------')
prop_predictor.load_state_dict(torch.load(test_model_path,map_location=lambda storage, loc: storage)['model_state_dict'])
prop_predictor.to(device)
#METHOD.EVAL
prop_predictor.eval()
for step, batch in enumerate(tqdm(loader, desc="Iteration")):
batch_mol = batch.to(device)
if args.feature == 'full':
pass
elif args.feature == 'simple':
# only retain the top two node/edge features
num_features = args.num_features
batch_mol.x = batch_mol.x[:, :num_features]
batch_mol.edge_attr = batch_mol.edge_attr[:, :num_features]
if batch_mol.x.shape[0] == 1:
pass
else:
with torch.set_grad_enabled(False):
pred = F.softmax(prop_predictor(batch_mol),dim=1)
y_true.append(batch_mol.y.view(batch_mol.y.shape).detach().cpu())
y_pred.append(pred.detach().cpu())
y_true = torch.cat(y_true, dim=0).numpy()
y_pred = torch.cat(y_pred, dim=0).numpy()
if args.binary:
auc = metrics_pharma.plotbinauc(y_pred, y_true)
nap, f = metrics_pharma.pltmap_bin(y_pred,y_true)
else:
nap, f = metrics_pharma.norm_ap(y_pred, y_true, num_classes)
auc = metrics_pharma.pltauc(y_pred, y_true, num_classes)
return nap
def main():
args = ArgsInit().args
if args.use_gpu:
device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available() else torch.device("cpu")
else:
device = torch.device('cpu')
if args.binary:
args.nclasses = 2
#Numpy and torch seeds
torch.manual_seed(args.seed)
np.random.seed(args.seed)
if device.type == 'cuda':
torch.cuda.manual_seed(args.seed)
print(args)
maps = []
for cross_val in range(1,5):
train_dataset = AMPsDataset(partition='Train', cross_val=cross_val, binary_task=args.binary, args=args)
valid_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True,
num_workers=args.num_workers)
model = DeeperGCN(args).to(device)
maps.append(eval(model, device, valid_loader, args.nclasses, cross_val, args))
map1, map2, map3, map4 = maps
save_items = {'Mean': [], 'Fold1': [], 'Fold2': [], 'Fold3': [], 'Fold4': []}
mean_map = np.mean([map1,map2,map3,map4])
save_items['Mean'] = mean_map
save_items["Fold1"] = map1
save_items["Fold2"] = map2
save_items["Fold3"] = map3
save_items["Fold4"] = map4
fieldnames = list(save_items.keys())
csv_file = os.path.join('./log/',args.save,'Train.csv')
with open(csv_file, 'a+') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames= fieldnames)
writer.writeheader()
writer.writerow(save_items)
print({'Mean' : mean_map,
'Fold1': map1,
'Fold2': map2,
'Fold3': map3,
'Fold4': map4})
if __name__ == "__main__":
main()