File size: 26,135 Bytes
d4ebf73 |
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import SyncBatchNorm as BatchNorm2d
import re
import os, sys
# from six import moves
class Exchange(nn.Module):
def __init__(self):
super(Exchange, self).__init__()
def forward(self, x, bn, bn_threshold):
bn1, bn2 = bn[0].weight.abs(), bn[1].weight.abs()
x1, x2 = torch.zeros_like(x[0]), torch.zeros_like(x[1])
x1[:, bn1 >= bn_threshold] = x[0][:, bn1 >= bn_threshold]
x1[:, bn1 < bn_threshold] = x[1][:, bn1 < bn_threshold]
x2[:, bn2 >= bn_threshold] = x[1][:, bn2 >= bn_threshold]
x2[:, bn2 < bn_threshold] = x[0][:, bn2 < bn_threshold]
return [x1, x2]
class ModuleParallel(nn.Module):
def __init__(self, module):
super(ModuleParallel, self).__init__()
self.module = module
def forward(self, x_parallel):
return [self.module(x) for x in x_parallel]
class BatchNorm2dParallel(nn.Module):
def __init__(self, num_features, num_parallel):
super(BatchNorm2dParallel, self).__init__()
for i in range(num_parallel):
setattr(self, 'bn_' + str(i), BatchNorm2d(num_features))
def forward(self, x_parallel):
return [getattr(self, 'bn_' + str(i))(x) for i, x in enumerate(x_parallel)]
class ChannelExchangingNetwork(nn.Module):
def __init__(self, num_layers, num_classes, num_parallel, l1_lambda, bn_threshold):
super(ChannelExchangingNetwork, self).__init__()
self.model = refinenet(num_layers, num_classes, num_parallel, bn_threshold)
self.model = model_init(self.model, num_layers, num_parallel, imagenet=True) #Only initializes the encoder
self.l1_lambda = l1_lambda
def get_slim_params(self):
slim_params = []
for name, param in self.model.named_parameters():
if param.requires_grad and name.endswith('weight') and 'bn2' in name:
if len(slim_params) % 2 == 0:
slim_params.append(param[:len(param) // 2])
else:
slim_params.append(param[len(param) // 2:])
return slim_params
def forward(self, data, get_sup_loss = False, gt = None, criterion = None):
b, c, h, w = data[0].shape #rgb is the 0th element
pred = self.model(data)
for i in range(len(pred)):
pred[i] = F.interpolate(pred[i], size=(h, w), mode='bilinear', align_corners=True)
if not self.training:
return pred
else: # training
if get_sup_loss:
l1_loss = self.l1_lambda * self.get_l1_loss(data[0].get_device())
sup_loss = self.get_sup_loss(pred, gt, criterion)
return pred, sup_loss + l1_loss
else:
return pred
def get_sup_loss(self, pred, gt, criterion):
sup_loss = 0
for p in pred:
p = p[:gt.shape[0]] #Getting loss for only those examples in batch where gt exists. Won't get sup loss for unlabeled data.
sup_loss += criterion(p, gt)
return sup_loss / len(pred)
def get_params(self):
self.slim_params = self.get_slim_params() #Doing it here and not in __init__ because first the model should be put in appropriate device before accumulating slim_params
# enc_params, dec_params = [], []
# for name, param in self.model.named_parameters():
# if bool(re.match('.*conv1.*|.*bn1.*|.*layer.*', name)):
# enc_params.append(param)
# else:
# dec_params.append(param)
# return enc_params, dec_params
param_groups = [[], [], []]
for name, param in self.model.named_parameters():
if "norm" in name:
param_groups[1].append(param)
elif bool(re.match('.*conv1.*|.*bn1.*|.*layer.*', name)):
param_groups[0].append(param)
else:
param_groups[2].append(param)
return param_groups
def get_l1_loss(self, device):
L1_norm = sum([L1_penalty(m, device) for m in self.slim_params])
if L1_norm > 0:
return L1_norm.to(device)
else:
return torch.tensor(0).to(device)
"""RefineNet-LightWeight
RefineNet-LigthWeight PyTorch for non-commercial purposes
Copyright (c) 2018, Vladimir Nekrasov ([email protected])
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
models_urls = {
'101_voc' : 'https://cloudstor.aarnet.edu.au/plus/s/Owmttk9bdPROwc6/download',
'18_imagenet' : 'https://download.pytorch.org/models/resnet18-f37072fd.pth',
'50_imagenet' : 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'101_imagenet': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'152_imagenet': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}
bottleneck_idx = 0
save_idx = 0
def conv3x3(in_planes, out_planes, stride=1, bias=False):
"3x3 convolution with padding"
return ModuleParallel(nn.Conv2d(in_planes, out_planes, kernel_size=3,
stride=stride, padding=1, bias=bias))
def conv1x1(in_planes, out_planes, stride=1, bias=False):
"1x1 convolution"
return ModuleParallel(nn.Conv2d(in_planes, out_planes, kernel_size=1,
stride=stride, padding=0, bias=bias))
class CRPBlock(nn.Module):
def __init__(self, in_planes, out_planes, num_stages, num_parallel):
super(CRPBlock, self).__init__()
for i in range(num_stages):
setattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'),
conv3x3(in_planes if (i == 0) else out_planes, out_planes))
self.stride = 1
self.num_stages = num_stages
self.num_parallel = num_parallel
self.maxpool = ModuleParallel(nn.MaxPool2d(kernel_size=5, stride=1, padding=2))
def forward(self, x):
top = x
for i in range(self.num_stages):
top = self.maxpool(top)
top = getattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'))(top)
x = [x[l] + top[l] for l in range(self.num_parallel)]
return x
stages_suffixes = {0 : '_conv', 1 : '_conv_relu_varout_dimred'}
class RCUBlock(nn.Module):
def __init__(self, in_planes, out_planes, num_blocks, num_stages, num_parallel):
super(RCUBlock, self).__init__()
for i in range(num_blocks):
for j in range(num_stages):
setattr(self, '{}{}'.format(i + 1, stages_suffixes[j]),
conv3x3(in_planes if (i == 0) and (j == 0) else out_planes,
out_planes, bias=(j == 0)))
self.stride = 1
self.num_blocks = num_blocks
self.num_stages = num_stages
self.num_parallel = num_parallel
self.relu = ModuleParallel(nn.ReLU(inplace=True))
def forward(self, x):
for i in range(self.num_blocks):
residual = x
for j in range(self.num_stages):
x = self.relu(x)
x = getattr(self, '{}{}'.format(i + 1, stages_suffixes[j]))(x)
x = [x[l] + residual[l] for l in range(self.num_parallel)]
return x
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, num_parallel, bn_threshold, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = BatchNorm2dParallel(planes, num_parallel)
self.relu = ModuleParallel(nn.ReLU(inplace=True))
self.conv2 = conv3x3(planes, planes)
self.bn2 = BatchNorm2dParallel(planes, num_parallel)
self.num_parallel = num_parallel
self.downsample = downsample
self.stride = stride
self.exchange = Exchange()
self.bn_threshold = bn_threshold
self.bn2_list = []
for module in self.bn2.modules():
if isinstance(module, BatchNorm2d):
self.bn2_list.append(module)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if len(x) > 1:
out = self.exchange(out, self.bn2_list, self.bn_threshold)
if self.downsample is not None:
residual = self.downsample(x)
out = [out[l] + residual[l] for l in range(self.num_parallel)]
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, num_parallel, bn_threshold, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = conv1x1(inplanes, planes)
self.bn1 = BatchNorm2dParallel(planes, num_parallel)
self.conv2 = conv3x3(planes, planes, stride=stride)
self.bn2 = BatchNorm2dParallel(planes, num_parallel)
self.conv3 = conv1x1(planes, planes * 4)
self.bn3 = BatchNorm2dParallel(planes * 4, num_parallel)
self.relu = ModuleParallel(nn.ReLU(inplace=True))
self.num_parallel = num_parallel
self.downsample = downsample
self.stride = stride
self.exchange = Exchange()
self.bn_threshold = bn_threshold
self.bn2_list = []
for module in self.bn2.modules():
if isinstance(module, BatchNorm2d):
self.bn2_list.append(module)
def forward(self, x):
residual = x
out = x
out = self.conv1(out)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if len(x) > 1:
out = self.exchange(out, self.bn2_list, self.bn_threshold)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out = [out[l] + residual[l] for l in range(self.num_parallel)]
out = self.relu(out)
return out
class RefineNet(nn.Module):
def __init__(self, block, layers, num_parallel, num_classes=21, bn_threshold=2e-2):
self.inplanes = 64
self.num_parallel = num_parallel
super(RefineNet, self).__init__()
self.dropout = ModuleParallel(nn.Dropout(p=0.5))
self.conv1 = ModuleParallel(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False))
self.bn1 = BatchNorm2dParallel(64, num_parallel)
self.relu = ModuleParallel(nn.ReLU(inplace=True))
self.maxpool = ModuleParallel(nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
self.layer1 = self._make_layer(block, 64, layers[0], bn_threshold)
self.layer2 = self._make_layer(block, 128, layers[1], bn_threshold, stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], bn_threshold, stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], bn_threshold, stride=2)
self.p_ims1d2_outl1_dimred = conv3x3(2048, 512)
self.adapt_stage1_b = self._make_rcu(512, 512, 2, 2)
self.mflow_conv_g1_pool = self._make_crp(512, 512, 4)
self.mflow_conv_g1_b = self._make_rcu(512, 512, 3, 2)
self.mflow_conv_g1_b3_joint_varout_dimred = conv3x3(512, 256)
self.p_ims1d2_outl2_dimred = conv3x3(1024, 256)
self.adapt_stage2_b = self._make_rcu(256, 256, 2, 2)
self.adapt_stage2_b2_joint_varout_dimred = conv3x3(256, 256)
self.mflow_conv_g2_pool = self._make_crp(256, 256, 4)
self.mflow_conv_g2_b = self._make_rcu(256, 256, 3, 2)
self.mflow_conv_g2_b3_joint_varout_dimred = conv3x3(256, 256)
self.p_ims1d2_outl3_dimred = conv3x3(512, 256)
self.adapt_stage3_b = self._make_rcu(256, 256, 2, 2)
self.adapt_stage3_b2_joint_varout_dimred = conv3x3(256, 256)
self.mflow_conv_g3_pool = self._make_crp(256, 256, 4)
self.mflow_conv_g3_b = self._make_rcu(256, 256, 3, 2)
self.mflow_conv_g3_b3_joint_varout_dimred = conv3x3(256, 256)
self.p_ims1d2_outl4_dimred = conv3x3(256, 256)
self.adapt_stage4_b = self._make_rcu(256, 256, 2, 2)
self.adapt_stage4_b2_joint_varout_dimred = conv3x3(256, 256)
self.mflow_conv_g4_pool = self._make_crp(256, 256, 4)
self.mflow_conv_g4_b = self._make_rcu(256, 256, 3, 2)
self.clf_conv = conv3x3(256, num_classes, bias=True)
self.alpha = nn.Parameter(torch.ones(num_parallel, requires_grad=True))
# self.alpha = nn.Parameter(torch.ones([1, num_parallel, 157, 157], requires_grad=True))
self.register_parameter('alpha', self.alpha)
def _make_crp(self, in_planes, out_planes, num_stages):
layers = [CRPBlock(in_planes, out_planes, num_stages, self.num_parallel)]
return nn.Sequential(*layers)
def _make_rcu(self, in_planes, out_planes, num_blocks, num_stages):
layers = [RCUBlock(in_planes, out_planes, num_blocks, num_stages, self.num_parallel)]
return nn.Sequential(*layers)
def _make_layer(self, block, planes, num_blocks, bn_threshold, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride=stride),
BatchNorm2dParallel(planes * block.expansion, self.num_parallel)
)
layers = []
layers.append(block(self.inplanes, planes, self.num_parallel, bn_threshold, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, num_blocks):
layers.append(block(self.inplanes, planes, self.num_parallel, bn_threshold))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
l1 = self.layer1(x)
l2 = self.layer2(l1)
l3 = self.layer3(l2)
l4 = self.layer4(l3)
l4 = self.dropout(l4)
l3 = self.dropout(l3)
x4 = self.p_ims1d2_outl1_dimred(l4)
x4 = self.adapt_stage1_b(x4)
x4 = self.relu(x4)
x4 = self.mflow_conv_g1_pool(x4)
x4 = self.mflow_conv_g1_b(x4)
x4 = self.mflow_conv_g1_b3_joint_varout_dimred(x4)
x4 = [nn.Upsample(size=l3[0].size()[2:], mode='bilinear', align_corners=True)(x4_) for x4_ in x4]
x3 = self.p_ims1d2_outl2_dimred(l3)
x3 = self.adapt_stage2_b(x3)
x3 = self.adapt_stage2_b2_joint_varout_dimred(x3)
x3 = [x3[l] + x4[l] for l in range(self.num_parallel)]
x3 = self.relu(x3)
x3 = self.mflow_conv_g2_pool(x3)
x3 = self.mflow_conv_g2_b(x3)
x3 = self.mflow_conv_g2_b3_joint_varout_dimred(x3)
x3 = [nn.Upsample(size=l2[0].size()[2:], mode='bilinear', align_corners=True)(x3_) for x3_ in x3]
x2 = self.p_ims1d2_outl3_dimred(l2)
x2 = self.adapt_stage3_b(x2)
x2 = self.adapt_stage3_b2_joint_varout_dimred(x2)
x2 = [x2[l] + x3[l] for l in range(self.num_parallel)]
x2 = self.relu(x2)
x2 = self.mflow_conv_g3_pool(x2)
x2 = self.mflow_conv_g3_b(x2)
x2 = self.mflow_conv_g3_b3_joint_varout_dimred(x2)
x2 = [nn.Upsample(size=l1[0].size()[2:], mode='bilinear', align_corners=True)(x2_) for x2_ in x2]
x1 = self.p_ims1d2_outl4_dimred(l1)
x1 = self.adapt_stage4_b(x1)
x1 = self.adapt_stage4_b2_joint_varout_dimred(x1)
x1 = [x1[l] + x2[l] for l in range(self.num_parallel)]
x1 = self.relu(x1)
x1 = self.mflow_conv_g4_pool(x1)
x1 = self.mflow_conv_g4_b(x1)
x1 = self.dropout(x1)
out = self.clf_conv(x1)
ens = 0
alpha_soft = F.softmax(self.alpha, dim = 0)
for l in range(self.num_parallel):
ens += alpha_soft[l] * out[l].detach()
# alpha_soft = F.softmax(self.alpha, dim=1)
# for l in range(self.num_parallel):
# print(out[l].shape, l)
# ens += alpha_soft[:, l].unsqueeze(1) * out[l].detach()
out.append(ens)
# return out, alpha_soft
return out
class RefineNet_Resnet18(nn.Module):
def __init__(self, block, layers, num_parallel, num_classes=21, bn_threshold=2e-2):
self.inplanes = 64
self.num_parallel = num_parallel
super(RefineNet_Resnet18, self).__init__()
self.dropout = ModuleParallel(nn.Dropout(p=0.5))
self.conv1 = ModuleParallel(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False))
self.bn1 = BatchNorm2dParallel(64, num_parallel)
self.relu = ModuleParallel(nn.ReLU(inplace=True))
self.maxpool = ModuleParallel(nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
self.layer1 = self._make_layer(block, 64, layers[0], bn_threshold)
self.layer2 = self._make_layer(block, 128, layers[1], bn_threshold, stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], bn_threshold, stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], bn_threshold, stride=2)
self.p_ims1d2_outl1_dimred = conv3x3(512, 256)
self.adapt_stage1_b = self._make_rcu(256, 256, 2, 2)
self.mflow_conv_g1_pool = self._make_crp(256, 256, 4)
self.mflow_conv_g1_b = self._make_rcu(256, 256, 3, 2)
self.mflow_conv_g1_b3_joint_varout_dimred = conv3x3(256, 64)
self.p_ims1d2_outl2_dimred = conv3x3(256, 64)
self.adapt_stage2_b = self._make_rcu(64, 64, 2, 2)
self.adapt_stage2_b2_joint_varout_dimred = conv3x3(64, 64)
self.mflow_conv_g2_pool = self._make_crp(64, 64, 4)
self.mflow_conv_g2_b = self._make_rcu(64, 64, 3, 2)
self.mflow_conv_g2_b3_joint_varout_dimred = conv3x3(64, 64)
self.p_ims1d2_outl3_dimred = conv3x3(128, 64)
self.adapt_stage3_b = self._make_rcu(64, 64, 2, 2)
self.adapt_stage3_b2_joint_varout_dimred = conv3x3(64, 64)
self.mflow_conv_g3_pool = self._make_crp(64, 64, 4)
self.mflow_conv_g3_b = self._make_rcu(64, 64, 3, 2)
self.mflow_conv_g3_b3_joint_varout_dimred = conv3x3(64, 64)
self.p_ims1d2_outl4_dimred = conv3x3(64, 64)
self.adapt_stage4_b = self._make_rcu(64, 64, 2, 2)
self.adapt_stage4_b2_joint_varout_dimred = conv3x3(64, 64)
self.mflow_conv_g4_pool = self._make_crp(64, 64, 4)
self.mflow_conv_g4_b = self._make_rcu(64, 64, 3, 2)
self.clf_conv = conv3x3(64, num_classes, bias=True)
self.alpha = nn.Parameter(torch.ones(num_parallel, requires_grad=True))
# self.alpha = nn.Parameter(torch.ones([1, num_parallel, 157, 157], requires_grad=True))
self.register_parameter('alpha', self.alpha)
def _make_crp(self, in_planes, out_planes, num_stages):
layers = [CRPBlock(in_planes, out_planes, num_stages, self.num_parallel)]
return nn.Sequential(*layers)
def _make_rcu(self, in_planes, out_planes, num_blocks, num_stages):
layers = [RCUBlock(in_planes, out_planes, num_blocks, num_stages, self.num_parallel)]
return nn.Sequential(*layers)
def _make_layer(self, block, planes, num_blocks, bn_threshold, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride=stride),
BatchNorm2dParallel(planes * block.expansion, self.num_parallel)
)
layers = []
layers.append(block(self.inplanes, planes, self.num_parallel, bn_threshold, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, num_blocks):
layers.append(block(self.inplanes, planes, self.num_parallel, bn_threshold))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
l1 = self.layer1(x)
l2 = self.layer2(l1)
l3 = self.layer3(l2)
l4 = self.layer4(l3)
l4 = self.dropout(l4)
l3 = self.dropout(l3)
x4 = self.p_ims1d2_outl1_dimred(l4)
x4 = self.adapt_stage1_b(x4)
x4 = self.relu(x4)
x4 = self.mflow_conv_g1_pool(x4)
x4 = self.mflow_conv_g1_b(x4)
x4 = self.mflow_conv_g1_b3_joint_varout_dimred(x4)
x4 = [nn.Upsample(size=l3[0].size()[2:], mode='bilinear', align_corners=True)(x4_) for x4_ in x4]
x3 = self.p_ims1d2_outl2_dimred(l3)
x3 = self.adapt_stage2_b(x3)
x3 = self.adapt_stage2_b2_joint_varout_dimred(x3)
x3 = [x3[l] + x4[l] for l in range(self.num_parallel)]
x3 = self.relu(x3)
x3 = self.mflow_conv_g2_pool(x3)
x3 = self.mflow_conv_g2_b(x3)
x3 = self.mflow_conv_g2_b3_joint_varout_dimred(x3)
x3 = [nn.Upsample(size=l2[0].size()[2:], mode='bilinear', align_corners=True)(x3_) for x3_ in x3]
x2 = self.p_ims1d2_outl3_dimred(l2)
x2 = self.adapt_stage3_b(x2)
x2 = self.adapt_stage3_b2_joint_varout_dimred(x2)
x2 = [x2[l] + x3[l] for l in range(self.num_parallel)]
x2 = self.relu(x2)
x2 = self.mflow_conv_g3_pool(x2)
x2 = self.mflow_conv_g3_b(x2)
x2 = self.mflow_conv_g3_b3_joint_varout_dimred(x2)
x2 = [nn.Upsample(size=l1[0].size()[2:], mode='bilinear', align_corners=True)(x2_) for x2_ in x2]
x1 = self.p_ims1d2_outl4_dimred(l1)
x1 = self.adapt_stage4_b(x1)
x1 = self.adapt_stage4_b2_joint_varout_dimred(x1)
x1 = [x1[l] + x2[l] for l in range(self.num_parallel)]
x1 = self.relu(x1)
x1 = self.mflow_conv_g4_pool(x1)
x1 = self.mflow_conv_g4_b(x1)
x1 = self.dropout(x1)
out = self.clf_conv(x1)
ens = 0
alpha_soft = F.softmax(self.alpha, dim = 0)
for l in range(self.num_parallel):
ens += alpha_soft[l] * out[l].detach()
# alpha_soft = F.softmax(self.alpha, dim=1)
# for l in range(self.num_parallel):
# print(out[l].shape, l)
# ens += alpha_soft[:, l].unsqueeze(1) * out[l].detach()
out.append(ens)
return out, alpha_soft
def refinenet(num_layers, num_classes, num_parallel, bn_threshold):
refinnetClass = RefineNet
if int(num_layers) == 18:
layers = [2, 2, 2, 2]
block = BasicBlock
refinnetClass = RefineNet_Resnet18
elif int(num_layers) == 50:
layers = [3, 4, 6, 3]
block = Bottleneck
elif int(num_layers) == 101:
layers = [3, 4, 23, 3]
block = Bottleneck
elif int(num_layers) == 152:
layers = [3, 8, 36, 3]
block = Bottleneck
else:
print('invalid num_layers')
model = refinnetClass(block, layers, num_parallel, num_classes, bn_threshold)
return model
def maybe_download(model_name, model_url, model_dir=None, map_location=None):
if model_dir is None:
torch_home = os.path.expanduser(os.getenv('TORCH_HOME', '~/.torch'))
model_dir = os.getenv('TORCH_MODEL_ZOO', os.path.join(torch_home, 'models'))
if not os.path.exists(model_dir):
os.makedirs(model_dir)
filename = '{}.pth.tar'.format(model_name)
cached_file = os.path.join(model_dir, filename)
if not os.path.exists(cached_file):
# url = model_url
# sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file))
# moves.urllib.request.urlretrieve(url, cached_file)
raise Exception('cached file not found, maybe_download failed')
return torch.load(cached_file, map_location=map_location)
def model_init(model, num_layers, num_parallel, imagenet=False):
if imagenet:
key = str(num_layers) + '_imagenet'
url = models_urls[key]
state_dict = maybe_download(key, url)
model_dict = expand_model_dict(model.state_dict(), state_dict, num_parallel)
model.load_state_dict(model_dict, strict=True)
return model
def expand_model_dict(model_dict, state_dict, num_parallel):
model_dict_keys = model_dict.keys()
state_dict_keys = state_dict.keys()
for model_dict_key in model_dict_keys:
model_dict_key_re = model_dict_key.replace('module.', '')
if model_dict_key_re in state_dict_keys:
model_dict[model_dict_key] = state_dict[model_dict_key_re]
for i in range(num_parallel):
bn = '.bn_%d' % i
replace = True if bn in model_dict_key_re else False
model_dict_key_re = model_dict_key_re.replace(bn, '')
if replace and model_dict_key_re in state_dict_keys:
model_dict[model_dict_key] = state_dict[model_dict_key_re]
return model_dict
def L1_penalty(var, device):
return torch.abs(var).sum().to(device) |