forked from caichengyi/BayesianLM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreprogramming.py
49 lines (39 loc) · 1.82 KB
/
reprogramming.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class PaddingVR(nn.Module):
def __init__(self, out_size, mask, init='zero', normalize=None):
super(PaddingVR, self).__init__()
assert mask.shape[0] == mask.shape[1]
in_size = mask.shape[0]
self.out_size = out_size
if init == "zero":
self.program = torch.nn.Parameter(data=torch.zeros(3, out_size, out_size))
elif init == "randn":
self.program = torch.nn.Parameter(data=torch.randn(3, out_size, out_size))
self.normalize = normalize
self.l_pad = int((out_size-in_size+1)/2)
self.r_pad = int((out_size-in_size)/2)
mask = np.repeat(np.expand_dims(mask, 0), repeats=3, axis=0)
mask = torch.Tensor(mask)
self.register_buffer("mask", F.pad(mask, (self.l_pad, self.r_pad, self.l_pad, self.r_pad), value=1)) # register a buffer that should not to be considered a model parameter
def forward(self, x):
x = F.pad(x, (self.l_pad, self.r_pad, self.l_pad, self.r_pad), value=0) + torch.sigmoid(self.program) * self.mask
if self.normalize is not None:
x = self.normalize(x)
return x
class WatermarkingVR(nn.Module):
def __init__(self, size, pad):
super(WatermarkingVR, self).__init__()
self.size = size
self.program = torch.nn.Parameter(data=torch.zeros(3, size, size))
if size > 2*pad:
mask = torch.zeros(3, size-2*pad, size-2*pad)
self.register_buffer("mask", F.pad(mask, [pad for _ in range(4)], value=1))
elif size == 2*pad:
mask = torch.ones(3, size, size)
self.register_buffer("mask", mask)
def forward(self, x):
x = x + self.program * self.mask
return x