File size: 10,714 Bytes
5fee096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F

class Conv2d_TRGP(nn.Conv2d):
    
    def __init__(self,   
                in_channels, 
                out_channels,              
                kernel_size, 
                padding=0, 
                stride=1, 
                dilation=1,
                groups=1,                                                   
                bias=True):
        super().__init__(in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=bias)

        # define the scale V
        size = self.weight.shape[1] * self.weight.shape[2] * self.weight.shape[3]
        self.identity_matrix = torch.eye(size, device = self.weight.device)

        self.space = []
        self.scale_param = nn.ParameterList()

    def enable_scale(self, space):
        self.space = space
        self.scale_param = nn.ParameterList([nn.Parameter(self.identity_matrix).to(self.weight.device) for _ in self.space])

    def disable_scale(self):

        self.space = []
        self.scale_param = nn.ParameterList()

    def forward(self, input, compute_input_matrix = False):
           
        # this should be only called once for each task
        if compute_input_matrix:
            self.input_matrix = input

        sz = self.weight.shape[0]

        masked_weight = self.weight

        for scale, space in zip(self.scale_param, self.space):

            cropped_scale = scale[:space.size(1), :space.size(1)]
            cropped_identity_matrix = self.identity_matrix[:space.shape[1], :space.shape[1]].to(self.weight.device)

            #masked_weight = masked_weight + (self.weight.view(sz, -1) @ space @ (cropped_scale - cropped_identity_matrix) @ space.T).\
            #                                view(self.weight.shape)

            masked_weight = masked_weight + (masked_weight.view(sz, -1) @ space @ (cropped_scale - cropped_identity_matrix) @ space.T).\
                                            view(masked_weight.shape)


        return F.conv2d(input, masked_weight, self.bias, self.stride, self.padding, self.dilation, self.groups)

class Linear_TRGP(nn.Linear):

    def __init__(self, in_features, out_features, bias = True):
        super().__init__(in_features, out_features, bias = bias)

        # define the scale Q
        self.identity_matrix = torch.eye(self.weight.shape[1], device = self.weight.device)

        self.space = []
        self.scale_param = nn.ParameterList()

    def enable_scale(self, space):
        self.space = space
        self.scale_param = nn.ParameterList([nn.Parameter(self.identity_matrix).to(self.weight.device) for _ in self.space])

    def disable_scale(self):

        self.space = []
        self.scale_param = nn.ParameterList()

    def forward(self, input, compute_input_matrix = False):

        # this should be only called once for each task
        if compute_input_matrix:
            self.input_matrix = input # save input_matrix here
            
        masked_weight = self.weight
        for scale, space in zip(self.scale_param, self.space):

            cropped_scale = scale[:space.shape[1], :space.shape[1]]
            cropped_identity_matrix = self.identity_matrix[:space.shape[1], :space.shape[1]].to(self.weight.device)

            masked_weight = masked_weight + masked_weight @ space @ (cropped_scale - cropped_identity_matrix) @ space.T # ?

        return F.linear(input, masked_weight, self.bias)

class AlexNet_TRGP(nn.Module):

    def __init__(self, dropout_rate_1 = 0.2, dropout_rate_2 = 0.5, **kwargs):

        super().__init__()

        self.conv1 = Conv2d_TRGP(in_channels = 3, out_channels = 64, kernel_size = 4, bias = False)
        self.bn1 = nn.BatchNorm2d(64, track_running_stats = False)
        
        self.conv2 = Conv2d_TRGP(in_channels = 64, out_channels = 128, kernel_size = 3, bias = False)
        self.bn2 = nn.BatchNorm2d(128, track_running_stats = False)

        self.conv3 = Conv2d_TRGP(in_channels = 128, out_channels = 256, kernel_size = 2, bias = False)
        self.bn3 = nn.BatchNorm2d(256, track_running_stats = False)

        self.fc1 = Linear_TRGP(in_features = 1024, out_features = 2048, bias = False)
        self.bn4 = nn.BatchNorm1d(2048, track_running_stats = False)

        self.fc2 = Linear_TRGP(in_features = 2048, out_features = 2048, bias=False)
        self.bn5 = nn.BatchNorm1d(2048, track_running_stats = False)

        self.feat_dim = 2048 # final feature's dim


        # common use
        self.relu = nn.ReLU()
        self.dropout1 = nn.Dropout(dropout_rate_1)
        self.dropout2 = nn.Dropout(dropout_rate_2)
        self.maxpool = nn.MaxPool2d(kernel_size = 2)

    def forward(self, x, compute_input_matrix):

        x = self.conv1(x, compute_input_matrix)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.dropout1(x)
        x = self.maxpool(x)
        
        x = self.conv2(x, compute_input_matrix)
        x = self.bn2(x)
        x = self.relu(x)
        x = self.dropout1(x)
        x = self.maxpool(x)

        x = self.conv3(x, compute_input_matrix)
        x = self.bn3(x)
        x = self.relu(x)
        x = self.dropout2(x)
        x = self.maxpool(x)

        x = x.view(x.size(0), -1)

        x = self.fc1(x, compute_input_matrix)
        x = self.bn4(x)
        x = self.relu(x)
        x = self.dropout2(x)

        x = self.fc2(x, compute_input_matrix)
        x = self.bn5(x)
        x = self.relu(x)
        x = self.dropout2(x)

        return x

# -----

class Conv2d_API(nn.Conv2d):
    def __init__(self,in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros'):
        super().__init__(in_channels, out_channels, kernel_size, stride, padding, bias=bias, dilation=dilation, groups=groups, padding_mode=padding_mode)

        self.extra_ws = nn.ParameterList([])
        self.expand = []

    def forward(self, input, t, compute_input_matrix = False):

        input = torch.cat([input] + [(input.permute(0, 2, 3, 1) @ self.extra_ws[i]).permute(0, 3, 1, 2) for i in range(t)], dim=1)

        if compute_input_matrix:
            self.input_matrix = input
        
        return F.conv2d(input, self.weight[:, :input.shape[1]], bias=self.bias, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups)

    def duplicate(self, in_channels, extra_w):
        dup = Conv2d_API(
            self.in_channels + in_channels,
            self.out_channels,
            self.kernel_size,
            self.stride,
            self.padding,
            self.dilation,
            self.groups,
            self.bias is not None,
            self.padding_mode
        )

        dup.extra_ws = self.extra_ws
        dup.extra_ws.append(extra_w)
        dup.expand = self.expand + [in_channels]

        dup.weight.data[:, :self.in_channels].data.copy_(self.weight.data)
       
        if self.bias is not None:
            dup.bias.data[:, :self.in_channels].data.copy_(self.bias.data)

        return dup

class Linear_API(nn.Linear):
    def __init__(self, in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None:
        super().__init__(in_features, out_features, bias, device, dtype)
    
        self.extra_ws = nn.ParameterList([])
        self.expand = []

    def forward(self, input, t, compute_input_matrix=False):

        input = torch.cat([input] + [input @ self.extra_ws[i] for i in range(t)], dim=1)
        
        if compute_input_matrix:
            self.input_matrix = input

        return F.linear(input, self.weight[:,:input.shape[1]], bias=self.bias)

    def duplicate(self, in_features, extra_w):
        dup = Linear_API(
            self.in_features + in_features, 
            self.out_features, 
            self.bias is not None
        )

        dup.extra_ws = self.extra_ws
        dup.extra_ws.append(extra_w)
        dup.expand = self.expand + [in_features]

        dup.weight.data[:, :self.in_features].data.copy_(self.weight.data)

        if self.bias is not None:
            dup.bias.data[:, :self.in_features].data.copy_(self.bias.data)

        return dup

class AlexNet_API(nn.Module):

    def __init__(self, dropout_rate_1 = 0.2, dropout_rate_2 = 0.5, **kwargs):

        super().__init__()

        self.select1, self.select2, self.select3, self.select4, self.select5 = [], [], [], [], []

        self.conv1 = Conv2d_API(in_channels = 3, out_channels = 64, kernel_size = 4, bias = False)
        self.bn1 = nn.BatchNorm2d(64, track_running_stats = False)
        
        self.conv2 = Conv2d_API(in_channels = 64, out_channels = 128, kernel_size = 3, bias = False)
        self.bn2 = nn.BatchNorm2d(128, track_running_stats = False)

        self.conv3 = Conv2d_API(in_channels = 128, out_channels = 256, kernel_size = 2, bias = False)
        self.bn3 = nn.BatchNorm2d(256, track_running_stats = False)

        self.fc1 = Linear_API(in_features = 1024, out_features = 2048, bias = False)
        self.bn4 = nn.BatchNorm1d(2048, track_running_stats = False)

        self.fc2 = Linear_API(in_features = 2048, out_features = 2048, bias=False)
        self.bn5 = nn.BatchNorm1d(2048, track_running_stats = False)

        self.feat_dim = 2048 # final feature's dim

        # common use
        self.relu = nn.ReLU()
        self.dropout1 = nn.Dropout(dropout_rate_1)
        self.dropout2 = nn.Dropout(dropout_rate_2)
        self.maxpool = nn.MaxPool2d(kernel_size = 2)

    def forward(self, x, t = 0, compute_input_matrix = False):

        x = self.conv1(x, t, compute_input_matrix)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.dropout1(x)
        x = self.maxpool(x)
        
        x = self.conv2(x, t, compute_input_matrix)
        x = self.bn2(x)
        x = self.relu(x)
        x = self.dropout1(x)
        x = self.maxpool(x)

        x = self.conv3(x, t, compute_input_matrix)
        x = self.bn3(x)
        x = self.relu(x)
        x = self.dropout2(x)
        x = self.maxpool(x)

        x = x.view(x.size(0), -1)

        x = self.fc1(x, t, compute_input_matrix)
        x = self.bn4(x)
        x = self.relu(x)
        x = self.dropout2(x)

        x = self.fc2(x, t, compute_input_matrix)
        x = self.bn5(x)
        x = self.relu(x)
        x = self.dropout2(x)

        return x

    def expand(self, sizes, extra_ws):
        self.conv1 = self.conv1.duplicate(sizes[0], extra_ws[0])
        self.conv2 = self.conv2.duplicate(sizes[1], extra_ws[1])
        self.conv3 = self.conv3.duplicate(sizes[2], extra_ws[2])
        self.fc1 = self.fc1.duplicate(sizes[3], extra_ws[3])
        self.fc2 = self.fc2.duplicate(sizes[4], extra_ws[4])