-
Notifications
You must be signed in to change notification settings - Fork 3
/
finetune_focalLoss.py
301 lines (267 loc) · 11.1 KB
/
finetune_focalLoss.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
## -*- coding: utf-8 -*-
import os
import gc
import argparse
import json
import random
import math
import random
from functools import reduce
import numpy as np
import pandas as pd
from scipy import sparse
from sklearn.model_selection import train_test_split, ShuffleSplit, StratifiedShuffleSplit, StratifiedKFold
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, precision_recall_fscore_support, classification_report
import torch
from torch import nn
from torch.optim import Adam, SGD, AdamW
from torch.nn import functional as F
from torch.optim.lr_scheduler import StepLR, CosineAnnealingWarmRestarts, CyclicLR
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.distributed import DistributedSampler
from torch.nn.parallel import DistributedDataParallel as DDP
import torch.distributed as dist
from performer_pytorch import PerformerLM
import scanpy as sc
import anndata as ad
from utils import *
import pickle as pkl
parser = argparse.ArgumentParser()
parser.add_argument("--local_rank", type=int, default=-1, help='Local process rank.')
parser.add_argument("--bin_num", type=int, default=5, help='Number of bins.')
parser.add_argument("--gene_num", type=int, default=16906, help='Number of genes.')
parser.add_argument("--epoch", type=int, default=100, help='Number of epochs.')
parser.add_argument("--seed", type=int, default=2021, help='Random seed.')
parser.add_argument("--batch_size", type=int, default=3, help='Number of batch size.')
parser.add_argument("--learning_rate", type=float, default=1e-4, help='Learning rate.')
parser.add_argument("--grad_acc", type=int, default=60, help='Number of gradient accumulation.')
parser.add_argument("--valid_every", type=int, default=1, help='Number of training epochs between twice validation.')
parser.add_argument("--pos_embed", type=bool, default=True, help='Using Gene2vec encoding or not.')
parser.add_argument("--data_path", type=str, default='./data/Zheng68K.h5ad', help='Path of data for finetune.')
parser.add_argument("--model_path", type=str, default='/ibex/scratch/ruizdeam/scBert/model/panglao_pretrain.pth', help='Path of pretrained model.')
parser.add_argument("--ckpt_dir", type=str, default='./ckpts/', help='Directory of checkpoint to save.')
parser.add_argument("--model_name", type=str, default='finetune', help='Finetuned model name.')
args = parser.parse_args()
rank = int(os.environ["RANK"])
local_rank = args.local_rank
is_master = local_rank == 0
SEED = args.seed
EPOCHS = args.epoch
BATCH_SIZE = args.batch_size
GRADIENT_ACCUMULATION = args.grad_acc
LEARNING_RATE = args.learning_rate
SEQ_LEN = args.gene_num + 1
VALIDATE_EVERY = args.valid_every
PATIENCE = 10
UNASSIGN_THRES = 0.0
CLASS = args.bin_num + 2
POS_EMBED_USING = args.pos_embed
model_name = args.model_name
ckpt_dir = args.ckpt_dir
dist.init_process_group(backend='nccl')
torch.cuda.set_device(local_rank)
device = torch.device("cuda", local_rank)
world_size = torch.distributed.get_world_size()
seed_all(SEED + torch.distributed.get_rank())
class SCDataset(Dataset):
def __init__(self, data, label):
super().__init__()
self.data = data
self.label = label
def __getitem__(self, index):
rand_start = random.randint(0, self.data.shape[0]-1)
full_seq = self.data[rand_start].toarray()[0]
full_seq[full_seq > (CLASS - 2)] = CLASS - 2
full_seq = torch.from_numpy(full_seq).long()
full_seq = torch.cat((full_seq, torch.tensor([0]))).to(device)
seq_label = self.label[rand_start]
return full_seq, seq_label
def __len__(self):
return self.data.shape[0]
class Identity(torch.nn.Module):
def __init__(self, dropout = 0., h_dim = 100, out_dim = 10):
super(Identity, self).__init__()
self.conv1 = nn.Conv2d(1, 1, (1, 200))
self.act = nn.ReLU()
self.fc1 = nn.Linear(in_features=SEQ_LEN, out_features=512, bias=True)
self.act1 = nn.ReLU()
self.dropout1 = nn.Dropout(dropout)
self.fc2 = nn.Linear(in_features=512, out_features=h_dim, bias=True)
self.act2 = nn.ReLU()
self.dropout2 = nn.Dropout(dropout)
self.fc3 = nn.Linear(in_features=h_dim, out_features=out_dim, bias=True)
def forward(self, x):
x = x[:,None,:,:]
x = self.conv1(x)
x = self.act(x)
x = x.view(x.shape[0],-1)
x = self.fc1(x)
x = self.act1(x)
x = self.dropout1(x)
x = self.fc2(x)
x = self.act2(x)
x = self.dropout2(x)
x = self.fc3(x)
return x
data = sc.read_h5ad(args.data_path)
label_dict, label = np.unique(np.array(data.obs['cell_type']), return_inverse=True) # Convert strings categorical to integrate categorical, and label_dict[label] can be restored
#store the label dict and label for prediction
with open('label_dict', 'wb') as fp:
pkl.dump(label_dict, fp)
with open('label', 'wb') as fp:
pkl.dump(label, fp)
class_num = np.unique(label, return_counts=True)[1].tolist()
class_weight = torch.tensor([(1 - (x / sum(class_num))) ** 2 for x in class_num])
label = torch.from_numpy(label)
data = data.X
acc = []
f1 = []
f1w = []
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=SEED)
pred_list = pd.Series(['un'] * data.shape[0])
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=SEED)
for index_train, index_val in sss.split(data, label):
data_train, label_train = data[index_train], label[index_train]
data_val, label_val = data[index_val], label[index_val]
train_dataset = SCDataset(data_train, label_train)
val_dataset = SCDataset(data_val, label_val)
train_sampler = DistributedSampler(train_dataset)
val_sampler = DistributedSampler(val_dataset)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, sampler=train_sampler)
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, sampler=val_sampler)
model = PerformerLM(
num_tokens = CLASS,
dim = 200,
depth = 6,
max_seq_len = SEQ_LEN,
heads = 10,
local_attn_heads = 0,
g2v_position_emb = POS_EMBED_USING
)
path = args.model_path
ckpt = torch.load(path)
model.load_state_dict(ckpt['model_state_dict'])
for param in model.parameters():
param.requires_grad = False
for param in model.norm.parameters():
param.requires_grad = True
for param in model.performer.net.layers[-2].parameters():
param.requires_grad = True
model.to_out = Identity(dropout=0., h_dim=128, out_dim=label_dict.shape[0])
model = model.to(device)
model = DDP(model, device_ids=[local_rank], output_device=local_rank)
# optimizer
optimizer = Adam(model.parameters(), lr=LEARNING_RATE)
scheduler = CosineAnnealingWarmupRestarts(
optimizer,
first_cycle_steps=15,
cycle_mult=2,
max_lr=LEARNING_RATE,
min_lr=1e-6,
warmup_steps=5,
gamma=0.9
)
# focal loss class
class FocalLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, num_classes = 7, reduction='mean'):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.num_classes = num_classes
self.reduction = reduction
def forward(self, inputs, targets):
effective_target = torch.eye(self.num_classes)[targets.to('cpu')]
# Calculate Cross entropy
logit = F.softmax(inputs, dim=1)
logit = logit.clamp(1e-7, 1.0)
ce = -(effective_target * torch.log(logit.to('cpu')))
# Calculate Focal Loss
weight = torch.pow(-logit + 1., self.gamma)
fl = ce * weight.to('cpu') * self.alpha
if self.reduction == 'sum':
return fl.sum()
elif self.reduction == 'mean':
return fl.mean()
loss_fn = FocalLoss(alpha=1, gamma=2, num_classes=7).to(local_rank)
dist.barrier()
trigger_times = 0
max_acc = 0.0
for i in range(1, EPOCHS+1):
train_loader.sampler.set_epoch(i)
model.train()
dist.barrier()
running_loss = 0.0
cum_acc = 0.0
for index, (data, labels) in enumerate(train_loader):
index += 1
data, labels = data.to(device), labels.to(device)
if index % GRADIENT_ACCUMULATION != 0:
with model.no_sync():
logits = model(data)
loss = loss_fn(logits, labels)
loss.backward()
if index % GRADIENT_ACCUMULATION == 0:
logits = model(data)
loss = loss_fn(logits, labels)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), int(1e6))
optimizer.step()
optimizer.zero_grad()
running_loss += loss.item()
softmax = nn.Softmax(dim=-1)
final = softmax(logits)
final = final.argmax(dim=-1)
pred_num = labels.size(0)
correct_num = torch.eq(final, labels).sum(dim=-1)
cum_acc += torch.true_divide(correct_num, pred_num).mean().item()
epoch_loss = running_loss / index
epoch_acc = 100 * cum_acc / index
epoch_loss = get_reduced(epoch_loss, local_rank, 0, world_size)
epoch_acc = get_reduced(epoch_acc, local_rank, 0, world_size)
if is_master:
print(f' == Epoch: {i} | Training Loss: {epoch_loss:.6f} | Accuracy: {epoch_acc:6.4f}% ==')
dist.barrier()
scheduler.step()
if i % VALIDATE_EVERY == 0:
model.eval()
dist.barrier()
running_loss = 0.0
predictions = []
truths = []
with torch.no_grad():
for index, (data_v, labels_v) in enumerate(val_loader):
index += 1
data_v, labels_v = data_v.to(device), labels_v.to(device)
logits = model(data_v)
loss = loss_fn(logits, labels_v)
running_loss += loss.item()
softmax = nn.Softmax(dim=-1)
final_prob = softmax(logits)
final = final_prob.argmax(dim=-1)
final[np.amax(np.array(final_prob.cpu()), axis=-1) < UNASSIGN_THRES] = -1
predictions.append(final)
truths.append(labels_v)
del data_v, labels_v, logits, final_prob, final
# gather
predictions = distributed_concat(torch.cat(predictions, dim=0), len(val_sampler.dataset), world_size)
truths = distributed_concat(torch.cat(truths, dim=0), len(val_sampler.dataset), world_size)
no_drop = predictions != -1
predictions = np.array((predictions[no_drop]).cpu())
truths = np.array((truths[no_drop]).cpu())
cur_acc = accuracy_score(truths, predictions)
f1 = f1_score(truths, predictions, average='macro')
val_loss = running_loss / index
val_loss = get_reduced(val_loss, local_rank, 0, world_size)
if is_master:
print(f' == Epoch: {i} | Validation Loss: {val_loss:.6f} | F1 Score: {f1:.6f} ==')
print(confusion_matrix(truths, predictions))
print(classification_report(truths, predictions, target_names=label_dict.tolist(), digits=4))
if cur_acc > max_acc:
max_acc = cur_acc
trigger_times = 0
save_best_ckpt(i, model, optimizer, scheduler, val_loss, model_name, ckpt_dir)
else:
trigger_times += 1
if trigger_times > PATIENCE:
break
del predictions, truths