-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclip_finetune_twi.py
280 lines (208 loc) · 9.34 KB
/
clip_finetune_twi.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
# External imports
import torch
import os
import torch.nn.functional as F
from transformers import CLIPProcessor, CLIPModel
from torch.utils.data import DataLoader
import glob
import pandas as pd
from sklearn.model_selection import train_test_split
from dataclasses import dataclass
from rich.progress import track
from rich import print
from rich.console import Console
import wandb
# Local imports
from src.utils.pickle_handler import save_object, load_object
from src.utils.train_argparser import build_arg_parser
from src.datasets import ImageTextDataset
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.cuda.empty_cache()
# Constants
PICKLE_REPO = './saved/pickles'
MODEL_REPO = './saved/models'
INDEX_REPO = './saved/index'
# Dataclass defining the experiment hparams
@dataclass
class ExperimentConfig:
epochs: int
batch_size: int
lr: float
# Method for ensureing that folders for outputs are there
def build_expected_folders():
if not os.path.isdir(os.path.abspath(PICKLE_REPO)):
os.makedirs(os.path.abspath(PICKLE_REPO))
if not os.path.isdir(os.path.abspath(MODEL_REPO)):
os.makedirs(os.path.abspath(MODEL_REPO))
if not os.path.isdir(os.path.abspath(INDEX_REPO)):
os.makedirs(os.path.abspath(INDEX_REPO))
#===========================================================================
# TRAIN AND VALIDATION PROCEDURES
#===========================================================================
def fn_train(
epoch: int,
model: CLIPModel,
optimizer: any,
train_loader: DataLoader
):
train_total, train_loss, train_correct = 0, 0, 0
for batch in train_loader:
optimizer.zero_grad()
train_total += 1
# Unpack the inputs and labels from the data loader
pixel_values, input_ids, attention_mask, labels = batch
# Forward pass
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
pixel_values=pixel_values,
return_loss=True)
loss = outputs.loss
train_loss += loss
# Calculate accuracy
logits = outputs.logits_per_image
predictions = torch.argmax(logits, dim=1)
train_correct += (predictions == labels).sum().item()
loss.backward()
optimizer.step()
mean_batch_loss = train_loss / train_total
train_accuracy = train_correct / len(train_loader.dataset)
wandb.log({"train": {"loss": mean_batch_loss, "accuracy": train_accuracy}, "epoch": epoch})
def fn_val(
model: CLIPModel,
val_loader: DataLoader
):
val_total, val_loss, val_correct = 0, 0, 0
with torch.no_grad():
for batch in val_loader:
val_total += 1
# Unpack the inputs and labels from the data loader
pixel_values, input_ids, attention_mask, labels = batch
# Forward pass
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
pixel_values=pixel_values,
return_loss=True)
loss = outputs.loss
val_loss += loss
# Calculate accuracy
logits = outputs.logits_per_image
predictions = torch.argmax(logits, dim=1)
val_correct += (predictions == labels).sum().item()
mean_val_loss = val_loss / val_total
val_accuracy = val_correct / len(val_loader.dataset)
wandb.log({"val": {"loss": mean_val_loss, "accuracy": val_accuracy}})
# Define the training function
def train_clip(
experiment_config: ExperimentConfig,
train_dataset: DataLoader,
val_dataset: DataLoader,
model: CLIPModel
):
# Create the optimizer and scheduler
optimizer = torch.optim.AdamW(model.parameters(), lr=experiment_config.lr)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=experiment_config.epochs)
# Create the data loaders
train_loader = DataLoader(
train_dataset,
batch_size=experiment_config.batch_size,
shuffle=True)
val_loader = DataLoader(
val_dataset,
batch_size=experiment_config.batch_size,
shuffle=True)
# Train and validate the model
for epoch in range(experiment_config.epochs):
# Train over each batch and report to WandB
model.train() # Sets model into train mode, thus enable grads
fn_train(epoch, model, optimizer, train_loader)
# Evaluate the model on the validation set
model.eval() # Sets model into val mode, thus no grads
fn_val(model, val_loader)
model_name = f'clip_epochs{epoch}_bz{args.batch_size}_lr{args.batch_size}'
model.save_pretrained(f'{MODEL_REPO}/{model_name}')
torch.cuda.empty_cache()
# Update the learning rate scheduler
scheduler.step()
return model
#===========================================================================
# DATASET LOADING
#===========================================================================
def parse_img_id(path: str):
return int(path.split('.')[0].split('/')[-1:][0])
def load_dataset_pairs():
images_pickle_exists = os.path.exists(f'{PICKLE_REPO}/images.pkl')
labels_pickle_exists = os.path.exists(f'{PICKLE_REPO}/labels.pkl')
# If the pickles exist the load into memory and use those instead
if images_pickle_exists and labels_pickle_exists:
labels = load_object(PICKLE_REPO, 'labels')
images = load_object(PICKLE_REPO, 'images')
else:
df_train = pd.read_csv("data/input/dataset/train_set.csv")
df_hotels = pd.read_csv("data/input/dataset/hotel_info.csv")
df_chains = pd.read_csv("data/input/dataset/chain_info.csv")
images = glob.glob("data/images/train/*.jpg")
labels = []
for path in track(images, description="Preparing dataset..."):
img_id = parse_img_id(path)
hotel_id = df_train.loc[df_train['image_id'] == img_id]['hotel_id'].iloc[0]
chain_id = df_hotels.loc[df_hotels['hotel_id'] == hotel_id]['chain_id'].iloc[0]
chain_name = df_chains.loc[df_chains['chain_id'] == chain_id]['chain_name'].iloc[0]
labels.append(chain_name)
# Cache as Pickle files to be loaded for another run
save_object(images, PICKLE_REPO, 'images')
save_object(labels, PICKLE_REPO, 'labels')
return (images, labels)
#===========================================================================
# MAIN CONTROL
#===========================================================================
if __name__ == "__main__":
build_expected_folders()
console = Console()
with console.status("Loading dataset...") as status:
status.update(status=f'Parsing experiment setup...')
# Parse arguments and build experiment configuration
arg_parser = build_arg_parser()
args = arg_parser.parse_args()
experiment_config = ExperimentConfig(
int(args.epochs),
int(args.batch_size),
float(args.lr)
)
console.log("Experiment parsed successfully!")
console.log(experiment_config.__dict__)
#========================= LOAD DATA & MODEL ============================
status.update(status=f'Loading and splitting dataset...')
# Setup WandB
run = wandb.init( project="MLP", config=experiment_config.__dict__)
# Load pretrained CLIP model for finetuning
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
# Load and split dataset
images, labels = load_dataset_pairs()
images_train, images_val, labels_train, labels_val = train_test_split(
images,
labels,
test_size=0.20,
random_state=42
)
console.log("Dataset loaded and split successfully!")
status.update(status=f'Creating dataset loading classes..')
#======================== TRAIN AND VALIDATE ===========================
# Build dataset loaders, such that not all images are loaded in mem at once
train_dataset = ImageTextDataset(images_train, labels_train, processor, device)
val_dataset = ImageTextDataset(images_val, labels_val, processor, device)
console.log("Dataset loaders created successfully!")
status.update(status=f'Training and validating CLIP..')
# Train and validate model
model = train_clip(experiment_config, train_dataset, val_dataset, model)
console.log("Trained successfully!")
#============================= FINALISE ================================
model_name = f'clip_epochs{args.epochs}_bz{args.batch_size}_lr{args.batch_size}'
status.update(status=f'Saving trained model into {MODEL_REPO}/{model_name}..')
# Save model parameters
model.save_pretrained(f'{MODEL_REPO}/{model_name}')
wandb.finish()