Esempio n. 1
0
    def __init__(self,
                 img_shape,
                 latent_size,
                 z_dist="uniform",
                 z_std=1,
                 gen_type=1,
                 disc_type=1):
        super().__init__()
        self.img_shape = img_shape
        self.latent_size = latent_size
        self.z_dist = z_dist
        self.z_std = z_std

        if gen_type == 1:
            self.generator = nn.Sequential(
                nn.Linear(latent_size, 3 * 3 * 128),
                nn.Unflatten(1, (128, 3, 3)), nn.BatchNorm2d(128,
                                                             momentum=0.8),
                _deconv2(128, 64, 3, 1, 1, upsize=(7, 7), bn=True,
                         act="lrelu"),
                _deconv2(64, 32, 3, 1, 1, 2, bn=True, act="lrelu"),
                _deconv2(32, 16, 3, 1, 1, 2, bn=False, act=None),
                _conv(16, 1, 3, 1, 1, False, None), nn.Tanh())
        else:
            self.generator = nn.Sequential(
                nn.Linear(latent_size, 3 * 3 * 128),
                nn.Unflatten(1, (128, 3, 3)), nn.BatchNorm2d(128,
                                                             momentum=0.8),
                _deconv(128, 64, 3, 2, 0, 0, True, "lrelu"),
                _deconv(64, 32, 3, 2, 1, 1, True, "lrelu"),
                _deconv(32, 16, 3, 2, 1, 1, True, "lrelu"),
                _conv(16, 1, 3, 1, 1, False, None), nn.Tanh())

        if disc_type == 1:
            self.discriminator = nn.Sequential(
                _conv(1, 16, 3, 2, 1, False, "lrelu", 0.25),  # --> 14x14
                _conv(16, 32, 3, 2, 1, False, "lrelu", 0.25),  # --> 7x7
                _conv(32, 64, 3, 2, 1, False, "lrelu", 0.25),  # --> 4x4
                _conv(64, 128, 3, 2, 1, False, "lrelu", 0.25),  # --> 2x2
                nn.Flatten(),
                nn.Linear(128 * 4, 1))
        elif disc_type == 2:
            self.discriminator = nn.Sequential(
                _conv(1, 16, 3, 2, 1, False, "lrelu", 0.25),  # --> 14x14
                _conv(16, 32, 3, 2, 1, False, "lrelu", 0.25),  # --> 7x7
                _conv(32, 64, 3, 2, 1, False, "lrelu", 0.25),  # --> 4x4
                _conv(64, 128, 3, 2, 1, False, "lrelu", 0.25),  # --> 2x2
                nn.AdaptiveMaxPool2d((1, 1)),
                nn.Flatten(),
                nn.Linear(128, 1))
        else:
            self.discriminator = nn.Sequential(
                _conv(1, 16, 3, 2, 1, False, "lrelu", 0.25),  # --> 14x14
                _conv(16, 32, 3, 2, 1, False, "lrelu", 0.25),  # --> 7x7
                _conv(32, 64, 3, 2, 1, False, "lrelu", 0.25),  # --> 4x4
                _conv(64, 128, 3, 2, 1, False, "lrelu", 0.25),  # --> 2x2
                _conv(128, 1, 3, 1, 1, False, None),  # --> 2x2
                nn.AdaptiveAvgPool2d((1, 1)),
                nn.Flatten(),
            )
Esempio n. 2
0
    def __init__(self, data_shape, hidden_layers, latent_dim, n_classes):
        super().__init__()
        self.c = data_shape[1]
        self.n = [8, 16, 32, 64, 128, 256][hidden_layers[0]:hidden_layers[1]]
        self.flat_dim = self.n[-1] * (data_shape[-1] // 2 ** len(self.n)) ** 2
        self.unflat_shape = (self.n[-1], data_shape[-1] // 2 ** len(self.n), data_shape[-1] // 2 ** len(self.n))
        self.inflat = data_shape[2] * data_shape[3]

        self.lable = nn.Sequential(
            nn.Linear(n_classes, self.inflat),
            nn.Unflatten(-1, (1, *data_shape[2:]))
        )

        self.encoder = nn.Sequential(
            DownConv(self.c + 1, self.n[0]),
            *[DownConv(self.n[i], self.n[i + 1]) for i in range(len(self.n) - 1)],
            nn.Flatten(start_dim=1)
        )

        self.mu = nn.Linear(self.flat_dim, latent_dim)
        self.logsig = nn.Linear(self.flat_dim, latent_dim)

        self.decoder = nn.Sequential(
            nn.Linear(latent_dim + self.inflat, self.flat_dim),
            nn.Unflatten(-1, self.unflat_shape),
            *[UpConv(self.n[i], self.n[i - 1]) for i in range(len(self.n) - 1, 0, -1)],
            UpConv(self.n[0], self.c),
            nn.Conv2d(in_channels=self.c, out_channels=self.c, kernel_size=3, padding=1),
            nn.BatchNorm2d(self.c),
            nn.Sigmoid()
        )
Esempio n. 3
0
 def __init__(self, embedding_dim=1024):
     super().__init__()
     self.per_tile_actions = (5, 12)
     self.mouse_action_size = (62, 62) + self.per_tile_actions
     self.mouse_action_dim = reduce(mul, self.mouse_action_size, 1)
     self.mouse_position = MapGenerator_62_62(embedding_dim)
     self.sidebar_decoder = SidebarDecoder(
         embedding_dim=embedding_dim, num_layers=2, out_dim=12
     )
     self.flatten = nn.Flatten()
     self.mouse_unflatten = nn.Unflatten(-1, self.mouse_action_size)
     self.sidebar_unflatten = nn.Unflatten(-1, (-1, self.sidebar_decoder.embedding_dim))
Esempio n. 4
0
    def __init__(self, channels, imsize, kern_size):
        super().__init__()

        impulse_size = imsize - kern_size + 1
        self.softmax_impulse = nn.Sequential(
            nn.Flatten(start_dim=2),
            nn.Softmax(-1),
            nn.Unflatten(dim=2, unflattened_size=(impulse_size, impulse_size))
        )

        self.softmax_feats = nn.Sequential(
            nn.Flatten(start_dim=1),
            nn.Softmax(dim=-1),
            nn.Unflatten(1, (channels, kern_size, kern_size))
        )
def build_dc_classifier():
    """
  Build and return a PyTorch nn.Sequential model for the DCGAN discriminator implementing
  the architecture in the notebook.
  """
    model = None
    ############################################################################
    # TODO: Implement build_dc_classifier.                                     #
    ############################################################################
    # Replace "pass" statement with your code
    model = nn.Sequential(
        nn.Unflatten(1, (1, 28, 28)),
        nn.Conv2d(1, 32, 5, 1),  #24
        nn.LeakyReLU(),
        nn.MaxPool2d(2, stride=2),  #12
        nn.Conv2d(32, 64, 5, 1),  #8
        nn.LeakyReLU(),
        nn.MaxPool2d(2, 2),  #4
        nn.Flatten(),
        nn.Linear(4 * 4 * 64, 4 * 4 * 64),
        nn.LeakyReLU(),
        nn.Linear(4 * 4 * 64, 1))
    ############################################################################
    #                             END OF YOUR CODE                             #
    ############################################################################

    return model
Esempio n. 6
0
def build_dc_generator(noise_dim=NOISE_DIM):
    """
    Build and return a PyTorch model implementing the DCGAN generator using
    the architecture described above.
    """

    ##############################################################################
    # TODO: Implement architecture                                               #
    #                                                                            #
    # HINT: nn.Sequential might be helpful.                                      #
    ##############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    model = nn.Sequential(OrderedDict([
          ( 'a1', nn.Linear(noise_dim,1024) ),
          ( 'r1', nn.ReLU() ),
          ( 'b1', nn.BatchNorm1d(1024) ),
          ( 'a2', nn.Linear(1024,6272) ),
          ( 'r2', nn.ReLU() ),
          ( 'b2', nn.BatchNorm1d(6272) ),
          ( 'unflat', nn.Unflatten(1, (128,7,7)) ),
          ( 't3', nn.ConvTranspose2d(128, 64, 4, 2, 1) ),
          ( 'r3', nn.ReLU() ),
          ( 'b3', nn.BatchNorm2d(64) ),
          ( 't4', nn.ConvTranspose2d(64, 1, 4, 2, 1) ),
          ( 'tanh', nn.Tanh() ),
          ( 'flat', nn.Flatten(1,3) )
        ]))

    return model
Esempio n. 7
0
    def __init__(self, encoded_space_dim, conv1_ch, conv2_ch, conv3_ch, fc_ch):
        super().__init__()

        ### Linear section
        self.decoder_lin = nn.Sequential(
            # First linear layer
            nn.Linear(encoded_space_dim, fc_ch),
            nn.ReLU(True),
            # Second linear layer
            nn.Linear(fc_ch, 2 * 2 * conv3_ch),
            nn.ReLU(True)
        )

        ### Unflatten
        self.unflatten = nn.Unflatten(dim=1, unflattened_size=(conv3_ch, 2, 2))

        ### Convolutional section
        self.decoder_conv = nn.Sequential(
            # First transposed convolution
            nn.ConvTranspose2d(conv3_ch, conv2_ch, 3, stride=1, output_padding=0),
            nn.ReLU(True),
            # Second transposed convolution
            nn.ConvTranspose2d(conv2_ch, conv1_ch, 3, stride=1, padding=1, output_padding=0),
            nn.ReLU(True),
            # Third transposed convolution
            nn.ConvTranspose2d(conv1_ch, 4, 3, stride=1, padding=1, output_padding=0)
        )
Esempio n. 8
0
 def __init__(self):
     super().__init__()
     self.latent_size = fc_layers[-1]
     new_h2, new_w2 = new_h, new_w  # used to determin
     layers = []
     if len(fc_layers) > 1:
         layers.append(MLP(list(reversed(fc_layers)), nn.ReLU, nn.ReLU))
         layers.append(nn.BatchNorm1d(new_h2 * new_w2 * channels[-1]))
     layers.append(nn.Unflatten(1, (channels[-1], new_h2, new_w2)))
     for i, (new_c, old_c, filter_size, stride,
             expand_ratio) in reversed(
                 list(
                     enumerate(
                         zip((c, ) + channels[:-1], channels,
                             filter_sizes, strides, expand_ratios)))):
         if stride != 1:
             new_h2 = new_h2 * stride - 1
             new_w2 = new_w2 * stride - 1
         layers.append(
             TransposedInvertedResidual(
                 old_c,
                 new_c,
                 stride,
                 expand_ratio,
                 filter_size,
                 norm_layer=nn.Identity if i == 0 else norm_layer))
     layers.append(nn.Tanh())
     if new_h2 != h or new_w2 != w:
         layers.append(Interpolate((h, w)))
     self.model = nn.Sequential(*layers)
Esempio n. 9
0
 def __init__(self):
     super().__init__()
     self.latent_size = fc_layers[-1]
     layers = []
     if len(fc_layers) > 1:
         layers.append(MLP(list(reversed(fc_layers)), nn.ReLU, nn.ReLU))
         layers.append(nn.BatchNorm1d(new_h * new_w * channels[-1]))
     layers.append(nn.Unflatten(1, (channels[-1], new_h, new_w)))
     for i, (new_c, old_c, filter_size, pool, stride) in reversed(
             list(
                 enumerate(
                     zip((c, ) + channels[:-1], channels, filter_sizes,
                         pools, strides)))):
         if pool != 1:
             layers.append(
                 nn.Upsample(scale_factor=pool, mode="bilinear"))
         layers.append(
             nn.ConvTranspose2d(old_c,
                                new_c,
                                filter_size,
                                stride=stride))
         if i != 0:  # no relu and batchnorm in last layer
             layers.append(nn.ReLU())
             layers.append(norm_layer(new_c))
     layers.append(nn.Tanh())
     layers.append(Interpolate((h, w)))  #
     self.model = nn.Sequential(*layers)
Esempio n. 10
0
    def __init__(
        self,
        embedding_size: int,
        n_instructions: int,
        encoded_question_size: int,
    ):
        super().__init__()
        # hardcode max seq len value, all questions in (my version of) clevr are
        # 41 tokens at most
        self.max_seq_len = 50
        self.encoded_question_size = encoded_question_size
        self.encoder = nn.Sequential(
            nn.Flatten(start_dim=1, end_dim=2),
            nn.Linear(self.max_seq_len * embedding_size,
                      self.max_seq_len * embedding_size),
            nn.ReLU(),
            nn.Linear(self.max_seq_len * embedding_size,
                      encoded_question_size),
            nn.ReLU(),
        )

        self.decoder = nn.Sequential(
            nn.Linear(encoded_question_size, encoded_question_size),
            nn.ReLU(),
            nn.Linear(encoded_question_size, n_instructions * embedding_size),
            nn.Unflatten(dim=1,
                         unflattened_size=(n_instructions, embedding_size)),
        )
Esempio n. 11
0
def build_dc_generator(noise_dim=NOISE_DIM):
    """
  Build and return a PyTorch model implementing the DCGAN generator using
  the architecture described above.
  """
    model = nn.Sequential(
        ############################################################################
        # TODO: Implement build_dc_generator.                                      #
        ############################################################################
        # Replace "pass" statement with your code
        nn.Linear(noise_dim, 1024),
        nn.ReLU(),
        nn.BatchNorm1d(1024),
        nn.Linear(1024, 6272),  # 7*7*128 = 6272
        nn.ReLU(),
        nn.BatchNorm1d(
            6272),  # We should do BatchNorm1d(F) for an input of (N,F)
        nn.Unflatten(1, (128, 7, 7)),
        nn.ConvTranspose2d(128, 64, 4, stride=2,
                           padding=1),  # output will be 14x14
        nn.ReLU(),
        nn.BatchNorm2d(
            64),  # We should do BatchNorm2d(C) for an input of (N,C,H,W)
        nn.ConvTranspose2d(64, 1, 4, stride=2,
                           padding=1),  # output will be 28x28
        nn.Tanh(),
        nn.Flatten()
        ############################################################################
        #                             END OF YOUR CODE                             #
        ############################################################################
    )

    return model
Esempio n. 12
0
def build_dc_classifier():
    """
  Build and return a PyTorch model for the DCGAN discriminator implementing
  the architecture above.
  """
    model = nn.Sequential(
        ############################################################################
        # TODO: Implement build_dc_classifier.                                     #
        ############################################################################
        # Replace "pass" statement with your code
        nn.Unflatten(1, (1, 28, 28)),
        nn.Conv2d(1, 32, 5),
        nn.LeakyReLU(negative_slope=0.01),
        nn.MaxPool2d(2, stride=2),
        nn.Conv2d(32, 64, 5),
        nn.LeakyReLU(negative_slope=0.01),
        nn.MaxPool2d(2, stride=2),
        nn.Flatten(),
        nn.Linear(1024, 1024),
        nn.LeakyReLU(negative_slope=0.01),
        nn.Linear(1024, 1)

        ############################################################################
        #                             END OF YOUR CODE                             #
        ############################################################################
    )

    return model
Esempio n. 13
0
 def __init__(self,
              img_size: tp.Tuple,
              scale: int,
              mode: str,
              in_channels: int,
              hidden_channels: int,
              out_channels: int,
              groups=1):
     super().__init__()
     self.img_size = img_size  # img_orig_shape // patch_size
     if mode == "up":
         interpolation_conv = nn.ConvTranspose2d
     elif mode == "down":
         assert img_size[0] % scale == img_size[1] % scale == 0
         interpolation_conv = nn.Conv2d
     else:
         raise Exception("Wrong mode")
     self.engine = nn.Sequential(
         nn.Unflatten(2, torch.Size(img_size)),
         interpolation_conv(in_channels=in_channels,
                            out_channels=hidden_channels,
                            kernel_size=(scale, scale),
                            stride=(scale, scale),
                            padding=(0, 0),
                            dilation=(1, 1),
                            bias=False),  #maybe nn interpolate is better
         nn.Conv2d(
             hidden_channels,
             out_channels,
             kernel_size=(3, 3),
             stride=(1, 1),
             padding=(1, 1),
             bias=False,
             groups=groups,
         ))
Esempio n. 14
0
 def __init__(self, config=reassemble_config, index=3, use_bn=False):
     super(Reassemble, self).__init__()
     img_size, input_features, features, s, output_features = (
         config["img_size"],
         config["input_features"],
         config["features"][index],
         config["s"][index],
         config["output_features"],
     )
     last_layer = nn.Identity()
     if s < 16:
         last_layer = nn.ConvTranspose2d(features,
                                         features,
                                         16 // s,
                                         stride=16 // s)
     elif s > 16:
         last_layer = nn.Conv2d(features,
                                features,
                                3,
                                stride=s // 16,
                                padding=1)
     self.conv = nn.Sequential(
         Transpose(1, 2),
         nn.Unflatten(2, torch.Size([img_size[0] // 16,
                                     img_size[1] // 16])),
         nn.Conv2d(input_features, features, 1, stride=1, padding=0),
         nn.ReLU(inplace=False),
         nn.BatchNorm2d(features) if use_bn else nn.Identity(),
         last_layer,
         nn.ReLU(inplace=False),
         nn.BatchNorm2d(features) if use_bn else nn.Identity(),
     )
     self.out = nn.Conv2d(features, output_features, 3, 1, 1, bias=False)
Esempio n. 15
0
 def __init__(self, hidden_dim=128):
     super(Generator, self).__init__()
     self.model = nn.Sequential(
         nn.Unflatten(1,
                      (hidden_dim, 1, 1, 1)),  # 1x1x1 (поменяли 1 на 64 ())
         nn.Upsample(
             size=(3, 4, 3),
             mode='trilinear',
         ),  #3x4x3
         nn.Conv3d(hidden_dim, hidden_dim, kernel_size=3, padding=1),
         nn.LeakyReLU(0.2),
         nn.BatchNorm3d(hidden_dim),
         nn.Upsample(size=(7, 8, 7), mode='trilinear'),  #3x4x3
         nn.Conv3d(hidden_dim, hidden_dim, kernel_size=3, padding=1),
         nn.LeakyReLU(0.2),
         nn.BatchNorm3d(hidden_dim),
         nn.Upsample(size=(14, 17, 14), mode='trilinear'),  #3x4x3
         nn.Conv3d(hidden_dim, hidden_dim, kernel_size=3, padding=1),
         nn.LeakyReLU(0.2),
         nn.BatchNorm3d(hidden_dim),
         nn.Upsample(size=(29, 35, 29), mode='trilinear'),  #3x4x3
         nn.Conv3d(hidden_dim, hidden_dim, kernel_size=3, padding=1),
         nn.LeakyReLU(0.2),
         nn.BatchNorm3d(hidden_dim),
         nn.Upsample(size=(58, 70, 58), mode='trilinear'),  #3x4x3
         nn.Conv3d(hidden_dim, hidden_dim, kernel_size=3, padding=1),
         nn.LeakyReLU(0.2),
         nn.BatchNorm3d(hidden_dim),
         nn.Conv3d(hidden_dim, 1, kernel_size=3, padding=1),
     )
Esempio n. 16
0
def build_dc_generator(noise_dim=NOISE_DIM):
    """
    Build and return a PyTorch model implementing the DCGAN generator using
    the architecture described above.
    """

    ##############################################################################
    # TODO: Implement architecture                                               #
    #                                                                            #
    # HINT: nn.Sequential might be helpful.                                      #
    ##############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    model =nn.Sequential(
      nn.Linear(noise_dim,1024),
      nn.ReLU(),
      nn.BatchNorm1d(1024),
      nn.Linear(1024, 7*7*128),
      nn.ReLU(),
      nn.BatchNorm1d(7*7*128),
      nn.Unflatten(1,(128,7,7)),
      nn.ConvTranspose2d(128,64,4,stride=2,padding=1),
      nn.ReLU(),
      nn.BatchNorm2d(64),
      nn.ConvTranspose2d(64,1,4,stride=2,padding=1),
      nn.Tanh(),
      nn.Flatten()
    )

    return model
    def __init__(self):
        super(ConvAutoEncoderTwo, self).__init__()
        self.encoder = nn.Sequential(OrderedDict([
            ('conv_1', nn.Conv2d(3, 16, 4, padding=1)),
            ('relu_1', nn.ReLU(True)),
            ('pooling_1', nn.MaxPool2d(2, 2)),
            ('conv_2', nn.Conv2d(16, 4, 3, padding=1)),
            ('relu_2', nn.ReLU(True)),
            ('pooling_2', nn.MaxPool2d(2, 2)),
            ('conv_3', nn.Conv2d(4, 4, 3, padding=1)),
            ('relu_3', nn.ReLU(True)),
            ('pooling_3', nn.MaxPool2d(2, 2)),
            ('conv_4', nn.Conv2d(4, 4, 3, padding=1)),
            ('relu_4', nn.ReLU(True)),
            ('pooling_4', nn.MaxPool2d(2, 2)),
            ('flatten', nn.Flatten()),
            ('dense', nn.Linear(4 * 13 * 13, 10))
        ]))

        self.decoder = nn.Sequential(
            nn.Linear(10, 4 * 13 * 13),
            nn.Unflatten(1, (4, 13, 13)),
            nn.MaxUnpool2d(self.encoder.pooling_4.size()),
            nn.Conv2d(4, 4, 3, stride=2),
            nn.ReLU(True),
            nn.MaxUnpool2d(self.encoder.pooling_3.size()),
            nn.Conv2d(4, 4, 3, stride=2),
            nn.ReLU(True),
            nn.MaxUnpool2d(self.encoder.pooling_2.size()),
            nn.Conv2d(16, 4, 3, padding=1),
            nn.ReLU(True),
            nn.MaxUnpool2d(self.encoder.pooling_1.size()),
            nn.Conv2d(3, 16, 4, padding=1)
        )
Esempio n. 18
0
 def __init__(self):
     super().__init__()
     self.latent_size = fc_layers[-1]
     new_h2, new_w2 = new_h, new_w
     layers = []
     inv_stacks = []
     if len(fc_layers) > 1:  # fully connected layers
         layers.append(MLP(list(reversed(fc_layers)), nn.ReLU, nn.ReLU))
         layers.append(nn.BatchNorm1d(new_h2 * new_w2 * channels[-1]))
     layers.append(nn.Unflatten(1, (channels[-1], new_h2, new_w2)))
     for i, (new_c, old_c, filter_size, pool) in reversed(
             list(
                 enumerate(
                     zip((c, ) + channels[:-1], channels, filter_sizes,
                         pools)))):
         inv_stacks.append(len(layers))
         if pool != 1:
             new_h2 *= pool
             new_w2 *= pool
             layers.append(
                 nn.Upsample(scale_factor=pool, mode="bilinear"))
         layers.append(nn.ConvTranspose2d(old_c, new_c, filter_size))
         new_h2 += filter_size - 1
         new_w2 += filter_size - 1
         if i != 0:  # only if not last layer
             layers.append(nn.ReLU())
             layers.append(nn.BatchNorm2d(new_c))
     layers.append(nn.Tanh())
     if new_h2 != h or new_w2 != w:
         layers.append(Interpolate((h, w)))
     self.stacks = []
     for i in reversed(inv_stacks):
         self.stacks.append(nn.Sequential(*layers[i:]))
     self.model = nn.Sequential(*layers)
    def __init__(self):
        super(ConvAutoEncoder, self).__init__()

        self.encoder = nn.Sequential(
            nn.Conv2d(3, 16, 4, padding=1),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(16, 4, 3, padding=1),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(4, 4, 3, padding=1),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(4, 4, 3, padding=1),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2),
            nn.Flatten(),
            nn.Linear(4 * 13 * 13, 10)
        )

        self.decoder = nn.Sequential(
            nn.Linear(10, 4 * 13 * 13),
            nn.Unflatten(1, (4, 13, 13)),
            nn.ConvTranspose2d(4, 4, 3, stride=2),
            nn.ReLU(True),
            nn.ConvTranspose2d(4, 4, 3, stride=2),
            nn.ReLU(True),
            nn.ConvTranspose2d(4, 16, 3, stride=2),
            nn.ReLU(True),
            nn.ConvTranspose2d(16, 3, 4, stride=2),
            nn.Sigmoid()
        )
    def __init__(self, coding_size, img_channels=3, hidden_dims=(16, 32, 16, 8, 2)):
        super(AutoEncoder, self).__init__()

        # encoder
        self.MaxPool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.Conv1 = BasicConvBlock(ch_in=img_channels, ch_out=hidden_dims[0])
        self.Conv2 = BasicConvBlock(ch_in=hidden_dims[0], ch_out=hidden_dims[1])
        self.Conv3 = BasicConvBlock(ch_in=hidden_dims[1], ch_out=hidden_dims[2])
        self.Conv4 = BasicConvBlock(ch_in=hidden_dims[2], ch_out=hidden_dims[3])
        self.Conv5 = BasicConvBlock(ch_in=hidden_dims[3], ch_out=hidden_dims[4])

        # decoder
        self.Up5 = UpConvBlock(ch_in=hidden_dims[4], ch_out=hidden_dims[3])
        self.Up4 = UpConvBlock(ch_in=hidden_dims[3], ch_out=hidden_dims[2])
        self.Up3 = UpConvBlock(ch_in=hidden_dims[2], ch_out=hidden_dims[1])
        self.Up2 = UpConvBlock(ch_in=hidden_dims[1], ch_out=hidden_dims[0])
        self.Up1 = nn.Conv2d(hidden_dims[0], img_channels, kernel_size=1, stride=1, padding=0)

        self.encoder = nn.Sequential(*[self.Conv1, self.MaxPool, self.Conv2, self.MaxPool,
                                       self.Conv3, self.MaxPool, self.Conv4, self.MaxPool, self.Conv5, nn.Flatten(),
                                       nn.Linear(392, coding_size)])
        self.decoder = nn.Sequential(
            *[nn.Linear(coding_size, 392), nn.Unflatten(1, (2, 14, 14)), self.Up5, self.Up4, self.Up3, self.Up2,
              self.Up1])
        init_weights(self.encoder)
        init_weights(self.decoder)
Esempio n. 21
0
def build_dc_generator(noise_dim=NOISE_DIM):
    """
  Build and return a PyTorch nn.Sequential model implementing the DCGAN generator using
  the architecture described in the notebook.
  """
    model = None
    ############################################################################
    # TODO: Implement build_dc_generator.                                      #
    ############################################################################
    # Replace "pass" statement with your code

    model = torch.nn.Sequential(
        nn.Linear(noise_dim, 1024),
        nn.ReLU(),
        nn.BatchNorm1d(1024),
        nn.Linear(1024, 7 * 7 * 128),
        nn.ReLU(),
        nn.BatchNorm1d(7 * 7 * 128),
        nn.Unflatten(-1, (128, 7, 7)),
        nn.ConvTranspose2d(128, 64, (4, 4), stride=2,
                           padding=1),  # (128, 7, 7) -> (64, 14, 14)
        nn.ReLU(),
        nn.BatchNorm2d(64),
        nn.ConvTranspose2d(64, 1, (4, 4), stride=2,
                           padding=1),  # (64, 14, 14) -> (1, 28, 28)
        nn.Tanh(),
        nn.Flatten())

    ############################################################################
    #                             END OF YOUR CODE                             #
    ############################################################################

    return model
Esempio n. 22
0
 def __init__(self):
     super().__init__()
     self.encoder = nn.Sequential(
         # 28 x 28
         nn.Conv2d(1, 4, kernel_size=5),
         # 4 x 24 x 24
         nn.ReLU(True),
         nn.Conv2d(4, 8, kernel_size=5),
         nn.ReLU(True),
         # 8 x 20 x 20 = 3200
         nn.Flatten(),
         nn.Linear(3200, 10),
         # 10
         nn.Softmax())
     self.decoder = nn.Sequential(
         # 10
         nn.Linear(10, 400),
         # 400
         nn.ReLU(True),
         nn.Linear(400, 4000),
         # 4000
         nn.ReLU(True),
         nn.Unflatten(1, (10, 20, 20)),
         # 10 x 20 x 20
         nn.ConvTranspose2d(10, 10, kernel_size=5),
         # 24 x 24
         nn.ConvTranspose2d(10, 1, kernel_size=5),
         # 28 x 28
         nn.Sigmoid())
Esempio n. 23
0
def build_dc_classifier():
    """
  Build and return a PyTorch nn.Sequential model for the DCGAN discriminator implementing
  the architecture in the notebook.
  """
    model = None
    ############################################################################
    # TODO: Implement build_dc_classifier.                                     #
    ############################################################################
    # Replace "pass" statement with your code

    # H' = 1 + (H + 2 * pad - HH) / stride
    # W' = 1 + (W + 2 * pad - WW) / stride
    model = torch.nn.Sequential(
        nn.Unflatten(-1, (1, 28, 28)),
        nn.Conv2d(1, 32, (5, 5), stride=1),
        nn.LeakyReLU(0.1),
        nn.MaxPool2d((2, 2), stride=2),  # (32, 24, 24) -> (32, 12, 12)
        nn.Conv2d(32, 64, (5, 5), stride=1),  # (32, 12, 12) -> (64, 8, 8)
        nn.LeakyReLU(0.1),
        nn.MaxPool2d((2, 2), stride=2),  # (32, 8, 8) -> (64, 4, 4)
        nn.Flatten(),
        nn.Linear(4 * 4 * 64, 4 * 4 * 64),
        nn.LeakyReLU(0.1),
        nn.Linear(4 * 4 * 64, 1))

    ############################################################################
    #                             END OF YOUR CODE                             #
    ############################################################################

    return model
Esempio n. 24
0
    def __init__(self, **config):
        self.input_shape = config.get("input_shape", (10, ))
        self.output_shape = config.get("output_shape", (1, 28, 28))
        self.learning_rate = config.get("learning_rate", 0.01)
        self.momentum = config.get("momentum", 0.5)
        self.log_interval = config.get("log_interval", 10)

        with self.setup(input_shape=self.input_shape,
                        output_shape=self.output_shape):
            self.add_module("fn1", nn.Linear(self.size_of_last_layer, 400))
            self.add_module("fn1_activation", nn.ReLU(True))

            self.add_module("fn2", nn.Linear(self.size_of_last_layer, 4000))
            self.add_module("fn2_activation", nn.ReLU(True))

            conv1_shape = [
                10, 20, 20
            ]  # needs to mupltiply together to be the size of the previous layer (currently 4000)
            conv2_size = 10
            self.add_module("conv1_prep", nn.Unflatten(1, conv1_shape))
            self.add_module(
                "conv1",
                nn.ConvTranspose2d(conv1_shape[0], conv2_size, kernel_size=5))
            self.add_module("conv2",
                            nn.ConvTranspose2d(conv2_size, 1, kernel_size=5))
            self.add_module("conv2_activation", nn.Sigmoid())

            self.loss_function = nn.MSELoss()
            self.optimizer = torch.optim.SGD(self.parameters(),
                                             lr=self.learning_rate,
                                             momentum=self.momentum)
def build_dc_generator(noise_dim=NOISE_DIM):
  """
  Build and return a PyTorch nn.Sequential model implementing the DCGAN generator using
  the architecture described in the notebook.
  """
  model = None
  ############################################################################
  # TODO: Implement build_dc_generator.                                      #
  ############################################################################
  # Replace "pass" statement with your code
  model = nn.Sequential(
    nn.Linear(in_features=noise_dim, out_features=1024),
    nn.ReLU(),
    nn.BatchNorm1d(1024),
    nn.Linear(in_features=1024, out_features=7*7*128),
    nn.ReLU(),
    nn.BatchNorm1d(7*7*128),
    nn.Unflatten(-1, (128, 7, 7)),
    nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=4, stride=2, padding=1),
    nn.ReLU(),
    nn.BatchNorm2d(64),
    nn.ConvTranspose2d(in_channels=64, out_channels=1, kernel_size=4, stride=2, padding=1),
    nn.Tanh(),
    nn.Flatten() 
  )
  ############################################################################
  #                             END OF YOUR CODE                             #
  ############################################################################

  return model
def build_dc_classifier():
  """
  Build and return a PyTorch nn.Sequential model for the DCGAN discriminator implementing
  the architecture in the notebook.
  """
  model = None
  ############################################################################
  # TODO: Implement build_dc_classifier.                                     #
  ############################################################################
  # Replace "pass" statement with your code
  model = nn.Sequential(
    nn.Unflatten(-1, (1, 28, 28)),
    nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5, stride=1),
    nn.LeakyReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1),
    nn.LeakyReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Flatten(),
    nn.Linear(in_features=4*4*64 , out_features=4*4*64),
    nn.LeakyReLU(),
    nn.Linear(in_features=4*4*64, out_features=1)
  )

  ############################################################################
  #                             END OF YOUR CODE                             #
  ############################################################################

  return model
Esempio n. 27
0
 def __init__(
     self,
     hidden_dim,
     image_size,
     image_channels=3,
     max_filters=512,
     num_layers=4,
     kernel_size=2,
     stride=2,
     padding=0,
     latent_dim=128,
     small_conv=False,
 ):
     super(VAEGANDecoder, self).__init__()
     if small_conv:
         num_layers += 1
     channel_sizes = self.calculate_channel_sizes(
         image_channels, max_filters, num_layers
     )
     # Decoder
     decoder_layers = nn.ModuleList()
     # Feedforward/Dense Layer to expand our latent dimensions
     decoder_layers.append(nn.Linear(latent_dim, hidden_dim))
     # Unflatten to a shape of (Channels, Height, Width)
     decoder_layers.append(nn.Unflatten(1, (max_filters, image_size, image_size)))
     # Decoder Convolutions
     for i, (out_channels, in_channels) in enumerate(channel_sizes[::-1]):
         if small_conv and i == num_layers - 1:
             # 1x1 Transposed Convolution
             decoder_layers.append(
                 nn.ConvTranspose2d(
                     in_channels=in_channels,
                     out_channels=out_channels,
                     kernel_size=1,
                     stride=1,
                     padding=0,
                 )
             )
         else:
             # Add Transposed Convolutional Layer
             decoder_layers.append(
                 nn.ConvTranspose2d(
                     in_channels=in_channels,
                     out_channels=out_channels,
                     kernel_size=kernel_size,
                     stride=stride,
                     padding=padding,
                     bias=False,
                 )
             )
         # Batch Norm
         decoder_layers.append(nn.BatchNorm2d(out_channels))
         # ReLU if not final layer
         if i != num_layers - 1:
             decoder_layers.append(nn.ReLU())
         # Sigmoid if final layer
         else:
             decoder_layers.append(nn.Sigmoid())
     self.decoder = nn.Sequential(*decoder_layers)
Esempio n. 28
0
    def __init__(self, input_size, latent_size=15):
        super(VAE, self).__init__()
        self.input_size = input_size # H*W
        self.latent_size = latent_size # Z
        self.hidden_dim = None # H_d
        self.encoder = None
        self.mu_layer = None
        self.logvar_layer = None
        self.decoder = None

        ############################################################################################
        # TODO: Implement the fully-connected encoder architecture described in the notebook.      #
        # Specifically, self.encoder should be a network that inputs a batch of input images of    #
        # shape (N, 1, H, W) into a batch of hidden features of shape (N, H_d). Set up             #
        # self.mu_layer and self.logvar_layer to be a pair of linear layers that map the hidden    #
        # features into estimates of the mean and log-variance of the posterior over the latent    #
        # vectors; the mean and log-variance estimates will both be tensors of shape (N, Z).       #
        ############################################################################################
        # Replace "pass" statement with your code

        # Define the hidden dimension as shown in course's lecture example.
        # Source: https://web.eecs.umich.edu/~justincj/slides/eecs498/498_FA2019_lecture20.pdf
        # Slide: 12/120.
        self.hidden_dim = 400

        # Define the encoder: A three Linear+ReLU layers neural network.
        self.encoder = nn.Sequential(
          nn.Flatten(),
          nn.Linear(self.input_size, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU()
        )

        # Define the mu_layer, with input (N, H_d) and output (N, Z)
        self.mu_layer = nn.Linear(self.hidden_dim, self.latent_size)
        # Define the logvar_layer, with input (N, H_d) and output (N, Z)
        self.logvar_layer = nn.Linear(self.hidden_dim, self.latent_size)

        ############################################################################################
        # TODO: Implement the fully-connected decoder architecture described in the notebook.      #
        # Specifically, self.decoder should be a network that inputs a batch of latent vectors of  #
        # shape (N, Z) and outputs a tensor of estimated images of shape (N, 1, H, W).             #
        ############################################################################################
        # Replace "pass" statement with your code

        self.decoder = nn.Sequential(
          nn.Linear(self.latent_size, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.input_size),
          nn.Sigmoid(),
          nn.Unflatten(dim=1, unflattened_size=(1, 28, 28))
        )
Esempio n. 29
0
    def __init__(self, input_size, num_classes=10, latent_size=15):
        super(CVAE, self).__init__()
        self.input_size = input_size # H*W
        self.latent_size = latent_size # Z
        self.num_classes = num_classes # C
        self.hidden_dim = None # H_d
        self.encoder = None
        self.mu_layer = None
        self.logvar_layer = None
        self.decoder = None

        ############################################################################################
        # TODO: Define a FC encoder as described in the notebook that transforms the image--after  #
        # flattening and now adding our one-hot class vector (N, H*W + C)--into a hidden_dimension #               #
        # (N, H_d) feature space, and a final two layers that project that feature space           #
        # to posterior mu and posterior log-variance estimates of the latent space (N, Z)          #
        ############################################################################################
        # Replace "pass" statement with your code
        
        # Define the hidden dimension as shown in course's lecture example.
        # Source: https://web.eecs.umich.edu/~justincj/slides/eecs498/498_FA2019_lecture20.pdf
        # Slide: 12/120.
        self.hidden_dim = 400

        # Define the encoder: A three Linear+ReLU layers neural network.
        # The encoder's input has shape of (N, H*W + C), i.e. The flattened images and their classes.
        # Note that the concatenation (between images and classes) is done in the "forward" function.
        self.encoder = nn.Sequential(
          nn.Linear(self.input_size + self.num_classes, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU()
        )

        # Define the mu_layer, with input (N, H_d) and output (N, Z)
        self.mu_layer = nn.Linear(self.hidden_dim, self.latent_size)
        # Define the logvar_layer, with input (N, H_d) and output (N, Z)
        self.logvar_layer = nn.Linear(self.hidden_dim, self.latent_size)

        ############################################################################################
        # TODO: Define a fully-connected decoder as described in the notebook that transforms the  #
        # latent space (N, Z + C) to the estimated images of shape (N, 1, H, W).                   #
        ############################################################################################
        # Replace "pass" statement with your code

        self.decoder = nn.Sequential(
          nn.Linear(self.latent_size + self.num_classes, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.hidden_dim),
          nn.ReLU(),
          nn.Linear(self.hidden_dim, self.input_size),
          nn.Sigmoid(),
          nn.Unflatten(dim=1, unflattened_size=(1, 28, 28))
        )
Esempio n. 30
0
def ifft2d(n1: int,
           n2: int,
           normalized: bool = False,
           br_first: bool = True,
           with_br_perm: bool = True,
           flatten=False) -> nn.Module:
    """ Construct an nn.Module based on Butterfly that exactly performs the 2D iFFT.
    Parameters:
        n1: size of the iFFT on the last input dimension. Must be a power of 2.
        n2: size of the iFFT on the second to last input dimension. Must be a power of 2.
        normalized: if True, corresponds to the unitary iFFT (i.e. multiplied by 1/sqrt(n))
        br_first: which decomposition of iFFT. True corresponds to decimation-in-frequency.
                  False corresponds to decimation-in-time.
        with_br_perm: whether to return both the butterfly and the bit reversal permutation.
        flatten: whether to combine the 2 butterflies into 1 with Kronecker product.
    """
    b_ifft1 = ifft(n1,
                   normalized=normalized,
                   br_first=br_first,
                   with_br_perm=False)
    b_ifft2 = ifft(n2,
                   normalized=normalized,
                   br_first=br_first,
                   with_br_perm=False)
    b = TensorProduct(b_ifft1,
                      b_ifft2) if not flatten else butterfly_kronecker(
                          b_ifft1, b_ifft2)
    if with_br_perm:
        br_perm1 = FixedPermutation(
            bitreversal_permutation(n1, pytorch_format=True))
        br_perm2 = FixedPermutation(
            bitreversal_permutation(n2, pytorch_format=True))
        br_perm = (TensorProduct(br_perm1, br_perm2) if not flatten else
                   permutation_kronecker(br_perm1, br_perm2))
        if not flatten:
            return nn.Sequential(br_perm, b) if br_first else nn.Sequential(
                b, br_perm)
        else:
            return (nn.Sequential(nn.Flatten(
                start_dim=-2), br_perm, b, nn.Unflatten(-1, (n2, n1)))
                    if br_first else nn.Sequential(nn.Flatten(
                        start_dim=-2), b, br_perm, nn.Unflatten(-1, (n2, n1))))
    else:
        return b if not flatten else nn.Sequential(nn.Flatten(
            start_dim=-2), b, nn.Unflatten(-1, (n2, n1)))