craft.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. Copyright (c) 2019-present NAVER Corp.
  3. MIT License
  4. """
  5. # -*- coding: utf-8 -*-
  6. import torch
  7. import torch.nn as nn
  8. import torch.nn.functional as F
  9. from CRAFT.basenet import vgg16_bn, init_weights
  10. from CRAFT.utils import copyStateDict
  11. from CRAFT.fp16 import FP16Module
  12. class double_conv(nn.Module):
  13. def __init__(self, in_ch, mid_ch, out_ch):
  14. super(double_conv, self).__init__()
  15. self.conv = nn.Sequential(
  16. nn.Conv2d(in_ch + mid_ch, mid_ch, kernel_size=1),
  17. nn.BatchNorm2d(mid_ch),
  18. nn.ReLU(inplace=True),
  19. nn.Conv2d(mid_ch, out_ch, kernel_size=3, padding=1),
  20. nn.BatchNorm2d(out_ch),
  21. nn.ReLU(inplace=True)
  22. )
  23. def forward(self, x):
  24. x = self.conv(x)
  25. return x
  26. class CRAFT(nn.Module):
  27. def __init__(self, pretrained=False, freeze=False):
  28. super(CRAFT, self).__init__()
  29. """ Base network """
  30. self.basenet = vgg16_bn(pretrained, freeze)
  31. """ U network """
  32. self.upconv1 = double_conv(1024, 512, 256)
  33. self.upconv2 = double_conv(512, 256, 128)
  34. self.upconv3 = double_conv(256, 128, 64)
  35. self.upconv4 = double_conv(128, 64, 32)
  36. num_class = 2
  37. self.conv_cls = nn.Sequential(
  38. nn.Conv2d(32, 32, kernel_size=3, padding=1), nn.ReLU(inplace=True),
  39. nn.Conv2d(32, 32, kernel_size=3, padding=1), nn.ReLU(inplace=True),
  40. nn.Conv2d(32, 16, kernel_size=3, padding=1), nn.ReLU(inplace=True),
  41. nn.Conv2d(16, 16, kernel_size=1), nn.ReLU(inplace=True),
  42. nn.Conv2d(16, num_class, kernel_size=1),
  43. )
  44. init_weights(self.upconv1.modules())
  45. init_weights(self.upconv2.modules())
  46. init_weights(self.upconv3.modules())
  47. init_weights(self.upconv4.modules())
  48. init_weights(self.conv_cls.modules())
  49. def forward(self, x):
  50. """ Base network """
  51. sources = self.basenet(x)
  52. """ U network """
  53. y = torch.cat([sources[0], sources[1]], dim=1)
  54. y = self.upconv1(y)
  55. y = F.interpolate(y, size=sources[2].size()[2:], mode='bilinear', align_corners=False)
  56. y = torch.cat([y, sources[2]], dim=1)
  57. y = self.upconv2(y)
  58. y = F.interpolate(y, size=sources[3].size()[2:], mode='bilinear', align_corners=False)
  59. y = torch.cat([y, sources[3]], dim=1)
  60. y = self.upconv3(y)
  61. y = F.interpolate(y, size=sources[4].size()[2:], mode='bilinear', align_corners=False)
  62. y = torch.cat([y, sources[4]], dim=1)
  63. feature = self.upconv4(y)
  64. y = self.conv_cls(feature)
  65. return y.permute(0,2,3,1), feature
  66. def init_CRAFT_model(chekpoint_path: str, device: str, fp16: bool = True) -> CRAFT:
  67. net = CRAFT()
  68. net.load_state_dict(copyStateDict(torch.load(chekpoint_path, map_location=torch.device('cpu'))))
  69. if fp16:
  70. net = FP16Module(net)
  71. net = net.to(device)
  72. net.eval()
  73. return net