generators.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from fastai.core import *
  2. from fastai.conv_learner import model_meta, cut_model
  3. from .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.up1 = UnetBlock(512,256,512*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  37. self.up2 = UnetBlock(512*nf_factor,128,512*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  38. self.up3 = UnetBlock(512*nf_factor,64,512*nf_factor, sn=sn, self_attention=self_attention, leakyReLu=leakyReLu, bn=bn)
  39. self.up4 = UnetBlock(512*nf_factor,64,256*nf_factor, sn=sn, leakyReLu=leakyReLu, bn=bn)
  40. self.up5 = UpSampleBlock(256*nf_factor, 32*nf_factor, 2*scale, sn=sn, leakyReLu=leakyReLu, bn=bn)
  41. self.out= nn.Sequential(ConvBlock(32*nf_factor, 3, ks=3, actn=False, bn=False, sn=sn), nn.Tanh())
  42. #Gets around irritating inconsistent halving coming from resnet
  43. def _pad(self, x:torch.Tensor, target:torch.Tensor, total_padh:int, total_padw:int)-> torch.Tensor:
  44. h = x.shape[2]
  45. w = x.shape[3]
  46. target_h = target.shape[2]*2
  47. target_w = target.shape[3]*2
  48. if h<target_h or w<target_w:
  49. padh = target_h-h if target_h > h else 0
  50. total_padh = total_padh + padh
  51. padw = target_w-w if target_w > w else 0
  52. total_padw = total_padw + padw
  53. return (F.pad(x, (0,padw,0,padh), "reflect",0), total_padh, total_padw)
  54. return (x, total_padh, total_padw)
  55. def _remove_padding(self, x:torch.Tensor, padh:int, padw:int)->torch.Tensor:
  56. if padw == 0 and padh == 0:
  57. return x
  58. target_h = x.shape[2]-padh
  59. target_w = x.shape[3]-padw
  60. return x[:,:,:target_h, :target_w]
  61. def forward(self, x_in:torch.Tensor):
  62. x = self.rn[0](x_in)
  63. x = self.rn[1](x)
  64. x = self.rn[2](x)
  65. enc0 = x
  66. x = self.rn[3](x)
  67. x = self.rn[4](x)
  68. enc1 = x
  69. x = self.rn[5](x)
  70. enc2 = x
  71. x = self.rn[6](x)
  72. enc3 = x
  73. x = self.rn[7](x)
  74. padw = 0
  75. padh = 0
  76. x = self.relu(x)
  77. penc3, padh, padw = self._pad(enc3, x, padh, padw)
  78. x = self.up1(x, penc3)
  79. penc2, padh, padw = self._pad(enc2, x, padh, padw)
  80. x = self.up2(x, penc2)
  81. penc1, padh, padw = self._pad(enc1, x, padh, padw)
  82. x = self.up3(x, penc1)
  83. penc0, padh, padw = self._pad(enc0, x, padh, padw)
  84. x = self.up4(x, penc0)
  85. x = self._remove_padding(x, padh, padw)
  86. x = self.up5(x)
  87. x = self.out(x)
  88. return x
  89. def get_layer_groups(self, precompute:bool=False)->[]:
  90. lgs = list(split_by_idxs(children(self.rn), [self.lr_cut]))
  91. return lgs + [children(self)[1:]]
  92. def close(self):
  93. for sf in self.sfs:
  94. sf.remove()