-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_coco_clip.py
273 lines (238 loc) · 6.88 KB
/
train_coco_clip.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
"""
@author: Adityam Ghosh
Date: 10-29-2023
"""
import argparse
import numpy as np
import pandas as pd
import polars as pol
import torch
import torch.utils.data as td
import pytorch_lightning as pl
import torchvision
import albumentations as alb
import os
import sys
import yaml
import zipfile
from pytorch_lightning.callbacks import (
EarlyStopping,
ModelCheckpoint,
RichProgressBar,
StochasticWeightAveraging,
)
from pytorch_lightning.loggers.wandb import WandbLogger
from transformers import CLIPTokenizerFast, CLIPModel, CLIPConfig, CLIPImageProcessor
from trainer_clip import LitCLIP
from utility.datasets import CocoCLIPDataset
from utility.transform_data import (
HorizontalFlip,
CenterSquareCrop,
)
torch.cuda.empty_cache()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="clip_training", description="To train the CLIP model"
)
parser.add_argument(
"--train_data_path",
"-P",
required=True,
type=str,
help="the csv file path for the train data",
)
parser.add_argument(
"--val_data_path",
"-V",
required=True,
type=str,
help="the csv file path for the val data",
)
parser.add_argument(
"--max_epochs",
"-E",
required=False,
type=int,
default=500,
help="maximum number of epochs to train the model",
)
parser.add_argument(
"--early_stopping_patience",
"-e",
required=False,
type=int,
default=10,
help="number of iterations to wait before early stopping",
)
parser.add_argument(
"--checkpoint_dir",
"-C",
required=False,
type=str,
default="./checkpoints",
help="the directory where the checkpoints will be saved",
)
parser.add_argument(
"--checkpoint_filename",
"-c",
required=False,
type=str,
default="model_checkpoint",
help="the name of the checkpoint file to be saved as",
)
parser.add_argument(
"--data_size",
"-D",
required=False,
type=float,
default=1.0,
help="the amount of data to train on.",
)
parser.add_argument(
"--accumulate_grad_batches",
"-a",
required=False,
type=int,
default=1,
help="the number of batches to accumulate gradients",
)
parser.add_argument(
"--use_swa",
"-s",
required=False,
type=int,
default=1,
help="whether to use SWA or not.",
)
parser.add_argument(
"--load_pretrained_checkpoint",
"-l",
required=False,
type=str,
default="",
help="load any previous checkpoint of the model to restart training",
)
parser.add_argument(
"--learning_rate",
"-L",
required=False,
type=float,
default=1e-4,
help="the learning rate for the model",
)
parser.add_argument(
"--gradient_clip_val",
"-g",
required=False,
type=float,
default=20.0,
help="the gradient clipping value",
)
args = parser.parse_args()
train_data_path = args.train_data_path
val_data_path = args.val_data_path
max_epochs = args.max_epochs
early_stopping_patience = args.early_stopping_patience
checkpoint_dir = args.checkpoint_dir
checkpoint_filename = args.checkpoint_filename
data_size = args.data_size
accumulate_grad_batches = args.accumulate_grad_batches
use_swa = args.use_swa
load_pretrained_checkpoint = args.load_pretrained_checkpoint
learning_rate = args.learning_rate
gradient_clip_val = args.gradient_clip_val
assert 0 < data_size <= 1, "Expected data size to be within the range (0, 1]"
assert use_swa in [0, 1], "Expected use_swa to be either 0/1"
train_csv_data = pol.read_csv(train_data_path)
val_csv_data = pol.read_csv(val_data_path)
train_csv_data = train_csv_data.select(
["file_name", "image_path", "caption"]
).sample(fraction=data_size, seed=32)
val_csv_data = val_csv_data.select(["file_name", "image_path", "caption"]).sample(
n=2000, seed=32
)
train_csv_data = train_csv_data.to_pandas()
val_csv_data = val_csv_data.to_pandas()
train_transforms = alb.Compose(
[
alb.SmallestMaxSize(256, always_apply=True),
CenterSquareCrop(224),
alb.ColorJitter(),
HorizontalFlip(),
alb.Resize(224, 224, always_apply=True),
]
)
val_transforms = alb.Compose([alb.Resize(224, 224, always_apply=True)])
tokenizer = CLIPTokenizerFast.from_pretrained("openai/clip-vit-base-patch32")
img_processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch32")
train_ds = CocoCLIPDataset(
data=train_csv_data,
text_tokenizer=tokenizer,
img_processor=img_processor,
transformations=train_transforms,
)
val_ds = CocoCLIPDataset(
data=val_csv_data,
text_tokenizer=tokenizer,
img_processor=img_processor,
transformations=val_transforms,
)
train_dl = td.DataLoader(
train_ds,
batch_size=512,
shuffle=True,
num_workers=12,
pin_memory=True,
)
val_dl = td.DataLoader(
val_ds,
batch_size=32,
shuffle=False,
num_workers=12,
pin_memory=True,
)
clip_model = CLIPModel(CLIPConfig())
model = LitCLIP(clip_model, lr=learning_rate)
early_stop = EarlyStopping(
monitor="val_loss", mode="min", patience=early_stopping_patience, verbose=True
)
model_chkpt = ModelCheckpoint(
monitor="val_loss",
mode="min",
dirpath=checkpoint_dir,
filename=checkpoint_filename,
save_on_train_epoch_end=False,
verbose=True,
)
rich_prog_bar = RichProgressBar()
swa = StochasticWeightAveraging(swa_lrs=[learning_rate])
callbacks = (
[early_stop, model_chkpt, rich_prog_bar, swa]
if use_swa
else [early_stop, model_chkpt, rich_prog_bar]
)
logger = WandbLogger(
project="MobileCLIP",
name="clip_model_coco",
)
logger.watch(model, log="gradients", log_freq=50)
trainer = pl.Trainer(
accelerator="cuda",
strategy="ddp" if torch.cuda.device_count() > 1 else "auto",
devices=torch.cuda.device_count(),
precision="16-mixed",
max_epochs=max_epochs,
callbacks=callbacks,
logger=logger,
gradient_clip_val=gradient_clip_val,
gradient_clip_algorithm="value",
accumulate_grad_batches=accumulate_grad_batches,
)
trainer.fit(
model,
train_dataloaders=train_dl,
val_dataloaders=val_dl,
ckpt_path=None
if load_pretrained_checkpoint == ""
else load_pretrained_checkpoint,
)