generators.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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):
  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()