-
Notifications
You must be signed in to change notification settings - Fork 48
/
utils_model.py
151 lines (114 loc) · 4.83 KB
/
utils_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
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# 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 importlib
import torch
import torch.nn as nn
### Load HiDDeN models
class ConvBNRelu(nn.Module):
"""
Building block used in HiDDeN network. Is a sequence of Convolution, Batch Normalization, and ReLU activation
"""
def __init__(self, channels_in, channels_out):
super(ConvBNRelu, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(channels_in, channels_out, 3, stride=1, padding=1),
nn.BatchNorm2d(channels_out, eps=1e-3),
nn.GELU()
)
def forward(self, x):
return self.layers(x)
class HiddenDecoder(nn.Module):
"""
Decoder module. Receives a watermarked image and extracts the watermark.
"""
def __init__(self, num_blocks, num_bits, channels, redundancy=1):
super(HiddenDecoder, self).__init__()
layers = [ConvBNRelu(3, channels)]
for _ in range(num_blocks - 1):
layers.append(ConvBNRelu(channels, channels))
layers.append(ConvBNRelu(channels, num_bits*redundancy))
layers.append(nn.AdaptiveAvgPool2d(output_size=(1, 1)))
self.layers = nn.Sequential(*layers)
self.linear = nn.Linear(num_bits*redundancy, num_bits*redundancy)
self.num_bits = num_bits
self.redundancy = redundancy
def forward(self, img_w):
x = self.layers(img_w) # b d 1 1
x = x.squeeze(-1).squeeze(-1) # b d
x = self.linear(x)
x = x.view(-1, self.num_bits, self.redundancy) # b k*r -> b k r
x = torch.sum(x, dim=-1) # b k r -> b k
return x
class HiddenEncoder(nn.Module):
"""
Inserts a watermark into an image.
"""
def __init__(self, num_blocks, num_bits, channels, last_tanh=True):
super(HiddenEncoder, self).__init__()
layers = [ConvBNRelu(3, channels)]
for _ in range(num_blocks-1):
layer = ConvBNRelu(channels, channels)
layers.append(layer)
self.conv_bns = nn.Sequential(*layers)
self.after_concat_layer = ConvBNRelu(channels + 3 + num_bits, channels)
self.final_layer = nn.Conv2d(channels, 3, kernel_size=1)
self.last_tanh = last_tanh
self.tanh = nn.Tanh()
def forward(self, imgs, msgs):
msgs = msgs.unsqueeze(-1).unsqueeze(-1) # b l 1 1
msgs = msgs.expand(-1,-1, imgs.size(-2), imgs.size(-1)) # b l h w
encoded_image = self.conv_bns(imgs)
concat = torch.cat([msgs, encoded_image, imgs], dim=1)
im_w = self.after_concat_layer(concat)
im_w = self.final_layer(im_w)
if self.last_tanh:
im_w = self.tanh(im_w)
return im_w
def get_hidden_decoder(num_bits, redundancy=1, num_blocks=7, channels=64):
decoder = HiddenDecoder(num_blocks=num_blocks, num_bits=num_bits, channels=channels, redundancy=redundancy)
return decoder
def get_hidden_decoder_ckpt(ckpt_path):
ckpt = torch.load(ckpt_path, map_location="cpu")
decoder_ckpt = { k.replace('module.', '').replace('decoder.', '') : v for k,v in ckpt['encoder_decoder'].items() if 'decoder' in k}
return decoder_ckpt
def get_hidden_encoder(num_bits, num_blocks=4, channels=64):
encoder = HiddenEncoder(num_blocks=num_blocks, num_bits=num_bits, channels=channels)
return encoder
def get_hidden_encoder_ckpt(ckpt_path):
ckpt = torch.load(ckpt_path, map_location="cpu")
encoder_ckpt = { k.replace('module.', '').replace('encoder.', '') : v for k,v in ckpt['encoder_decoder'].items() if 'encoder' in k}
return encoder_ckpt
### Load LDM models
def instantiate_from_config(config):
if not "target" in config:
if config == '__is_first_stage__':
return None
elif config == "__is_unconditional__":
return None
raise KeyError("Expected key `target` to instantiate.")
return get_obj_from_str(config["target"])(**config.get("params", dict()))
def get_obj_from_str(string, reload=False):
module, cls = string.rsplit(".", 1)
if reload:
module_imp = importlib.import_module(module)
importlib.reload(module_imp)
return getattr(importlib.import_module(module, package=None), cls)
def load_model_from_config(config, ckpt, verbose=False):
print(f"Loading model from {ckpt}")
pl_sd = torch.load(ckpt, map_location="cpu")
if "global_step" in pl_sd:
print(f"Global Step: {pl_sd['global_step']}")
sd = pl_sd["state_dict"]
model = instantiate_from_config(config.model)
m, u = model.load_state_dict(sd, strict=False)
if len(m) > 0 and verbose:
print("missing keys:")
print(m)
if len(u) > 0 and verbose:
print("unexpected keys:")
print(u)
model.cuda()
model.eval()
return model