Spaces:
Runtime error
Runtime error
File size: 19,419 Bytes
9e90264 a491ee5 a458b71 9e90264 31eb488 9e90264 a491ee5 78e1dd9 d06b274 78e1dd9 9e90264 78e1dd9 9e90264 78e1dd9 9e90264 a491ee5 9e90264 a491ee5 9e90264 681d043 9e90264 681d043 9e90264 d06b274 9e90264 d06b274 9e90264 681d043 9e90264 681d043 9e90264 a491ee5 9e90264 681d043 9e90264 681d043 9e90264 681d043 9e90264 681d043 9e90264 a491ee5 9e90264 a491ee5 31eb488 9e90264 a491ee5 9e90264 a491ee5 9e90264 a491ee5 9e90264 a491ee5 9e90264 d06b274 9e90264 d06b274 9e90264 a491ee5 9e90264 a491ee5 9e90264 d06b274 31eb488 9e90264 78e1dd9 9e90264 78e1dd9 9e90264 31eb488 9e90264 d06b274 31eb488 9e90264 31eb488 9e90264 31eb488 9e90264 31eb488 9e90264 31eb488 9e90264 31eb488 9e90264 31eb488 9e90264 31eb488 9e90264 a0b4d21 31eb488 9e90264 a458b71 9e90264 d06b274 a458b71 78e1dd9 |
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
import logging
import os
import time
from typing import Any
from huggingface_hub import PyTorchModelHubMixin
from pytorch_lightning import Trainer, LightningModule, LightningDataModule
from pytorch_lightning.utilities.types import OptimizerLRScheduler, STEP_OUTPUT, EVAL_DATALOADERS
from torch.utils.data import DataLoader, Dataset, IterableDataset
from torcheval.metrics import BinaryAccuracy, BinaryAUROC, BinaryF1Score, BinaryPrecision, BinaryRecall
from transformers import BertModel, BatchEncoding, BertTokenizer, TrainingArguments
from transformers.modeling_outputs import BaseModelOutputWithPoolingAndCrossAttentions
import torch
from torch import nn
from datasets import load_dataset
timber = logging.getLogger()
# logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO) # change to level=logging.DEBUG to print more logs...
NO_REGULARIZATION = 0
L1_REGULARIZATION_CODE = 1
L2_REGULARIZATION_CODE = 2
L1_AND_L2_REGULARIZATION_CODE = 3
black = "\u001b[30m"
red = "\u001b[31m"
green = "\u001b[32m"
yellow = "\u001b[33m"
blue = "\u001b[34m"
magenta = "\u001b[35m"
cyan = "\u001b[36m"
white = "\u001b[37m"
FORWARD = "FORWARD_INPUT"
BACKWARD = "BACKWARD_INPUT"
DNA_BERT_6 = "zhihan1996/DNA_bert_6"
class CommonAttentionLayer(nn.Module):
def __init__(self, hidden_size, *args, **kwargs):
super().__init__(*args, **kwargs)
self.attention_linear = nn.Linear(hidden_size, 1)
pass
def forward(self, hidden_states):
# Apply linear layer
attn_weights = self.attention_linear(hidden_states)
# Apply softmax to get attention scores
attn_weights = torch.softmax(attn_weights, dim=1)
# Apply attention weights to hidden states
context_vector = torch.sum(attn_weights * hidden_states, dim=1)
return context_vector, attn_weights
class ReshapedBCEWithLogitsLoss(nn.BCEWithLogitsLoss):
def forward(self, input, target):
return super().forward(input.squeeze(), target.float())
class MQtlDnaBERT6Classifier(nn.Module, PyTorchModelHubMixin):
def __init__(self,
bert_model=BertModel.from_pretrained(pretrained_model_name_or_path=DNA_BERT_6),
hidden_size=768,
num_classes=1,
*args,
**kwargs
):
super().__init__(*args, **kwargs)
self.model_name = "MQtlDnaBERT6Classifier"
self.bert_model = bert_model
self.attention = CommonAttentionLayer(hidden_size)
self.classifier = nn.Linear(hidden_size, num_classes)
pass
def forward(self, input_ids: torch.tensor, attention_mask: torch.tensor, token_type_ids):
"""
# torch.Size([128, 1, 512]) --> [128, 512]
input_ids = input_ids.squeeze(dim=1).to(DEVICE)
# torch.Size([16, 1, 512]) --> [16, 512]
attention_mask = attention_mask.squeeze(dim=1).to(DEVICE)
token_type_ids = token_type_ids.squeeze(dim=1).to(DEVICE)
"""
bert_output: BaseModelOutputWithPoolingAndCrossAttentions = self.bert_model(
input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids
)
last_hidden_state = bert_output.last_hidden_state
context_vector, ignore_attention_weight = self.attention(last_hidden_state)
y = self.classifier(context_vector)
return y
"""
class TorchMetrics:
def __init__(self):
self.binary_accuracy = BinaryAccuracy() #.to(device)
self.binary_auc = BinaryAUROC() # .to(device)
self.binary_f1_score = BinaryF1Score() # .to(device)
self.binary_precision = BinaryPrecision() # .to(device)
self.binary_recall = BinaryRecall() # .to(device)
pass
def update_on_each_step(self, batch_predicted_labels, batch_actual_labels): # todo: Add log if needed
# it looks like the library maintainers changed preds to input, ie, before: preds, now: input
self.binary_accuracy.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_auc.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_f1_score.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_precision.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_recall.update(input=batch_predicted_labels, target=batch_actual_labels)
pass
def compute_and_log_on_each_step(self, log, log_prefix: str, log_color: str = green):
b_accuracy = self.binary_accuracy.compute()
b_auc = self.binary_auc.compute()
b_f1_score = self.binary_f1_score.compute()
b_precision = self.binary_precision.compute()
b_recall = self.binary_recall.compute()
timber.info(log_color + f"{log_prefix}_acc = {b_accuracy}, {log_prefix}_auc = {b_auc}, {log_prefix}_f1_score = {b_f1_score}, {log_prefix}_precision = {b_precision}, {log_prefix}_recall = {b_recall}")
log(f"{log_prefix}_accuracy", b_accuracy)
log(f"{log_prefix}_auc", b_auc)
log(f"{log_prefix}_f1_score", b_f1_score)
log(f"{log_prefix}_precision", b_precision)
log(f"{log_prefix}_recall", b_recall)
# def reset_on_epoch_end(self):
# self.binary_accuracy.reset()
# self.binary_auc.reset()
# self.binary_f1_score.reset()
# self.binary_precision.reset()
# self.binary_recall.reset()
def compute_and_reset_on_epoch_end(self, log, log_prefix: str, log_color: str = green):
b_accuracy = self.binary_accuracy.compute()
b_auc = self.binary_auc.compute()
b_f1_score = self.binary_f1_score.compute()
b_precision = self.binary_precision.compute()
b_recall = self.binary_recall.compute()
timber.info( log_color + f"{log_prefix}_acc = {b_accuracy}, {log_prefix}_auc = {b_auc}, {log_prefix}_f1_score = {b_f1_score}, {log_prefix}_precision = {b_precision}, {log_prefix}_recall = {b_recall}")
log(f"{log_prefix}_accuracy", b_accuracy)
log(f"{log_prefix}_auc", b_auc)
log(f"{log_prefix}_f1_score", b_f1_score)
log(f"{log_prefix}_precision", b_precision)
log(f"{log_prefix}_recall", b_recall)
self.binary_accuracy.reset()
self.binary_auc.reset()
self.binary_f1_score.reset()
self.binary_precision.reset()
self.binary_recall.reset()
pass
"""
class TorchMetrics:
def __init__(self):
self.binary_accuracy = BinaryAccuracy() #.to(device)
self.binary_auc = BinaryAUROC() # .to(device)
self.binary_f1_score = BinaryF1Score() # .to(device)
self.binary_precision = BinaryPrecision() # .to(device)
self.binary_recall = BinaryRecall() # .to(device)
pass
def update_on_each_step(self, batch_predicted_labels, batch_actual_labels): # todo: Add log if needed
# it looks like the library maintainers changed preds to input, ie, before: preds, now: input
self.binary_accuracy.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_auc.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_f1_score.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_precision.update(input=batch_predicted_labels, target=batch_actual_labels)
self.binary_recall.update(input=batch_predicted_labels, target=batch_actual_labels)
pass
def compute_metrics_and_log(self, log, log_prefix: str, log_color: str = green):
b_accuracy = self.binary_accuracy.compute()
b_auc = self.binary_auc.compute()
b_f1_score = self.binary_f1_score.compute()
b_precision = self.binary_precision.compute()
b_recall = self.binary_recall.compute()
timber.info( log_color + f"{log_prefix}_acc = {b_accuracy}, {log_prefix}_auc = {b_auc}, {log_prefix}_f1_score = {b_f1_score}, {log_prefix}_precision = {b_precision}, {log_prefix}_recall = {b_recall}")
log(f"{log_prefix}_accuracy", b_accuracy)
log(f"{log_prefix}_auc", b_auc)
log(f"{log_prefix}_f1_score", b_f1_score)
log(f"{log_prefix}_precision", b_precision)
log(f"{log_prefix}_recall", b_recall)
pass
def reset_on_epoch_end(self):
self.binary_accuracy.reset()
self.binary_auc.reset()
self.binary_f1_score.reset()
self.binary_precision.reset()
self.binary_recall.reset()
class MQtlBertClassifierLightningModule(LightningModule):
def __init__(self,
classifier: nn.Module,
criterion=None, # nn.BCEWithLogitsLoss(),
regularization: int = L2_REGULARIZATION_CODE, # 1 == L1, 2 == L2, 3 (== 1 | 2) == both l1 and l2, else ignore / don't care
l1_lambda=0.0001,
l2_wright_decay=0.0001,
*args: Any,
**kwargs: Any):
super().__init__(*args, **kwargs)
self.classifier = classifier
self.criterion = criterion
self.train_metrics = TorchMetrics()
self.validate_metrics = TorchMetrics()
self.test_metrics = TorchMetrics()
self.regularization = regularization
self.l1_lambda = l1_lambda
self.l2_weight_decay = l2_wright_decay
pass
def forward(self, x, *args: Any, **kwargs: Any) -> Any:
input_ids: torch.tensor = x["input_ids"]
attention_mask: torch.tensor = x["attention_mask"]
token_type_ids: torch.tensor = x["token_type_ids"]
# print(f"\n{ type(input_ids) = }, {input_ids = }")
# print(f"{ type(attention_mask) = }, { attention_mask = }")
# print(f"{ type(token_type_ids) = }, { token_type_ids = }")
return self.classifier.forward(input_ids, attention_mask, token_type_ids)
def configure_optimizers(self) -> OptimizerLRScheduler:
# Here we add weight decay (L2 regularization) to the optimizer
weight_decay = 0.0
if self.regularization == 2 or self.regularization == 3:
weight_decay = self.l2_weight_decay
return torch.optim.Adam(self.parameters(), lr=1e-5, weight_decay=weight_decay) # , weight_decay=0.005)
def training_step(self, batch, batch_idx, *args: Any, **kwargs: Any) -> STEP_OUTPUT:
# Accuracy on training batch data
x, y = batch
preds = self.forward(x)
loss = self.criterion(preds, y)
if self.regularization == 1 or self.regularization == 3: # apply l1 regularization
l1_norm = sum(p.abs().sum() for p in self.parameters())
loss += self.l1_lambda * l1_norm
self.log("train_loss", loss)
# calculate the scores start
self.train_metrics.update_on_each_step(batch_predicted_labels=preds.squeeze(), batch_actual_labels=y)
self.train_metrics.compute_metrics_and_log(log=self.log, log_prefix="train")
# self.train_metrics.compute_and_log_on_each_step(log=self.log, log_prefix="train")
# calculate the scores end
return loss
def on_train_epoch_end(self) -> None:
self.train_metrics.compute_metrics_and_log(log=self.log, log_prefix="train")
self.train_metrics.reset_on_epoch_end()
pass
def validation_step(self, batch, batch_idx, *args: Any, **kwargs: Any) -> STEP_OUTPUT:
# Accuracy on validation batch data
# print(f"debug { batch = }")
x, y = batch
preds = self.forward(x)
loss = self.criterion(preds, y)
""" loss = 0 # <------------------------- maybe the loss calculation is problematic """
self.log("valid_loss", loss)
# calculate the scores start
self.validate_metrics.update_on_each_step(batch_predicted_labels=preds.squeeze(), batch_actual_labels=y)
self.validate_metrics.compute_metrics_and_log(log=self.log, log_prefix="validate", log_color=blue)
# self.validate_metrics.compute_and_log_on_each_step(log=self.log, log_prefix="validate", log_color=blue)
# calculate the scores end
return loss
def on_validation_epoch_end(self) -> None:
self.validate_metrics.compute_metrics_and_log(log=self.log, log_prefix="validate", log_color=blue)
self.validate_metrics.reset_on_epoch_end()
return None
def test_step(self, batch, batch_idx, *args: Any, **kwargs: Any) -> STEP_OUTPUT:
# Accuracy on validation batch data
x, y = batch
preds = self.forward(x)
loss = self.criterion(preds, y)
self.log("test_loss", loss) # do we need this?
# calculate the scores start
self.test_metrics.update_on_each_step(batch_predicted_labels=preds.squeeze(), batch_actual_labels=y)
self.test_metrics.compute_metrics_and_log(log=self.log, log_prefix="test", log_color=magenta)
# self.test_metrics.compute_and_log_on_each_step(log=self.log, log_prefix="test", log_color=magenta)
# calculate the scores end
return loss
def on_test_epoch_end(self) -> None:
self.test_metrics.compute_metrics_and_log(log=self.log, log_prefix="test", log_color=magenta)
self.test_metrics.reset_on_epoch_end()
return None
pass
class PagingMQTLDnaBertDataset(IterableDataset):
def __init__(self, dataset, tokenizer, max_length=512): # hold on! why is it 512? I added 4000, and it crashed, the error suggested 512, that's why 512
self.dataset = dataset
self.bert_tokenizer = tokenizer
self.max_length = max_length
# def __len__(self):
# return len(self.dataset)
def __iter__(self):
for row in self.dataset:
processed = self.preprocess(row)
if processed is not None:
yield processed
def preprocess(self, row):
sequence = row['sequence'] # Fetch the 'sequence' column
label = row['label'] # Fetch the 'label' column (or whatever target you use)
# Tokenize the sequence
encoded_sequence: BatchEncoding = self.bert_tokenizer(
sequence,
truncation=True,
padding='max_length',
max_length=self.max_length,
return_tensors='pt'
)
encoded_sequence_squeezed = {key: val.squeeze() for key, val in encoded_sequence.items()}
return encoded_sequence_squeezed, label
class DNABERTDataModule(LightningDataModule):
def __init__(self, model_name=DNA_BERT_6, batch_size=8, WINDOW=-1, is_local=False):
super().__init__()
self.tokenized_dataset = None
self.dataset = None
self.train_dataset: PagingMQTLDnaBertDataset = None
self.validate_dataset: PagingMQTLDnaBertDataset = None
self.test_dataset: PagingMQTLDnaBertDataset = None
self.tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path=model_name)
self.batch_size = batch_size
self.is_local = is_local
self.window = WINDOW
def prepare_data(self):
# Download and prepare dataset
data_files = {
# small samples
"train_binned_200": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_200_train_binned.csv",
"validate_binned_200": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_200_validate_binned.csv",
"test_binned_200": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_200_test_binned.csv",
# medium samples
"train_binned_1000": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_1000_train_binned.csv",
"validate_binned_1000": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_1000_validate_binned.csv",
"test_binned_1000": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_1000_test_binned.csv",
# large samples
"train_binned_4000": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_4000_train_binned.csv",
"validate_binned_4000": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_4000_validate_binned.csv",
"test_binned_4000": "/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_4000_test_binned.csv",
# really tiny
# "tiny_train": "/home/soumic/Codes/mqtl-classification/src/inputdata/tiny_dataset_4000_train_binned.csv",
# "tiny_validate": "/home/soumic/Codes/mqtl-classification/src/inputdata/tiny_dataset_4000_validate_binned.csv",
# "tiny_test": "/home/soumic/Codes/mqtl-classification/src/inputdata/tiny_dataset_4000_test_binned.csv",
"tiny_train": "/home/soumic/Codes/mqtl-classification/src/inputdata/medium_dataset_4000_train_binned.csv",
"tiny_validate": "/home/soumic/Codes/mqtl-classification/src/inputdata/medium_dataset_4000_validate_binned.csv",
"tiny_test": "/home/soumic/Codes/mqtl-classification/src/inputdata/medium_dataset_4000_test_binned.csv",
}
if self.is_local:
self.dataset = load_dataset("csv", data_files=data_files, streaming=True)
else:
self.dataset = load_dataset("fahimfarhan/mqtl-classification-datasets")
def setup(self, stage=None):
self.train_dataset = PagingMQTLDnaBertDataset(self.dataset['tiny_train'], self.tokenizer)
self.validate_dataset = PagingMQTLDnaBertDataset(self.dataset['tiny_validate'], self.tokenizer)
self.test_dataset = PagingMQTLDnaBertDataset(self.dataset['tiny_test'], self.tokenizer)
def train_dataloader(self):
return DataLoader(self.train_dataset, batch_size=self.batch_size, num_workers=1)
def val_dataloader(self):
return DataLoader(self.validate_dataset, batch_size=self.batch_size, num_workers=1)
def test_dataloader(self) -> EVAL_DATALOADERS:
return DataLoader(self.test_dataset, batch_size=self.batch_size, num_workers=1)
def start_bert(classifier_model, model_save_path, criterion, WINDOW, batch_size=4,
is_binned=True, is_debug=False, max_epochs=10, regularization_code = L2_REGULARIZATION_CODE):
is_my_laptop = os.path.isfile("/home/soumic/Codes/mqtl-classification/src/inputdata/dataset_4000_train_binned.csv")
model_local_directory = f"my-awesome-model-{WINDOW}"
model_remote_repository = f"fahimfarhan/dnabert-6-mqtl-classifier-{WINDOW}"
file_suffix = ""
if is_binned:
file_suffix = "_binned"
data_module = DNABERTDataModule(batch_size=batch_size, WINDOW=WINDOW, is_local=is_my_laptop)
# classifier_model = classifier_model.to(DEVICE)
classifier_module = MQtlBertClassifierLightningModule(
classifier=classifier_model,
regularization=regularization_code, criterion=criterion)
# if os.path.exists(model_save_path):
# classifier_module.load_state_dict(torch.load(model_save_path))
classifier_module = classifier_module # .double()
# Prepare data using the DataModule
data_module.prepare_data()
data_module.setup()
trainer = Trainer(max_epochs=max_epochs, precision="32")
# Train the model
trainer.fit(model=classifier_module, datamodule=data_module)
trainer.test(model=classifier_module, datamodule=data_module)
torch.save(classifier_module.state_dict(), model_save_path)
# classifier_module.push_to_hub("fahimfarhan/mqtl-classifier-model")
classifier_model.save_pretrained(save_directory=model_local_directory, safe_serialization=False)
# push to the hub
commit_message = f":tada: Push model for window size {WINDOW} from huggingface space"
if is_my_laptop:
commit_message = f":tada: Push model for window size {WINDOW} from zephyrus"
classifier_model.push_to_hub(
repo_id=model_remote_repository,
# subfolder=f"my-awesome-model-{WINDOW}", subfolder didn't work :/
commit_message=commit_message, # f":tada: Push model for window size {WINDOW}"
# safe_serialization=False
)
pass
if __name__ == "__main__":
start_time = time.time()
dataset_folder_prefix = "inputdata/"
pytorch_model = MQtlDnaBERT6Classifier()
start_bert(classifier_model=pytorch_model, model_save_path=f"weights_{pytorch_model.model_name}.pth",
criterion=ReshapedBCEWithLogitsLoss(), WINDOW=4000, batch_size=12, # max 14 on my laptop...
max_epochs=1, regularization_code=L2_REGULARIZATION_CODE)
# Record the end time
end_time = time.time()
# Calculate the duration
duration = end_time - start_time
# Print the runtime
print(f"Runtime: {duration:.2f} seconds")
pass
|