parlorsky commited on
Commit
db3a717
·
verified ·
1 Parent(s): b4cc337

Upload ComfyUI_Oz/modules/neural_grain/net.py with huggingface_hub

Browse files
ComfyUI_Oz/modules/neural_grain/net.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ #%%
4
+ import torch
5
+ import torch.nn as nn
6
+
7
+ #%%
8
+
9
+ class ResidualBlock(nn.Module):
10
+ def __init__(self, channel, k_size = (3,3)):
11
+ super().__init__()
12
+ self.block = nn.Sequential(nn.Conv2d(channel,channel,kernel_size = k_size, stride=1, padding='same', padding_mode='reflect'),
13
+ nn.LeakyReLU(),
14
+ nn.InstanceNorm2d(channel),
15
+ nn.Conv2d(channel,channel,kernel_size = k_size, stride=1, padding='same', padding_mode='reflect'),
16
+ )
17
+ self.relu_out = nn.LeakyReLU()
18
+ def forward(self,x):
19
+ return self.relu_out(self.block(x) + x)
20
+
21
+ class MyNorm(nn.Module):
22
+ """
23
+ Custom Adaptive Instance Normalization layer
24
+ """
25
+ def __init__(self, channel_size, insize = 1):
26
+ super().__init__()
27
+ self.insize = insize
28
+ self.channel_size = channel_size
29
+ self.std_weight = nn.Linear(insize, self.channel_size)
30
+ self.mean_weight = nn.Linear(insize, self.channel_size)
31
+
32
+ def forward(self, x, grain_type):
33
+
34
+ std = self.std_weight(grain_type)
35
+ mean = self.mean_weight(grain_type)
36
+
37
+ x = x * std.view(*std.shape,1,1).repeat(1,1,*x.shape[-2:])
38
+ x = x + mean.view(*mean.shape,1,1).repeat(1,1,*x.shape[-2:])
39
+ return x
40
+
41
+ class GrainNet(nn.Module):
42
+ def __init__(self, activation = 'tanh', block_nb = 2):
43
+ """
44
+ Network which adds grain to a given image.
45
+
46
+ Parameters
47
+ ----------
48
+ activation : bool, optional
49
+ Tells if we put a sigmoid at the end of the network. The default is True.
50
+
51
+ Returns
52
+ -------
53
+ None.
54
+
55
+ """
56
+ super(GrainNet, self).__init__()
57
+
58
+ if not block_nb in [1,2,3]:
59
+ raise ValueError('block_nb must be 1,2 or 3')
60
+ self.block_nb = block_nb
61
+
62
+ self.entry_conv = nn.Sequential(nn.Conv2d(2,16,kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'),
63
+ nn.LeakyReLU())
64
+
65
+ self.block1 = nn.Sequential(ResidualBlock(16),
66
+ nn.InstanceNorm2d(16))
67
+ self.mn1 = MyNorm(16)
68
+
69
+ if self.block_nb == 3:
70
+
71
+ self.augment = nn.Sequential(nn.Conv2d(16,32,kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'),
72
+ nn.LeakyReLU())
73
+
74
+ self.block2 = nn.Sequential(ResidualBlock(32),
75
+ nn.InstanceNorm2d(32))
76
+ self.mn2 = MyNorm(32)
77
+
78
+ self.reduce = nn.Sequential(nn.Conv2d(32,16,kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'),
79
+ nn.LeakyReLU())
80
+
81
+ if self.block_nb > 1:
82
+
83
+ self.block3 = nn.Sequential(ResidualBlock(16),
84
+ nn.InstanceNorm2d(16))
85
+ self.mn3 = MyNorm(16)
86
+
87
+ self.out_conv = nn.Sequential(nn.Conv2d(16,1,kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'))
88
+
89
+
90
+ self.activation = activation
91
+ self.tanh = nn.Tanh()
92
+ self.sigmoid = nn.Sigmoid()
93
+
94
+ def forward(self, img, grain_radius, seed=None):
95
+ if not (seed is None):
96
+ torch.manual_seed(seed)
97
+
98
+ noise = torch.randn(img.shape)
99
+ if self.entry_conv[0].weight.is_cuda:
100
+ noise = noise.cuda()
101
+
102
+ x = torch.cat((noise,img), dim=1)
103
+
104
+ x0 = self.entry_conv(x)
105
+
106
+ x1 = self.block1(x0)
107
+ x1 = self.mn1(x1, grain_radius)
108
+
109
+ if self.block_nb == 3:
110
+ x2 = self.augment(x1)
111
+
112
+ x3 = self.block2(x2)
113
+ x3 = self.mn2(x3, grain_radius)
114
+
115
+ x4 = self.reduce(x3)
116
+
117
+ x5 = self.block3(x4 + x1)
118
+ x5 = self.mn3(x5, grain_radius)
119
+ x6 = self.out_conv(x5 + x0)
120
+
121
+ if self.block_nb == 2:
122
+ x5 = self.block3(x1)
123
+ x5 = self.mn3(x5, grain_radius)
124
+ x6 = self.out_conv(x5 + x0)
125
+
126
+ if self.block_nb == 1:
127
+ x6 = self.out_conv(x1)
128
+
129
+ if self.activation == 'tanh':
130
+ x6 = self.tanh(x6)
131
+ x6 = torch.clamp((0.5*x6 + 0.5), 0, 1) #Normalise images
132
+ elif self.activation == 'sigmoid':
133
+ x6 = self.sigmoid(x6)
134
+ return x6
135
+
136
+ class Classifier(nn.Module):
137
+ def __init__(self, nb_channels = 1, latent_size = 1, activation = 'sigmoid'):
138
+ """
139
+ Classifier architecture
140
+
141
+ Parameters
142
+ ----------
143
+ size: int
144
+ Input size (we assume image is square), it defines how many pooling and conv layers we add to the network.
145
+
146
+ nb_channels: int
147
+ Indicates how many channels has the input
148
+
149
+ latent_size: int
150
+ Indicates how many dimensions has the output
151
+
152
+ Returns
153
+ -------
154
+ None.
155
+
156
+ """
157
+ super(Classifier, self).__init__()
158
+
159
+ self.latent_size = latent_size
160
+ self.nb_channels = nb_channels
161
+
162
+ self.layers = nn.Sequential(
163
+ nn.Conv2d(self.nb_channels, 16, kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'),
164
+ nn.SiLU(),
165
+ nn.Conv2d(16, 16, kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'),
166
+ nn.SiLU(),
167
+ nn.Conv2d(16, 16, kernel_size = (3,3), stride=1, padding='same', padding_mode='reflect'),
168
+ nn.SiLU(),
169
+ nn.AdaptiveAvgPool2d((16,16)))
170
+
171
+ self.dense1 = nn.Linear(4096,512)
172
+ self.dense2 = nn.Linear(512,latent_size)
173
+
174
+ self.activation = activation
175
+ if not self.activation is None:
176
+ self.acti = nn.Sigmoid()
177
+
178
+ def to(self, device):
179
+ for i in range(len(self.layers)):
180
+ self.layers[i].to(device)
181
+ (self.dense1).to(device)
182
+ (self.dense2).to(device)
183
+
184
+ def forward(self, x):
185
+ x = self.layers(x)
186
+ z = self.dense1(x.flatten(start_dim=1))
187
+ z = self.dense2(z)
188
+ if not self.activation is None:
189
+ z = self.acti(z)
190
+ return z
191
+
192
+
193
+ # %%