| 74 | return x.sequential([self.block_1, self.attn_1, self.block_2]) |
| 75 | |
| 76 | class Decoder: |
| 77 | def __init__(self): |
| 78 | sz = [(128, 256), (256, 512), (512, 512), (512, 512)] |
| 79 | self.conv_in = Conv2d(4,512,3, padding=1) |
| 80 | self.mid = Mid(512) |
| 81 | |
| 82 | arr = [] |
| 83 | for i,s in enumerate(sz): |
| 84 | arr.append({"block": |
| 85 | [ResnetBlock(s[1], s[0]), |
| 86 | ResnetBlock(s[0], s[0]), |
| 87 | ResnetBlock(s[0], s[0])]}) |
| 88 | if i != 0: arr[-1]['upsample'] = {"conv": Conv2d(s[0], s[0], 3, padding=1)} |
| 89 | self.up = arr |
| 90 | |
| 91 | self.norm_out = GroupNorm(32, 128) |
| 92 | self.conv_out = Conv2d(128, 3, 3, padding=1) |
| 93 | |
| 94 | def __call__(self, x): |
| 95 | x = self.conv_in(x) |
| 96 | x = self.mid(x) |
| 97 | |
| 98 | for l in self.up[::-1]: |
| 99 | for b in l['block']: |
| 100 | x = b(x) |
| 101 | if 'upsample' in l: |
| 102 | # https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html ? |
| 103 | bs,c,py,px = x.shape |
| 104 | x = x.reshape(bs, c, py, 1, px, 1).expand(bs, c, py, 2, px, 2).reshape(bs, c, py*2, px*2) |
| 105 | x = l['upsample']['conv'](x) |
| 106 | x.realize() |
| 107 | |
| 108 | return self.conv_out(self.norm_out(x).swish()) |
| 109 | |
| 110 | class Encoder: |
| 111 | def __init__(self): |