-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
196 lines (131 loc) · 5.16 KB
/
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import torch
import numpy as np
import copy
from torch.utils.data import DataLoader
from gensim.models import KeyedVectors
from memory_profiler import profile
import torch
from transformers import logging
import dataset
import util
import models
import eval
logging.set_verbosity_error()
N_EPOCH = 50
S2_LEVEL = 13
DATA_DIR = 'data/human'
REGION_TRAIN = 'Tel_Aviv'
REGION_DEV = 'Haifa'
REGION_TEST = 'Jerusalem'
GRAPH_EMBEDDING_TRAIN = f'data/cell_embedding/embedding_tel_aviv_{S2_LEVEL}.npy'
GRAPH_EMBEDDING_DEV = f'data/cell_embedding/embedding_haifa_{S2_LEVEL}.npy'
GRAPH_EMBEDDING_TEST = f'data/cell_embedding/embedding_jerusalem_{S2_LEVEL}.npy'
TRAIN_BATCH_SIZE = 32
TEST_BATCH_SIZE = 32
DEV_BATCH_SIZE = 32
EARLY_STOP = 10
def train():
dataset_train = dataset.HeGeLSplit(
region=REGION_TRAIN,
data_dir=DATA_DIR,
split_set='train',
s2level=S2_LEVEL,
graph_embed_path=GRAPH_EMBEDDING_TRAIN,
)
dataset_dev = dataset.HeGeLSplit(
region=REGION_DEV,
data_dir=DATA_DIR,
split_set='dev',
s2level=S2_LEVEL,
graph_embed_path=GRAPH_EMBEDDING_DEV,
)
dataset_test = dataset.HeGeLSplit(
region=REGION_TEST,
data_dir=DATA_DIR,
split_set='test',
s2level=S2_LEVEL,
graph_embed_path=GRAPH_EMBEDDING_TEST,
)
train_loader = DataLoader(
dataset_train, batch_size=TRAIN_BATCH_SIZE, shuffle=True)
dev_loader = DataLoader(
dataset_dev, batch_size=TEST_BATCH_SIZE, shuffle=False)
test_loader = DataLoader(
dataset_test, batch_size=DEV_BATCH_SIZE, shuffle=False)
device = torch.device(
'cuda') if torch.cuda.is_available() else torch.device('cpu')
model = models.DualEncoder()
model.to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
all_cells_tensor_train = get_cells(GRAPH_EMBEDDING_TRAIN, train_loader, device)
all_cells_tensor_test = get_cells(GRAPH_EMBEDDING_TEST, test_loader, device)
all_cells_tensor_dev = get_cells(GRAPH_EMBEDDING_DEV, dev_loader, device)
model.train()
best_error_distances = np.inf
early_stop_counter = 0
for epoch_idx in range(0, N_EPOCH):
model.train()
running_loss = 0
for _, batch in enumerate(train_loader):
optimizer.zero_grad()
text = {key: val.to(device) for key, val in batch['text'].items()}
target = batch['label'].to(device)
loss, _ = model(text, target, all_cells_tensor_train)
running_loss += loss
loss.backward()
optimizer.step()
print (f"Finished training epoch {epoch_idx}, Loss: {running_loss}")
model.eval()
true_polygon_list, pred_points_list = [], []
for _, batch in enumerate(dev_loader):
text = {key: val.to(device) for key, val in batch['text'].items()}
target = batch['label'].to(device)
_, score = model(text, target, all_cells_tensor_dev)
predictions = np.argmax(score.detach().cpu().numpy(), axis=-1)
pred_points = util.predictions_to_points(predictions, dev_loader.dataset.label_to_cellid)
pred_points_list.append(pred_points)
true_polygon_list += [poly for poly in batch['geometry'][0]]
pred_points_list = np.concatenate(pred_points_list, axis=0)
error_distances_list, error_distances = eval.get_error_distances(true_polygon_list, pred_points_list)
if best_error_distances > error_distances:
best_error_distances = error_distances
print (f"Found a better model with mean distance error {error_distances}")
best_model = copy.deepcopy(model)
print ("Evaluationg development set")
eval.compute_metrics(error_distances_list)
else:
early_stop_counter += 1
if early_stop_counter >= EARLY_STOP:
print ("Early stopping")
break
del model
best_model.eval()
print ("Evaluationg test set")
true_polygon_list, pred_points_list = [], []
for _, batch in enumerate(test_loader):
text = {key: val.to(device) for key, val in batch['text'].items()}
target = batch['label'].to(device)
_, score = best_model(text, target, all_cells_tensor_test)
predictions = np.argmax(score.detach().cpu().numpy(), axis=-1)
pred_points = util.predictions_to_points(predictions, test_loader.dataset.label_to_cellid)
pred_points_list.append(pred_points)
true_polygon_list += [poly for poly in batch['geometry'][0]]
pred_points_list = np.concatenate(pred_points_list, axis=0)
error_distances_list, _ = eval.get_error_distances(true_polygon_list, pred_points_list)
eval.compute_metrics(error_distances_list)
def get_cells(graph_embedding_path, data_loader, device):
graph_embed_file = KeyedVectors.load_word2vec_format(graph_embedding_path)
all_cells = [graph_embed_file[str(c)] for c in data_loader.dataset.cellid_to_label.keys()]
all_cells_tensor = torch.from_numpy(np.array(all_cells)).to(device)
return all_cells_tensor
def check_grad(model):
list_not_learned = []
list_learned = []
for name, param in model.named_parameters():
if param.grad is None or param.grad.float().sum().tolist() == 0:
list_not_learned.append(name)
if param.grad is not None:
list_learned.append(name)
print(f"{len(list_not_learned)} Un-Learned params: {','.join(list_not_learned)}")
if __name__ == "__main__":
train()