-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwf_mc.py
executable file
·88 lines (68 loc) · 2.92 KB
/
lwf_mc.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
# Copyright 2020-present, Pietro Buzzega, Matteo Boschini, Angelo Porrello, Davide Abati, 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.
from copy import deepcopy
import torch
import torch.nn.functional as F
from datasets import get_dataset
from utils.args import *
from models.utils.continual_model import ContinualModel
import numpy as np
def get_parser() -> ArgumentParser:
parser = ArgumentParser(description='Continual Learning via iCaRL.')
add_management_args(parser)
add_experiment_args(parser)
parser.add_argument('--wd_reg', type=float, required=True,
help='L2 regularization applied to the parameters.')
return parser
class LwFMC(ContinualModel):
NAME = 'lwf_mc'
COMPATIBILITY = ['class-il', 'task-il']
def __init__(self, backbone, loss, args, transform):
super(LwFMC, self).__init__(backbone, loss, args, transform)
self.dataset = get_dataset(args)
# Instantiate buffers
self.eye = torch.eye(self.dataset.N_CLASSES_PER_TASK *
self.dataset.N_TASKS).to(self.device)
self.class_means = None
self.old_net = None
self.current_task = 0
def observe(self, inputs, labels, not_aug_inputs, logits=None, epoch=None):
if self.current_task > 0:
with torch.no_grad():
logits = torch.sigmoid(self.old_net(inputs))
self.opt.zero_grad()
loss = self.get_loss(inputs, labels, self.current_task, logits)
loss.backward()
self.opt.step()
return loss.item()
def get_loss(self, inputs: torch.Tensor, labels: torch.Tensor,
task_idx: int, logits: torch.Tensor) -> torch.Tensor:
"""
Computes the loss tensor.
:param inputs: the images to be fed to the network
:param labels: the ground-truth labels
:param task_idx: the task index
:return: the differentiable loss value
"""
pc = task_idx * self.dataset.N_CLASSES_PER_TASK
ac = (task_idx + 1) * self.dataset.N_CLASSES_PER_TASK
outputs = self.net(inputs)[:, :ac]
if task_idx == 0:
# Compute loss on the current task
targets = self.eye[labels][:, :ac]
loss = F.binary_cross_entropy_with_logits(outputs, targets)
assert loss >= 0
else:
targets = self.eye[labels][:, pc:ac]
comb_targets = torch.cat((logits[:, :pc], targets), dim=1)
loss = F.binary_cross_entropy_with_logits(outputs, comb_targets)
assert loss >= 0
if self.args.wd_reg:
loss += self.args.wd_reg * torch.sum(self.net.get_params() ** 2)
return loss
def end_task(self, dataset) -> None:
self.old_net = deepcopy(self.net.eval())
self.net.train()
self.current_task += 1