generators.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from fastai.core import *
  2. from fastai.conv_learner import model_meta, cut_model
  3. from fasterai.modules import ConvBlock, UnetBlock, UpSampleBlock, SaveFeatures
  4. from abc import ABC, abstractmethod
  5. class GeneratorModule(ABC, nn.Module):
  6. def __init__(self):
  7. super().__init__()
  8. def set_trainable(self, trainable:bool):
  9. set_trainable(self, trainable)
  10. @abstractmethod
  11. def get_layer_groups(self, precompute:bool=False)->[]:
  12. pass
  13. def freeze_to(self, n:int):
  14. c=self.get_layer_groups()
  15. for l in c: set_trainable(l, False)
  16. for l in c[n:]: set_trainable(l, True)
  17. def get_device(self):
  18. return next(self.parameters()).device
  19. class Unet34(GeneratorModule):
  20. @staticmethod
  21. def get_pretrained_resnet_base(layers_cut:int=0):
  22. f = resnet34
  23. cut,lr_cut = model_meta[f]
  24. cut-=layers_cut
  25. layers = cut_model(f(True), cut)
  26. return nn.Sequential(*layers), lr_cut
  27. def __init__(self, nf_factor:int=1, scale:int=1):
  28. super().__init__()
  29. assert (math.log(scale,2)).is_integer()
  30. leakyReLu=False
  31. self_attention=True
  32. bn=True
  33. sn=True
  34. self.rn, self.lr_cut = Unet34.get_pretrained_resnet_base()
  35. self.relu = nn.ReLU()
  36. self.sfs = [SaveFeatures(self.rn[i]) for i in [2,4,5,6]]
  37. self.up1 = UnetBlock(512,256,512*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  38. self.up2 = UnetBlock(512*nf_factor,128,512*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  39. self.up3 = UnetBlock(512*nf_factor,64,512*nf_factor, sn=sn, self_attention=self_attention, leakyReLu=leakyReLu, bn=bn)
  40. self.up4 = UnetBlock(512*nf_factor,64,256*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  41. self.up5 = UpSampleBlock(256*nf_factor, 32*nf_factor, 2*scale, sn=sn, leakyReLu=leakyReLu, bn=bn)
  42. self.out= nn.Sequential(ConvBlock(32*nf_factor, 3, ks=3, actn=False, bn=False, sn=sn), nn.Tanh())
  43. #Gets around irritating inconsistent halving coming from resnet
  44. def _pad(self, x:torch.Tensor, target:torch.Tensor)-> torch.Tensor:
  45. h = x.shape[2]
  46. w = x.shape[3]
  47. target_h = target.shape[2]*2
  48. target_w = target.shape[3]*2
  49. if h<target_h or w<target_w:
  50. padh = target_h-h if target_h > h else 0
  51. padw = target_w-w if target_w > w else 0
  52. return F.pad(x, (0,padw,0,padh), "constant",0)
  53. return x
  54. def forward(self, x_in:torch.Tensor):
  55. x = self.rn(x_in)
  56. x = self.relu(x)
  57. x = self.up1(x, self._pad(self.sfs[3].features, x))
  58. x = self.up2(x, self._pad(self.sfs[2].features, x))
  59. x = self.up3(x, self._pad(self.sfs[1].features, x))
  60. x = self.up4(x, self._pad(self.sfs[0].features, x))
  61. x = self.up5(x)
  62. x = self.out(x)
  63. return x
  64. def get_layer_groups(self, precompute:bool=False)->[]:
  65. lgs = list(split_by_idxs(children(self.rn), [self.lr_cut]))
  66. return lgs + [children(self)[1:]]
  67. def close(self):
  68. for sf in self.sfs:
  69. sf.remove()
  70. class Unet34_V2(GeneratorModule):
  71. @staticmethod
  72. def get_pretrained_resnet_base(layers_cut:int=0):
  73. f = resnet34
  74. cut,lr_cut = model_meta[f]
  75. cut-=layers_cut
  76. layers = cut_model(f(True), cut)
  77. return nn.Sequential(*layers), lr_cut
  78. def __init__(self, nf_factor:int=1, scale:int=1):
  79. super().__init__()
  80. assert (math.log(scale,2)).is_integer()
  81. leakyReLu=False
  82. self_attention=True
  83. bn=True
  84. sn=True
  85. self.rn, self.lr_cut = Unet34.get_pretrained_resnet_base()
  86. self.relu = nn.ReLU()
  87. self.sfs = [SaveFeatures(self.rn[i]) for i in [2,4,5,6]]
  88. self.up1 = UnetBlock(512,256,512*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  89. self.up2 = UnetBlock(512*nf_factor,128,512*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  90. self.up3 = UnetBlock(512*nf_factor,64,256*nf_factor, sn=sn, self_attention=self_attention, leakyReLu=leakyReLu, bn=bn)
  91. self.up4 = UnetBlock(256*nf_factor,64,128*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  92. self.up5 = UpSampleBlock(128*nf_factor, 32*nf_factor, 2*scale, sn=sn, leakyReLu=leakyReLu, bn=bn)
  93. self.out= nn.Sequential(ConvBlock(32*nf_factor, 3, ks=3, actn=False, bn=False, sn=sn), nn.Tanh())
  94. #Gets around irritating inconsistent halving coming from resnet
  95. def _pad(self, x:torch.Tensor, target:torch.Tensor)-> torch.Tensor:
  96. h = x.shape[2]
  97. w = x.shape[3]
  98. target_h = target.shape[2]*2
  99. target_w = target.shape[3]*2
  100. if h<target_h or w<target_w:
  101. padh = target_h-h if target_h > h else 0
  102. padw = target_w-w if target_w > w else 0
  103. return F.pad(x, (0,padw,0,padh), "constant",0)
  104. return x
  105. def forward(self, x_in:torch.Tensor):
  106. x = self.rn(x_in)
  107. x = self.relu(x)
  108. x = self.up1(x, self._pad(self.sfs[3].features, x))
  109. x = self.up2(x, self._pad(self.sfs[2].features, x))
  110. x = self.up3(x, self._pad(self.sfs[1].features, x))
  111. x = self.up4(x, self._pad(self.sfs[0].features, x))
  112. x = self.up5(x)
  113. x = self.out(x)
  114. return x
  115. def get_layer_groups(self, precompute:bool=False)->[]:
  116. lgs = list(split_by_idxs(children(self.rn), [self.lr_cut]))
  117. return lgs + [children(self)[1:]]
  118. def close(self):
  119. for sf in self.sfs:
  120. sf.remove()