-
Notifications
You must be signed in to change notification settings - Fork 1
/
instance_model.py
106 lines (94 loc) · 4.02 KB
/
instance_model.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
import torch
import torch.nn as nn
class AttributeNet(nn.Module):
def __init__(self, layers=5, patch_size=8, channels=3):
super(AttributeNet, self).__init__()
self.layers = layers
self.patch_size = patch_size
self.channels = channels
self.pooling = nn.MaxPool2d(2, 2)
self.conv1 = nn.Conv2d(3, 8, 3, 1, 1)
self.bn1 = nn.BatchNorm2d(8)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(8, 16, 3, 1, 1)
self.bn2 = nn.BatchNorm2d(16)
self.relu2 = nn.ReLU(inplace=True)
self.conv3 = nn.Conv2d(16, 32, 3, 1, 1)
self.bn3 = nn.BatchNorm2d(32)
self.relu3 = nn.ReLU(inplace=True)
self.conv4 = nn.Conv2d(32, 64, 3, 1, 1)
self.bn4 = nn.BatchNorm2d(64)
self.relu4 = nn.ReLU(inplace=True)
if self.layers == 5 and self.channels == 3:
self.conv6 = nn.Conv2d(64, 3, 3, 1, 1)
elif self.layers == 6:
self.conv5 = nn.Conv2d(64, 128, 3, 1, 1)
self.bn5 = nn.BatchNorm2d(128)
self.relu5 = nn.ReLU(inplace=True)
if self.channels == 3:
self.conv6 = nn.Conv2d(128, 3, 3, 1, 1)
def forward(self, x):
y = self.conv1(x)
y = self.bn1(y)
y = self.relu1(y)
if self.patch_size in [2, 4, 8, 16, 32]:
y = self.pooling(y)
y = self.conv2(y)
y = self.bn2(y)
y = self.relu2(y)
if self.patch_size in [4, 8, 16, 32]:
y = self.pooling(y)
y = self.conv3(y)
y = self.bn3(y)
y = self.relu3(y)
if self.patch_size in [8, 16, 32]:
y = self.pooling(y)
y = self.conv4(y)
y = self.bn4(y)
y = self.relu4(y)
if self.patch_size in [16, 32]:
y = self.pooling(y)
if self.layers == 6:
y = self.conv5(y)
y = self.bn5(y)
y = self.relu5(y)
if self.patch_size == 32:
y = self.pooling(y)
if self.channels == 3:
y = self.conv6(y)
elif self.channels == 1:
y = torch.mean(y, dim=1)
return y
class InstancewiseVisualPrompt(nn.Module):
def __init__(self, size, layers=5, patch_size=8, channels=3):
'''
Args:
size: input image size
layers: the number of layers of mask-training CNN
patch_size: the size of patches with the same mask value
channels: 3 means that the mask value for RGB channels are different, 1 means the same
keep_watermark: whether to keep the reprogram (\delta) in the model
'''
super(InstancewiseVisualPrompt, self).__init__()
if layers not in [5, 6]:
raise ValueError("Input layer number is not supported")
if patch_size not in [1, 2, 4, 8, 16, 32]:
raise ValueError("Input patch size is not supported")
if channels not in [1, 3]:
raise ValueError("Input channel number is not supported")
if patch_size == 32 and layers != 6:
raise ValueError("Input layer number and patch size are conflict with each other")
# Set the attribute mask CNN
self.patch_num = int(size / patch_size)
self.imagesize = size
self.patch_size = patch_size
self.channels = channels
self.priority = AttributeNet(layers, patch_size, channels)
# Set reprogram (\delta) according to the image size
self.size = size
self.program = torch.nn.Parameter(data=torch.zeros(3, size, size))
def forward(self, x):
attention = self.priority(x).view(-1, self.channels, self.patch_num * self.patch_num, 1).expand(-1, 3, -1, self.patch_size * self.patch_size).view(-1, 3, self.patch_num, self.patch_num, self.patch_size, self.patch_size).transpose(3, 4)
attention = attention.reshape(-1, 3, self.imagesize, self.imagesize)
x = x + self.program * attention
return x