File size: 7,843 Bytes
7f43945 9ff21bd 7f43945 9ff21bd 7f43945 |
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 |
import torch.nn as nn
import torch
from .deform_conv import DCN_layer
import clip
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
clip_model, preprocess = clip.load("ViT-B/32", device=device)
# 동적으로 텍스트 임베딩 차원 가져오기
text_embed_dim = clip_model.text_projection.shape[1]
def default_conv(in_channels, out_channels, kernel_size, bias=True):
return nn.Conv2d(in_channels, out_channels, kernel_size, padding=(kernel_size // 2), bias=bias)
class DGM(nn.Module):
def __init__(self, channels_in, channels_out, kernel_size):
super(DGM, self).__init__()
self.channels_out = channels_out
self.channels_in = channels_in
self.kernel_size = kernel_size
self.dcn = DCN_layer(self.channels_in, self.channels_out, kernel_size,
padding=(kernel_size - 1) // 2, bias=False)
self.sft = SFT_layer(self.channels_in, self.channels_out)
self.relu = nn.LeakyReLU(0.1, True)
def forward(self, x, inter, text_prompt):
'''
:param x: feature map: B * C * H * W
:inter: degradation map: B * C * H * W
'''
dcn_out = self.dcn(x, inter)
sft_out = self.sft(x, inter, text_prompt)
out = dcn_out + sft_out
out = x + out
return out
# Projection Head 정의
class TextProjectionHead(nn.Module):
def __init__(self, input_dim, output_dim):
super(TextProjectionHead, self).__init__()
self.proj = nn.Sequential(
nn.Linear(input_dim, output_dim),
nn.ReLU(),
nn.Linear(output_dim, output_dim)
).float()
def forward(self, x):
return self.proj(x.float())
class SFT_layer(nn.Module):
def __init__(self, channels_in, channels_out):
super(SFT_layer, self).__init__()
self.conv_gamma = nn.Sequential(
nn.Conv2d(channels_in, channels_out, 1, 1, 0, bias=False),
nn.LeakyReLU(0.1, True),
nn.Conv2d(channels_out, channels_out, 1, 1, 0, bias=False),
)
self.conv_beta = nn.Sequential(
nn.Conv2d(channels_in, channels_out, 1, 1, 0, bias=False),
nn.LeakyReLU(0.1, True),
nn.Conv2d(channels_out, channels_out, 1, 1, 0, bias=False),
)
self.text_proj_head = TextProjectionHead(text_embed_dim, channels_out)
'''
self.text_gamma = nn.Sequential(
nn.Conv2d(channels_out, channels_out, 1, 1, 0, bias=False),
nn.LeakyReLU(0.1, True),
nn.Conv2d(channels_out, channels_out, 1, 1, 0, bias=False),
).float()
self.text_beta = nn.Sequential(
nn.Conv2d(channels_out, channels_out, 1, 1, 0, bias=False),
nn.LeakyReLU(0.1, True),
nn.Conv2d(channels_out, channels_out, 1, 1, 0, bias=False),
).float()
'''
self.cross_attention = nn.MultiheadAttention(embed_dim=channels_out, num_heads=2)
def forward(self, x, inter, text_prompt):
'''
:param x: degradation representation: B * C
:param inter: degradation intermediate representation map: B * C * H * W
'''
# img_gamma = self.conv_gamma(inter)
# img_beta = self.conv_beta(inter)
B, C, H, W = inter.shape #cross attention
text_tokens = clip.tokenize(text_prompt).to(device) # Tokenize the text prompts (Batch size)
with torch.no_grad():
text_embed = clip_model.encode_text(text_tokens)
text_proj = self.text_proj_head(text_embed).float()
# 텍스트 임베딩 차원 확장: (B, C, H, W)로 변경 #concat
# text_proj_expanded = text_proj.unsqueeze(-1).unsqueeze(-1).expand(B, self.conv_gamma[0].out_channels, H, W)
text_proj_expanded = text_proj.unsqueeze(-1).unsqueeze(-1).expand(B, C, H, W)
# 이미지 중간 표현과 텍스트 임베딩 결합 (concat)
combined = inter * text_proj_expanded
# combined = torch.cat([inter, text_proj_expanded], dim=1)
# 이미지와 텍스트 기반 gamma와 beta 계산
img_gamma = self.conv_gamma(combined)
img_beta = self.conv_beta(combined)
''' simple concat
text_gamma = self.text_gamma(text_proj.unsqueeze(-1).unsqueeze(-1)) # Reshape to match (B, C, H, W)
text_beta = self.text_beta(text_proj.unsqueeze(-1).unsqueeze(-1)) # Reshape to match (B, C, H, W)
'''
'''
text_proj = text_proj.unsqueeze(1).expand(-1, H*W, -1) # B * (H*W) * C
# 이미지 중간 표현 변환: B * (H*W) * C로 변경
inter_flat = inter.view(B, C, -1).permute(2, 0, 1) # (H*W) * B * C
# Cross-attention 적용
attn_output, _ = self.cross_attention(text_proj.permute(1, 0, 2), inter_flat, inter_flat)
attn_output = attn_output.permute(1, 2, 0).view(B, C, H, W) # B * C * H * W
# Gamma와 Beta 계산
img_gamma = self.conv_gamma(attn_output)
img_beta = self.conv_beta(attn_output)
'''
# concat으로 text 결합 실험
return x * img_gamma + img_beta
class DGB(nn.Module):
def __init__(self, conv, n_feat, kernel_size):
super(DGB, self).__init__()
# self.da_conv1 = DGM(n_feat, n_feat, kernel_size)
# self.da_conv2 = DGM(n_feat, n_feat, kernel_size)
self.dgm1 = DGM(n_feat, n_feat, kernel_size)
self.dgm2 = DGM(n_feat, n_feat, kernel_size)
self.conv1 = conv(n_feat, n_feat, kernel_size)
self.conv2 = conv(n_feat, n_feat, kernel_size)
self.relu = nn.LeakyReLU(0.1, True)
def forward(self, x, inter, text_prompt):
'''
:param x: feature map: B * C * H * W
:param inter: degradation representation: B * C * H * W
'''
out = self.relu(self.dgm1(x, inter, text_prompt))
out = self.relu(self.conv1(out))
out = self.relu(self.dgm2(out, inter, text_prompt))
out = self.conv2(out) + x
return out
class DGG(nn.Module):
def __init__(self, conv, n_feat, kernel_size, n_blocks):
super(DGG, self).__init__()
self.n_blocks = n_blocks
modules_body = [
DGB(conv, n_feat, kernel_size) \
for _ in range(n_blocks)
]
modules_body.append(conv(n_feat, n_feat, kernel_size))
self.body = nn.Sequential(*modules_body)
def forward(self, x, inter, text_prompt):
'''
:param x: feature map: B * C * H * W
:param inter: degradation representation: B * C * H * W
'''
res = x
for i in range(self.n_blocks):
res = self.body[i](res, inter, text_prompt)
res = self.body[-1](res)
res = res + x
return res
class DGRN(nn.Module):
def __init__(self, opt, conv=default_conv):
super(DGRN, self).__init__()
self.n_groups = 5
n_blocks = 5
n_feats = 64
kernel_size = 3
# head module
modules_head = [conv(3, n_feats, kernel_size)]
self.head = nn.Sequential(*modules_head)
# body
modules_body = [
DGG(default_conv, n_feats, kernel_size, n_blocks) \
for _ in range(self.n_groups)
]
modules_body.append(conv(n_feats, n_feats, kernel_size))
self.body = nn.Sequential(*modules_body)
# tail
modules_tail = [conv(n_feats, 3, kernel_size)]
self.tail = nn.Sequential(*modules_tail)
def forward(self, x, inter, text_prompt):
# head
x = self.head(x)
# body
res = x
for i in range(self.n_groups):
res = self.body[i](res, inter, text_prompt)
res = self.body[-1](res)
res = res + x
# tail
x = self.tail(res)
return x
|