File size: 15,393 Bytes
377dccd | 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 | # Copyright 2022-present, Lorenzo Bonicelli, Pietro Buzzega, Matteo Boschini, Angelo Porrello, Simone Calderara.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import csv
import math
import sys
from argparse import Namespace
from typing import Tuple
import torch
from datasets import get_dataset
from datasets.utils.continual_dataset import ContinualDataset
from models.utils.continual_model import ContinualModel
from typing import Tuple, List
from utils.loggers import *
from utils.mlflow_logger import MLFlowLogger
from utils.status import ProgressBar
import torch.nn.functional as F
import utils.metrics
def evaluate_ece(model: ContinualModel, dataset: ContinualDataset, last=False) -> Tuple[List[float], List[float], float, float, float]:
"""
Evaluates accuracy and computes AURC, FPR95, AUROC for each loader, then averages.
:param model: model to evaluate
:param dataset: continual dataset
:return: class-il acc, task-il acc, mean AURC, mean FPR95, mean AUROC
"""
status = model.net.training
model.net.eval()
accs, accs_mask_classes = [], []
ece_list = []
for k, test_loader in enumerate(dataset.test_loaders):
if last and k < len(dataset.test_loaders) - 1:
continue
correct, correct_mask_classes, total = 0.0, 0.0, 0.0
val_log = {'softmax' : [], 'correct' : [], 'logit' : [], 'target':[]}
for data in test_loader:
with torch.no_grad():
inputs, labels = data
inputs, labels = inputs.to(model.device), labels.to(model.device)
if 'class-il' not in model.COMPATIBILITY:
output = model(inputs, k)
else:
output = model(inputs)
softmax = F.softmax(output, dim=1)
_, pred_cls = softmax.max(1)
val_log['correct'].append(pred_cls.cpu().eq(labels.cpu().data.view_as(pred_cls)).numpy())
val_log['softmax'].append(softmax.cpu().data.numpy())
val_log['logit'].append(output.cpu().data.numpy())
val_log['target'].append(labels.cpu().data.numpy())
_, pred = torch.max(output.data, 1)
correct += torch.sum(pred == labels).item()
total += labels.shape[0]
if dataset.SETTING == 'class-il':
mask_classes(output, dataset, k)
_, pred_masked = torch.max(output.data, 1)
correct_mask_classes += pred_masked.eq(labels).sum().item()
for key in val_log :
val_log[key] = np.concatenate(val_log[key])
acc = 100. * val_log['correct'].mean()
ece = utils.metrics.calc_ece(val_log['softmax'], val_log['target'], bins=15)
accs.append(correct / total * 100 if 'class-il' in model.COMPATIBILITY else 0)
accs_mask_classes.append(correct_mask_classes / total * 100)
ece_list.append(ece*100)
model.net.train(status)
# averagge
mean_ece = np.mean(ece_list)
print('evaluation acc:', accs)
print(f'ECE per loader: {ece_list}')
print(f'Mean ECE: {mean_ece:.4f}')
return accs, accs_mask_classes
def evaluate_eceid(model: ContinualModel, dataset: ContinualDataset, last=False) -> Tuple[List[float], List[float], float, float, float]:
"""
Evaluates accuracy and computes AURC, FPR95, AUROC for each loader, then averages.
:param model: model to evaluate
:param dataset: continual dataset
:return: class-il acc, task-il acc, mean AURC, mean FPR95, mean AUROC
"""
status = model.net.training
model.net.eval()
accs, accs_mask_classes = [], []
ece_list = []
for k, test_loader in enumerate(dataset.test_loaders):
if last and k < len(dataset.test_loaders) - 1:
continue
correct, correct_mask_classes, total = 0.0, 0.0, 0.0
val_log = {'softmax' : [], 'correct' : [], 'logit' : [], 'target':[]}
for data in test_loader:
with torch.no_grad():
inputs, labels = data
inputs, labels = inputs.to(model.device), labels.to(model.device)
batch_size, _, H, W = inputs.shape
if 'class-il' not in model.COMPATIBILITY:
output = model(inputs, k)
else:
y_0 = torch.ones(batch_size, dataset.N_CLASSES).to(inputs.device) /dataset.N_CLASSES
z = model.net.f1(inputs)
output, z1 = model.net.f2(z, y_0)
softmax = F.softmax(output, dim=1)
_, pred_cls = softmax.max(1)
val_log['correct'].append(pred_cls.cpu().eq(labels.cpu().data.view_as(pred_cls)).numpy())
val_log['softmax'].append(softmax.cpu().data.numpy())
val_log['logit'].append(output.cpu().data.numpy())
val_log['target'].append(labels.cpu().data.numpy())
_, pred = torch.max(output.data, 1)
correct += torch.sum(pred == labels).item()
total += labels.shape[0]
if dataset.SETTING == 'class-il':
mask_classes(output, dataset, k)
_, pred_masked = torch.max(output.data, 1)
correct_mask_classes += pred_masked.eq(labels).sum().item()
for key in val_log :
val_log[key] = np.concatenate(val_log[key])
acc = 100. * val_log['correct'].mean()
ece = utils.metrics.calc_ece(val_log['softmax'], val_log['target'], bins=15)
accs.append(correct / total * 100 if 'class-il' in model.COMPATIBILITY else 0)
accs_mask_classes.append(correct_mask_classes / total * 100)
ece_list.append(ece*100)
model.net.train(status)
# average
mean_ece = np.mean(ece_list)
#
print('evaluation acc:', accs)
print(f'ECE per loader: {ece_list}')
print(f'Mean ECE: {mean_ece:.4f}')
return accs, accs_mask_classes
def mask_classes(outputs: torch.Tensor, dataset: ContinualDataset, k: int) -> None:
"""
Given the output tensor, the dataset at hand and the current task,
masks the former by setting the responses for the other tasks at -inf.
It is used to obtain the results for the task-il setting.
:param outputs: the output tensor
:param dataset: the continual dataset
:param k: the task index
"""
outputs[:, 0:k * dataset.N_CLASSES_PER_TASK] = -float('inf')
outputs[:, (k + 1) * dataset.N_CLASSES_PER_TASK:
dataset.N_TASKS * dataset.N_CLASSES_PER_TASK] = -float('inf')
def evaluate(model: ContinualModel, dataset: ContinualDataset, last=False) -> Tuple[list, list]:
"""
Evaluates the accuracy of the model for each past task.
:param model: the model to be evaluated
:param dataset: the continual dataset at hand
:return: a tuple of lists, containing the class-il
and task-il accuracy for each task
"""
status = model.net.training
model.net.eval()
accs, accs_mask_classes = [], []
for k, test_loader in enumerate(dataset.test_loaders):
if last and k < len(dataset.test_loaders) - 1:
continue
correct, correct_mask_classes, total = 0.0, 0.0, 0.0
for data in test_loader:
with torch.no_grad():
inputs, labels = data
inputs, labels = inputs.to(model.device), labels.to(model.device)
if 'class-il' not in model.COMPATIBILITY:
outputs = model(inputs, k)
else:
outputs = model(inputs)
_, pred = torch.max(outputs.data, 1)
correct += torch.sum(pred == labels).item()
total += labels.shape[0]
if dataset.SETTING == 'class-il':
mask_classes(outputs, dataset, k)
_, pred = torch.max(outputs.data, 1)
correct_mask_classes += torch.sum(pred == labels).item()
accs.append(correct / total * 100
if 'class-il' in model.COMPATIBILITY else 0)
accs_mask_classes.append(correct_mask_classes / total * 100)
model.net.train(status)
print('evaluation acc:')
print(accs)
return accs, accs_mask_classes
def evaluateid(model: ContinualModel, dataset: ContinualDataset, last=False) -> Tuple[list, list]:
"""
Evaluates the accuracy of the model for each past task.
:param model: the model to be evaluated
:param dataset: the continual dataset at hand
:return: a tuple of lists, containing the class-il
and task-il accuracy for each task
"""
status = model.net.training
model.net.eval()
accs, accs_mask_classes = [], []
for k, test_loader in enumerate(dataset.test_loaders):
if last and k < len(dataset.test_loaders) - 1:
continue
correct, correct_mask_classes, total = 0.0, 0.0, 0.0
for data in test_loader:
with torch.no_grad():
inputs, labels = data
inputs, labels = inputs.to(model.device), labels.to(model.device)
batch_size, _, H, W = inputs.shape
if 'class-il' not in model.COMPATIBILITY:
outputs = model(inputs, k)
else:
y_0 = torch.ones(batch_size, dataset.N_CLASSES).to(inputs.device) /dataset.N_CLASSES
z = model.net.f1(inputs)
outputs, z1 = model.net.f2(z, y_0)
_, pred = torch.max(outputs.data, 1)
correct += torch.sum(pred == labels).item()
total += labels.shape[0]
if dataset.SETTING == 'class-il':
mask_classes(outputs, dataset, k)
_, pred = torch.max(outputs.data, 1)
correct_mask_classes += torch.sum(pred == labels).item()
accs.append(correct / total * 100
if 'class-il' in model.COMPATIBILITY else 0)
accs_mask_classes.append(correct_mask_classes / total * 100)
model.net.train(status)
print('evaluation acc:')
print(accs)
return accs, accs_mask_classes
def train(model: ContinualModel, dataset: ContinualDataset,
args: Namespace) -> None:
"""
The training process, including evaluations and loggers.
:param model: the module to be trained
:param dataset: the continual dataset at hand
:param args: the arguments of the current execution
"""
model.net.to(model.device)
results, results_mask_classes = [], []
if not args.disable_log and not args.debug:
logger = MLFlowLogger(dataset.SETTING, dataset.NAME, model.NAME,
experiment_name=args.experiment_name, parent_run_id=args.parent_run_id, run_name=args.run_name)
logger.log_args(args.__dict__)
progress_bar = ProgressBar(verbose=not args.non_verbose)
if not args.ignore_other_metrics and not args.debug:
dataset_copy = get_dataset(args)
for t in range(dataset.N_TASKS):
model.net.train()
_, _ = dataset_copy.get_data_loaders()
if model.NAME != 'icarl' and model.NAME != 'pnn':
if model.NAME =='ider':
random_results_class, random_results_task = evaluateid(model, dataset_copy)
else:
random_results_class, random_results_task = evaluate(model, dataset_copy)
if os.path.exists('old_model.pt'):
os.remove('old_model.pt')
if os.path.exists('net.pt'):
os.remove('net.pt')
print(file=sys.stderr)
for t in range(dataset.N_TASKS):
model.net.train()
train_loader, test_loader = dataset.get_data_loaders()
if hasattr(model, 'begin_task'):
model.begin_task(dataset)
if t and not args.ignore_other_metrics and not args.debug:
if model.NAME =='ider':
accs = evaluateid(model, dataset, last=True)
else:
accs = evaluate(model, dataset, last=True)
results[t-1] = results[t-1] + accs[0]
if dataset.SETTING == 'class-il':
results_mask_classes[t-1] = results_mask_classes[t-1] + accs[1]
scheduler = dataset.get_scheduler(model, args)
for epoch in range(model.args.n_epochs):
if args.model == 'joint':
continue
for i, data in enumerate(train_loader):
if args.debug and i > 3:
break
if hasattr(dataset.train_loader.dataset, 'logits'):
inputs, labels, not_aug_inputs, logits = data
inputs = inputs.to(model.device)
labels = labels.to(model.device)
not_aug_inputs = not_aug_inputs.to(model.device)
logits = logits.to(model.device)
loss = model.meta_observe(inputs, labels, not_aug_inputs, logits)
else:
inputs, labels, not_aug_inputs = data
inputs, labels = inputs.to(model.device), labels.to(
model.device)
not_aug_inputs = not_aug_inputs.to(model.device)
loss = model.meta_observe(inputs, labels, not_aug_inputs)
assert not math.isnan(loss)
progress_bar.prog(i, len(train_loader), epoch, t, loss)
if scheduler is not None:
scheduler.step()
if hasattr(model, 'end_epoch'):
model.end_epoch(dataset)
if hasattr(model, 'end_task'):
model.end_task(dataset)
if model.NAME =='ider':
accs = evaluateid(model, dataset)
else:
accs = evaluate(model, dataset)
results.append(accs[0])
results_mask_classes.append(accs[1])
if model.NAME =='ider':
eces= evaluate_eceid(model, dataset)
else:
eces= evaluate_ece(model, dataset)
mean_acc = np.mean(accs, axis=1)
print_mean_accuracy(mean_acc, t + 1, dataset.SETTING)
if not args.disable_log and not args.debug:
logger.log(mean_acc)
logger.log_fullacc(accs)
if not args.disable_log and not args.ignore_other_metrics and not args.debug:
logger.add_bwt(results, results_mask_classes)
logger.add_forgetting(results, results_mask_classes)
if model.NAME != 'icarl' and model.NAME != 'pnn':
logger.add_fwt(results, random_results_class,
results_mask_classes, random_results_task)
if args.savecheckpoint:
dataset_name = args.dataset if hasattr(args, 'dataset') and args.dataset else 'unknown_dataset'
buffer_tag = f"buffer_{args.buffer_size}" if hasattr(args, 'buffer_size') and args.buffer_size is not None else "buffer_none"
save_dir = os.path.join("./experiments", dataset_name, buffer_tag)
os.makedirs(save_dir, exist_ok=True)
model_filename = f"{model.NAME}_seed_{args.seed}.pth"
model_path = os.path.join(save_dir, model_filename)
torch.save(model.net.state_dict(), model_path)
print(f"Model saved to: {model_path}")
|