Пример #1
0
 def _init_layers(self,n,f):
     # 上分支
     setattr(self,'res'+str(n)+'_1',Residual(f,f))
     # 下分支
     setattr(self,'pool'+str(n)+'_1',nn.MaxPool2d(2,2))
     setattr(self,'res'+str(n)+'_2',Residual(f,f))
     if n > 1:
         self._init_layers(n-1,f)
     else:
         self.res_center = Residual(f,f)
     setattr(self,'res'+str(n)+'_3',Residual(f,f))
     setattr(self,'unsample'+str(n),Upsample(scale_factor=2))
Пример #2
0
    def _initialise_networks(self):
        self.generator = Generator(final_size=self.final_size)
        self.generator.generate_network()
        self.g_optimizer = Adam(self.generator.parameters(), lr=0.001, betas=(0, 0.99))

        self.discriminator = Discriminator(final_size=self.final_size)
        self.discriminator.generate_network()
        self.d_optimizer = Adam(self.discriminator.parameters(), lr=0.001, betas=(0, 0.99))

        self.num_channels = min(self.generator.num_channels,
                                self.generator.max_channels)
        self.upsample = [Upsample(scale_factor=2**i)
                for i in reversed(range(self.generator.num_blocks))]
Пример #3
0
 def __init__(self,
              in_channels,
              out_channels,
              scale_factor=2,
              mode='bilinear'):
     super(UpSampleBlock, self).__init__()
     self.model = nn.Sequential(
         Upsample(scale_factor=scale_factor, mode=mode),
         Conv2d(in_channels,
                out_channels,
                3,
                1,
                padding=1,
                padding_mode='reflect'))
Пример #4
0
    def forward(self, x):
        original_size = tuple(x.size())[2:]

        conv1 = self.conv_block_1(x)
        pool1 = AvgPool2d(2)(conv1)
        conv2 = self.conv_block_2(pool1)
        unpool1 = Upsample(size=original_size, mode='bilinear')(conv2)
        conv3 = self.conv_block_3(unpool1)
        conv4 = self.conv_block_4(conv3)
        conv5 = self.conv5(conv4)

        output = Sigmoid()(conv5)

        return output
Пример #5
0
    def __init__(self, noise_size=512):
        """
        constructor of the class
        :param noise_size: dimensionality of the input prior Z
        """

        super(Generator, self).__init__()  # super constructor call

        # define the state of the object
        self.z_size = noise_size

        # define all the required modules for the generator
        from torch.nn import ConvTranspose2d, Conv2d, Upsample, LeakyReLU
        from torch.nn.functional import local_response_norm

        ch = self.z_size

        # Layer 1:
        self.conv_1_1 = ConvTranspose2d(ch, ch, (4, 4), bias=False)
        self.conv_1_2 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)

        # Layer 2:
        self.conv_2_1 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)
        self.conv_2_2 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)

        # Layer 3:
        self.conv_3_1 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)
        self.conv_3_2 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)

        # Layer 4:
        self.conv_4_1 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)
        self.conv_4_2 = Conv2d(ch, ch, (3, 3), padding=1, bias=False)

        # Layer 5:
        self.conv_5_1 = Conv2d(ch, ch // 2, (3, 3), padding=1, bias=False)
        self.conv_5_2 = Conv2d(ch // 2, ch // 2, (3, 3), padding=1, bias=False)

        # Upsampler
        self.upsample = Upsample(scale_factor=2)

        # To RGB converter operation:
        self.ToRGB = Conv2d(ch // 2, 3, (1, 1), bias=False)

        # Pixelwise feature vector normalization operation
        self.pixNorm = lambda x: local_response_norm(
            x, 2 * x.shape[1], alpha=2, beta=0.5, k=1e-8)

        # Leaky Relu to be applied as activation
        self.lrelu = LeakyReLU(negative_slope=0.2)
Пример #6
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride,
                 upsample=None):
        super(UpsampleConvLayer, self).__init__()
        self.upsample = upsample
        if upsample:
            self.upsample_layer = Upsample(scale_factor=upsample)

        pad = [int(np.floor(x / 2)) for x in kernel_size]
        self.circular_pad = CirularPad3d(pad)

        self.conv3d = Conv3d(in_channels, out_channels, kernel_size, stride)
def infer_depth(image_tensor, model, upsample=True):
    '''Image_tensor should be of shape C * H * W (and between 0 and 1) and H,W should be divisible by 32 perfectly.'''
    device = torch.device(
        'cuda') if torch.cuda.is_available() else torch.device('cpu')
    model = model.to(device)
    model.eval()

    image = normalize_batch(
        torch.autograd.Variable(image_tensor.unsqueeze(0).to(device)))

    with torch.no_grad():
        depth = model(image)
    if upsample:
        depth = Upsample(scale_factor=2, mode='bilinear',
                         align_corners=True)(depth)
    return depth
Пример #8
0
        def __init__(self, in_channels, out_channels):
            from torch.nn import Conv2d, LeakyReLU, Upsample
            from torch.nn.functional import local_response_norm

            super().__init__()

            self.upsample = Upsample(scale_factor=2)
            self.conv_1 = Conv2d(in_channels, out_channels, (3, 3), padding=1)
            self.conv_2 = Conv2d(out_channels, out_channels, (3, 3), padding=1)

            # Pixelwise feature vector normalization operation
            self.pixNorm = lambda x: local_response_norm(
                x, 2 * x.shape[1], alpha=2, beta=0.5, k=1e-8)

            # leaky_relu:
            self.lrelu = LeakyReLU(0.2)
Пример #9
0
 def __init__(self, conf):
     super(Yolo_model, self).__init__()
     self.num_class = conf.class_num
     self.res50_pyramid = res50_pyramid()
     self.head = Yolo_head(conf)
     self.pyramid1, self.classifier1 = make_pyramids_classifier(
         2048, 1024, self.num_class)
     self.squeeze1 = Squeeze_block(1024, 512)
     self.pyramid2, self.classifier2 = make_pyramids_classifier(
         1536, 512, self.num_class)
     self.squeeze2 = Squeeze_block(512, 256)
     self.pyramid3, self.classifier3 = make_pyramids_classifier(
         768, 256, self.num_class)
     self.upsample = Upsample(scale_factor=2,
                              mode='bilinear',
                              align_corners=False)
Пример #10
0
    def __init__(self, depth=7, latent_size=512):
        """
        constructor for the Generator class
        :param depth: required depth of the Network
        :param latent_size: size of the latent manifold
        """
        from torch.nn import Conv2d, ModuleList, Upsample

        super(Generator, self).__init__()

        assert latent_size != 0 and ((latent_size & (latent_size - 1)) == 0), \
            "latent size not a power of 2"
        if depth >= 4:
            assert latent_size >= np.power(
                2, depth - 4), "latent size will diminish to zero"

        # state of the generator:
        self.depth = depth
        self.latent_size = latent_size

        # register the modules required for the GAN
        self.initial_block = self.InitialBlock(self.latent_size)

        # create a module list of the other required general convolution blocks
        self.layers = ModuleList([])  # initialize to empty list

        # create the ToRGB layers for various outputs:
        self.toRGB = lambda in_channels: Conv2d(
            in_channels, 3, (1, 1), bias=False)
        self.rgb_converters = ModuleList([self.toRGB(self.latent_size)])

        # create the remaining layers
        for i in range(self.depth - 1):
            if i <= 2:
                layer = self.GeneralConvBlock(self.latent_size,
                                              self.latent_size)
                rgb = self.toRGB(self.latent_size)
            else:
                layer = self.GeneralConvBlock(
                    int(self.latent_size // np.power(2, i - 3)),
                    int(self.latent_size // np.power(2, i - 2)))
                rgb = self.toRGB(int(self.latent_size // np.power(2, i - 2)))
            self.layers.append(layer)
            self.rgb_converters.append(rgb)

        # register the temporary upsampler
        self.temporaryUpsampler = Upsample(scale_factor=2)
Пример #11
0
    def __init__(self, input_nc=1, output_nc=1, depth=32, norm_type="batch", active_type="LeakyReLU"):
        super(Unet_G, self).__init__()
        self.norm = getNormLayer(norm_type)
        self.active = getActiveLayer(active_type)
        self.depth = depth
        self.d0 = Sequential(Conv2d(input_nc, depth, 7, 1, 3), self.active)  # 256, 256, 16
        self.d1 = Sequential(Conv2d(depth * 1, depth * 2, 5, 1, 2), self.norm(depth * 2, affine=True), self.active,
                             MaxPool2d(2, 2))  # 128, 128, 32
        self.d2 = Sequential(Conv2d(depth * 2, depth * 2, 3, 1, 1), self.norm(depth * 2, affine=True), self.active,
                             MaxPool2d(2, 2))  # 64, 64, 32
        self.d3 = Sequential(Conv2d(depth * 2, depth * 4, 3, 1, 1), self.norm(depth * 4, affine=True), self.active,
                             MaxPool2d(2, 2))  # 32, 32, 64
        self.d4 = Sequential(Conv2d(depth * 4, depth * 4, 3, 1, 1), self.norm(depth * 4, affine=True), self.active,
                             MaxPool2d(2, 2))  # 16, 16, 64
        self.d5 = Sequential(Conv2d(depth * 4, depth * 8, 3, 1, 1), self.norm(depth * 8, affine=True), self.active,
                             MaxPool2d(2, 2))  # 8, 8, 128
        self.d6 = Sequential(Conv2d(depth * 8, depth * 8, 3, 1, 1), self.norm(depth * 8, affine=True), self.active,
                             AvgPool2d(2, 2))  # 4, 4, 128
        self.d7 = Sequential(Conv2d(depth * 8, depth * 16, 3, 1, 1), self.norm(depth * 16, affine=True), self.active,
                             AvgPool2d(2, 2))  # 2, 2, 256
        self.d8 = Sequential(Conv2d(depth * 16, depth * 16, 3, 1, 1), self.norm(depth * 16, affine=True),
                             self.active)  # 2, 2, 256

        self.u0 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1), self.norm(depth * 16, affine=True), self.active)
        self.u1 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1), self.norm(depth * 8, affine=True), self.active)
        self.u2 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1), self.norm(depth * 4, affine=True), self.active)
        self.u3 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1), self.norm(depth * 4, affine=True), self.active)
        self.u4 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1), self.norm(depth * 2, affine=True), self.active)
        self.u5 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1), self.norm(depth * 2, affine=True), self.active)
        self.u6 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 1, depth * 1, 5, 1, 2), self.norm(depth * 1, affine=True), self.active)

        self.u7 = Sequential(Conv2d(depth * 1, output_nc, 7, 1, 3), Tanh())

        self.leaky_relu = self.active
        self.conv_32_8 = Conv2d(depth * 16 * 2, depth * 8, 3, 1, 1)
        self.conv_16_8 = Conv2d(depth * 8 * 2, depth * 8, 3, 1, 1)
        self.conv_16_4 = Conv2d(depth * 8 * 2, depth * 4, 3, 1, 1)
        self.conv_8_4 = Conv2d(depth * 4 * 2, depth * 4, 3, 1, 1)
        self.conv_8_2 = Conv2d(depth * 4 * 2, depth * 2, 3, 1, 1)
        self.conv_4_2 = Conv2d(depth * 2 * 2, depth * 2, 3, 1, 1)
        self.conv_4_1 = Conv2d(depth * 2 * 2, depth * 1, 3, 1, 1)
        self.conv_2_1 = Conv2d(depth * 1 * 2, depth * 1, 3, 1, 1)
Пример #12
0
def upsample(inp, size):
    '''
    Args:
        inp: (Tensor) input
        size: (Tuple [int, int]) height x width
    '''
    #backend = type2backend[inp.type()]
    #f = getattr(backend, 'SpatialUpSamplingBilinear_updateOutput')
    #upsample_inp = inp.new()
    #f(backend.library_state, inp, upsample_inp, size[0], size[1])
    m = Upsample(scale_factor=size, mode='trilinear')
    #print np.min(inp.squeeze().cpu().numpy())
    upsample_inp = m(inp)

    attr = upsample_inp

    return attr
Пример #13
0
    def __init__(self, input_size: int, output_filters: int, dropout=0.0):
        super(UNetUp, self).__init__()

        self.model = Sequential(
            Upsample(scale_factor=(2, 1)),
            ZeroPad2d((0, 0, 1, 0)),
            Conv2d(input_size,
                   output_filters,
                   kernel_size=(4, 1),
                   stride=1,
                   padding=(1, 0),
                   bias=False),
            ReLU(inplace=True),
            BatchNorm2d(output_filters, momentum=0.8),
        )

        if dropout:
            self.model.add_module("Dropout", Dropout(dropout))
    def __init__(self, x_shape, y_shape, num_filters, layer_i):
        super(DecodeSubBlock, self).__init__()
        self.x_shape = x_shape
        self.y_shape = y_shape
        self.layer_i = layer_i
        self.up = Upsample(scale_factor=2.)
        [N_in, C_y, H_y, W_y] = y_shape

        pad_type = convArgs['padding']
        convArgs_ = convArgs.copy()
        convArgs_['padding'] = (1, 1) if pad_type is 'same' else (0, 0)
        self.conv = ModelConv2D(in_channels=x_shape[CHANNEL_AXIS], out_channels=num_filters,
                      kernel_size=(3, 3), stride=(1, 1), **convArgs_)
        if opts.use_group_norm:
            self.bn = GroupNormBlock(num_channels=self.conv.out_channels*n_divs + C_y)
        else:
            self.bn = ModelBN(num_features=self.conv.out_channels*n_divs + C_y, **bnArgs)
        self.act = Activation(**actArgs)
        self.output_shape = [N_in, C_y+num_filters, H_y, W_y]
Пример #15
0
    def __init__(self, n, init_scale=0.0, p=1):
        super().__init__()

        # Number related to basis size
        self.n = n
        self.N = 2 * n**2 + n

        # Upsampler required in forward pass
        self.upsample = Upsample(scale_factor=n, mode='nearest')

        # Create weight vector
        self.weights = Parameter(init_scale *
                                 torch.randn(2 * self.N, requires_grad=True))

        # Vectors used function evaluation and projection.
        self.nvec = torch.arange(1, n + 1, dtype=torch.float)
        self.L = self.lipschitz_vector()
        self.p = p
        self.project()
Пример #16
0
        def __init__(self, in_channels, out_channels):
            """
            constructor for the class
            :param in_channels: number of input channels to the block
            :param out_channels: number of output channels required
            """
            from torch.nn import Conv2d, LeakyReLU, Upsample
            from torch.nn.functional import local_response_norm

            super().__init__()

            self.upsample = Upsample(scale_factor=2)
            self.conv_1 = Conv2d(in_channels, out_channels, (3, 3), padding=1)
            self.conv_2 = Conv2d(out_channels, out_channels, (3, 3), padding=1)

            # Pixelwise feature vector normalization operation
            self.pixNorm = lambda x: local_response_norm(
                x, 2 * x.shape[1], alpha=2, beta=0.5, k=1e-8)

            # leaky_relu:
            self.lrelu = LeakyReLU(0.2)
Пример #17
0
 def __init__(self, feature_channels=64, layer_num=4):
     """
     :param feature_channels: 在HGNet里面传播的feature map的通道数
     :param layer_num:  每一个HGNet的一块的downsample的次数
     """
     super(HGNetwork, self).__init__()
     self.feature_channels = feature_channels
     self.layer_num = layer_num
     # down sample part
     for i in range(layer_num):
         setattr(self, 'down_res_{}'.format(i), ResidualBlock(feature_channels, feature_channels))
         setattr(self, 'down_pool_{}'.format(i), MaxPool2d(kernel_size=(2, 2)))
     # parallel part
     for i in range(3):
         setattr(self, 'paral_res_{}'.format(i), ResidualBlock(feature_channels, feature_channels))
     # up sample part
     for i in range(layer_num):
         setattr(self, 'up_res_{}'.format(i), ResidualBlock(feature_channels, feature_channels))
         setattr(self, 'up_sample_{}'.format(i), Upsample(scale_factor=2))
     # short cut part
     self.shortcut_conv = Conv2d(in_channels=feature_channels, out_channels=feature_channels, kernel_size=(1, 1))
Пример #18
0
    def __init__(self, in_channels, out_channels, use_eql):
        """
        constructor for the class
        :param in_channels: number of input channels to the block
        :param out_channels: number of output channels required
        :param use_eql: whether to use equalized learning rate
        """
        from torch.nn import LeakyReLU, Upsample
        from torch.nn.functional import local_response_norm

        super(GenGeneralConvBlock, self).__init__()

        self.upsample = Upsample(scale_factor=2)

        if use_eql:
            self.conv_1 = _equalized_conv2d(in_channels,
                                            out_channels, (3, 3),
                                            pad=1,
                                            bias=True)
            self.conv_2 = _equalized_conv2d(out_channels,
                                            out_channels, (3, 3),
                                            pad=1,
                                            bias=True)
        else:
            from torch.nn import Conv2d
            self.conv_1 = Conv2d(in_channels,
                                 out_channels, (3, 3),
                                 padding=1,
                                 bias=True)
            self.conv_2 = Conv2d(out_channels,
                                 out_channels, (3, 3),
                                 padding=1,
                                 bias=True)

        # Pixelwise feature vector normalization operation
        self.pixNorm = lambda x: local_response_norm(
            x, 2 * x.shape[1], alpha=2, beta=0.5, k=1e-8)

        # leaky_relu:
        self.lrelu = LeakyReLU(0.2)
Пример #19
0
    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        kernel_size: tuple,
        stride: int,
        padding: int,
        up_scale_factor: int,
        dropout_p: float,
    ):
        """Initializes a Left Block.

        Parameters
        ----------
        in_channels : int
            Number of input channels to a convolutional block.
        out_channels : int
            Number of output channels to a convolutional block.
        kernel_size : tuple
            Size of a kernel in a convolutional layer.
        stride : int
            Stride used in a convolutional layer.
        padding : int
            Number of padded pixels in a convolutional layer.
        up_scale_factor : int
            Scale factor of an upsampling.
        dropout_p : float
            Probability of an element to be zeroed.
        """
        super(RightBlock, self).__init__()

        self.up_layer = Sequential(
            Upsample(scale_factor=up_scale_factor,
                     mode="bilinear",
                     align_corners=True),
            Conv2d(in_channels, out_channels, kernel_size, stride, padding),
        )
        self.conv_layers = ConvBlock(in_channels, out_channels, kernel_size,
                                     stride, padding, dropout_p)
Пример #20
0
    def __init__(self, inc, outc):
        super(FinalStem, self).__init__()

        filters = outc * 2

        self.conv1 = Conv3d(inc,
                            filters,
                            kernel_size=3,
                            stride=1,
                            padding=1,
                            bias=False)
        self.norm1 = InstanceNorm3d(filters)

        self.upsample = Upsample(scale_factor=2, mode="trilinear")

        self.conv2 = Conv3d(filters,
                            outc,
                            kernel_size=3,
                            stride=1,
                            padding=1,
                            bias=False)
        self.norm2 = InstanceNorm3d(outc)
Пример #21
0
    def __init__(self, in_size=20, bias=True, use_gru=False):
        super(Generator, self).__init__()
        rnn_class = GRU if use_gru else LSTM

        self.fc1 = Linear(in_size, 50, bias=bias)
        self.rnn1 = rnn_class(input_size=1,
                              hidden_size=64,
                              batch_first=True,
                              bidirectional=True,
                              bias=bias,
                              dropouti=0.0,
                              dropoutw=0.2,
                              dropouto=0.2)
        self.up = Upsample(scale_factor=2, mode='nearest')
        self.rnn2 = rnn_class(input_size=2 * 64,
                              hidden_size=64,
                              batch_first=True,
                              bidirectional=True,
                              bias=bias,
                              dropouti=0.1,
                              dropoutw=0.2,
                              dropouto=0.2)
        self.fc2 = SequenceWise(Linear(2 * 64, 1, bias=bias))
Пример #22
0
 def __init__(self):
     super(G_NET, self).__init__()
     self.gf_dim = cfg.GAN.GF_DIM
     self.define_module()
     self.upsampling = Upsample(scale_factor=2, mode='bilinear')
     self.scale_fimg = nn.UpsamplingBilinear2d(size=[126, 126])
Пример #23
0
    def __init__(
            self, in_channels,
            out_channels):  # 默认 [1024, 512, 256] 和 [(cls_num + 1 + 4)*3] *3
        super().__init__()

        in_channel1, in_channel2, in_channel3 = in_channels
        out_channel1, out_channel2, out_channel3 = out_channels

        # yolo_layer1
        self.conv1_0 = nn.Sequential(
            Convolutional(in_channel1, 512, 1, stride=1, padding=0),  # Y_74
            Convolutional(512, 1024, 3, stride=1, padding=1),
            Convolutional(1024, 512, 1, stride=1, padding=0),
            Convolutional(512, 1024, 3, stride=1, padding=1),
            Convolutional(1024, 512, 1, stride=1, padding=0),
        )  # 输出Y_79,即input1 of yolo_layer1
        # 之后上采样然后concat,在yolo_layer2中也要被使用,因此需要单独提出来
        self.conv1_1 = nn.Sequential(
            Convolutional(512, 1024, 3, stride=1, padding=1),
            nn.Conv2d(
                1024, out_channel1, 1, stride=1, padding=0
            )  # clw note: 这里out_channels = (cls_num + 4 + 1) * 3, cls_num就是类别数
            #           在 YOLO 中,预测是通过卷积层完成的,它是一个全卷积神经网络,
            #           其核心尺寸为:1×1×(B×(5+C)),其中C就是上面的cls_num,
            #           B是每层的anchor个数,这里是9 / 3 = 3
        )
        # 输出Y_81,即output1 of yolo_layer1

        # yolo_layer2
        self.conv2_0 = nn.Sequential(
            Convolutional(in_channel2, 256, 1, stride=1, padding=0),
            Upsample(scale_factor=2, mode='nearest'),
        )
        self.route2_1 = Route()
        self.conv2_2 = nn.Sequential(
            Convolutional(768, 256, 1, stride=1, padding=0),
            Convolutional(256, 512, 3, stride=1, padding=1),
            Convolutional(512, 256, 1, stride=1, padding=0),
            Convolutional(256, 512, 3, stride=1, padding=1),
            Convolutional(512, 256, 1, stride=1, padding=0),
        )
        self.conv2_3 = nn.Sequential(
            Convolutional(256, 512, 3, stride=1, padding=1),
            nn.Conv2d(512, out_channel2, 1, stride=1, padding=0),
        )

        # yolo_layer3
        self.conv3_0 = nn.Sequential(
            Convolutional(in_channel3, 128, 1, stride=1, padding=0),
            Upsample(scale_factor=2, mode='nearest'),
        )
        self.route3_1 = Route()  # Y_98
        self.conv3_2 = nn.Sequential(
            Convolutional(384, 128, 1, stride=1, padding=0),
            Convolutional(128, 256, 3, stride=1, padding=1),
            Convolutional(256, 128, 1, stride=1, padding=0),
            Convolutional(128, 256, 3, stride=1, padding=1),
            Convolutional(256, 128, 1, stride=1, padding=0),
            Convolutional(128, 256, 3, stride=1, padding=1),
            nn.Conv2d(256, out_channel3, 1, stride=1, padding=0),
        )
Пример #24
0
def train(data_path,
          crop_size=128,
          final_size=64,
          batch_size=16,
          alternating_step=10000,
          ncritic=1,
          lambda_gp=0.1,
          debug_step=100):
    # define networks
    generator = Generator(final_size=final_size)
    generator.generate_network()
    g_optimizer = Adam(generator.parameters())

    discriminator = Discriminator(final_size=final_size)
    discriminator.generate_network()
    d_optimizer = Adam(discriminator.parameters())

    num_channels = min(generator.num_channels, generator.max_channels)

    # get debugging vectors
    N = (5, 10)
    debug_vectors = torch.randn(N[0] * N[1], num_channels, 1, 1).to(device)
    global upsample
    upsample = [
        Upsample(scale_factor=2**i)
        for i in reversed(range(generator.num_blocks))
    ]

    # get loader
    loader = get_loader(data_path, crop_size, batch_size)

    # training loop
    start_time = time.time()
    for index in range(generator.num_blocks):
        loader.dataset.set_transform_by_index(index)
        data_iterator = iter(loader)
        for phase in ('fade', 'stabilize'):
            if index == 0 and phase == 'fade': continue
            print("index: {}, size: {}x{}, phase: {}".format(
                index, 2**(index + 2), 2**(index + 2), phase))
            for i in range(alternating_step):
                print(i)
                try:
                    batch = next(data_iterator)
                except:
                    data_iterator = iter(loader)
                    batch = next(data_iterator)

                alpha = i / alternating_step if phase == "fade" else 1.0

                batch = batch.to(device)

                d_loss_real = -torch.mean(discriminator(batch, index, alpha))

                latent = torch.randn(batch_size, num_channels, 1, 1).to(device)
                fake_batch = generator(latent, index, alpha).detach()
                d_loss_fake = torch.mean(
                    discriminator(fake_batch, index, alpha))

                d_loss = d_loss_real + d_loss_fake
                d_optimizer.zero_grad()
                d_loss.backward()  # if retain_graph=True
                # then gp works but I'm not sure it's right
                d_optimizer.step()

                # Compute gradient penalty
                alpha_gp = torch.rand(batch.size(0), 1, 1, 1).to(device)
                # mind that x_hat must be both detached from the previous
                # gradient graph (from fake_barch) and with
                # requires_graph=True so that the gradient can be computed
                x_hat = (alpha_gp * batch +
                         (1 - alpha_gp) * fake_batch).requires_grad_(True)
                # x_hat = torch.cuda.FloatTensor(x_hat).requires_grad_(True)
                out = discriminator(x_hat, index, alpha)
                grad = torch.autograd.grad(
                    outputs=out,
                    inputs=x_hat,
                    grad_outputs=torch.ones_like(out).to(device),
                    retain_graph=True,
                    create_graph=True,
                    only_inputs=True)[0]
                grad = grad.view(grad.size(0), -1)  #is this the same as
                # detach?
                l2norm = torch.sqrt(torch.sum(grad**2, dim=1))
                d_loss_gp = torch.mean((l2norm - 1)**2)

                d_loss_gp *= lambda_gp
                d_optimizer.zero_grad()
                d_loss_gp.backward()
                d_optimizer.step()

                if (i + 1) % ncritic == 0:
                    latent = torch.randn(batch_size, num_channels, 1,
                                         1).to(device)
                    fake_batch = generator(latent, index, alpha)
                    g_loss = -torch.mean(
                        discriminator(fake_batch, index, alpha))
                    g_optimizer.zero_grad()
                    g_loss.backward()
                    g_optimizer.step()

                # print debugging images
                if (i + 1) % debug_step == 0:
                    print_debugging_images(generator, debug_vectors, N, index,
                                           alpha, i)
Пример #25
0
    def build_model(self):

        num_featmaps_lay1 = self.num_featmaps_base
        self.convolution_downlay1_1 = Conv3d(self.num_channels_in,
                                             num_featmaps_lay1,
                                             kernel_size=3,
                                             padding=1)
        self.convolution_downlay1_2 = Conv3d(num_featmaps_lay1,
                                             num_featmaps_lay1,
                                             kernel_size=3,
                                             padding=1)
        self.pooling_downlay1 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay2 = 2 * num_featmaps_lay1
        self.convolution_downlay2_1 = Conv3d(num_featmaps_lay1,
                                             num_featmaps_lay2,
                                             kernel_size=3,
                                             padding=1)
        self.convolution_downlay2_2 = Conv3d(num_featmaps_lay2,
                                             num_featmaps_lay2,
                                             kernel_size=3,
                                             padding=1)
        self.pooling_downlay2 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay3 = 2 * num_featmaps_lay2
        self.convolution_downlay3_1 = Conv3d(num_featmaps_lay2,
                                             num_featmaps_lay3,
                                             kernel_size=3,
                                             padding=1)
        self.convolution_downlay3_2 = Conv3d(num_featmaps_lay3,
                                             num_featmaps_lay3,
                                             kernel_size=3,
                                             padding=1)
        self.pooling_downlay3 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay4 = 2 * num_featmaps_lay3
        self.convolution_downlay4_1 = Conv3d(num_featmaps_lay3,
                                             num_featmaps_lay4,
                                             kernel_size=3,
                                             padding=1)
        self.convolution_downlay4_2 = Conv3d(num_featmaps_lay4,
                                             num_featmaps_lay4,
                                             kernel_size=3,
                                             padding=1)
        self.pooling_downlay4 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay5 = 2 * num_featmaps_lay4
        self.convolution_downlay5_1 = Conv3d(num_featmaps_lay4,
                                             num_featmaps_lay5,
                                             kernel_size=3,
                                             padding=1)
        self.convolution_downlay5_2 = Conv3d(num_featmaps_lay5,
                                             num_featmaps_lay5,
                                             kernel_size=3,
                                             padding=1)
        self.upsample_uplay5 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay4pl5 = num_featmaps_lay4 + num_featmaps_lay5
        self.convolution_uplay4_1 = Conv3d(num_featmaps_lay4pl5,
                                           num_featmaps_lay4,
                                           kernel_size=3,
                                           padding=1)
        self.convolution_uplay4_2 = Conv3d(num_featmaps_lay4,
                                           num_featmaps_lay4,
                                           kernel_size=3,
                                           padding=1)
        self.upsample_uplay4 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay3pl4 = num_featmaps_lay3 + num_featmaps_lay4
        self.convolution_uplay3_1 = Conv3d(num_featmaps_lay3pl4,
                                           num_featmaps_lay3,
                                           kernel_size=3,
                                           padding=1)
        self.convolution_uplay3_2 = Conv3d(num_featmaps_lay3,
                                           num_featmaps_lay3,
                                           kernel_size=3,
                                           padding=1)
        self.upsample_uplay3 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay2pl3 = num_featmaps_lay2 + num_featmaps_lay3
        self.convolution_uplay2_1 = Conv3d(num_featmaps_lay2pl3,
                                           num_featmaps_lay2,
                                           kernel_size=3,
                                           padding=1)
        self.convolution_uplay2_2 = Conv3d(num_featmaps_lay2,
                                           num_featmaps_lay2,
                                           kernel_size=3,
                                           padding=1)
        self.upsample_uplay2 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay1pl2 = num_featmaps_lay1 + num_featmaps_lay2
        self.convolution_uplay1_1 = Conv3d(num_featmaps_lay1pl2,
                                           num_featmaps_lay1,
                                           kernel_size=3,
                                           padding=1)
        self.convolution_uplay1_2 = Conv3d(num_featmaps_lay1,
                                           num_featmaps_lay1,
                                           kernel_size=3,
                                           padding=1)

        self.classification_layer = Conv3d(num_featmaps_lay1,
                                           self.num_classes_out,
                                           kernel_size=1,
                                           padding=0)
        self.activation_layer = Sigmoid()
Пример #26
0
    def build_model(self):

        num_featmaps_lay1 = self.num_featmaps_base
        self.convolution_downlay1_1 = Conv3d(self.num_channels_in,
                                             num_featmaps_lay1,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay1_1 = ReLU(inplace=True)
        # self.batchnorm_downlay1_1 = BatchNorm3d(num_featmaps_lay1)
        self.convolution_downlay1_2 = Conv3d(num_featmaps_lay1,
                                             num_featmaps_lay1,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay1_2 = ReLU(inplace=True)
        # self.batchnorm_downlay1_2 = BatchNorm3d(num_featmaps_lay1)
        # self.dropout_downlay1 = Dropout3d(p=self.dropout_rate)
        self.pooling_downlay1 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay2 = 2 * num_featmaps_lay1
        self.convolution_downlay2_1 = Conv3d(num_featmaps_lay1,
                                             num_featmaps_lay2,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay2_1 = ReLU(inplace=True)
        # self.batchnorm_downlay2_1 = BatchNorm3d(num_featmaps_lay2)
        self.convolution_downlay2_2 = Conv3d(num_featmaps_lay2,
                                             num_featmaps_lay2,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay2_2 = ReLU(inplace=True)
        # self.batchnorm_downlay2_2 = BatchNorm3d(num_featmaps_lay2)
        # self.dropout_downlay2 = Dropout3d(p =self.dropout_rate)
        self.pooling_downlay2 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay3 = 2 * num_featmaps_lay2
        self.convolution_downlay3_1 = Conv3d(num_featmaps_lay2,
                                             num_featmaps_lay3,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay3_1 = ReLU(inplace=True)
        # self.batchnorm_downlay3_1 = BatchNorm3d(num_featmaps_lay3)
        self.convolution_downlay3_2 = Conv3d(num_featmaps_lay3,
                                             num_featmaps_lay3,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay3_2 = ReLU(inplace=True)
        # self.batchnorm_downlay3_2 = BatchNorm3d(num_featmaps_lay3)
        # self.dropout_downlay3 = Dropout3d(p=self.dropout_rate)
        self.pooling_downlay3 = MaxPool3d(kernel_size=2, padding=0)

        num_featmaps_lay4 = 2 * num_featmaps_lay3
        self.convolution_downlay4_1 = Conv3d(num_featmaps_lay3,
                                             num_featmaps_lay4,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay4_1 = ReLU(inplace=True)
        # self.batchnorm_downlay4_1 = BatchNorm3d(num_featmaps_lay4)
        self.convolution_downlay4_2 = Conv3d(num_featmaps_lay4,
                                             num_featmaps_lay4,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay4_2 = ReLU(inplace=True)
        # self.batchnorm_downlay4_2 = BatchNorm3d(num_featmaps_lay4)
        # self.dropout_downlay4 = Dropout3d(p=self.dropout_rate)
        self.pooling_downlay4 = MaxPool3d(kernel_size=(1, 2, 2), padding=0)

        num_featmaps_lay5 = 2 * num_featmaps_lay4
        self.convolution_downlay5_1 = Conv3d(num_featmaps_lay4,
                                             num_featmaps_lay5,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay5_1 = ReLU(inplace=True)
        # self.batchnorm_downlay5_1 = BatchNorm3d(num_featmaps_lay5)
        self.convolution_downlay5_2 = Conv3d(num_featmaps_lay5,
                                             num_featmaps_lay5,
                                             kernel_size=3,
                                             padding=1)
        self.activation_downlay5_2 = ReLU(inplace=True)
        # self.batchnorm_downlay5_2 = BatchNorm3d(num_featmaps_lay5)
        # self.dropout_downlay5 = Dropout3d(p=self.dropout_rate)
        self.upsample_downlay5 = Upsample(scale_factor=(1, 2, 2),
                                          mode='nearest')

        num_featmaps_lay4pl5 = num_featmaps_lay4 + num_featmaps_lay5
        self.convolution_uplay4_1 = Conv3d(num_featmaps_lay4pl5,
                                           num_featmaps_lay4,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay4_1 = ReLU(inplace=True)
        # self.batchnorm_uplay4_1 = BatchNorm3d(num_featmaps_lay4)
        self.convolution_uplay4_2 = Conv3d(num_featmaps_lay4,
                                           num_featmaps_lay4,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay4_2 = ReLU(inplace=True)
        # self.batchnorm_uplay4_2 = BatchNorm3d(num_featmaps_lay4)
        # self.dropout_uplay4 = Dropout3d(p=self.dropout_rate)
        self.upsample_uplay4 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay3pl4 = num_featmaps_lay3 + num_featmaps_lay4
        self.convolution_uplay3_1 = Conv3d(num_featmaps_lay3pl4,
                                           num_featmaps_lay3,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay3_1 = ReLU(inplace=True)
        # self.batchnorm_uplay3_1 = BatchNorm3d(num_featmaps_lay3)
        self.convolution_uplay3_2 = Conv3d(num_featmaps_lay3,
                                           num_featmaps_lay3,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay3_2 = ReLU(inplace=True)
        # self.batchnorm_uplay3_2 = BatchNorm3d(num_featmaps_lay3)
        # self.dropout_uplay3 = Dropout3d(p=self.dropout_rate)
        self.upsample_uplay3 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay2pl3 = num_featmaps_lay2 + num_featmaps_lay3
        self.convolution_uplay2_1 = Conv3d(num_featmaps_lay2pl3,
                                           num_featmaps_lay2,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay2_1 = ReLU(inplace=True)
        # self.batchnorm_uplay2_1 = BatchNorm3d(num_featmaps_lay2)
        self.convolution_uplay2_2 = Conv3d(num_featmaps_lay2,
                                           num_featmaps_lay2,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay2_2 = ReLU(inplace=True)
        # self.batchnorm_uplay2_2 = BatchNorm3d(num_featmaps_lay2)
        # self.dropout_uplay2 = Dropout3d(p=self.dropout_rate)
        self.upsample_uplay2 = Upsample(scale_factor=2, mode='nearest')

        num_featmaps_lay1pl2 = num_featmaps_lay1 + num_featmaps_lay2
        self.convolution_uplay1_1 = Conv3d(num_featmaps_lay1pl2,
                                           num_featmaps_lay1,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay1_1 = ReLU(inplace=True)
        # self.batchnorm_uplay1_1 = BatchNorm3d(num_featmaps_lay1)
        self.convolution_uplay1_2 = Conv3d(num_featmaps_lay1,
                                           num_featmaps_lay1,
                                           kernel_size=3,
                                           padding=1)
        self.activation_uplay1_2 = ReLU(inplace=True)
        # self.batchnorm_uplay1_2 = BatchNorm3d(num_featmaps_lay1)
        # self.dropout_uplay1 = Dropout3d(p=self.dropout_rate)
        self.classification_layer = Conv3d(num_featmaps_lay1,
                                           self.num_classes_out,
                                           kernel_size=1,
                                           padding=0)

        self.activation_output = Sigmoid()
Пример #27
0
from torch.nn import UpsamplingBilinear2d, Upsample
from torch.autograd import Variable
import numpy as np
import tqdm
import matplotlib.pyplot as plt

import config as c
import opts
import time

opts.parse(sys.argv)
config_str = ""
config_str += "==="*30 + "\n"
config_str += "Config options:\n\n"

upsampler = Upsample(size=(c.org_size, c.org_size), align_corners=True, mode='bilinear')

for v in dir(c):
    if v[0]=='_': continue
    s=eval('c.%s'%(v))
    config_str += "  {:25}\t{}\n".format(v,s)

config_str += "==="*30 + "\n"

print(config_str)

import model
import data

# Loading the flow-based model, as well as the target classifier
if c.load_file:
Пример #28
0
    def __init__(self,
                 output_size,
                 dropout=0,
                 noise_size=20,
                 channel_last=True,
                 hidden_size=50,
                 **kwargs):
        # defaults
        self.initial_length = 5
        self.output_size = output_size
        self.hidden_size = hidden_size
        self.encoding_dims = noise_size
        self.num_classes = 2
        self.label_embedding_size = self.num_classes
        self.prob_classes = torch.ones(self.num_classes)
        self.dropout = dropout
        self.label_type = 'generated'
        self.channel_last = channel_last
        self._labels = None

        # Set kwargs (might overried above attributes)
        for key, value in kwargs.items():
            setattr(self, key, value)

        super(CNNCGANGenerator, self).__init__(self.encoding_dims,
                                               self.label_type)
        self.label_embeddings = nn.Embedding(self.num_classes,
                                             self.label_embedding_size)

        Conv1d_ = lambda k: Conv1d(self.hidden_size, self.hidden_size, k)
        ## Build CNN layer
        # (batch_size, channels, sequence length)

        layers_input_size = self.encoding_dims * (1 +
                                                  self.label_embedding_size)
        self.layers = nn.Sequential(
            Linear(layers_input_size, self.initial_length * self.hidden_size),
            View(-1, self.hidden_size, self.initial_length),
            LeakyReLU(0.2),
            Dropout(dropout),
            # output size: (-1, 50, 5)
            Upsample(scale_factor=2, mode='linear', align_corners=True),
            # output size: (-1, 50, 10)
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            Dropout(dropout),
            # output size: (-1, 50, 10)
            Upsample(scale_factor=2, mode='linear', align_corners=True),
            # output size: (-1, 50, 20)
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            Dropout(dropout),
            # output size: (-1, 50, 20)
            Conv1d(self.hidden_size, self.output_size, 1)
            # output size: (-1, 5, 20)
        )

        # Initialize all weights.
        self._weight_initializer()
Пример #29
0
 def __init__(self, scale_factor=2):
     super(PGUpsample, self).__init__()
     self.scale_factor = scale_factor
     self.upsample = Upsample(scale_factor=scale_factor)
Пример #30
0
    def __init__(self, input_nc=1, output_nc=1, depth=32, momentum=0.8,dropout = 0):
        super(UnetGenerator, self).__init__()
        '''
        # 256 x 256
        self.e1 = nn.Sequential(nn.Conv2d(input_nc,depth,kernel_size=4,stride=2,padding=1),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 128 x 128
        self.e2 = nn.Sequential(nn.Conv2d(depth,depth*2,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*2),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 64 x 64
        self.e3 = nn.Sequential(nn.Conv2d(depth*2,depth*4,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*4),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 32 x 32
        self.e4 = nn.Sequential(nn.Conv2d(depth*4,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 16 x 16
        self.e5 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 8 x 8
        self.e6 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 4 x 4
        self.e7 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.BatchNorm2d(depth*8),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 2 x 2
        self.e8 = nn.Sequential(nn.Conv2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                 nn.LeakyReLU(0.2, inplace=True))
        # 1 x 1
        self.d1 = nn.Sequential(nn.ConvTranspose2d(depth*8,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8),
                                nn.Dropout())
        # 2 x 2
        self.d2 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8),
                                nn.Dropout())
        # 4 x 4
        self.d3 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8),
                                nn.Dropout())
        # 8 x 8
        self.d4 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*8,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*8))
        # 16 x 16
        self.d5 = nn.Sequential(nn.ConvTranspose2d(depth*8*2,depth*4,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*4))
        # 32 x 32
        self.d6 = nn.Sequential(nn.ConvTranspose2d(depth*4*2,depth*2,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth*2))
        # 64 x 64
        self.d7 = nn.Sequential(nn.ConvTranspose2d(depth*2*2,depth,kernel_size=4,stride=2,padding=1),
                                nn.BatchNorm2d(depth))
        # 128 x 128
        self.d8 = nn.ConvTranspose2d(depth*2,output_nc,kernel_size=4,stride=2,padding=1)
        # 256 x 256
        self.relu = nn.ReLU(inplace=True)
        self.tanh = nn.Tanh()
    def forward(self,x):
        # encoder
        out_e1 = self.e1(x)              # 128 x 128
        out_e2 = self.e2(out_e1)             # 64 x 64
        out_e3 = self.e3(out_e2)             # 32 x 32
        out_e4 = self.e4(out_e3)             # 16 x 16
        out_e5 = self.e5(out_e4)             # 8 x 8
        out_e6 = self.e6(out_e5)             # 4 x 4
        out_e7 = self.e7(out_e6)             # 2 x 2
        out_e8 = self.e8(out_e7)             # 1 x 1
        # decoder
        out_d1 = self.d1(self.relu(out_e8))  # 2 x 2
        out_d1_ = torch.cat((out_d1, out_e7),1)
        out_d2 = self.d2(self.relu(out_d1_)) # 4 x 4
        out_d2_ = torch.cat((out_d2, out_e6),1)
        out_d3 = self.d3(self.relu(out_d2_)) # 8 x 8
        out_d3_ = torch.cat((out_d3, out_e5),1)
        out_d4 = self.d4(self.relu(out_d3_)) # 16 x 16
        out_d4_ = torch.cat((out_d4, out_e4),1)
        out_d5 = self.d5(self.relu(out_d4_)) # 32 x 32
        out_d5_ = torch.cat((out_d5, out_e3),1)
        out_d6 = self.d6(self.relu(out_d5_)) # 64 x 64
        out_d6_ = torch.cat((out_d6, out_e2),1)
        out_d7 = self.d7(self.relu(out_d6_)) # 128 x 128
        out_d7_ = torch.cat((out_d7, out_e1),1)
        out_d8 = self.d8(self.relu(out_d7_)) # 256 x 256
        out = self.tanh(out_d8)
        return out
        '''
        # input_img = Input(shape=self.img_shape)  # 256, 256, 1
        # # Downsampling
        # d0 = conv2d(input_img, depth * 1, bn=False, stride=1, name="d0")  # 256, 256, 16
        # d0 = conv2d(d0, depth * 1, bn=False, f_size=3, stride=1, name="d0_1")  # 256, 256, 16
        # d1 = conv2d(d0, depth * 2, bn=False, name="d1")  # 128, 128, 32
        # d2 = conv2d(d1, depth * 2, name="d2")  # 64, 64, 32
        # d2 = conv2d(d2, depth * 2, f_size=3, stride=1, name="d2_1")  # 64, 64, 32
        # d3 = conv2d(d2, depth * 4, name="d3")  # 32, 32, 64
        # d4 = conv2d(d3, depth * 4, name="d4")  # 16, 16, 64
        # d4 = conv2d(d4, depth * 4, f_size=3, stride=1, name="d4_1")  # 16, 16, 64
        # d5 = conv2d(d4, depth * 8, name="d5")  # 8, 8, 128
        # d6 = conv2d(d5, depth * 8, name="d6")  # 4, 4, 128
        # d6 = conv2d(d6, depth * 8, f_size=3, stride=1, name="d6_1")  # 4, 4, 128
        # d7 = conv2d(d6, depth * 16, name="d7")  # 2, 2, 256
        # d8 = conv2d(d7, depth * 16, f_size=2, stride=1, padding="valid", name="d8")
        self.depth = depth
        self.d0 = Sequential(Conv2d(input_nc, depth, 7, 1, 3), LeakyReLU(0.2),
                             Conv2d(depth, depth, 5, 1, 2), LeakyReLU(0.2))  # 256, 256, 16
        self.d1 = Sequential(Conv2d(depth, depth * 2, 4, 2, 1), LeakyReLU(0.2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1),
                             BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2))  # 128, 128, 32
        self.d2 = Sequential(Conv2d(depth * 2, depth * 2, 4, 2, 1),
                             BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1),
                             BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2))  # 64, 64, 32
        self.d3 = Sequential(Conv2d(depth * 2, depth * 4, 4, 2, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2))  # 32, 32, 64
        self.d4 = Sequential(Conv2d(depth * 4, depth * 4, 4, 2, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1),
                             BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2))  # 16, 16, 64
        self.d5 = Sequential(Conv2d(depth * 4, depth * 8, 4, 2, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2))  # 8, 8, 128
        self.d6 = Sequential(Conv2d(depth * 8, depth * 8, 4, 2, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1),
                             BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2))  # 4, 4, 128
        self.d7 = Sequential(Conv2d(depth * 8, depth * 16, 4, 2, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2))  # 2, 2, 256
        self.d8 = Sequential(Conv2d(depth * 16, depth * 16, 4, 2, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1),
                             BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             )  # 2, 2, 256

        # # Upsampling
        # u0 = deconv2d(d8, d7, depth * 16)  # 2, 2, depth * 16  + depth * 16,depth * 8
        # u1 = deconv2d(u0, d6, depth * 8)  # 4, 4, depth * 8  + depth * 8,depth * 8
        # u2 = deconv2d(u1, d5, depth * 8)  # 8 ,8, depth * 8 + depth * 8,depth * 4
        # u3 = deconv2d(u2, d4, depth * 4)  # 16,16,depth * 4 + depth * 4,depth * 4
        # u4 = deconv2d(u3, d3, depth * 4)  # 32,32,depth * 4 + depth * 4,depth * 2
        # u5 = deconv2d(u4, d2, depth * 2)  # 64,64,depth * 2 + depth * 2,depth * 2
        # u6 = deconv2d(u5, d1, depth * 2)  # 128,128,depth * 2 + depth * 2,depth * 2
        # u7 = deconv2d(u6, d0, depth * 1)  # 256,256,depth * 2 + depth * 2, depth * 1
        # # u8 = UpSampling2D(size=2)(u7)  #256,256,depth * 2 + depth * 2,depth * 2
        # u8 = Conv2D(self.channels, kernel_size=5, strides=1, padding='same', activation=None)(u7)  # 256,256,1
        # output_img = Activation(sigmoid10)(u8)
        self.u0 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 16, depth * 16, 3, 1, 1), BatchNorm2d(depth * 16, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u1 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 8, depth * 8, 3, 1, 1), BatchNorm2d(depth * 8, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u2 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1) ,BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u3 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 4, depth * 4, 3, 1, 1), BatchNorm2d(depth * 4, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u4 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1), BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u5 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 2, depth * 2, 3, 1, 1), BatchNorm2d(depth * 2, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        self.u6 = Sequential(Upsample(scale_factor=2),
                             Conv2d(depth * 1, depth * 1, 3, 1, 1), BatchNorm2d(depth * 1, momentum=momentum), LeakyReLU(0.2),
                             Dropout(dropout, True))
        # self.u7 = Sequential(Upsample(scale_factor=2),
        #                      Conv2d(depth * 1, depth * 1, 3, 1, 1), LeakyReLU(0.2),
        #                      Dropout(0.1), BatchNorm2d(depth * 1, momentum=momentum))
        self.u7 = Sequential(Conv2d(depth * 1, output_nc, 5, 1, 2), Tanh())
        # self.conv1 = Conv2d(input_nc, depth, 4, 2, 1)
        # self.conv2 = Conv2d(depth, depth * 2, 4, 2, 1)
        # self.conv3 = Conv2d(depth * 2, depth * 4, 4, 2, 1)
        # self.conv4 = Conv2d(depth * 4, depth * 8, 4, 2, 1)
        # self.conv5 = Conv2d(depth * 8, depth * 8, 4, 2, 1)
        # self.conv6 = Conv2d(depth * 8, depth * 8, 4, 2, 1)
        # self.conv7 = Conv2d(depth * 8, depth * 8, 4, 2, 1)
        # self.conv8 = Conv2d(depth * 8, depth * 8, 4, 2, 1)

        # self.dconv1 = ConvTranspose2d(depth * 8, depth * 8, 4, 2, 1)
        # self.dconv2 = ConvTranspose2d(depth * 8 * 2, depth * 8, 4, 2, 1)
        # self.dconv3 = ConvTranspose2d(depth * 8 * 2, depth * 8, 4, 2, 1)
        # self.dconv4 = ConvTranspose2d(depth * 8 * 2, depth * 8, 4, 2, 1)
        # self.dconv5 = ConvTranspose2d(depth * 8 * 2, depth * 4, 4, 2, 1)
        # self.dconv6 = ConvTranspose2d(depth * 4 * 2, depth * 2, 4, 2, 1)
        # self.dconv7 = ConvTranspose2d(depth * 2 * 2, depth, 4, 2, 1)
        # self.dconv8 = ConvTranspose2d(depth * 2, output_nc, 4, 2, 1)

        # self.batch_norm = BatchNorm2d(depth)
        # self.batch_norm2 = BatchNorm2d(depth * 2)
        # self.batch_norm4 = BatchNorm2d(depth * 4)
        # self.batch_norm8 = BatchNorm2d(depth * 8)
        #
        self.leaky_relu = LeakyReLU(0.2)
        # self.relu = ReLU(True)

        # self.dropout = Dropout(0.2)

        # self.sigmoid = Sigmoid()
        self.conv_32_8 = Conv2d(depth * 16 * 2, depth * 8, 3, 1, 1)
        self.conv_16_8 = Conv2d(depth * 8 * 2, depth * 8, 3, 1, 1)
        self.conv_16_4 = Conv2d(depth * 8 * 2, depth * 4, 3, 1, 1)
        self.conv_8_4 = Conv2d(depth * 4 * 2, depth * 4, 3, 1, 1)
        self.conv_8_2 = Conv2d(depth * 4 * 2, depth * 2, 3, 1, 1)
        self.conv_4_2 = Conv2d(depth * 2 * 2, depth * 2, 3, 1, 1)
        self.conv_4_1 = Conv2d(depth * 2 * 2, depth * 1, 3, 1, 1)
        self.conv_2_1 = Conv2d(depth * 1 * 2, depth * 1, 3, 1, 1)