code: def build_torch_model(weights_path=None):
      from collections import namedtuple, OrderedDict
  
      import torch
      import torchvision
  
      def init_weights(modules):
          for m in modules:
              if isinstance(m, torch.nn.Conv2d):
                  torch.nn.init.xavier_uniform_(m.weight.data)
                  if m.bias is not None:
                      m.bias.data.zero_()
              elif isinstance(m, torch.nn.BatchNorm2d):
                  m.weight.data.fill_(1)
                  m.bias.data.zero_()
              elif isinstance(m, torch.nn.Linear):
                  m.weight.data.normal_(0, 0.01)
                  m.bias.data.zero_()
  
      class vgg16_bn(torch.nn.Module):
          def __init__(self, pretrained=True, freeze=True):
              super().__init__()
              # We don't bother loading the pretrained VGG
              # because we're going to use the weights
              # at weights_path.
              vgg_pretrained_features = torchvision.models.vgg16_bn(
                  pretrained=False
              ).features
              self.slice1 = torch.nn.Sequential()
              self.slice2 = torch.nn.Sequential()
              self.slice3 = torch.nn.Sequential()
              self.slice4 = torch.nn.Sequential()
              self.slice5 = torch.nn.Sequential()
              for x in range(12):  # conv2_2
                  self.slice1.add_module(
                      str(x), vgg_pretrained_features[x]
                  )
              for x in range(12, 19):  # conv3_3
                  self.slice2.add_module(
                      str(x), vgg_pretrained_features[x]
                  )
              for x in range(19, 29):  # conv4_3
                  self.slice3.add_module(
                      str(x), vgg_pretrained_features[x]
                  )
              for x in range(29, 39):  # conv5_3
                  self.slice4.add_module(
                      str(x), vgg_pretrained_features[x]
                  )
  
              # fc6, fc7 without atrous conv
              self.slice5 = torch.nn.Sequential(
                  torch.nn.MaxPool2d(
                      kernel_size=3, stride=1, padding=1
                  ),
                  torch.nn.Conv2d(
                      512, 1024, kernel_size=3, padding=6, dilation=6
                  ),
                  torch.nn.Conv2d(1024, 1024, kernel_size=1),
              )
  
              if not pretrained:
                  init_weights(self.slice1.modules())
                  init_weights(self.slice2.modules())
                  init_weights(self.slice3.modules())
                  init_weights(self.slice4.modules())
  
              init_weights(
                  self.slice5.modules()
              )  # no pretrained model for fc6 and fc7
  
              if freeze:
                  for (
                      param
                  ) in self.slice1.parameters():  # only first conv
                      param.requires_grad = False
  
          def forward(self, X):  # pylint: disable=arguments-differ
              h = self.slice1(X)
              h_relu2_2 = h
              h = self.slice2(h)
              h_relu3_2 = h
              h = self.slice3(h)
              h_relu4_3 = h
              h = self.slice4(h)
              h_relu5_3 = h
              h = self.slice5(h)
              h_fc7 = h
              vgg_outputs = namedtuple(
                  "vgg_outputs",
                  ["fc7", "relu5_3", "relu4_3", "relu3_2", "relu2_2"],
              )
              out = vgg_outputs(
                  h_fc7, h_relu5_3, h_relu4_3, h_relu3_2, h_relu2_2
              )
              return out
  
      class double_conv(torch.nn.Module):
          def __init__(self, in_ch, mid_ch, out_ch):
              super().__init__()
              self.conv = torch.nn.Sequential(
                  torch.nn.Conv2d(
                      in_ch + mid_ch, mid_ch, kernel_size=1
                  ),
                  torch.nn.BatchNorm2d(mid_ch),
                  torch.nn.ReLU(inplace=True),
                  torch.nn.Conv2d(
                      mid_ch, out_ch, kernel_size=3, padding=1
                  ),
                  torch.nn.BatchNorm2d(out_ch),
                  torch.nn.ReLU(inplace=True),
              )
  
          def forward(self, x):  # pylint: disable=arguments-differ
              x = self.conv(x)
              return x
  
      class CRAFT(torch.nn.Module):
          def __init__(self, pretrained=False, freeze=False):
              super().__init__()
              # Base network
              self.basenet = vgg16_bn(pretrained, freeze)
              # U network
              self.upconv1 = double_conv(1024, 512, 256)
              self.upconv2 = double_conv(512, 256, 128)
              self.upconv3 = double_conv(256, 128, 64)
              self.upconv4 = double_conv(128, 64, 32)
  
              num_class = 2
              self.conv_cls = torch.nn.Sequential(
                  torch.nn.Conv2d(32, 32, kernel_size=3, padding=1),
                  torch.nn.ReLU(inplace=True),
                  torch.nn.Conv2d(32, 32, kernel_size=3, padding=1),
                  torch.nn.ReLU(inplace=True),
                  torch.nn.Conv2d(32, 16, kernel_size=3, padding=1),
                  torch.nn.ReLU(inplace=True),
                  torch.nn.Conv2d(16, 16, kernel_size=1),
                  torch.nn.ReLU(inplace=True),
                  torch.nn.Conv2d(16, num_class, kernel_size=1),
              )
  
              init_weights(self.upconv1.modules())
              init_weights(self.upconv2.modules())
              init_weights(self.upconv3.modules())
              init_weights(self.upconv4.modules())
              init_weights(self.conv_cls.modules())
  
          def forward(self, x):  # pylint: disable=arguments-differ
              # Base network
              sources = self.basenet(x)
              # U network
              # pylint: disable=E1101
              y = torch.cat([sources[0], sources[1]], dim=1)
  
              y = self.upconv1(y)
  
              y = torch.nn.functional.interpolate(
                  y,
                  size=sources[2].size()[2:],
                  mode="bilinear",
                  align_corners=False,
              )
              y = torch.cat([y, sources[2]], dim=1)
              y = self.upconv2(y)
  
              y = torch.nn.functional.interpolate(
                  y,
                  size=sources[3].size()[2:],
                  mode="bilinear",
                  align_corners=False,
              )
              y = torch.cat([y, sources[3]], dim=1)
              y = self.upconv3(y)
  
              y = torch.nn.functional.interpolate(
                  y,
                  size=sources[4].size()[2:],
                  mode="bilinear",
                  align_corners=False,
              )
              y = torch.cat([y, sources[4]], dim=1)
              # pylint: enable=E1101
              feature = self.upconv4(y)
  
              y = self.conv_cls(feature)
  
              return y.permute(0, 2, 3, 1), feature
  
      def copyStateDict(state_dict):
          if list(state_dict.keys())[0].startswith("module"):
              start_idx = 1
          else:
              start_idx = 0
          new_state_dict = OrderedDict()
          for k, v in state_dict.items():
              name = ".".join(k.split(".")[start_idx:])
              new_state_dict[name] = v
          return new_state_dict
  
      model = CRAFT(pretrained=True).eval()
      if weights_path is not None:
          model.load_state_dict(
              copyStateDict(
                  torch.load(
                      weights_path, map_location=torch.device("cpu")
                  )
              )
          )
      return model
  
imports: import typing
  import cv2
  import numpy
  import tensorflow
  import efficientnet.tfkeras
  from tensorflow import keras
  from .None import tools
label_desc: Use image node_id 13 for calling a global function with example usage: build_torch_model(weights_path) and returns: model, new_state_dict, out, x, y,  feature