|
| 1 | +""" |
| 2 | +Copyright (c) 2019-present NAVER Corp. |
| 3 | +MIT License |
| 4 | +""" |
| 5 | + |
| 6 | +# -*- coding: utf-8 -*- |
| 7 | +import torch |
| 8 | +import torch.nn as nn |
| 9 | +import torch.nn.functional as F |
| 10 | + |
| 11 | +from .basenet.vgg16_bn import vgg16_bn, init_weights |
| 12 | +# from basenet.vgg16_bn import vgg16_bn, init_weights |
| 13 | + |
| 14 | +class double_conv(nn.Module): |
| 15 | + def __init__(self, in_ch, mid_ch, out_ch): |
| 16 | + super(double_conv, self).__init__() |
| 17 | + self.conv = nn.Sequential( |
| 18 | + nn.Conv2d(in_ch + mid_ch, mid_ch, kernel_size=1), |
| 19 | + nn.BatchNorm2d(mid_ch), |
| 20 | + nn.ReLU(inplace=True), |
| 21 | + nn.Conv2d(mid_ch, out_ch, kernel_size=3, padding=1), |
| 22 | + nn.BatchNorm2d(out_ch), |
| 23 | + nn.ReLU(inplace=True) |
| 24 | + ) |
| 25 | + |
| 26 | + def forward(self, x): |
| 27 | + x = self.conv(x) |
| 28 | + return x |
| 29 | + |
| 30 | + |
| 31 | +class CRAFT(nn.Module): |
| 32 | + def __init__(self, pretrained=False, freeze=False): |
| 33 | + super(CRAFT, self).__init__() |
| 34 | + |
| 35 | + """ Base network """ |
| 36 | + self.basenet = vgg16_bn(pretrained, freeze) |
| 37 | + |
| 38 | + """ U network """ |
| 39 | + self.upconv1 = double_conv(1024, 512, 256) |
| 40 | + self.upconv2 = double_conv(512, 256, 128) |
| 41 | + self.upconv3 = double_conv(256, 128, 64) |
| 42 | + self.upconv4 = double_conv(128, 64, 32) |
| 43 | + |
| 44 | + num_class = 2 |
| 45 | + self.conv_cls = nn.Sequential( |
| 46 | + nn.Conv2d(32, 32, kernel_size=3, padding=1), nn.ReLU(inplace=True), |
| 47 | + nn.Conv2d(32, 32, kernel_size=3, padding=1), nn.ReLU(inplace=True), |
| 48 | + nn.Conv2d(32, 16, kernel_size=3, padding=1), nn.ReLU(inplace=True), |
| 49 | + nn.Conv2d(16, 16, kernel_size=1), nn.ReLU(inplace=True), |
| 50 | + nn.Conv2d(16, num_class, kernel_size=1), |
| 51 | + ) |
| 52 | + |
| 53 | + init_weights(self.upconv1.modules()) |
| 54 | + init_weights(self.upconv2.modules()) |
| 55 | + init_weights(self.upconv3.modules()) |
| 56 | + init_weights(self.upconv4.modules()) |
| 57 | + init_weights(self.conv_cls.modules()) |
| 58 | + |
| 59 | + def forward(self, x): |
| 60 | + """ Base network """ |
| 61 | + sources = self.basenet(x) |
| 62 | + |
| 63 | + """ U network """ |
| 64 | + y = torch.cat([sources[0], sources[1]], dim=1) |
| 65 | + y = self.upconv1(y) |
| 66 | + |
| 67 | + y = F.interpolate(y, size=sources[2].size()[2:], mode='bilinear', align_corners=False) |
| 68 | + y = torch.cat([y, sources[2]], dim=1) |
| 69 | + y = self.upconv2(y) |
| 70 | + |
| 71 | + y = F.interpolate(y, size=sources[3].size()[2:], mode='bilinear', align_corners=False) |
| 72 | + y = torch.cat([y, sources[3]], dim=1) |
| 73 | + y = self.upconv3(y) |
| 74 | + |
| 75 | + y = F.interpolate(y, size=sources[4].size()[2:], mode='bilinear', align_corners=False) |
| 76 | + y = torch.cat([y, sources[4]], dim=1) |
| 77 | + feature = self.upconv4(y) |
| 78 | + |
| 79 | + y = self.conv_cls(feature) |
| 80 | + |
| 81 | + return y.permute(0,2,3,1), feature |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + model = CRAFT(pretrained=True).cuda() |
| 85 | + output, _ = model(torch.randn(1, 3, 768, 768).cuda()) |
| 86 | + print(output.shape) |
0 commit comments