-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
130 lines (109 loc) · 4.02 KB
/
Copy pathtrain_model.py
File metadata and controls
130 lines (109 loc) · 4.02 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
import os
import numpy as np
import pandas as pd
import wandb
import torch
import torch.nn.functional as F
import evaluate
from transformers import (
Trainer,
TrainingArguments,
TrainerCallback,
default_data_collator,
)
from utils.loss_graph import plot_loss_curve
os.environ["WANDB_PROJECT"] = "deepfake-detection"
accuracy_metric = evaluate.load("accuracy")
precision_metric = evaluate.load("precision")
recall_metric = evaluate.load("recall")
f1_metric = evaluate.load("f1")
roc_auc_metric = evaluate.load("roc_auc")
class EpochProgressCallback(TrainerCallback):
def on_epoch_begin(self, args, state, control, **kwargs):
current = int(state.epoch) + 1
total = int(args.num_train_epochs)
print(f"\n\n>>> Starting epoch {current}/{total}")
def compute_metrics(eval_pred):
logits, labels = eval_pred
predictions = np.argmax(logits, axis=-1)
accuracy = accuracy_metric.compute(references=labels, predictions=predictions)
precision = precision_metric.compute(predictions=predictions, references=labels, average="binary")
recall = recall_metric.compute(references=labels, predictions=predictions, average="binary", zero_division=0)
f1 = f1_metric.compute(predictions=predictions, references=labels, average="binary")
logits_t = torch.from_numpy(logits)
probs = F.softmax(logits_t.float(), dim=1).cpu().numpy()
auc = roc_auc_metric.compute(prediction_scores=probs[:, 1], references=labels)
try:
wandb.log({"roc": wandb.plot.roc_curve(labels, probs, labels=["real", "fake"])})
wandb.log({"pr": wandb.plot.pr_curve(labels, probs, labels=["real", "fake"])})
except Exception as e:
print(f"Warning: Failed to log to wandb: {e}")
metrics = {
"accuracy": accuracy["accuracy"],
"precision": precision["precision"],
"recall": recall["recall"],
"f1": f1["f1"],
"auc": auc["roc_auc"]
}
return metrics
def train_model(
model,
train_dataset,
val_dataset,
num_epochs,
warmup_epochs,
resume_from_checkpoint=None
):
per_device_batch_size = 8
total_steps = num_epochs * (len(train_dataset) // per_device_batch_size)
warmup_steps = warmup_epochs * (len(train_dataset) // per_device_batch_size)
training_args = TrainingArguments(
output_dir="./ckpt",
overwrite_output_dir=True,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
optim="adamw_torch",
learning_rate=1.5e-5,
weight_decay=0.01,
label_smoothing_factor=0.1,
max_grad_norm=1.0,
gradient_accumulation_steps=1,
lr_scheduler_type="cosine",
num_train_epochs=num_epochs,
warmup_steps=warmup_steps,
evaluation_strategy="epoch",
save_strategy="epoch",
logging_strategy="epoch",
load_best_model_at_end=True,
metric_for_best_model="accuracy",
dataloader_num_workers=4,
dataloader_pin_memory=True,
dataloader_persistent_workers=True,
dataloader_prefetch_factor=4,
fp16=True,
# deepspeed="ds_config.json",
disable_tqdm=False,
report_to='wandb',
run_name="TALL-TimeSformer-Tesla V100-Dropout(0.2)"
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
data_collator=default_data_collator,
compute_metrics=compute_metrics,
callbacks=[EpochProgressCallback()]
)
trainer.train(resume_from_checkpoint=resume_from_checkpoint)
try:
os.makedirs("logs", exist_ok=True)
logs = trainer.state.log_history
df = pd.DataFrame(logs)
df = df.drop(columns=['step'], errors='ignore')
df.to_json("logs/metrics_log.json", orient="records", lines=True)
plot_loss_curve()
except Exception as e:
print(f"Something went wrong in post-training hooks: {e}")
model.save_pretrained('./weights/best_model')
return model