Art generation with VQGAN + CLIP in Docker, with a simple web UI for anyone with a GPU. A simplified and expanded take on Kevin Costa's work.
aiai-artclipdockerdocker-composegpuhandcodedimagenetpythonpytorchtorchtorchvisionvqganvqgan-clip
1import torch
2import torch.nn as nn
3
4from core.taming.utils import Normalize, nonlinearity
5
6from core.taming.modules.diffusion import AttnBlock, ResnetBlock, Downsample
7
8
9class Encoder(nn.Module):
10 def __init__(self, *, ch, out_ch, ch_mult=(1, 2, 4, 8), num_res_blocks,
11 attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels,
12 resolution, z_channels, double_z=True, **ignore_kwargs):
13 super().__init__()
14 self.ch = ch
15 self.temb_ch = 0
16 self.num_resolutions = len(ch_mult)
17 self.num_res_blocks = num_res_blocks
18 self.resolution = resolution
19 self.in_channels = in_channels
20
21 # downsampling
22 self.conv_in = torch.nn.Conv2d(in_channels, self.ch, kernel_size=3, stride=1, padding=1)
23
24 curr_res = resolution
25 in_ch_mult = (1,) + tuple(ch_mult)
26 self.down = nn.ModuleList()
27 for i_level in range(self.num_resolutions):
28 block = nn.ModuleList()
29 attn = nn.ModuleList()
30 block_in = ch * in_ch_mult[i_level]
31 block_out = ch * ch_mult[i_level]
32 for i_block in range(self.num_res_blocks):
33 block.append(ResnetBlock(in_channels=block_in,
34 out_channels=block_out,
35 temb_channels=self.temb_ch,
36 dropout=dropout))
37 block_in = block_out
38 if curr_res in attn_resolutions:
39 attn.append(AttnBlock(block_in))
40 down = nn.Module()
41 down.block = block
42 down.attn = attn
43 if i_level != self.num_resolutions - 1:
44 down.downsample = Downsample(block_in, resamp_with_conv)
45 curr_res = curr_res // 2
46 self.down.append(down)
47
48 # middle
49 self.mid = nn.Module()
50 self.mid.block_1 = ResnetBlock(
51 in_channels=block_in, out_channels=block_in, temb_channels=self.temb_ch, dropout=dropout
52 )
53 self.mid.attn_1 = AttnBlock(block_in)
54 self.mid.block_2 = ResnetBlock(
55 in_channels=block_in, out_channels=block_in, temb_channels=self.temb_ch, dropout=dropout
56 )
57
58 # end
59 self.norm_out = Normalize(block_in)
60 self.conv_out = torch.nn.Conv2d(
61 block_in, 2 * z_channels if double_z else z_channels, kernel_size=3, stride=1, padding=1
62 )
63
64 def forward(self, x):
65 # assert x.shape[2] == x.shape[3] == self.resolution, "{}, {}, {}".format(
66 # x.shape[2], x.shape[3], self.resolution
67 # )
68
69 # timestep embedding
70 temb = None
71
72 # downsampling
73 hs = [self.conv_in(x)]
74 for i_level in range(self.num_resolutions):
75 for i_block in range(self.num_res_blocks):
76 h = self.down[i_level].block[i_block](hs[-1], temb)
77 if len(self.down[i_level].attn) > 0:
78 h = self.down[i_level].attn[i_block](h)
79 hs.append(h)
80 if i_level != self.num_resolutions - 1:
81 hs.append(self.down[i_level].downsample(hs[-1]))
82
83 # middle
84 h = hs[-1]
85 h = self.mid.block_1(h, temb)
86 h = self.mid.attn_1(h)
87 h = self.mid.block_2(h, temb)
88
89 # end
90 h = self.norm_out(h)
91 h = nonlinearity(h)
92 h = self.conv_out(h)
93 return h