-
Notifications
You must be signed in to change notification settings - Fork 17
/
engine_IC.py
291 lines (240 loc) · 9.54 KB
/
engine_IC.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
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved.
"""Train/Evaluation workflow."""
import os
import random
import json
import open_clip
from open_clip import create_model_and_transforms, trace_model, get_tokenizer, create_loss
import open_clip.utils.checkpoint as cu
import open_clip.utils.distributed as du
import open_clip.utils.logging as logging
import open_clip.utils.misc as misc
import numpy as np
import torch
from datasets import loader
from torchvision import transforms
from open_clip.utils.meters import EpochTimer, TrainMeter, ValMeter
from sklearn.metrics import average_precision_score, roc_auc_score
from binary_focal_loss import BinaryFocalLoss
import torch.distributed as dist
import matplotlib.pyplot as plt
from open_clip.model import get_cast_dtype
from open_clip.utils.env import checkpoint_pathmgr as pathmgr
logger = logging.get_logger(__name__)
def _convert_to_rgb(image):
return image.convert('RGB')
def train_epoch(
train_loader,
model,
optimizer,
tokenizer,
cfg
):
"""
Perform the training for one epoch.
Args:
train_loader (loader): training loader.
model (model): the model to train.
optimizer (optim): the optimizer to perform optimization on the model's
parameters.
scaler (GradScaler): the `GradScaler` to help perform the steps of gradient scaling.
train_meter (TrainMeter): training meters to log the training performance.
cur_epoch (int): current epoch of training.
cfg (CfgNode): configs. Details can be found in
open_clip/config/defaults.py
"""
# Enable train mode.
model.train()
all_loss = 0.0
for cur_iter, (inputs, types, labels) in enumerate(train_loader):
if cfg.NUM_GPUS:
labels = labels.cuda()
preds, preds2 = model(tokenizer, inputs, types, None)
loss_fun = BinaryFocalLoss()
loss_fun = loss_fun.cuda()
# Compute the loss.
loss = loss_fun(preds, labels.float()) + loss_fun(preds2, labels.float())
# check Nan Loss.
misc.check_nan_losses(loss)
# Perform the backward pass.
optimizer.zero_grad()
loss.backward()
# Update the parameters.
optimizer.step()
# dist.all_reduce(loss)
loss_value = loss.item()
all_loss = all_loss + loss_value
all_loss = all_loss / (cur_iter + 1)
print("train_loss: ", all_loss)
return all_loss
@torch.no_grad()
def eval_epoch(val_loader, model, cfg, tokenizer, mode=None):
"""
Evaluate the model on the val set.
Args:
val_loader (loader): data loader to provide validation data.
model (model): model to evaluate the performance.
val_meter (ValMeter): meter instance to record and calculate the metrics.
cur_epoch (int): number of the current epoch of training.
cfg (CfgNode): configs. Details can be found in
open_clip/config/defaults.py
"""
# Evaluation mode enabled. The running stats would not be updated.
model.eval()
total_label = torch.Tensor([]).cuda()
total_pred = torch.Tensor([]).cuda()
for cur_iter, (inputs, types, labels) in enumerate(val_loader):
if cfg.NUM_GPUS:
labels = labels.cuda()
preds, _ = model(tokenizer, inputs, types, None)
total_pred = torch.cat((total_pred, preds), 0)
total_label = torch.cat((total_label, labels), 0)
total_pred = total_pred.cpu().numpy() #.squeeze()
total_label = total_label.cpu().numpy()
print("Predict " + mode + " set: ")
total_roc, total_pr = aucPerformance(total_pred, total_label)
return total_roc
def aucPerformance(mse, labels, prt=True):
roc_auc = roc_auc_score(labels, mse)
ap = average_precision_score(labels, mse)
if prt:
print("AUC-ROC: %.4f, AUC-PR: %.4f" % (roc_auc, ap))
return roc_auc, ap;
def train(cfg):
"""
Train a model on train set and evaluate it on val set.
Args:
cfg (CfgNode): configs. Details can be found in open_clip/config/defaults.py
"""
# Set up environment.
du.init_distributed_training(cfg)
# Set random seed from configs.
np.random.seed(cfg.RNG_SEED)
torch.manual_seed(cfg.RNG_SEED)
if cfg.NUM_GPUS:
device = torch.cuda.current_device()
# Build the model and print model statistics.
cf = './open_clip/model_configs/ViT-B-16-plus-240.json'
with open(cf, 'r') as f:
model_cfg = json.load(f)
embed_dim = model_cfg["embed_dim"]
vision_cfg = model_cfg["vision_cfg"]
text_cfg = model_cfg["text_cfg"]
cast_dtype = get_cast_dtype('fp32')
quick_gelu = False
model = open_clip.model.InCTRL(cfg, embed_dim, vision_cfg, text_cfg, quick_gelu, cast_dtype=cast_dtype)
if torch.cuda.is_available():
assert (
cfg.NUM_GPUS <= torch.cuda.device_count()
), "Cannot use more GPU devices than available"
else:
assert (
cfg.NUM_GPUS == 0
), "Cuda is not available. Please set `NUM_GPUS: 0 for running on CPUs."
if cfg.NUM_GPUS:
# Transfer the model to the current GPU device
model = model.cuda(device=device)
# Use multi-process data parallel model in the multi-gpu setting
if cfg.NUM_GPUS > 1:
# Make model replica operate on the current device
model = torch.nn.parallel.DistributedDataParallel(
module=model, device_ids=[device], output_device=device
)
transform = transforms.Compose([
transforms.Resize(size=240, interpolation=transforms.InterpolationMode.BICUBIC),
transforms.CenterCrop(size=(240, 240)),
_convert_to_rgb,
transforms.ToTensor(),
transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
])
# Load a checkpoint to resume training if applicable.
with pathmgr.open("./vit_b_16_plus_240-laion400m_e32-699c4b84.pt", "rb") as f:
checkpoint = torch.load(f, map_location="cpu")
start_epoch = 0
# model = model.module
model.load_state_dict(checkpoint, strict=False)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, betas=[0.9, 0.999])
# Create the train and val loaders.
train_loader = loader.construct_loader(cfg, "train", transform)
test_loader = loader.construct_loader(cfg, "test", transform)
tokenizer = open_clip.get_tokenizer('ViT-B-16-plus-240')
# Perform the training loop.
logger.info("Start epoch: {}".format(start_epoch + 1))
epoch_losses = []
epoch_timer = EpochTimer()
for cur_epoch in range(start_epoch, 10):
print("Epoch: ", cur_epoch)
# Train for one epoch.
epoch_timer.epoch_tic()
epoch_loss = train_epoch(
train_loader,
model,
optimizer,
tokenizer,
cfg,
)
epoch_losses.append(epoch_loss)
epoch_timer.epoch_toc()
logger.info(
f"Epoch {cur_epoch} takes {epoch_timer.last_epoch_time():.2f}s. Epochs "
f"from {start_epoch} to {cur_epoch} take "
f"{epoch_timer.avg_epoch_time():.2f}s in average and "
f"{epoch_timer.median_epoch_time():.2f}s in median."
)
logger.info(
f"For epoch {cur_epoch}, each iteraction takes "
f"{epoch_timer.last_epoch_time()/len(train_loader):.2f}s in average. "
f"From epoch {start_epoch} to {cur_epoch}, each iteraction takes "
f"{epoch_timer.avg_epoch_time()/len(train_loader):.2f}s in average."
)
path = "./tmp/checkpoints/checkpoint_" + str(cur_epoch + 1) + ".pyth"
torch.save(model.state_dict(), path)
total_roc = eval_epoch(train_loader, model, cfg, tokenizer, "train")
test_roc = eval_epoch(test_loader, model, cfg, tokenizer, "test")
def drawing(cfg, data, xlabel, ylabel, dir):
plt.switch_backend('Agg')
plt.figure()
plt.plot(data, 'b', label='loss')
plt.ylabel(ylabel)
plt.xlabel(xlabel)
plt.legend()
plt.savefig(os.path.join(cfg.OUTPUT_DIR, dir))
def test(cfg, load=None, mode = None):
"""
Perform testing on the pretrained model.
Args:
cfg (CfgNode): configs. Details can be found in open_clip/config/defaults.py
"""
# Set up environment.
du.init_distributed_training(cfg)
# Set random seed from configs.
np.random.seed(cfg.RNG_SEED)
torch.manual_seed(cfg.RNG_SEED)
# Setup logging format.
logging.setup_logging(cfg.OUTPUT_DIR)
device = torch.cuda.current_device()
transform = transforms.Compose([
transforms.Resize(size=240, interpolation=transforms.InterpolationMode.BICUBIC),
transforms.CenterCrop(size=(240, 240)),
_convert_to_rgb,
transforms.ToTensor(),
transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
])
cf = './open_clip/model_configs/ViT-B-16-plus-240.json'
with open(cf, 'r') as f:
model_cfg = json.load(f)
embed_dim = model_cfg["embed_dim"]
vision_cfg = model_cfg["vision_cfg"]
text_cfg = model_cfg["text_cfg"]
cast_dtype = get_cast_dtype('fp32')
quick_gelu = False
model = open_clip.model.InCTRL(cfg, embed_dim, vision_cfg, text_cfg, quick_gelu, cast_dtype=cast_dtype)
model = model.cuda(device=device)
cu.load_test_checkpoint(cfg, model)
tokenizer = open_clip.get_tokenizer('ViT-B-16-plus-240')
if load == None:
load = loader.construct_loader(cfg, "test", transform)
mode = "test"
# Create meters.
total_roc = eval_epoch(load, model, cfg, tokenizer, mode)
return total_roc