-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransferlearningvit.py
223 lines (184 loc) · 8.14 KB
/
transferlearningvit.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
import torch
import torch.nn as nn
import torch.optim as optim
from transformers import AutoModel, AutoConfig
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import numpy as np
import pandas as pd
from tqdm import tqdm # Import tqdm for progress bars
import os
def get_device():
if torch.backends.mps.is_available():
device = torch.device("mps")
print ("MPS device found.")
else:
device = 'cpu'
print ("MPS device not found.")
return device
# Define a new classifier on top of the pre-trained encoder
class ViTClassifier(nn.Module):
def __init__(self, vit_model, num_classes, config):
super(ViTClassifier, self).__init__()
self.vit = vit_model
self.classifier = nn.Linear(config.hidden_size, num_classes)
def forward(self, x):
outputs = self.vit(x)
cls_output = outputs.last_hidden_state[:, 0, :] # Use the CLS token output
logits = self.classifier(cls_output)
return logits
# Define EarlyStopping class
class EarlyStopping:
def __init__(self, patience=5, verbose=False, delta=0, path='checkpoints.pt'):
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
self.path = path
def __call__(self, val_loss, model):
score = -val_loss
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model)
elif score < self.best_score + self.delta:
self.counter += 1
if self.verbose:
print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_loss, model)
self.counter = 0
def save_checkpoint(self, val_loss, model):
'''Saves model when validation loss decrease.'''
if self.verbose:
print(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}). Saving model ...')
torch.save(model.state_dict(), self.path)
self.val_loss_min = val_loss
def create_dirs(folder_path):
if not os.path.exists(folder_path):
os.makedirs(folder_path)
print(f"Created folder: {folder_path}")
else:
print(f"Folder already exists: {folder_path}")
def transfer_learning_pretrain_vit(model_checkpoint, train_loader, val_loader, test_loader, dataset, num_labels_ = 120,
learning_rate = 5e-5, num_epochs = 10, patience_ = 3):
device = get_device()
subfolder_name = model_checkpoint.split('/')[1]
create_dirs(f'results/transferlearning/{dataset}/{subfolder_name}')
create_dirs(f'checkpoints/transferlearning/{dataset}')
# Load the pre-trained ViT model
config = AutoConfig.from_pretrained(model_checkpoint)
pretrained_model = AutoModel.from_pretrained(model_checkpoint)
# vit_model = ViTModel.from_pretrained(model_)
pretrained_model.to(device)
# Freeze the pre-trained model parameters
for param in pretrained_model.parameters():
param.requires_grad = False
model = ViTClassifier(pretrained_model, num_classes=num_labels_, config = config)
model.to(device)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=learning_rate)
# Define learning rate scheduler
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', patience=3, factor=0.5, verbose=True)
# Initialize the early stopping object
early_stopping = EarlyStopping(patience=patience_, verbose=True, path = f'checkpoints/transferlearning/{dataset}/{subfolder_name}.pt')
# Metrics storage
train_loss_list = []
val_loss_list = []
test_loss_list = []
accuracy_list = []
precision_list = []
recall_list = []
f1_list = []
# Training and validation loop
for epoch in range(num_epochs):
# Training phase
model.train()
running_loss = 0.0
for inputs, labels in tqdm(train_loader, desc=f"Training Epoch {epoch+1}/{num_epochs}", unit="batch"):
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
avg_train_loss = running_loss / len(train_loader)
train_loss_list.append(avg_train_loss)
# Validation phase
model.eval()
val_running_loss = 0.0
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in tqdm(val_loader, desc=f"Validation Epoch {epoch+1}/{num_epochs}", unit="batch"):
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
val_running_loss += loss.item()
preds = torch.argmax(outputs, dim=1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
avg_val_loss = val_running_loss / len(val_loader)
val_loss_list.append(avg_val_loss)
accuracy = accuracy_score(all_labels, all_preds)
precision = precision_score(all_labels, all_preds, average='weighted')
recall = recall_score(all_labels, all_preds, average='weighted')
f1 = f1_score(all_labels, all_preds, average='weighted')
accuracy_list.append(accuracy)
precision_list.append(precision)
recall_list.append(recall)
f1_list.append(f1)
print(f"Epoch {epoch+1}/{num_epochs}, Train Loss: {avg_train_loss:.4f}, Val Loss: {avg_val_loss:.4f}, Accuracy: {accuracy:.4f}, Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}")
# Step the scheduler based on validation loss
scheduler.step(avg_val_loss)
# Early stopping
early_stopping(avg_val_loss, model)
if early_stopping.early_stop:
print("Early stopping")
break
# Load the best model
model.load_state_dict(torch.load(early_stopping.path))
# Test phase
model.eval()
test_running_loss = 0.0
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in tqdm(test_loader, desc="Testing", unit="batch"):
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
test_running_loss += loss.item()
preds = torch.argmax(outputs, dim=1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
avg_test_loss = test_running_loss / len(test_loader)
test_loss_list.append(avg_test_loss)
test_accuracy = accuracy_score(all_labels, all_preds)
test_precision = precision_score(all_labels, all_preds, average='weighted')
test_recall = recall_score(all_labels, all_preds, average='weighted')
test_f1 = f1_score(all_labels, all_preds, average='weighted')
print(f"Test Loss: {avg_test_loss:.4f}, Accuracy: {test_accuracy:.4f}, Precision: {test_precision:.4f}, Recall: {test_recall:.4f}, F1 Score: {test_f1:.4f}")
# Save metrics to a CSV file
metrics = {
'Train Loss': train_loss_list,
'Validation Loss': val_loss_list,
'Accuracy': accuracy_list,
'Precision': precision_list,
'Recall': recall_list,
'F1 Score': f1_list
}
metrics_df = pd.DataFrame(metrics)
metrics_df.to_csv(f'results/transferlearning/{dataset}/{subfolder_name}/training_metrics.csv', index=False)
# Sample list of test metrics
numbers = [test_accuracy, test_precision, test_recall, test_f1]
# Create a DataFrame from the list
df = pd.DataFrame(numbers, columns=['Numbers'])
# Save the DataFrame to a CSV file
df.to_csv(f'results/transferlearning/{dataset}/{subfolder_name}/test_metrics.csv', index=False)