2019-08-23 13:42:47 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
2020-11-11 18:25:49 +00:00
|
|
|
|
2020-12-18 16:24:31 +00:00
|
|
|
from models.arch_util import ConvBnLelu, ConvGnLelu, ExpansionBlock, ConvGnSilu, ResidualBlockGN
|
2020-07-06 17:15:52 +00:00
|
|
|
import torch.nn.functional as F
|
2021-01-01 22:56:09 +00:00
|
|
|
|
|
|
|
from trainer.networks import register_model
|
|
|
|
from utils.util import checkpoint, opt_get
|
2019-08-23 13:42:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Discriminator_VGG_128(nn.Module):
|
2020-04-21 22:32:59 +00:00
|
|
|
# input_img_factor = multiplier to support images over 128x128. Only certain factors are supported.
|
2020-06-23 15:40:33 +00:00
|
|
|
def __init__(self, in_nc, nf, input_img_factor=1, extra_conv=False):
|
2019-08-23 13:42:47 +00:00
|
|
|
super(Discriminator_VGG_128, self).__init__()
|
|
|
|
# [64, 128, 128]
|
|
|
|
self.conv0_0 = nn.Conv2d(in_nc, nf, 3, 1, 1, bias=True)
|
|
|
|
self.conv0_1 = nn.Conv2d(nf, nf, 4, 2, 1, bias=False)
|
|
|
|
self.bn0_1 = nn.BatchNorm2d(nf, affine=True)
|
|
|
|
# [64, 64, 64]
|
2020-05-12 16:08:12 +00:00
|
|
|
self.conv1_0 = nn.Conv2d(nf, nf * 2, 3, 1, 1, bias=False)
|
2019-08-23 13:42:47 +00:00
|
|
|
self.bn1_0 = nn.BatchNorm2d(nf * 2, affine=True)
|
|
|
|
self.conv1_1 = nn.Conv2d(nf * 2, nf * 2, 4, 2, 1, bias=False)
|
|
|
|
self.bn1_1 = nn.BatchNorm2d(nf * 2, affine=True)
|
|
|
|
# [128, 32, 32]
|
2020-05-12 16:08:12 +00:00
|
|
|
self.conv2_0 = nn.Conv2d(nf * 2, nf * 4, 3, 1, 1, bias=False)
|
2019-08-23 13:42:47 +00:00
|
|
|
self.bn2_0 = nn.BatchNorm2d(nf * 4, affine=True)
|
|
|
|
self.conv2_1 = nn.Conv2d(nf * 4, nf * 4, 4, 2, 1, bias=False)
|
|
|
|
self.bn2_1 = nn.BatchNorm2d(nf * 4, affine=True)
|
|
|
|
# [256, 16, 16]
|
|
|
|
self.conv3_0 = nn.Conv2d(nf * 4, nf * 8, 3, 1, 1, bias=False)
|
|
|
|
self.bn3_0 = nn.BatchNorm2d(nf * 8, affine=True)
|
|
|
|
self.conv3_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False)
|
|
|
|
self.bn3_1 = nn.BatchNorm2d(nf * 8, affine=True)
|
|
|
|
# [512, 8, 8]
|
|
|
|
self.conv4_0 = nn.Conv2d(nf * 8, nf * 8, 3, 1, 1, bias=False)
|
|
|
|
self.bn4_0 = nn.BatchNorm2d(nf * 8, affine=True)
|
|
|
|
self.conv4_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False)
|
|
|
|
self.bn4_1 = nn.BatchNorm2d(nf * 8, affine=True)
|
2020-06-23 15:40:33 +00:00
|
|
|
final_nf = nf * 8
|
2019-08-23 13:42:47 +00:00
|
|
|
|
2020-06-23 15:40:33 +00:00
|
|
|
self.extra_conv = extra_conv
|
|
|
|
if self.extra_conv:
|
|
|
|
self.conv5_0 = nn.Conv2d(nf * 8, nf * 16, 3, 1, 1, bias=False)
|
|
|
|
self.bn5_0 = nn.BatchNorm2d(nf * 16, affine=True)
|
|
|
|
self.conv5_1 = nn.Conv2d(nf * 16, nf * 16, 4, 2, 1, bias=False)
|
|
|
|
self.bn5_1 = nn.BatchNorm2d(nf * 16, affine=True)
|
|
|
|
input_img_factor = input_img_factor // 2
|
|
|
|
final_nf = nf * 16
|
|
|
|
|
|
|
|
self.linear1 = nn.Linear(final_nf * 4 * input_img_factor * 4 * input_img_factor, 100)
|
2019-08-23 13:42:47 +00:00
|
|
|
self.linear2 = nn.Linear(100, 1)
|
|
|
|
|
|
|
|
# activation function
|
|
|
|
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
|
|
|
|
2020-04-30 17:45:07 +00:00
|
|
|
def forward(self, x):
|
2019-08-23 13:42:47 +00:00
|
|
|
fea = self.lrelu(self.conv0_0(x))
|
|
|
|
fea = self.lrelu(self.bn0_1(self.conv0_1(fea)))
|
|
|
|
|
2020-05-12 16:08:12 +00:00
|
|
|
#fea = torch.cat([fea, skip_med], dim=1)
|
2019-08-23 13:42:47 +00:00
|
|
|
fea = self.lrelu(self.bn1_0(self.conv1_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn1_1(self.conv1_1(fea)))
|
|
|
|
|
2020-05-12 16:08:12 +00:00
|
|
|
#fea = torch.cat([fea, skip_lo], dim=1)
|
2019-08-23 13:42:47 +00:00
|
|
|
fea = self.lrelu(self.bn2_0(self.conv2_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn2_1(self.conv2_1(fea)))
|
|
|
|
|
|
|
|
fea = self.lrelu(self.bn3_0(self.conv3_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn3_1(self.conv3_1(fea)))
|
|
|
|
|
|
|
|
fea = self.lrelu(self.bn4_0(self.conv4_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn4_1(self.conv4_1(fea)))
|
|
|
|
|
2020-06-23 15:40:33 +00:00
|
|
|
if self.extra_conv:
|
|
|
|
fea = self.lrelu(self.bn5_0(self.conv5_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn5_1(self.conv5_1(fea)))
|
|
|
|
|
2020-06-16 20:27:16 +00:00
|
|
|
fea = fea.contiguous().view(fea.size(0), -1)
|
2019-08-23 13:42:47 +00:00
|
|
|
fea = self.lrelu(self.linear1(fea))
|
|
|
|
out = self.linear2(fea)
|
|
|
|
return out
|
|
|
|
|
2020-11-11 18:25:49 +00:00
|
|
|
|
2021-01-01 22:56:09 +00:00
|
|
|
@register_model
|
|
|
|
def register_discriminator_vgg_128(opt_net, opt):
|
|
|
|
return Discriminator_VGG_128(in_nc=opt_net['in_nc'], nf=opt_net['nf'], input_img_factor=opt_net['image_size'] / 128,
|
|
|
|
extra_conv=opt_net['extra_conv'])
|
|
|
|
|
|
|
|
|
2020-08-02 18:55:08 +00:00
|
|
|
class Discriminator_VGG_128_GN(nn.Module):
|
|
|
|
# input_img_factor = multiplier to support images over 128x128. Only certain factors are supported.
|
2020-11-29 22:39:50 +00:00
|
|
|
def __init__(self, in_nc, nf, input_img_factor=1, do_checkpointing=False, extra_conv=False):
|
2020-08-02 18:55:08 +00:00
|
|
|
super(Discriminator_VGG_128_GN, self).__init__()
|
2020-10-18 18:10:24 +00:00
|
|
|
self.do_checkpointing = do_checkpointing
|
|
|
|
|
2020-08-02 18:55:08 +00:00
|
|
|
# [64, 128, 128]
|
|
|
|
self.conv0_0 = nn.Conv2d(in_nc, nf, 3, 1, 1, bias=True)
|
|
|
|
self.conv0_1 = nn.Conv2d(nf, nf, 4, 2, 1, bias=False)
|
|
|
|
self.bn0_1 = nn.GroupNorm(8, nf, affine=True)
|
|
|
|
# [64, 64, 64]
|
|
|
|
self.conv1_0 = nn.Conv2d(nf, nf * 2, 3, 1, 1, bias=False)
|
|
|
|
self.bn1_0 = nn.GroupNorm(8, nf * 2, affine=True)
|
|
|
|
self.conv1_1 = nn.Conv2d(nf * 2, nf * 2, 4, 2, 1, bias=False)
|
|
|
|
self.bn1_1 = nn.GroupNorm(8, nf * 2, affine=True)
|
|
|
|
# [128, 32, 32]
|
|
|
|
self.conv2_0 = nn.Conv2d(nf * 2, nf * 4, 3, 1, 1, bias=False)
|
|
|
|
self.bn2_0 = nn.GroupNorm(8, nf * 4, affine=True)
|
|
|
|
self.conv2_1 = nn.Conv2d(nf * 4, nf * 4, 4, 2, 1, bias=False)
|
|
|
|
self.bn2_1 = nn.GroupNorm(8, nf * 4, affine=True)
|
|
|
|
# [256, 16, 16]
|
|
|
|
self.conv3_0 = nn.Conv2d(nf * 4, nf * 8, 3, 1, 1, bias=False)
|
|
|
|
self.bn3_0 = nn.GroupNorm(8, nf * 8, affine=True)
|
|
|
|
self.conv3_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False)
|
|
|
|
self.bn3_1 = nn.GroupNorm(8, nf * 8, affine=True)
|
|
|
|
# [512, 8, 8]
|
|
|
|
self.conv4_0 = nn.Conv2d(nf * 8, nf * 8, 3, 1, 1, bias=False)
|
|
|
|
self.bn4_0 = nn.GroupNorm(8, nf * 8, affine=True)
|
|
|
|
self.conv4_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False)
|
|
|
|
self.bn4_1 = nn.GroupNorm(8, nf * 8, affine=True)
|
2020-11-29 22:39:50 +00:00
|
|
|
|
|
|
|
self.extra_conv = extra_conv
|
|
|
|
if extra_conv:
|
|
|
|
self.conv5_0 = nn.Conv2d(nf * 8, nf * 8, 3, 1, 1, bias=False)
|
|
|
|
self.bn5_0 = nn.GroupNorm(8, nf * 8, affine=True)
|
|
|
|
self.conv5_1 = nn.Conv2d(nf * 8, nf * 8, 4, 2, 1, bias=False)
|
|
|
|
self.bn5_1 = nn.GroupNorm(8, nf * 8, affine=True)
|
2020-12-02 00:45:56 +00:00
|
|
|
input_img_factor = input_img_factor / 2
|
2020-08-02 18:55:08 +00:00
|
|
|
final_nf = nf * 8
|
|
|
|
|
|
|
|
# activation function
|
|
|
|
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
|
|
|
|
2020-10-18 15:57:47 +00:00
|
|
|
self.linear1 = nn.Linear(int(final_nf * 4 * input_img_factor * 4 * input_img_factor), 100)
|
|
|
|
self.linear2 = nn.Linear(100, 1)
|
|
|
|
|
2020-10-18 18:10:24 +00:00
|
|
|
def compute_body(self, x):
|
2020-08-02 18:55:08 +00:00
|
|
|
fea = self.lrelu(self.conv0_0(x))
|
|
|
|
fea = self.lrelu(self.bn0_1(self.conv0_1(fea)))
|
|
|
|
|
|
|
|
#fea = torch.cat([fea, skip_med], dim=1)
|
|
|
|
fea = self.lrelu(self.bn1_0(self.conv1_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn1_1(self.conv1_1(fea)))
|
|
|
|
|
|
|
|
#fea = torch.cat([fea, skip_lo], dim=1)
|
|
|
|
fea = self.lrelu(self.bn2_0(self.conv2_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn2_1(self.conv2_1(fea)))
|
|
|
|
|
|
|
|
fea = self.lrelu(self.bn3_0(self.conv3_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn3_1(self.conv3_1(fea)))
|
|
|
|
|
|
|
|
fea = self.lrelu(self.bn4_0(self.conv4_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn4_1(self.conv4_1(fea)))
|
2020-11-29 22:39:50 +00:00
|
|
|
|
|
|
|
if self.extra_conv:
|
|
|
|
fea = self.lrelu(self.bn5_0(self.conv5_0(fea)))
|
|
|
|
fea = self.lrelu(self.bn5_1(self.conv5_1(fea)))
|
2020-10-18 18:10:24 +00:00
|
|
|
return fea
|
2020-10-18 15:57:47 +00:00
|
|
|
|
|
|
|
def forward(self, x):
|
2020-10-18 18:10:24 +00:00
|
|
|
if self.do_checkpointing:
|
|
|
|
fea = checkpoint(self.compute_body, x)
|
|
|
|
else:
|
|
|
|
fea = self.compute_body(x)
|
2020-08-02 18:55:08 +00:00
|
|
|
fea = fea.contiguous().view(fea.size(0), -1)
|
|
|
|
fea = self.lrelu(self.linear1(fea))
|
|
|
|
out = self.linear2(fea)
|
|
|
|
return out
|
|
|
|
|
2020-08-06 14:56:21 +00:00
|
|
|
|
2021-01-01 22:56:09 +00:00
|
|
|
@register_model
|
|
|
|
def register_discriminator_vgg_128(opt_net, opt):
|
|
|
|
return Discriminator_VGG_128_GN(in_nc=opt_net['in_nc'], nf=opt_net['nf'], input_img_factor=opt_net['image_size'],
|
|
|
|
extra_conv=opt_get(opt_net, ['extra_conv'], False),
|
|
|
|
do_checkpointing=opt_get(opt_net, ['do_checkpointing'], False))
|