-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_cross_attention.py
302 lines (248 loc) · 10.1 KB
/
train_cross_attention.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
302
import torch
import torch.nn as nn
import mlflow
import mlflow.pytorch
import timm
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, roc_curve, auc
import numpy as np
from data_handler import get_dataloaders
import logging
import os
from tqdm import tqdm
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class CrossAttention(nn.Module):
def __init__(self, dim):
super().__init__()
self.query = nn.Linear(dim, dim)
self.key = nn.Linear(dim, dim)
self.value = nn.Linear(dim, dim)
self.scale = dim ** -0.5
def forward(self, x):
q = self.query(x)
k = self.key(x)
v = self.value(x)
attn = (q @ k.transpose(-2, -1)) * self.scale
attn = attn.softmax(dim=-1)
out = attn @ v
return out
class DeepfakeCrossAttention(nn.Module):
def __init__(self):
super().__init__()
# Load pretrained backbone (using EfficientNet-B0 as feature extractor)
self.backbone = timm.create_model(
'efficientnet_b0',
pretrained=True,
num_classes=0,
global_pool=''
)
# Get feature dimensions
with torch.no_grad():
dummy_input = torch.zeros(1, 3, 224, 224)
features = self.backbone(dummy_input)
self.feature_dim = features.shape[1]
self.spatial_dim = features.shape[2] * features.shape[3]
# Cross-attention layers
self.cross_attention1 = CrossAttention(self.feature_dim)
self.cross_attention2 = CrossAttention(self.feature_dim)
# Global average pooling
self.gap = nn.AdaptiveAvgPool2d(1)
# Classification head
self.classifier = nn.Sequential(
nn.LayerNorm(self.feature_dim),
nn.Dropout(0.5),
nn.Linear(self.feature_dim, 512),
nn.GELU(),
nn.LayerNorm(512),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.GELU(),
nn.LayerNorm(256),
nn.Dropout(0.2),
nn.Linear(256, 1)
)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.trunc_normal_(m.weight, std=0.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
def forward(self, x):
# Extract features
features = self.backbone(x) # B, C, H, W
# Reshape for attention
B, C, H, W = features.shape
features = features.view(B, C, -1).transpose(1, 2) # B, HW, C
# Apply cross-attention
attended1 = self.cross_attention1(features)
attended2 = self.cross_attention2(attended1)
# Global average pooling
pooled = attended2.mean(dim=1) # B, C
# Classification
return self.classifier(pooled)
class DeepfakeTrainer:
def __init__(self, model, train_loader, val_loader, test_loader, device):
self.model = model.to(device)
self.train_loader = train_loader
self.val_loader = val_loader
self.test_loader = test_loader
self.device = device
self.criterion = nn.BCEWithLogitsLoss()
# Different learning rates for different components
backbone_params = []
attention_params = []
classifier_params = []
for name, param in model.named_parameters():
if 'backbone' in name:
backbone_params.append(param)
elif 'cross_attention' in name:
attention_params.append(param)
else:
classifier_params.append(param)
self.optimizer = torch.optim.AdamW([
{'params': backbone_params, 'lr': 1e-5},
{'params': attention_params, 'lr': 5e-5},
{'params': classifier_params, 'lr': 1e-4}
], weight_decay=0.01)
self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
self.optimizer, mode='min', patience=3, factor=0.1
)
def train_epoch(self, epoch):
self.model.train()
running_loss = 0.0
predictions = []
targets = []
pbar = tqdm(self.train_loader, desc=f'Epoch {epoch}')
for inputs, labels in pbar:
inputs, labels = inputs.to(self.device), labels.to(self.device)
self.optimizer.zero_grad()
outputs = self.model(inputs).squeeze()
loss = self.criterion(outputs, labels)
loss.backward()
self.optimizer.step()
running_loss += loss.item()
predictions.extend(torch.sigmoid(outputs).cpu().detach().numpy())
targets.extend(labels.cpu().numpy())
pbar.set_postfix({'loss': loss.item()})
epoch_loss = running_loss / len(self.train_loader)
epoch_acc = ((np.array(predictions) > 0.5) == np.array(targets)).mean()
return epoch_loss, epoch_acc, predictions, targets
def validate(self, loader):
self.model.eval()
running_loss = 0.0
predictions = []
targets = []
with torch.no_grad():
for inputs, labels in loader:
inputs, labels = inputs.to(self.device), labels.to(self.device)
outputs = self.model(inputs).squeeze()
loss = self.criterion(outputs, labels)
running_loss += loss.item()
predictions.extend(torch.sigmoid(outputs).cpu().numpy())
targets.extend(labels.cpu().numpy())
avg_loss = running_loss / len(loader)
accuracy = ((np.array(predictions) > 0.5) == np.array(targets)).mean()
return avg_loss, accuracy, predictions, targets
def log_metrics(self, epoch, train_loss, train_acc, val_loss, val_acc):
mlflow.log_metrics({
'train_loss': train_loss,
'train_accuracy': train_acc,
'val_loss': val_loss,
'val_accuracy': val_acc,
'learning_rate': self.optimizer.param_groups[0]['lr']
}, step=epoch)
def log_plots(self, y_true, y_pred, phase='train'):
# Confusion Matrix
cm = confusion_matrix(y_true, np.array(y_pred) > 0.5)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d')
plt.title(f'{phase} Confusion Matrix')
mlflow.log_figure(plt.gcf(), f'{phase}_confusion_matrix.png')
plt.close()
# ROC Curve
fpr, tpr, _ = roc_curve(y_true, y_pred)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, label=f'ROC curve (AUC = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], 'k--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title(f'{phase} ROC Curve')
plt.legend()
mlflow.log_figure(plt.gcf(), f'{phase}_roc_curve.png')
plt.close()
def train(self, num_epochs):
best_val_loss = float('inf')
for epoch in range(num_epochs):
train_loss, train_acc, train_preds, train_targets = self.train_epoch(epoch)
val_loss, val_acc, val_preds, val_targets = self.validate(self.val_loader)
# Log metrics
self.log_metrics(epoch, train_loss, train_acc, val_loss, val_acc)
# Log plots every few epochs
if epoch % 5 == 0:
self.log_plots(train_targets, train_preds, 'train')
self.log_plots(val_targets, val_preds, 'validation')
# Model checkpoint
if val_loss < best_val_loss:
best_val_loss = val_loss
mlflow.pytorch.log_model(self.model, "best_model")
# Learning rate scheduling
self.scheduler.step(val_loss)
logger.info(f'Epoch {epoch}: Train Loss={train_loss:.4f}, '
f'Train Acc={train_acc:.4f}, Val Loss={val_loss:.4f}, '
f'Val Acc={val_acc:.4f}')
def test(self):
test_loss, test_acc, test_preds, test_targets = self.validate(self.test_loader)
self.log_plots(test_targets, test_preds, 'test')
mlflow.log_metrics({
'test_loss': test_loss,
'test_accuracy': test_acc
})
return test_loss, test_acc
def main():
# Set random seeds
torch.manual_seed(42)
np.random.seed(42)
# MLflow setup
mlflow.set_tracking_uri('file:./mlruns')
mlflow.set_experiment('deepfake_cross_attention')
# Configuration
DATA_DIR = '/kaggle/input/3body-filtered-v2-10k' # Adjust as needed
IMAGE_SIZE = 224
BATCH_SIZE = 64
NUM_EPOCHS = 15 #Change in future for main run
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Get data
train_loader, val_loader, test_loader = get_dataloaders(
DATA_DIR, IMAGE_SIZE, BATCH_SIZE
)
with mlflow.start_run():
# Log parameters
mlflow.log_params({
'model_type': 'cross_attention',
'image_size': IMAGE_SIZE,
'batch_size': BATCH_SIZE,
'num_epochs': NUM_EPOCHS,
'optimizer': 'AdamW',
'backbone_lr': 1e-5,
'attention_lr': 5e-5,
'classifier_lr': 1e-4,
'weight_decay': 0.01
})
# Create and train model
model = DeepfakeCrossAttention()
trainer = DeepfakeTrainer(model, train_loader, val_loader, test_loader, DEVICE)
# Train
trainer.train(NUM_EPOCHS)
# Test
test_loss, test_acc = trainer.test()
logger.info(f'Test Loss: {test_loss:.4f}, Test Accuracy: {test_acc:.4f}')
if __name__ == '__main__':
main()