Spaces:
Runtime error
Runtime error
Commit ·
240c20c
1
Parent(s): 2ade88a
Upload 12 files
Browse filesFiles changed (12)
- app.pyAdded+97
- distorted/42_2 copy.pngAdded
- distorted/63_2 copy.pngAdded
- extractor.pyAdded+115
- inference.pyAdded+128
- model.pyAdded+267
- model_pretrained/DocGeoNet.pthAdded+3
- model_pretrained/preprocess.pthAdded+3
- position_encoding.pyAdded+110
- requirements.txtAdded+7
- seg.pyAdded+567
- unet.pyAdded+401
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import torchimport torch.nn as nnimport torch.nn.functional as Fimport numpy as npimport cv2import osfrom PIL import Imageimport warningsimport gradio as gr
from model import DocGeoNetfrom seg import U2NETPimport glob
warnings.filterwarnings('ignore')
class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.msk = U2NETP(3, 1) self.DocTr = DocGeoNet()
def forward(self, x): msk, _1,_2,_3,_4,_5,_6 = self.msk(x) msk = (msk > 0.5).float() x = msk * x
_, _, bm = self.DocTr(x) bm = (2 * (bm / 255.) - 1) * 0.99
return bm
def reload_seg_model(model, path=""): if not bool(path): return model else: model_dict = model.state_dict() pretrained_dict = torch.load(path, map_location='cpu') pretrained_dict = {k[6:]: v for k, v in pretrained_dict.items() if k[6:] in model_dict} model_dict.update(pretrained_dict) model.load_state_dict(model_dict) return model
def reload_rec_model(model, path=""): if not bool(path): return model else: model_dict = model.state_dict() pretrained_dict = torch.load(path, map_location='cpu') pretrained_dict = {k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict} model_dict.update(pretrained_dict) model.load_state_dict(model_dict) return model
def rec(input_image): seg_model_path = './model_pretrained/preprocess.pth' rec_model_path = './model_pretrained/DocGeoNet.pth'
net = Net() reload_rec_model(net.DocTr, rec_model_path) reload_seg_model(net.msk, seg_model_path) net.eval()
im_ori = np.array(input_image)[:, :, :3] / 255. # read image 0-255 to 0-1 h, w, _ = im_ori.shape im = cv2.resize(im_ori, (256, 256)) im = im.transpose(2, 0, 1) im = torch.from_numpy(im).float().unsqueeze(0)
with torch.no_grad(): bm = net(im) bm = bm.cpu()
bm0 = cv2.resize(bm[0, 0].numpy(), (w, h)) # x flow bm1 = cv2.resize(bm[0, 1].numpy(), (w, h)) # y flow bm0 = cv2.blur(bm0, (3, 3)) bm1 = cv2.blur(bm1, (3, 3)) lbl = torch.from_numpy(np.stack([bm0, bm1], axis=2)).unsqueeze(0) # h * w * 2 out = F.grid_sample(torch.from_numpy(im_ori).permute(2, 0, 1).unsqueeze(0).float(), lbl, align_corners=True) img_rec = ((out[0] * 255).permute(1, 2, 0).numpy())[:,:,::-1].astype(np.uint8)
# Convert from BGR to RGB img_rec = cv2.cvtColor(img_rec, cv2.COLOR_BGR2RGB) return Image.fromarray(img_rec)
demo_img_files = glob.glob('./distorted/*.[jJ][pP][gG]') + glob.glob('./distorted/*.[pP][nN][gG]')
# Gradio Interfaceinput_image = gr.inputs.Image()output_image = gr.outputs.Image(type='pil')
iface = gr.Interface(fn=rec, inputs=input_image, outputs=output_image, title="DocGeoNet",examples=demo_img_files)
#iface.launch(server_port=8821, server_name="0.0.0.0")iface.launch(server_port=8821, server_name="0.0.0.0")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import torchimport torch.nn as nnimport torch.nn.functional as F
class ResidualBlock(nn.Module): def __init__(self, in_planes, planes, norm_fn='group', stride=1): super(ResidualBlock, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, padding=1, stride=stride) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1) self.relu = nn.ReLU(inplace=True)
num_groups = planes // 8
if norm_fn == 'group': self.norm1 = nn.GroupNorm(num_groups=num_groups, num_channels=planes) self.norm2 = nn.GroupNorm(num_groups=num_groups, num_channels=planes) if not stride == 1: self.norm3 = nn.GroupNorm(num_groups=num_groups, num_channels=planes) elif norm_fn == 'batch': self.norm1 = nn.BatchNorm2d(planes) self.norm2 = nn.BatchNorm2d(planes) if not stride == 1: self.norm3 = nn.BatchNorm2d(planes) elif norm_fn == 'instance': self.norm1 = nn.InstanceNorm2d(planes) self.norm2 = nn.InstanceNorm2d(planes) if not stride == 1: self.norm3 = nn.InstanceNorm2d(planes)
elif norm_fn == 'none': self.norm1 = nn.Sequential() self.norm2 = nn.Sequential() if not stride == 1: self.norm3 = nn.Sequential()
if stride == 1: self.downsample = None else: self.downsample = nn.Sequential( nn.Conv2d(in_planes, planes, kernel_size=1, stride=stride), self.norm3)
def forward(self, x): y = x y = self.relu(self.norm1(self.conv1(y))) y = self.relu(self.norm2(self.conv2(y)))
if self.downsample is not None: x = self.downsample(x)
return self.relu(x+y)
class BasicEncoder(nn.Module): def __init__(self, input_dim=128, output_dim=128, norm_fn='batch'): super(BasicEncoder, self).__init__() self.norm_fn = norm_fn
if self.norm_fn == 'group': self.norm1 = nn.GroupNorm(num_groups=8, num_channels=64) elif self.norm_fn == 'batch': self.norm1 = nn.BatchNorm2d(64)
elif self.norm_fn == 'instance': self.norm1 = nn.InstanceNorm2d(64)
elif self.norm_fn == 'none': self.norm1 = nn.Sequential()
self.conv1 = nn.Conv2d(input_dim, 64, kernel_size=7, stride=2, padding=3) self.relu1 = nn.ReLU(inplace=True)
self.in_planes = 64 self.layer1 = self._make_layer(64, stride=1) self.layer2 = self._make_layer(128, stride=2) self.layer3 = self._make_layer(192, stride=2)
# output convolution self.conv2 = nn.Conv2d(192, output_dim, kernel_size=1) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.InstanceNorm2d, nn.GroupNorm)): if m.weight is not None: nn.init.constant_(m.weight, 1) if m.bias is not None: nn.init.constant_(m.bias, 0)
def _make_layer(self, dim, stride=1): layer1 = ResidualBlock(self.in_planes, dim, self.norm_fn, stride=stride) layer2 = ResidualBlock(dim, dim, self.norm_fn, stride=1) layers = (layer1, layer2) self.in_planes = dim return nn.Sequential(*layers)
def forward(self, x): x = self.conv1(x) x = self.norm1(x) x = self.relu1(x)
x = self.layer1(x) x = self.layer2(x) x = self.layer3(x)
x = self.conv2(x)
return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128from model import DocGeoNetfrom seg import U2NETP
import torchimport torch.nn as nnimport torch.nn.functional as Fimport skimage.io as ioimport numpy as npimport cv2import osfrom PIL import Imageimport argparseimport warningswarnings.filterwarnings('ignore')
class Net(nn.Module): def __init__(self, opt): super(Net, self).__init__() self.msk = U2NETP(3, 1) self.DocTr = DocGeoNet()
def forward(self, x): msk, _1,_2,_3,_4,_5,_6 = self.msk(x) msk = (msk > 0.5).float() x = msk * x
_, _, bm = self.DocTr(x) bm = (2 * (bm / 255.) - 1) * 0.99
return bm
def reload_seg_model(model, path=""): if not bool(path): return model else: model_dict = model.state_dict() pretrained_dict = torch.load(path, map_location='cpu') print(len(pretrained_dict.keys())) pretrained_dict = {k[6:]: v for k, v in pretrained_dict.items() if k[6:] in model_dict} print(len(pretrained_dict.keys())) model_dict.update(pretrained_dict) model.load_state_dict(model_dict)
return model
def reload_rec_model(model, path=""): if not bool(path): return model else: model_dict = model.state_dict() pretrained_dict = torch.load(path, map_location='cpu') print(len(pretrained_dict.keys())) pretrained_dict = {k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict} print(len(pretrained_dict.keys())) model_dict.update(pretrained_dict) model.load_state_dict(model_dict)
return model
def rec(seg_model_path, rec_model_path, distorrted_path, save_path, opt): print(torch.__version__)
# distorted images list img_list = sorted(os.listdir(distorrted_path))
# creat save path for rectified images if not os.path.exists(save_path): os.makedirs(save_path)
net = Net(opt)#.cuda() print(get_parameter_number(net))
# reload rec model reload_rec_model(net.DocTr, rec_model_path) reload_seg_model(net.msk, opt.seg_model_path)
net.eval()
for img_path in img_list: name = img_path.split('.')[-2] # image name img_path = distorrted_path + img_path # image path
im_ori = np.array(Image.open(img_path))[:, :, :3] / 255. # read image 0-255 to 0-1 h, w, _ = im_ori.shape im = cv2.resize(im_ori, (256, 256)) im = im.transpose(2, 0, 1) im = torch.from_numpy(im).float().unsqueeze(0)
with torch.no_grad(): bm = net(im) bm = bm.cpu()
# save rectified image bm0 = cv2.resize(bm[0, 0].numpy(), (w, h)) # x flow bm1 = cv2.resize(bm[0, 1].numpy(), (w, h)) # y flow bm0 = cv2.blur(bm0, (3, 3)) bm1 = cv2.blur(bm1, (3, 3)) lbl = torch.from_numpy(np.stack([bm0, bm1], axis=2)).unsqueeze(0) # h * w * 2 out = F.grid_sample(torch.from_numpy(im_ori).permute(2, 0, 1).unsqueeze(0).float(), lbl, align_corners=True) cv2.imwrite(save_path + name + '_rec' + '.png', ((out[0] * 255).permute(1, 2, 0).numpy())[:,:,::-1].astype(np.uint8))
def get_parameter_number(net): total_num = sum(p.numel() for p in net.parameters()) trainable_num = sum(p.numel() for p in net.parameters() if p.requires_grad) return {'Total': total_num, 'Trainable': trainable_num}
def main(): parser = argparse.ArgumentParser() parser.add_argument('--seg_model_path', default='./model_pretrained/preprocess.pth') parser.add_argument('--rec_model_path', default='./model_pretrained/DocGeoNet.pth') parser.add_argument('--distorrted_path', default='./distorted/') parser.add_argument('--save_path', default='./rec/') opt = parser.parse_args()
rec(seg_model_path=opt.seg_model_path, rec_model_path=opt.rec_model_path, distorrted_path=opt.distorrted_path, save_path=opt.save_path, opt=opt)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267from extractor import BasicEncoderfrom position_encoding import build_position_encodingfrom unet import U_Net_mini
import argparseimport numpy as npimport torchfrom torch import nn, Tensorimport torch.nn.functional as Fimport copyfrom typing import Optional
class attnLayer(nn.Module): def __init__(self, d_model, nhead=8, dim_feedforward=2048, dropout=0.1, activation="relu", normalize_before=False): super().__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) self.multihead_attn_list = nn.ModuleList( [copy.deepcopy(nn.MultiheadAttention(d_model, nhead, dropout=dropout)) for i in range(2)]) # Implementation of Feedforward model self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model)
self.norm1 = nn.LayerNorm(d_model) self.norm2_list = nn.ModuleList([copy.deepcopy(nn.LayerNorm(d_model)) for i in range(2)])
self.norm3 = nn.LayerNorm(d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2_list = nn.ModuleList([copy.deepcopy(nn.Dropout(dropout)) for i in range(2)]) self.dropout3 = nn.Dropout(dropout)
self.activation = _get_activation_fn(activation) self.normalize_before = normalize_before
def with_pos_embed(self, tensor, pos: Optional[Tensor]): return tensor if pos is None else tensor + pos
def forward_post(self, tgt, memory_list, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None, pos=None, memory_pos=None): q = k = self.with_pos_embed(tgt, pos) tgt2 = self.self_attn(q, k, value=tgt, attn_mask=tgt_mask, key_padding_mask=tgt_key_padding_mask)[0] tgt = tgt + self.dropout1(tgt2) tgt = self.norm1(tgt) for memory, multihead_attn, norm2, dropout2, m_pos in zip(memory_list, self.multihead_attn_list, self.norm2_list, self.dropout2_list, memory_pos): tgt2 = multihead_attn(query=self.with_pos_embed(tgt, pos), key=self.with_pos_embed(memory, m_pos), value=memory, attn_mask=memory_mask, key_padding_mask=memory_key_padding_mask)[0] tgt = tgt + dropout2(tgt2) tgt = norm2(tgt) tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt)))) tgt = tgt + self.dropout3(tgt2) tgt = self.norm3(tgt) return tgt
def forward_pre(self, tgt, memory, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None, pos=None, memory_pos=None): tgt2 = self.norm1(tgt) q = k = self.with_pos_embed(tgt2, pos) tgt2 = self.self_attn(q, k, value=tgt2, attn_mask=tgt_mask, key_padding_mask=tgt_key_padding_mask)[0] tgt = tgt + self.dropout1(tgt2) tgt2 = self.norm2(tgt) tgt2 = self.multihead_attn(query=self.with_pos_embed(tgt2, pos), key=self.with_pos_embed(memory, memory_pos), value=memory, attn_mask=memory_mask, key_padding_mask=memory_key_padding_mask)[0] tgt = tgt + self.dropout2(tgt2) tgt2 = self.norm3(tgt) tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2)))) tgt = tgt + self.dropout3(tgt2) return tgt
def forward(self, tgt, memory_list, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None, pos=None, memory_pos=None): if self.normalize_before: return self.forward_pre(tgt, memory_list, tgt_mask, memory_mask, tgt_key_padding_mask, memory_key_padding_mask, pos, memory_pos) return self.forward_post(tgt, memory_list, tgt_mask, memory_mask, tgt_key_padding_mask, memory_key_padding_mask, pos, memory_pos)
def _get_clones(module, N): return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
def _get_activation_fn(activation): """Return an activation function given a string""" if activation == "relu": return F.relu if activation == "gelu": return F.gelu if activation == "glu": return F.glu raise RuntimeError(F"activation should be relu/gelu, not {activation}.")
class TransDecoder(nn.Module): def __init__(self, num_attn_layers, hidden_dim=128): super(TransDecoder, self).__init__() attn_layer = attnLayer(hidden_dim) self.layers = _get_clones(attn_layer, num_attn_layers) self.position_embedding = build_position_encoding(hidden_dim)
def forward(self, imgf, query_embed): pos = self.position_embedding( torch.ones(imgf.shape[0], imgf.shape[2], imgf.shape[3]).bool()) # torch.Size([1, 128, 36, 36])
bs, c, h, w = imgf.shape imgf = imgf.flatten(2).permute(2, 0, 1) # torch.Size([1296, 1, 256]) query_embed = query_embed.unsqueeze(1).repeat(1, bs, 1) pos = pos.flatten(2).permute(2, 0, 1) # torch.Size([1296, 1, 256])
for layer in self.layers: query_embed = layer(query_embed, [imgf], pos=pos, memory_pos=[pos, pos]) query_embed = query_embed.permute(1, 2, 0).reshape(bs, c, h, w)
return query_embed
class TransEncoder(nn.Module): def __init__(self, num_attn_layers, hidden_dim=128): super(TransEncoder, self).__init__() attn_layer = attnLayer(hidden_dim) self.layers = _get_clones(attn_layer, num_attn_layers) self.position_embedding = build_position_encoding(hidden_dim)
def forward(self, imgf): pos = self.position_embedding( torch.ones(imgf.shape[0], imgf.shape[2], imgf.shape[3]).bool()) # torch.Size([1, 128, 36, 36]) bs, c, h, w = imgf.shape imgf = imgf.flatten(2).permute(2, 0, 1) pos = pos.flatten(2).permute(2, 0, 1)
for layer in self.layers: imgf = layer(imgf, [imgf], pos=pos, memory_pos=[pos, pos]) imgf = imgf.permute(1, 2, 0).reshape(bs, c, h, w)
return imgf
class FlowHead(nn.Module): def __init__(self, input_dim=128, hidden_dim=256, out_cha=2): super(FlowHead, self).__init__() self.conv1 = nn.Conv2d(input_dim, hidden_dim, 3, padding=1) self.conv2 = nn.Conv2d(hidden_dim, out_cha, 3, padding=1) self.relu = nn.ReLU(inplace=True)
def forward(self, x): return self.conv2(self.relu(self.conv1(x)))
class UpdateBlock(nn.Module): def __init__(self, hidden_dim=128): super(UpdateBlock, self).__init__() self.flow_head = FlowHead(hidden_dim, hidden_dim=256) self.mask = nn.Sequential( nn.Conv2d(hidden_dim, 256, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(256, 64 * 9, 1, padding=0))
def forward(self, imgf, coords1): mask = .25 * self.mask(imgf) # scale mask to balence gradients dflow = self.flow_head(imgf) coords1 = coords1 + dflow
return mask, coords1
def coords_grid(batch, ht, wd): coords = torch.meshgrid(torch.arange(ht), torch.arange(wd)) coords = torch.stack(coords[::-1], dim=0).float() return coords[None].repeat(batch, 1, 1, 1)
def upflow8(flow, mode='bilinear'): new_size = (8 * flow.shape[2], 8 * flow.shape[3]) return 8 * F.interpolate(flow, size=new_size, mode=mode, align_corners=True)
class Up_block(nn.Module): def __init__(self, hidden_dim=128, out_cha=3): super(Up_block, self).__init__() self.flow_head = FlowHead(hidden_dim, hidden_dim=256, out_cha=out_cha) self.acf = nn.Hardtanh(0, 1)
def forward(self, x): x = self.flow_head(x) x = upflow8(x) x = self.acf(x) return x
class DocGeoNet(nn.Module): def __init__(self): super(DocGeoNet, self).__init__()
self.hidden_dim = hdim = 128 self.imcnn = BasicEncoder(input_dim=3, output_dim=hdim, norm_fn='instance')
# uv self.wc_encoder = TransEncoder(4, hidden_dim=hdim) # uv tail self.Up_block_wc = nn.Sequential(TransEncoder(2, hidden_dim=hdim), Up_block(self.hidden_dim))
# text self.text_encoder = U_Net_mini(3, 1) self.textcnn = nn.Conv2d(128, 64, 3, 2, 1) # BasicEncoder(input_dim=32, output_dim=64, norm_fn='instance')
# 6 self.bm_encoder = TransEncoder(6, hidden_dim=hdim + 64)
# bm tail self.update_block = UpdateBlock(self.hidden_dim + 64)
def initialize_flow(self, img): N, C, H, W = img.shape coodslar = coords_grid(N, H, W).to(img.device) coords0 = coords_grid(N, H // 8, W // 8).to(img.device) coords1 = coords_grid(N, H // 8, W // 8).to(img.device)
return coodslar, coords0, coords1
def upsample_flow(self, flow, mask): N, _, H, W = flow.shape mask = mask.view(N, 1, 9, 8, 8, H, W) mask = torch.softmax(mask, dim=2)
up_flow = F.unfold(8 * flow, [3, 3], padding=1) up_flow = up_flow.view(N, 2, 9, 1, 1, H, W)
up_flow = torch.sum(mask * up_flow, dim=2) up_flow = up_flow.permute(0, 1, 4, 2, 5, 3)
return up_flow.reshape(N, 2, 8 * H, 8 * W)
def forward(self, image1): # wc imfmap = self.imcnn(image1) imfmap = torch.relu(imfmap) wcfea = self.wc_encoder(imfmap) wc_pred = self.Up_block_wc(wcfea)
# text d4, text_pred = self.text_encoder(image1) textfea = self.textcnn(d4) fmap = torch.cat((wcfea, textfea), 1)
# bm encoder fmap = self.bm_encoder(fmap)
# upsample coodslar, coords0, coords1 = self.initialize_flow(image1) coords1 = coords1.detach() mask, coords1 = self.update_block(fmap, coords1) flow_up = self.upsample_flow(coords1 - coords0, mask) bm_up = coodslar + flow_up
return wc_pred, text_pred, bm_up123version https://git-lfs.github.com/spec/v1oid sha256:27d7a379a92b4fe5bb347d26ef37da7c9cffbfefb09fcd8705bc9beae26e6146size 95196536123version https://git-lfs.github.com/spec/v1oid sha256:cb79fdec55a5ed435dc74d8112aa9285d8213bae475022f711c709744fb19dd4size 4715923123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved"""Various positional encodings for the transformer."""import mathimport torchfrom torch import nnfrom typing import Listfrom typing import Optionalfrom torch import Tensor
class NestedTensor(object): def __init__(self, tensors, mask: Optional[Tensor]): self.tensors = tensors self.mask = mask
def to(self, device): # type: (Device) -> NestedTensor # noqa cast_tensor = self.tensors.to(device) mask = self.mask if mask is not None: assert mask is not None cast_mask = mask.to(device) else: cast_mask = None return NestedTensor(cast_tensor, cast_mask)
def decompose(self): return self.tensors, self.mask
def __repr__(self): return str(self.tensors)
class PositionEmbeddingSine(nn.Module): """ This is a more standard version of the position embedding, very similar to the one used by the Attention is all you need paper, generalized to work on images. """ def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None): super().__init__() self.num_pos_feats = num_pos_feats self.temperature = temperature self.normalize = normalize if scale is not None and normalize is False: raise ValueError("normalize should be True if scale is passed") if scale is None: scale = 2 * math.pi self.scale = scale
def forward(self, mask): assert mask is not None y_embed = mask.cumsum(1, dtype=torch.float32) x_embed = mask.cumsum(2, dtype=torch.float32) if self.normalize: eps = 1e-6 y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale
dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32)#.cuda() dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)
pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t pos_x = torch.stack((pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4).flatten(3) pos_y = torch.stack((pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4).flatten(3) pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2) # print(pos.shape) return pos
class PositionEmbeddingLearned(nn.Module): """ Absolute pos embedding, learned. """ def __init__(self, num_pos_feats=256): super().__init__() self.row_embed = nn.Embedding(50, num_pos_feats) self.col_embed = nn.Embedding(50, num_pos_feats) self.reset_parameters()
def reset_parameters(self): nn.init.uniform_(self.row_embed.weight) nn.init.uniform_(self.col_embed.weight)
def forward(self, tensor_list: NestedTensor): x = tensor_list.tensors h, w = x.shape[-2:] i = torch.arange(w, device=x.device) j = torch.arange(h, device=x.device) x_emb = self.col_embed(i) y_emb = self.row_embed(j) pos = torch.cat([ x_emb.unsqueeze(0).repeat(h, 1, 1), y_emb.unsqueeze(1).repeat(1, w, 1), ], dim=-1).permute(2, 0, 1).unsqueeze(0).repeat(x.shape[0], 1, 1, 1) return pos
def build_position_encoding(hidden_dim=512, position_embedding='sine'): N_steps = hidden_dim // 2 if position_embedding in ('v2', 'sine'): position_embedding = PositionEmbeddingSine(N_steps, normalize=True) elif position_embedding in ('v3', 'learned'): position_embedding = PositionEmbeddingLearned(N_steps) else: raise ValueError(f"not supported {position_embedding}")
return position_embedding
1234567numpyopencv_pythonPillowscikit_imagetorchtorchvisiongradio123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567import torchimport torch.nn as nnfrom torchvision import modelsimport torch.nn.functional as Fimport numpy as np
class sobel_net(nn.Module): def __init__(self): super().__init__() self.conv_opx = nn.Conv2d(1, 1, 3, bias=False) self.conv_opy = nn.Conv2d(1, 1, 3, bias=False) sobel_kernelx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype='float32').reshape((1, 1, 3, 3)) sobel_kernely = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype='float32').reshape((1, 1, 3, 3)) self.conv_opx.weight.data = torch.from_numpy(sobel_kernelx) self.conv_opy.weight.data = torch.from_numpy(sobel_kernely)
for p in self.parameters(): p.requires_grad = False
def forward(self, im): # input rgb x = (0.299 * im[:, 0, :, :] + 0.587 * im[:, 1, :, :] + 0.114 * im[:, 2, :, :]).unsqueeze(1) # rgb2gray gradx = self.conv_opx(x) grady = self.conv_opy(x)
x = (gradx ** 2 + grady ** 2) ** 0.5 x = (x - x.min()) / (x.max() - x.min()) x = F.pad(x, (1, 1, 1, 1))
x = torch.cat([im, x], dim=1) return x
class REBNCONV(nn.Module): def __init__(self, in_ch=3, out_ch=3, dirate=1): super(REBNCONV, self).__init__()
self.conv_s1 = nn.Conv2d(in_ch, out_ch, 3, padding=1 * dirate, dilation=1 * dirate) self.bn_s1 = nn.BatchNorm2d(out_ch) self.relu_s1 = nn.ReLU(inplace=True)
def forward(self, x): hx = x xout = self.relu_s1(self.bn_s1(self.conv_s1(hx)))
return xout
## upsample tensor 'src' to have the same spatial size with tensor 'tar'def _upsample_like(src, tar): src = F.interpolate(src, size=tar.shape[2:], mode='bilinear', align_corners=False)
return src
### RSU-7 ###class RSU7(nn.Module): # UNet07DRES(nn.Module):
def __init__(self, in_ch=3, mid_ch=12, out_ch=3): super(RSU7, self).__init__()
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool5 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=1)
self.rebnconv7 = REBNCONV(mid_ch, mid_ch, dirate=2)
self.rebnconv6d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
def forward(self, x): hx = x hxin = self.rebnconvin(hx)
hx1 = self.rebnconv1(hxin) hx = self.pool1(hx1)
hx2 = self.rebnconv2(hx) hx = self.pool2(hx2)
hx3 = self.rebnconv3(hx) hx = self.pool3(hx3)
hx4 = self.rebnconv4(hx) hx = self.pool4(hx4)
hx5 = self.rebnconv5(hx) hx = self.pool5(hx5)
hx6 = self.rebnconv6(hx)
hx7 = self.rebnconv7(hx6)
hx6d = self.rebnconv6d(torch.cat((hx7, hx6), 1)) hx6dup = _upsample_like(hx6d, hx5)
hx5d = self.rebnconv5d(torch.cat((hx6dup, hx5), 1)) hx5dup = _upsample_like(hx5d, hx4)
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1)) hx4dup = _upsample_like(hx4d, hx3)
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) hx3dup = _upsample_like(hx3d, hx2)
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) hx2dup = _upsample_like(hx2d, hx1)
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
return hx1d + hxin
### RSU-6 ###class RSU6(nn.Module): # UNet06DRES(nn.Module):
def __init__(self, in_ch=3, mid_ch=12, out_ch=3): super(RSU6, self).__init__()
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1)
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=2)
self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
def forward(self, x): hx = x
hxin = self.rebnconvin(hx)
hx1 = self.rebnconv1(hxin) hx = self.pool1(hx1)
hx2 = self.rebnconv2(hx) hx = self.pool2(hx2)
hx3 = self.rebnconv3(hx) hx = self.pool3(hx3)
hx4 = self.rebnconv4(hx) hx = self.pool4(hx4)
hx5 = self.rebnconv5(hx)
hx6 = self.rebnconv6(hx5)
hx5d = self.rebnconv5d(torch.cat((hx6, hx5), 1)) hx5dup = _upsample_like(hx5d, hx4)
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1)) hx4dup = _upsample_like(hx4d, hx3)
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) hx3dup = _upsample_like(hx3d, hx2)
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) hx2dup = _upsample_like(hx2d, hx1)
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
return hx1d + hxin
### RSU-5 ###class RSU5(nn.Module): # UNet05DRES(nn.Module):
def __init__(self, in_ch=3, mid_ch=12, out_ch=3): super(RSU5, self).__init__()
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=2)
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
def forward(self, x): hx = x
hxin = self.rebnconvin(hx)
hx1 = self.rebnconv1(hxin) hx = self.pool1(hx1)
hx2 = self.rebnconv2(hx) hx = self.pool2(hx2)
hx3 = self.rebnconv3(hx) hx = self.pool3(hx3)
hx4 = self.rebnconv4(hx)
hx5 = self.rebnconv5(hx4)
hx4d = self.rebnconv4d(torch.cat((hx5, hx4), 1)) hx4dup = _upsample_like(hx4d, hx3)
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) hx3dup = _upsample_like(hx3d, hx2)
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) hx2dup = _upsample_like(hx2d, hx1)
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
return hx1d + hxin
### RSU-4 ###class RSU4(nn.Module): # UNet04DRES(nn.Module):
def __init__(self, in_ch=3, mid_ch=12, out_ch=3): super(RSU4, self).__init__()
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=2)
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
def forward(self, x): hx = x
hxin = self.rebnconvin(hx)
hx1 = self.rebnconv1(hxin) hx = self.pool1(hx1)
hx2 = self.rebnconv2(hx) hx = self.pool2(hx2)
hx3 = self.rebnconv3(hx)
hx4 = self.rebnconv4(hx3)
hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1)) hx3dup = _upsample_like(hx3d, hx2)
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) hx2dup = _upsample_like(hx2d, hx1)
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
return hx1d + hxin
### RSU-4F ###class RSU4F(nn.Module): # UNet04FRES(nn.Module):
def __init__(self, in_ch=3, mid_ch=12, out_ch=3): super(RSU4F, self).__init__()
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=2) self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=4)
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=8)
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=4) self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=2) self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
def forward(self, x): hx = x
hxin = self.rebnconvin(hx)
hx1 = self.rebnconv1(hxin) hx2 = self.rebnconv2(hx1) hx3 = self.rebnconv3(hx2)
hx4 = self.rebnconv4(hx3)
hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1)) hx2d = self.rebnconv2d(torch.cat((hx3d, hx2), 1)) hx1d = self.rebnconv1d(torch.cat((hx2d, hx1), 1))
return hx1d + hxin
##### U^2-Net ####class U2NET(nn.Module):
def __init__(self, in_ch=3, out_ch=1): super(U2NET, self).__init__() self.edge = sobel_net()
self.stage1 = RSU7(in_ch, 32, 64) self.pool12 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage2 = RSU6(64, 32, 128) self.pool23 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage3 = RSU5(128, 64, 256) self.pool34 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage4 = RSU4(256, 128, 512) self.pool45 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage5 = RSU4F(512, 256, 512) self.pool56 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage6 = RSU4F(512, 256, 512)
# decoder self.stage5d = RSU4F(1024, 256, 512) self.stage4d = RSU4(1024, 128, 256) self.stage3d = RSU5(512, 64, 128) self.stage2d = RSU6(256, 32, 64) self.stage1d = RSU7(128, 16, 64)
self.side1 = nn.Conv2d(64, out_ch, 3, padding=1) self.side2 = nn.Conv2d(64, out_ch, 3, padding=1) self.side3 = nn.Conv2d(128, out_ch, 3, padding=1) self.side4 = nn.Conv2d(256, out_ch, 3, padding=1) self.side5 = nn.Conv2d(512, out_ch, 3, padding=1) self.side6 = nn.Conv2d(512, out_ch, 3, padding=1)
self.outconv = nn.Conv2d(6, out_ch, 1)
def forward(self, x): x = self.edge(x) hx = x
# stage 1 hx1 = self.stage1(hx) hx = self.pool12(hx1)
# stage 2 hx2 = self.stage2(hx) hx = self.pool23(hx2)
# stage 3 hx3 = self.stage3(hx) hx = self.pool34(hx3)
# stage 4 hx4 = self.stage4(hx) hx = self.pool45(hx4)
# stage 5 hx5 = self.stage5(hx) hx = self.pool56(hx5)
# stage 6 hx6 = self.stage6(hx) hx6up = _upsample_like(hx6, hx5)
# -------------------- decoder -------------------- hx5d = self.stage5d(torch.cat((hx6up, hx5), 1)) hx5dup = _upsample_like(hx5d, hx4)
hx4d = self.stage4d(torch.cat((hx5dup, hx4), 1)) hx4dup = _upsample_like(hx4d, hx3)
hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1)) hx3dup = _upsample_like(hx3d, hx2)
hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1)) hx2dup = _upsample_like(hx2d, hx1)
hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1))
# side output d1 = self.side1(hx1d)
d2 = self.side2(hx2d) d2 = _upsample_like(d2, d1)
d3 = self.side3(hx3d) d3 = _upsample_like(d3, d1)
d4 = self.side4(hx4d) d4 = _upsample_like(d4, d1)
d5 = self.side5(hx5d) d5 = _upsample_like(d5, d1)
d6 = self.side6(hx6) d6 = _upsample_like(d6, d1)
d0 = self.outconv(torch.cat((d1, d2, d3, d4, d5, d6), 1))
return torch.sigmoid(d0), torch.sigmoid(d1), torch.sigmoid(d2), torch.sigmoid(d3), torch.sigmoid( d4), torch.sigmoid(d5), torch.sigmoid(d6)
### U^2-Net small ###class U2NETP(nn.Module):
def __init__(self, in_ch=3, out_ch=1): super(U2NETP, self).__init__()
self.stage1 = RSU7(in_ch, 16, 64) self.pool12 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage2 = RSU6(64, 16, 64) self.pool23 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage3 = RSU5(64, 16, 64) self.pool34 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage4 = RSU4(64, 16, 64) self.pool45 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage5 = RSU4F(64, 16, 64) self.pool56 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.stage6 = RSU4F(64, 16, 64)
# decoder self.stage5d = RSU4F(128, 16, 64) self.stage4d = RSU4(128, 16, 64) self.stage3d = RSU5(128, 16, 64) self.stage2d = RSU6(128, 16, 64) self.stage1d = RSU7(128, 16, 64)
self.side1 = nn.Conv2d(64, out_ch, 3, padding=1) self.side2 = nn.Conv2d(64, out_ch, 3, padding=1) self.side3 = nn.Conv2d(64, out_ch, 3, padding=1) self.side4 = nn.Conv2d(64, out_ch, 3, padding=1) self.side5 = nn.Conv2d(64, out_ch, 3, padding=1) self.side6 = nn.Conv2d(64, out_ch, 3, padding=1)
self.outconv = nn.Conv2d(6, out_ch, 1)
def forward(self, x): hx = x
# stage 1 hx1 = self.stage1(hx) hx = self.pool12(hx1)
# stage 2 hx2 = self.stage2(hx) hx = self.pool23(hx2)
# stage 3 hx3 = self.stage3(hx) hx = self.pool34(hx3)
# stage 4 hx4 = self.stage4(hx) hx = self.pool45(hx4)
# stage 5 hx5 = self.stage5(hx) hx = self.pool56(hx5)
# stage 6 hx6 = self.stage6(hx) hx6up = _upsample_like(hx6, hx5)
# decoder hx5d = self.stage5d(torch.cat((hx6up, hx5), 1)) hx5dup = _upsample_like(hx5d, hx4)
hx4d = self.stage4d(torch.cat((hx5dup, hx4), 1)) hx4dup = _upsample_like(hx4d, hx3)
hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1)) hx3dup = _upsample_like(hx3d, hx2)
hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1)) hx2dup = _upsample_like(hx2d, hx1)
hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1))
# side output d1 = self.side1(hx1d)
d2 = self.side2(hx2d) d2 = _upsample_like(d2, d1)
d3 = self.side3(hx3d) d3 = _upsample_like(d3, d1)
d4 = self.side4(hx4d) d4 = _upsample_like(d4, d1)
d5 = self.side5(hx5d) d5 = _upsample_like(d5, d1)
d6 = self.side6(hx6) d6 = _upsample_like(d6, d1)
d0 = self.outconv(torch.cat((d1, d2, d3, d4, d5, d6), 1))
return torch.sigmoid(d0), torch.sigmoid(d1), torch.sigmoid(d2), torch.sigmoid(d3), torch.sigmoid( d4), torch.sigmoid(d5), torch.sigmoid(d6)
def get_parameter_number(net): total_num = sum(p.numel() for p in net.parameters()) trainable_num = sum(p.numel() for p in net.parameters() if p.requires_grad) return {'Total': total_num, 'Trainable': trainable_num}
if __name__ == '__main__': net = U2NET(4, 1)#.cuda() print(get_parameter_number(net)) # 69090500 加attention后69442032 with torch.no_grad(): inputs = torch.zeros(1, 3, 256, 256)#.cuda() outs = net(inputs) print(outs[0].shape) # torch.Size([2, 3, 256, 256]) torch.Size([2, 2, 256, 256])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401import torchimport torch.nn as nnfrom torchvision import modelsimport torch.nn.functional as F
class sobel_net(nn.Module): def __init__(self): super().__init__() self.conv_opx = nn.Conv2d(1, 1, 3, bias=False) self.conv_opy = nn.Conv2d(1, 1, 3, bias=False) sobel_kernelx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype='float32').reshape((1, 1, 3, 3)) sobel_kernely = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype='float32').reshape((1, 1, 3, 3)) self.conv_opx.weight.data = torch.from_numpy(sobel_kernelx) self.conv_opy.weight.data = torch.from_numpy(sobel_kernely)
for p in self.parameters(): p.requires_grad = False
def forward(self, im): # input rgb x = (0.299 * im[:, 0, :, :] + 0.587 * im[:, 1, :, :] + 0.114 * im[:, 2, :, :]).unsqueeze(1) # rgb2gray gradx = self.conv_opx(x) grady = self.conv_opy(x)
x = (gradx ** 2 + grady ** 2) ** 0.5 x = (x - x.min()) / (x.max() - x.min()) x = F.pad(x, (1, 1, 1, 1))
x = torch.cat([im, x], dim=1) return x
class conv_block(nn.Module): def __init__(self, ch_in, ch_out): super(conv_block, self).__init__() self.conv = nn.Sequential( nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True), nn.BatchNorm2d(ch_out), nn.ReLU(inplace=True), nn.Conv2d(ch_out, ch_out, kernel_size=3, stride=1, padding=1, bias=True), nn.BatchNorm2d(ch_out), nn.ReLU(inplace=True) )
def forward(self, x): x = self.conv(x) return x
class up_conv(nn.Module): def __init__(self, ch_in, ch_out): super(up_conv, self).__init__() self.up = nn.Sequential( nn.Upsample(scale_factor=2), nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True), nn.BatchNorm2d(ch_out), nn.ReLU(inplace=True) )
def forward(self, x): x = self.up(x) return x
class Recurrent_block(nn.Module): def __init__(self, ch_out, t=2): super(Recurrent_block, self).__init__() self.t = t self.ch_out = ch_out self.conv = nn.Sequential( nn.Conv2d(ch_out, ch_out, kernel_size=3, stride=1, padding=1, bias=True), nn.BatchNorm2d(ch_out), nn.ReLU(inplace=True) )
def forward(self, x): for i in range(self.t):
if i == 0: x1 = self.conv(x)
x1 = self.conv(x + x1) return x1
class RRCNN_block(nn.Module): def __init__(self, ch_in, ch_out, t=2): super(RRCNN_block, self).__init__() self.RCNN = nn.Sequential( Recurrent_block(ch_out, t=t), Recurrent_block(ch_out, t=t) ) self.Conv_1x1 = nn.Conv2d(ch_in, ch_out, kernel_size=1, stride=1, padding=0)
def forward(self, x): x = self.Conv_1x1(x) x1 = self.RCNN(x) return x + x1
class single_conv(nn.Module): def __init__(self, ch_in, ch_out): super(single_conv, self).__init__() self.conv = nn.Sequential( nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True), nn.BatchNorm2d(ch_out), nn.ReLU(inplace=True) )
def forward(self, x): x = self.conv(x) return x
class Attention_block(nn.Module): def __init__(self, F_g, F_l, F_int): super(Attention_block, self).__init__() self.W_g = nn.Sequential( nn.Conv2d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True), nn.BatchNorm2d(F_int) )
self.W_x = nn.Sequential( nn.Conv2d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True), nn.BatchNorm2d(F_int) )
self.psi = nn.Sequential( nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True), nn.BatchNorm2d(1), nn.Sigmoid() )
self.relu = nn.ReLU(inplace=True)
def forward(self, g, x): g1 = self.W_g(g) x1 = self.W_x(x) psi = self.relu(g1 + x1) psi = self.psi(psi)
return x * psi
class U_Net(nn.Module): def __init__(self, img_ch=3, output_ch=1): super(U_Net, self).__init__()
self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.Conv1 = conv_block(ch_in=img_ch, ch_out=64) self.Conv2 = conv_block(ch_in=64, ch_out=128) self.Conv3 = conv_block(ch_in=128, ch_out=256) self.Conv4 = conv_block(ch_in=256, ch_out=512) self.Conv5 = conv_block(ch_in=512, ch_out=1024)
self.Up5 = up_conv(ch_in=1024, ch_out=512) self.Up_conv5 = conv_block(ch_in=1024, ch_out=512)
self.Up4 = up_conv(ch_in=512, ch_out=256) self.Up_conv4 = conv_block(ch_in=512, ch_out=256)
self.Up3 = up_conv(ch_in=256, ch_out=128) self.Up_conv3 = conv_block(ch_in=256, ch_out=128)
self.Up2 = up_conv(ch_in=128, ch_out=64) self.Up_conv2 = conv_block(ch_in=128, ch_out=64)
self.Conv_1x1 = nn.Conv2d(64, output_ch, kernel_size=1, stride=1, padding=0, bias=False)
def forward(self, x): # encoding path x1 = self.Conv1(x)
x2 = self.Maxpool(x1) x2 = self.Conv2(x2)
x3 = self.Maxpool(x2) x3 = self.Conv3(x3)
x4 = self.Maxpool(x3) x4 = self.Conv4(x4)
x5 = self.Maxpool(x4) x5 = self.Conv5(x5)
# decoding + concat path d5 = self.Up5(x5) d5 = torch.cat((x4, d5), dim=1)
d5 = self.Up_conv5(d5)
d4 = self.Up4(d5) d4 = torch.cat((x3, d4), dim=1) d4 = self.Up_conv4(d4)
d3 = self.Up3(d4) d3 = torch.cat((x2, d3), dim=1) d3 = self.Up_conv3(d3)
d2 = self.Up2(d3) d2 = torch.cat((x1, d2), dim=1) d2 = self.Up_conv2(d2)
out = self.Conv_1x1(d2) out = torch.sigmoid(out)
return out
class U_Net_mini(nn.Module): def __init__(self, img_ch=3, output_ch=1): super(U_Net_mini, self).__init__()
self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.Conv1 = conv_block(ch_in=img_ch, ch_out=32) self.Conv2 = conv_block(ch_in=32, ch_out=64) self.Conv3 = conv_block(ch_in=64, ch_out=128) self.Conv4 = conv_block(ch_in=128, ch_out=256) self.Conv5 = conv_block(ch_in=256, ch_out=512)
self.Up5 = up_conv(ch_in=512, ch_out=256) self.Up_conv5 = conv_block(ch_in=512, ch_out=256)
self.Up4 = up_conv(ch_in=256, ch_out=128) self.Up_conv4 = conv_block(ch_in=256, ch_out=128)
self.Up3 = up_conv(ch_in=128, ch_out=64) self.Up_conv3 = conv_block(ch_in=128, ch_out=64)
self.Up2 = up_conv(ch_in=64, ch_out=32) self.Up_conv2 = conv_block(ch_in=64, ch_out=32)
self.Conv_1x1 = nn.Conv2d(32, output_ch, kernel_size=1, stride=1, padding=0, bias=False)
def forward(self, x): # encoding path x1 = self.Conv1(x)
x2 = self.Maxpool(x1) x2 = self.Conv2(x2)
x3 = self.Maxpool(x2) x3 = self.Conv3(x3)
x4 = self.Maxpool(x3) x4 = self.Conv4(x4)
x5 = self.Maxpool(x4) x5 = self.Conv5(x5)
# decoding + concat path d5 = self.Up5(x5) d5 = torch.cat((x4, d5), dim=1)
d5 = self.Up_conv5(d5)
d4 = self.Up4(d5) d4 = torch.cat((x3, d4), dim=1) d4 = self.Up_conv4(d4)
d3 = self.Up3(d4) d3 = torch.cat((x2, d3), dim=1) d3 = self.Up_conv3(d3)
d2 = self.Up2(d3) d2 = torch.cat((x1, d2), dim=1) d2 = self.Up_conv2(d2)
out = self.Conv_1x1(d2) out = torch.sigmoid(out)
return d4, out
class AttU_Net(nn.Module): def __init__(self, img_ch=3, output_ch=1, need_feature_maps=False): super(AttU_Net, self).__init__()
self.conv1_ = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3), nn.BatchNorm2d(64), nn.ReLU(inplace=True))
self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.Conv1 = conv_block(ch_in=64, ch_out=64) self.Conv2 = conv_block(ch_in=64, ch_out=128) self.Conv3 = conv_block(ch_in=128, ch_out=256) self.Conv4 = conv_block(ch_in=256, ch_out=512) self.Conv5 = conv_block(ch_in=512, ch_out=1024)
self.Up5 = up_conv(ch_in=1024, ch_out=512) self.Att5 = Attention_block(F_g=512, F_l=512, F_int=256) self.Up_conv5 = conv_block(ch_in=1024, ch_out=512)
self.Up4 = up_conv(ch_in=512, ch_out=256) self.Att4 = Attention_block(F_g=256, F_l=256, F_int=128) self.Up_conv4 = conv_block(ch_in=512, ch_out=256)
self.Up3 = up_conv(ch_in=256, ch_out=128) self.Att3 = Attention_block(F_g=128, F_l=128, F_int=64) self.Up_conv3 = conv_block(ch_in=256, ch_out=128)
self.Up2 = up_conv(ch_in=128, ch_out=64) self.Att2 = Attention_block(F_g=64, F_l=64, F_int=32) self.Up_conv2 = conv_block(ch_in=128, ch_out=64)
self.Conv_1x1 = nn.Conv2d(64, output_ch, kernel_size=1, stride=1, padding=0)
self.need_feature_maps = need_feature_maps
# self.loc_xy = (torch.stack(torch.meshgrid([torch.arange(0, 256), torch.arange(0, 256)])).permute(0, 2, 1).unsqueeze(0).float() - 127.5) / 127.5 # 1*2*256*256
def forward(self, x): # encoding path # batch = x.size(0) # if self.need_feature_maps: # x = torch.cat((x, self.loc_xy.repeat(batch, 1, 1, 1).cuda()), dim=1) x1 = self.conv1_(x) x1 = self.Conv1(x1)
x2 = self.Maxpool(x1) x2 = self.Conv2(x2)
x3 = self.Maxpool(x2) x3 = self.Conv3(x3)
x4 = self.Maxpool(x3) x4 = self.Conv4(x4)
x5 = self.Maxpool(x4) x5 = self.Conv5(x5)
# decoding + concat path d5 = self.Up5(x5) x4 = self.Att5(g=d5, x=x4) d5 = torch.cat((x4, d5), dim=1) d5 = self.Up_conv5(d5)
d4 = self.Up4(d5) x3 = self.Att4(g=d4, x=x3) d4 = torch.cat((x3, d4), dim=1) d4 = self.Up_conv4(d4)
d3 = self.Up3(d4) x2 = self.Att3(g=d3, x=x2) d3 = torch.cat((x2, d3), dim=1) d3 = self.Up_conv3(d3)
d2 = self.Up2(d3) x1 = self.Att2(g=d2, x=x1) d2 = torch.cat((x1, d2), dim=1) d2 = self.Up_conv2(d2)
wc = self.Conv_1x1(d2)
if self.need_feature_maps: return d2, wc else: return bm
class Doc_UNet(nn.Module): def __init__(self): super(Doc_UNet, self).__init__() self.U_net1 = AttU_Net(3, 3, need_feature_maps=True) self.U_net2 = U_Net(64 + 3 + 2, 2, need_feature_maps=False) self.htan = nn.Hardtanh(0, 1.0) self.f_activation = nn.Hardtanh()
self.loc_xy = (torch.stack(torch.meshgrid([torch.arange(0, 128), torch.arange(0, 128)])).permute(0, 2, 1).unsqueeze( 0).float() - 63.5) / 63.5 # 1*2*256*256
def forward(self, x): batch = x.size(0)
feature_maps, wc = self.U_net1(x) wc = self.htan(wc)
x = torch.cat((self.loc_xy.repeat(batch, 1, 1, 1).cuda(), wc, feature_maps), dim=1) bm = self.U_net2(x) bm = self.f_activation(bm)
return wc, bm
def get_parameter_number(net): total_num = sum(p.numel() for p in net.parameters()) trainable_num = sum(p.numel() for p in net.parameters() if p.requires_grad) return {'Total': total_num, 'Trainable': trainable_num}
if __name__ == '__main__': net = U2NET(3, 1).cuda() print(get_parameter_number(net)) # 69090500 加attention后69442032 with torch.no_grad(): inputs = torch.zeros(1, 3, 256, 256).cuda() outs = net(inputs) print(outs[0].shape) # torch.Size([2, 3, 256, 256]) torch.Size([2, 2, 256, 256])app.pyAdded+97
distorted/42_2 copy.pngAdded

distorted/63_2 copy.pngAdded

extractor.pyAdded+115
inference.pyAdded+128
model.pyAdded+267
model_pretrained/DocGeoNet.pthAdded+3
model_pretrained/preprocess.pthAdded+3
position_encoding.pyAdded+110
requirements.txtAdded+7
seg.pyAdded+567
unet.pyAdded+401