-
Notifications
You must be signed in to change notification settings - Fork 3
/
mlp.py
463 lines (400 loc) · 15.6 KB
/
mlp.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import math
import os
import time
import json
import argparse
import numpy as np
import pandas as pd
from sklearn.metrics import (
r2_score,
mean_squared_error,
mean_absolute_error,
mean_absolute_percentage_error,
)
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import open_clip
from utils import (
LinearProbDataset,
count_trainable_parameters,
count_all_parameters,
set_random_seed,
)
from tqdm import tqdm
from loguru import logger
"""
Frozen CoCa to liner probe
"""
def create_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="Beijing",
choices=["Beijing", "Shanghai", "Guangzhou", "Shenzhen"],
help="which dataset",
)
parser.add_argument(
"--test_file",
type=str,
default="./data/downstream_task/Beijing_test.csv",
help="test file path, if None then only train and val",
)
parser.add_argument(
"--linear_probe", type=bool, default=True, help="training if True else testing"
)
parser.add_argument(
"--indicator",
type=str,
default="carbon",
choices=["carbon", "population", "gdp"],
help="indicator",
)
parser.add_argument("--lr", type=float, default=0.0003, help="learning rate")
parser.add_argument("--wd", type=float, default=0.01, help="weight decay")
parser.add_argument(
"--drop_out", type=float, default=0.01, help="dropout in linear probe"
)
parser.add_argument("--batch_size", type=int, default=2, help="batch size")
parser.add_argument("--epoch_num", type=int, default=100, help="epoch number")
parser.add_argument(
"--log_every_n_steps", type=int, default=100, help="log every n steps"
)
parser.add_argument(
"--pretrained_model",
type=str,
default="./checkpoints/best_model.bin",
help="pretrained model after running main.py",
)
parser.add_argument(
"--img_embedding_dim", type=int, default=768, help="image encoder output dim"
)
parser.add_argument("--seed", type=int, default=132, help="random seed")
parser.add_argument(
"--logging_dir", type=str, default="logs/downtask1", help="logging directory"
)
parser.add_argument(
"--checkpoint_dir",
type=str,
default="checkpoints/downtask1",
help="checkpoint path",
)
# MLP parameters
parser.add_argument(
"--project_dim", type=int, default=256, help="project dimension"
)
parser.add_argument(
"--activation",
type=str,
default="relu",
choices=["relu", "gelu"],
help="activation function",
)
# data hyper-parameters
parser.add_argument(
"--train_dataset_ratio",
type=float,
default=0.8,
help="ratio of training dataset",
)
# parser.add_argument("--val_dataset_ratio", type=float, default=0.2,
# help="ratio of validation dataset")
args = parser.parse_args()
return args
class CoCaLinearProbe(nn.Module):
def __init__(self, coca_model, args):
super().__init__()
self.coca = coca_model
self.project = nn.Linear(args.img_embedding_dim, args.project_dim)
self.activation = nn.ReLU() if args.activation == "relu" else nn.GELU()
self.dropout = nn.Dropout(args.drop_out)
self.predict = nn.Linear(args.project_dim, 1)
def forward(self, image_features):
image_latent = self.coca.encode_image(image_features)
image_latent = self.project(image_latent)
image_latent = self.activation(image_latent)
image_latent = self.dropout(image_latent)
logits = self.predict(image_latent)
return logits.squeeze(1)
def create_datasets(args, transform):
"""To create train, val, test datasets."""
if args.dataset == "Beijing":
data = pd.read_csv("data/downstream_task/Beijing_train.csv")
elif args.dataset == "Shanghai":
data = pd.read_csv("data/downstream_task/Shanghai_train.csv")
elif args.dataset == "Shenzhen":
data = pd.read_csv("data/downstream_task/Shenzhen_train.csv")
elif args.dataset == "Guangzhou":
data = pd.read_csv("data/downstream_task/Guangzhou_train.csv")
else:
raise ValueError("dataset not found")
# split dataset into train, val, test
data = data.sample(frac=1).reset_index(drop=True) # shuffle rows
train_data = data[: int(len(data) * args.train_dataset_ratio)].reset_index(
drop=True
)
val_data = data[int(len(data) * args.train_dataset_ratio) :].reset_index(drop=True)
mean = np.mean(train_data[args.indicator])
std = np.std(train_data[args.indicator])
# create datasets
train_dataset = LinearProbDataset(
args.dataset, train_data, args.indicator, transform, mean, std, False
)
val_dataset = LinearProbDataset(
args.dataset, val_data, args.indicator, transform, mean, std, False
)
if args.test_file is not None:
test_data = pd.read_csv(args.test_file)
test_dataset = LinearProbDataset(
args.dataset, test_data, args.indicator, transform, mean, std, True
)
return train_dataset, val_dataset, test_dataset, mean, std
else:
return train_dataset, val_dataset, None, mean, std
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def train_one_epoch(model, criterion, data, epoch, optimizer, args, logger):
"""To train one epoch."""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.train()
dataloader = data["train_loader"]
num_batches_per_epoch = len(dataloader)
sample_digits = math.ceil(math.log(len(dataloader) * args.batch_size + 1, 10))
losses_m = {}
batch_time_m = AverageMeter()
data_time_m = AverageMeter() # data loading time
end = time.time()
for batch_count, batch in enumerate(dataloader):
step = num_batches_per_epoch * epoch + batch_count
(
images,
y,
) = batch # images: [batch_size, 3, 224, 224], y: [batch_size]
images = images.to(device=device, non_blocking=True)
y = y.to(device=device, non_blocking=True)
# print("y.shape: {}".format(y.shape))
data_time_m.update(time.time() - end)
optimizer.zero_grad()
predicts = model(images)
# print("predicts.shape: {}".format(predicts.shape))
# print("y.shape: {}".format(y.shape))
loss = criterion(predicts, y)
loss.backward()
# if args.grad_clip_norm is not None:
# torch.nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip_norm, norm_type=2.0)
optimizer.step()
batch_time_m.update(time.time() - end)
end = time.time()
batch_count += 1
if step % args.log_every_n_steps == 0:
batch_size = len(images)
num_samples = step * batch_size
samples_per_epoch = (
num_batches_per_epoch * batch_size
) # sample size per epoch
percent_complete = 100.0 * batch_count / num_batches_per_epoch
# NOTE loss is coarsely sampled
for key in ["mse", "r2", "rmse", "mae", "mape"]:
if key not in losses_m:
losses_m[key] = AverageMeter()
losses_m["mse"].update(loss.item(), batch_size)
losses_m["r2"].update(
r2_score(y.cpu().numpy(), predicts.detach().cpu().numpy()), batch_size
)
losses_m["rmse"].update(
np.sqrt(
mean_squared_error(y.cpu().numpy(), predicts.detach().cpu().numpy())
),
batch_size,
)
losses_m["mae"].update(
mean_absolute_error(y.cpu().numpy(), predicts.detach().cpu().numpy()),
batch_size,
)
losses_m["mape"].update(
mean_absolute_percentage_error(
y.cpu().numpy(), predicts.detach().cpu().numpy()
),
batch_size,
)
loss_log = " ".join(
[
f"{loss_name.capitalize()}: {loss_m.val:#.5g} ({loss_m.avg:#.5g})"
for loss_name, loss_m in losses_m.items()
]
)
samples_per_second = batch_size / batch_time_m.val
logger.info(
f"Train Epoch: {epoch} [{num_samples:>{sample_digits}}/{samples_per_epoch} ({percent_complete:.0f}%)] "
f"Data (t): {data_time_m.avg:.3f} "
f"Batch (t): {batch_time_m.avg:.3f}, {samples_per_second:#g}/s, "
# f"LR: {optimizer.param_groups[0]['lr']:5f} "
f"Metrics: " + loss_log
)
# Save train loss / etc. Using non avg meter values as loggers have their own smoothing
log_data = {
"data_time": data_time_m.val,
"batch_time": batch_time_m.val,
"samples_per_second": samples_per_second,
# "lr": optimizer.param_groups[0]["lr"]
}
log_data.update({name: val.val for name, val in losses_m.items()})
for name, val in log_data.items():
name = "train/" + name
logger.info({name: val, "step": step})
# resetting batch / data time meters per log window
batch_time_m.reset()
data_time_m.reset()
def evaluate(model, data, epoch, args, logger):
"""To evaluate on val dataset."""
metrics = {}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.eval()
dataloader = data["val_loader"]
all_y, all_predicts = [], []
with torch.no_grad():
for i, batch in enumerate(dataloader):
images, y = batch
images = images.to(device=device, non_blocking=True)
y = y.to(device=device, non_blocking=True)
y_hat = model(images)
all_y.append(y.cpu().numpy())
all_predicts.append(y_hat.cpu().numpy())
all_y = np.concatenate(all_y)
all_predicts = np.concatenate(all_predicts)
metrics["mse"] = mean_squared_error(all_y, all_predicts)
metrics["r2"] = r2_score(all_y, all_predicts)
metrics["rmse"] = np.sqrt(metrics["mse"])
metrics["mae"] = mean_absolute_error(all_y, all_predicts)
metrics["mape"] = mean_absolute_percentage_error(all_y, all_predicts)
logger.info(
f"Eval Epoch: {epoch} "
+ "\t".join([f"{k}: {round(v, 4):.4f}" for k, v in metrics.items()])
)
# for name, val in metrics.items():
# logger.info({f"val/{name}": val, "epoch": epoch})
return metrics
def inference(model, data, args, logger):
"""test on test dataset."""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.eval()
dataloader = data["test_loader"]
all_predicts = []
with torch.no_grad():
for i, batch in enumerate(dataloader):
images, y = batch
images = images.to(device=device, non_blocking=True)
y = y.to(device=device, non_blocking=True)
y_hat = model(images)
# all_y.append(y.cpu().numpy())
all_predicts.append(y_hat.cpu().numpy())
# all_y = np.concatenate(all_y)
all_predicts = np.concatenate(all_predicts)
# metrics["mse"] = mean_squared_error(all_y, all_predicts)
# metrics["r2"] = r2_score(all_y, all_predicts)
# metrics["rmse"] = np.sqrt(metrics["mse"])
# metrics["mae"] = mean_absolute_error(all_y, all_predicts)
# metrics["mape"] = mean_absolute_percentage_error(all_y, all_predicts)
# logger.info(
# f"Test: " + "\t".join([f"{k}: {round(v, 4):.4f}" for k, v in metrics.items()])
# )
y_hat = [item * data["std"] + data["mean"] for item in all_predicts]
test_data = pd.read_csv(args.test_file)
test_data[args.indicator + "_predict"] = y_hat
test_data.to_csv(args.test_file[:-4] + "_predicted.csv", index=False)
# return metrics
def main():
args = create_args()
set_random_seed(args.seed)
# create logger
if not os.path.exists(args.logging_dir):
os.makedirs(args.logging_dir)
if not os.path.exists(args.checkpoint_dir):
os.makedirs(args.checkpoint_dir)
logger.remove(handler_id=None) # remove default logger
logger.add(os.path.join(args.logging_dir, str(args.seed) + ".log"), level="INFO")
logger.info(args)
# create model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
coca_model, _, transform = open_clip.create_model_and_transforms(
model_name="coca_ViT-L-14", pretrained=args.pretrained_model
)
model = CoCaLinearProbe(coca_model, args)
model.to(device)
for param in model.coca.parameters():
param.requires_grad = False
logger.info("model parameters: {}".format(count_all_parameters(model)))
logger.info(
"model trainable parameters: {}".format(count_trainable_parameters(model))
)
# tokenizer = open_clip.get_tokenizer("coca_ViT-L-14")
# create datasets
train_dataset, val_dataset, test_dataset, mean, std = create_datasets(
args, transform
)
logger.info("train dataset size: {}".format(len(train_dataset)))
logger.info("val dataset size: {}".format(len(val_dataset)))
logger.info("test dataset size: {}".format(len(test_dataset)))
# create dataloaders
train_dataloader = DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True, drop_last=True
)
val_dataloader = DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=False, drop_last=False
)
test_dataloader = DataLoader(
test_dataset, batch_size=args.batch_size, shuffle=False, drop_last=False
)
data = {}
data["train_loader"] = train_dataloader
data["val_loader"] = val_dataloader
data["test_loader"] = test_dataloader
data["mean"] = mean
data["std"] = std
# create optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.wd)
criterion = nn.MSELoss()
best_mse_val_loss = float("inf")
for epoch in tqdm(range(args.epoch_num), desc="Training"):
logger.info("Start epoch {}".format(epoch))
train_one_epoch(model, criterion, data, epoch, optimizer, args, logger)
completed_epoch = epoch + 1
cur_metrics = evaluate(model, data, completed_epoch, args, logger)
# Saving checkpoints.
# if args.save_logs:
# TODO maybe we should only save best checkpoints
checkpoint_dict = {
"epoch": completed_epoch,
"state_dict": model.state_dict(),
"optimizer": optimizer.state_dict(),
}
if cur_metrics["mse"] < best_mse_val_loss:
torch.save(
checkpoint_dict,
# os.path.join(args.checkpoint_dir, f"epoch_{completed_epoch}.pt"),
os.path.join(args.checkpoint_dir, "best.pt"),
)
best_mse_val_loss = cur_metrics["mse"]
best_checkpoint = torch.load(
os.path.join(args.checkpoint_dir, "best.pt"), map_location=torch.device("cpu")
)
model.load_state_dict(best_checkpoint["state_dict"])
model.to(device)
if args.test_file is not None:
inference(model, data, args, logger)
if __name__ == "__main__":
main()