-
Notifications
You must be signed in to change notification settings - Fork 3
/
cliptrainer.py
executable file
·308 lines (256 loc) · 9.48 KB
/
cliptrainer.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
303
304
305
306
307
import argparse, os, sys, datetime, glob
import numpy as np
import time
import itertools
import torch
import torchvision
import pytorch_lightning as pl
from torch.linalg import multi_dot
from packaging import version
from omegaconf import OmegaConf
from torch.utils.data import random_split, DataLoader, Dataset
from functools import partial
from PIL import Image
from helpers.helpers import *
from pytorch_lightning import seed_everything
from pytorch_lightning.trainer import Trainer
from pytorch_lightning.callbacks import Callback
from pytorch_lightning.callbacks import ModelCheckpoint
# from pytorch_lightning.utilities.distributed import rank_zero_only
from pytorch_lightning.utilities.rank_zero import rank_zero_only
from pytorch_lightning.utilities import rank_zero_info
from zoodatasets.sampler import ZooDataset
# from data.base import Txt2ImgIterableBaseDataset
from utils.util import instantiate_from_config
# from utils import AvgMeter, get_lr
from tqdm import tqdm
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def get_parser(**parser_kwargs):
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
parser = argparse.ArgumentParser(description='clip encoder Training')
parser.add_argument('--data', default='dataset', type=str, help='dataset root')
parser.add_argument('--data_root', default='../Datasets/', type=str, help='dataset root for cifar10, mnist, ..')
parser.add_argument('--topk', default=None, type=int, help='number of sample per dataset in training loader')
parser.add_argument('--dataset', default='joint', type=str, help='dataset choice amoung'
' [mnist, svhn, cifar10, stl10, joint')
parser.add_argument('--split', default='train', type=str, help='dataset split{ train, test, val]')
parser.add_argument('--ae_type', default='ldm', type=str, help='auto encoder type [ldm, vqvae, simple]')
parser.add_argument('--save_path', default='clipcheckpoints', type=str, help='checkpointys folders')
parser.add_argument('--gpus', default=0, type=int, help='device')
parser.add_argument('--lr', default=1e-4, type=float, help='learning rate')
parser.add_argument('--weight_decay', default=0.0, type=float, help='weight decaay')
parser.add_argument('--patience', default=10, type=int, help='scheduler patience')
parser.add_argument('--factor', default=0.5, type=float, help='scheduler param')
# parser.add_argument('--num_workers', default=4, type=int, help='device')
parser.add_argument('--n_epochs', default=100, type=int, help='max epoch')
parser.add_argument(
"-n",
"--name",
type=str,
const=True,
default="adt",
nargs="?",
help="postfix for logdir",
)
parser.add_argument(
"-r",
"--resume",
type=str,
const=True,
default="",
nargs="?",
help="resume from logdir or checkpoint in logdir",
)
parser.add_argument(
"-b",
"--base",
nargs="*",
metavar="base_config.yaml",
help="paths to base configs. Loaded from left-to-right. "
"Parameters can be overwritten or added with command-line options of the form `--key value`.",
default='clips/configs/base_config.yaml',
)
parser.add_argument(
"-t",
"--train",
type=str2bool,
const=True,
default=False,
nargs="?",
help="train",
)
parser.add_argument(
"--no-test",
type=str2bool,
const=True,
default=False,
nargs="?",
help="disable test",
)
parser.add_argument(
"-p",
"--project",
help="name of new or path to existing project"
)
parser.add_argument(
"-d",
"--debug",
type=str2bool,
nargs="?",
const=True,
default=False,
help="enable post-mortem debugging",
)
parser.add_argument(
"-s",
"--seed",
type=int,
default=23,
help="seed for seed_everything",
)
parser.add_argument(
"-f",
"--postfix",
type=str,
default="",
help="post-postfix for default name",
)
parser.add_argument(
"-l",
"--logdir",
type=str,
default="logs",
help="directory for logging dat shit",
)
parser.add_argument(
"--scale_lr",
type=str2bool,
nargs="?",
const=True,
default=True,
help="scale base-lr by ngpu * batch_size * n_accumulate",
)
return parser
def nondefault_trainer_args(opt):
parser = argparse.ArgumentParser()
parser = Trainer.add_argparse_args(parser)
args = parser.parse_args([])
return sorted(k for k in vars(args) if getattr(opt, k) != getattr(args, k))
class AvgMeter:
def __init__(self, name="Metric"):
self.name = name
self.reset()
def reset(self):
self.avg, self.sum, self.count = [0] * 3
def update(self, val, count=1):
self.count += count
self.sum += val * count
self.avg = self.sum / self.count
def __repr__(self):
text = f"{self.name}: {self.avg:.4f}"
return text
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group["lr"]
def train_epoch(model, train_loader, optimizer, lr_scheduler, step):
loss_meter = AvgMeter()
tqdm_object = tqdm(train_loader, total=len(train_loader))
for batch in tqdm_object:
# batch = {k: v.to(CFG.device) for k, v in batch.items() if k != "caption"}
loss = model(batch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step == "batch":
lr_scheduler.step()
count = batch["weight"].size(0)
loss_meter.update(loss.item(), count)
tqdm_object.set_postfix(train_loss=loss_meter.avg, lr=get_lr(optimizer))
return loss_meter
def valid_epoch(model, valid_loader):
loss_meter = AvgMeter()
tqdm_object = tqdm(valid_loader, total=len(valid_loader))
for batch in tqdm_object:
# batch = {k: v.to(device) for k, v in batch.items() if k != "caption"}
loss = model(batch)
count = batch["weight"].size(0)
loss_meter.update(loss.item(), count)
tqdm_object.set_postfix(valid_loss=loss_meter.avg)
return loss_meter
def main():
optimizer = torch.optim.AdamW(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode="min", patience=args.patience, factor=args.factor
)
step = "epoch"
best_loss = float('inf')
best_train_loss = float('inf')
for epoch in range(args.n_epochs):
print(f"Epoch: {epoch + 1}")
model.train()
train_loss = train_epoch(model, train_loader, optimizer, lr_scheduler, step)
model.eval()
with torch.no_grad():
valid_loss = valid_epoch(model, valid_loader)
if valid_loss.avg < best_loss:
best_loss = valid_loss.avg
torch.save(model.state_dict(), "save best val checkpoint")
print("Saved Best Model!")
if train_loss.avg < best_train_loss:
best_train_loss = train_loss.avg
torch.save(model, "clipcheckpoints/save full model")
print("Saved Best training Model!")
print(f'best train loss is : {best_train_loss}')
def my_collate(batch):
sample = {}
data = [item['weight'] for item in batch]
conds = [item['dataset'] for item in batch]
target =[]
for items in conds:
target += [item for item in items]
data = torch.cat(data, 0)
sample['weight']=data
sample['dataset'] = target
return sample
def m_collate(batch):
sample = {}
data = [item['weight'] for item in batch]
cond = [item['dataset'] for item in batch]
data = torch.stack(data, 0).type(torch.float32)
sample['weight']=data
sample['dataset'] = cond
return sample
if __name__ == "__main__":
now = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
sys.path.append(os.getcwd())
parser = get_parser()
args = parser.parse_args()
opt, unknown = parser.parse_known_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
trainset = ZooDataset(root=args.data, dataset=args.dataset, split=args.split, normalize=False, num_sample=5)
valset = ZooDataset(root=args.data, dataset=args.dataset, split='val', normalize=False, num_sample=5)
# train_loader = DataLoader(trainset, shuffle=True, batch_size=100, num_workers=4)
# valid_loader = DataLoader(valset, shuffle=False, batch_size=100, num_workers=4)
train_loader = DataLoader(trainset, shuffle=True, batch_size=64, collate_fn=my_collate, num_workers=4)
valid_loader = DataLoader(valset, shuffle=False, batch_size=24, collate_fn=my_collate)
nowname= opt.name+now
print(opt.base)
print('----------------------')
configs = [OmegaConf.load(opt.base)]
cli = OmegaConf.from_dotlist(unknown)
config = OmegaConf.merge(*configs, cli)
model = instantiate_from_config(config.model)
# args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
main()