File size: 12,367 Bytes
abd2a81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
from collections import OrderedDict
from torch import nn
from torch.nn import functional as F
import torch
import torch.utils.checkpoint
# from pytorch_memlab import profile, profile_every
def humanbytes(B):
'Return the given bytes as a human friendly KB, MB, GB, or TB string'
B = float(B)
KB = float(1024)
MB = float(KB ** 2) # 1,048,576
GB = float(KB ** 3) # 1,073,741,824
TB = float(KB ** 4) # 1,099,511,627,776
if B < KB:
return '{0} {1}'.format(B, 'Bytes' if 0 == B > 1 else 'Byte')
elif KB <= B < MB:
return '{0:.2f} KB'.format(B / KB)
elif MB <= B < GB:
return '{0:.2f} MB'.format(B / MB)
elif GB <= B < TB:
return '{0:.2f} GB'.format(B / GB)
elif TB <= B:
return '{0:.2f} TB'.format(B / TB)
def get_preact_conv(in_channels, out_channels, kernel_size=3, padding=1, dropout_2d=0.2):
block = nn.Sequential(
nn.BatchNorm2d(in_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding),
nn.Dropout2d(dropout_2d)
)
return block
def _dense_layer_function_factory(norm, relu, conv):
def bn_function(*inputs):
concated_features = torch.cat(inputs, 1)
bottleneck_output = conv(relu(norm(concated_features)))
return bottleneck_output
return bn_function
class DenseLayer(nn.Module):
def __init__(self, in_channels, out_channels, dropout_2d=0.2, efficient=False):
super(DenseLayer, self).__init__()
self.add_module('norm', nn.BatchNorm2d(in_channels)),
self.add_module('relu', nn.ReLU(inplace=True)),
self.add_module('conv', nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)),
self.dropout_2d = dropout_2d
self.efficient = efficient
def forward(self, *prev_features):
dense_layer_function = _dense_layer_function_factory(self.norm, self.relu, self.conv)
if self.efficient and any(prev_feature.requires_grad for prev_feature in prev_features):
new_features = torch.utils.checkpoint.checkpoint(dense_layer_function, *prev_features)
else:
new_features = dense_layer_function(*prev_features)
if 0 < self.dropout_2d:
new_features = F.dropout2d(new_features, p=self.dropout_2d, training=self.training)
return new_features
class SELayer(nn.Module):
def __init__(self, in_channels, ratio):
super(SELayer, self).__init__()
self.block = nn.Sequential(
nn.AdaptiveAvgPool2d(1), # Global average pooling
nn.Flatten(), # Prepare for fully-connected layers
nn.Linear(in_channels, in_channels // ratio),
nn.ReLU(inplace=True),
nn.Linear(in_channels // ratio, in_channels),
nn.Sigmoid()
)
def forward(self, x):
excitation = self.block(x)
x *= excitation[:, :, None, None]
return x
class DenseBlock(nn.Module):
def __init__(self, in_channels, n_layers, growth_rate, dropout_2d, return_only_new=False, efficient=False):
super(DenseBlock, self).__init__()
assert 0 < n_layers, "n_layers should be at least 1"
self.in_channels = in_channels
self.return_only_new = return_only_new
channels = in_channels
self.layers = torch.nn.ModuleList()
for j in range(n_layers):
# Compute new feature maps
layer = DenseLayer(channels, growth_rate, dropout_2d=dropout_2d, efficient=efficient)
self.layers.append(layer)
channels += growth_rate
if return_only_new:
se_layer_in_channel = channels - in_channels # Remove input, only keep new features
else:
se_layer_in_channel = channels
self.se_layer = SELayer(se_layer_in_channel, ratio=1)
# @profile_every(1)
def forward(self, x):
features = [x]
for layer in self.layers:
new_features = layer(*features)
features.append(new_features)
if self.return_only_new:
features = features[1:]
features = torch.cat(features, 1)
features = self.se_layer(features)
return features
def get_transition_down(in_channels, out_channels, dropout_2d=0.2):
block = nn.Sequential(
get_preact_conv(in_channels, out_channels, kernel_size=1, padding=0, dropout_2d=dropout_2d),
nn.MaxPool2d(kernel_size=2, stride=2)
)
return block
def cat_non_matching(x1, x2):
diffY = x1.size()[2] - x2.size()[2]
diffX = x1.size()[3] - x2.size()[3]
x2 = F.pad(x2, (diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2))
# for padding issues, see
# https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
# https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
x = torch.cat([x1, x2], dim=1)
return x
class TransitionUp(nn.Module):
def __init__(self, in_channels, n_filters_keep):
super(TransitionUp, self).__init__()
self.conv_transpose_2d = nn.ConvTranspose2d(in_channels, n_filters_keep, kernel_size=4, stride=2, padding=1)
def forward(self, x, skip_connection):
x = self.conv_transpose_2d(x)
x = cat_non_matching(x, skip_connection)
return x
class ICTNetBackbone(nn.Module):
"""
ICTNet model: https://theictlab.org/lp/2019ICTNet.
"""
def __init__(self, preset_model='FC-DenseNet56', in_channels=3, out_channels=2, n_filters_first_conv=48, n_pool=5, growth_rate=12, n_layers_per_block=4, dropout_2d=0.2, efficient=False):
super().__init__()
# --- Handle args
if preset_model == 'FC-DenseNet56':
n_pool = 5
growth_rate = 12
n_layers_per_block = 4
elif preset_model == 'FC-DenseNet67':
n_pool = 5
growth_rate = 16
n_layers_per_block = 5
elif preset_model == 'FC-DenseNet103':
n_pool = 5
growth_rate = 16
n_layers_per_block = [4, 5, 7, 10, 12, 15, 12, 10, 7, 5, 4]
else:
n_pool = n_pool
growth_rate = growth_rate
n_layers_per_block = n_layers_per_block
if type(n_layers_per_block) == list:
assert (len(n_layers_per_block) == 2 * n_pool + 1)
elif type(n_layers_per_block) == int:
n_layers_per_block = [n_layers_per_block] * (2 * n_pool + 1)
else:
raise ValueError
# --- Instantiate layers
self.first_conv = nn.Conv2d(in_channels, n_filters_first_conv, 3, padding=1)
# Downsampling path
channels = n_filters_first_conv
self.down_dense_blocks = torch.nn.ModuleList()
self.transition_downs = torch.nn.ModuleList()
skip_connection_channels = []
for i in range(n_pool):
# Dense Block
self.down_dense_blocks.append(DenseBlock(in_channels=channels, n_layers=n_layers_per_block[i], growth_rate=growth_rate, dropout_2d=dropout_2d, return_only_new=False, efficient=efficient))
channels += growth_rate * n_layers_per_block[i]
skip_connection_channels.append(channels)
# Transition Down
self.transition_downs.append(get_transition_down(in_channels=channels, out_channels=channels, dropout_2d=dropout_2d))
# Bottleneck Dense Block
self.bottleneck_dense_block = DenseBlock(in_channels=channels, n_layers=n_layers_per_block[n_pool], growth_rate=growth_rate, dropout_2d=dropout_2d, return_only_new=True, efficient=efficient)
up_in_channels = n_layers_per_block[n_pool] * growth_rate # We will only upsample the new feature maps
# Upsampling path
self.transition_ups = torch.nn.ModuleList()
self.up_dense_blocks = torch.nn.ModuleList()
for i in range(n_pool):
# Transition Up (Upsampling + concatenation with the skip connection)
n_filters_keep = growth_rate * n_layers_per_block[n_pool + i]
self.transition_ups.append(TransitionUp(in_channels=up_in_channels, n_filters_keep=n_filters_keep))
up_out_channels = skip_connection_channels[n_pool - i - 1] + n_filters_keep # After concatenation
# Dense Block
# We will only upsample the new feature maps
self.up_dense_blocks.append(
DenseBlock(in_channels=up_out_channels, n_layers=n_layers_per_block[n_pool + i + 1], growth_rate=growth_rate,
dropout_2d=dropout_2d, return_only_new=True, efficient=efficient))
up_in_channels = growth_rate * n_layers_per_block[n_pool + i + 1] # We will only upsample the new feature maps
# Last layer
self.final_conv = nn.Conv2d(up_in_channels, out_channels, 1, padding=0)
# @profile
def forward(self, x):
stack = self.first_conv(x)
skip_connection_list = []
# print(humanbytes(torch.cuda.memory_allocated()))
for down_dense_block, transition_down in zip(self.down_dense_blocks, self.transition_downs):
# Dense Block
stack = down_dense_block(stack)
# At the end of the dense block, the current stack is stored in the skip_connections list
skip_connection_list.append(stack)
# Transition Down
stack = transition_down(stack)
# print(humanbytes(torch.cuda.memory_allocated()))
skip_connection_list = skip_connection_list[::-1]
# Bottleneck Dense Block
# We will only upsample the new feature maps
stack = self.bottleneck_dense_block(stack)
# Upsampling path
# print(humanbytes(torch.cuda.memory_allocated()))
for transition_up, up_dense_block, skip_connection in zip(self.transition_ups, self.up_dense_blocks, skip_connection_list):
# Transition Up ( Upsampling + concatenation with the skip connection)
stack = transition_up(stack, skip_connection)
# Dense Block
# We will only upsample the new feature maps
stack = up_dense_block(stack)
# print(humanbytes(torch.cuda.memory_allocated()))
# Final conv
stack = self.final_conv(stack)
result = OrderedDict()
result["out"] = stack
# print(humanbytes(torch.cuda.memory_allocated()))
return result
def count_trainable_params(model):
count = 0
for param in model.parameters():
if param.requires_grad:
count += param.numel()
return count
def main():
device = "cuda"
b = 2
c = 3
h = 512
w = 512
features = 32
# Init input
x = torch.rand((b, c, h, w), device=device)
print("x: ", x.shape, x.min().item(), x.max().item())
# # Test SELayer
# print("--- Test SELayer:")
# se_layer = SELayer(in_channels=c, ratio=1)
# y = se_layer(x)
# print("y: ", y.shape)
# print("------")
# # Test DenseBlock
# print("--- Test DenseBlock:")
# dense_block = DenseBlock(in_channels=c, n_layers=5, growth_rate=16, dropout_2d=0.2, path_type="down")
# y, new_y = dense_block(x)
# print("y: ", y.shape)
# print("new_y: ", new_y.shape)
# print("------")
# # Test transition_down
# print("--- Test transition_down:")
# transition_down = get_transition_down(in_channels=c, out_channels=features, dropout_2d=0.2)
# x_down = transition_down(x)
# print("x_down: ", x_down.shape)
# print("------")
#
# # Test TransitionUp
# print("--- Test TransitionUp:")
# transition_up = TransitionUp(in_channels=features, n_filters_keep=features//2)
# y = transition_up(x_down, x)
# print("y: ", y.shape)
# print("------")
# Test ICTNetBackboneICTNetBackboneTest SELayer:")
backbone = ICTNetBackbone(out_channels=features, preset_model="FC-DenseNet103", dropout_2d=0.0, efficient=True)
print("ICTNetBackbone has {} trainable params".format(count_trainable_params(backbone)))
# print(backbone)
backbone.to(device)
result = backbone(x)
y = result["out"]
print("y: ", y.shape)
print("------")
print("Back-prop:")
loss = torch.sum(y)
loss.backward()
if __name__ == "__main__":
main()
|