Esempio n. 1
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)

        # Here we define the layers of our network

        # Seventh convolutional layer
        self.conv7 = nn.Conv2d(
            self.get_input_shape()[0], 384,
            3)  # 256 input channels, 384 output channels, 3x3 convolutions
        self.bn7 = nn.BatchNorm2d(384)  # Batch Norm layer
        # Eightth convolutional layer
        self.conv8 = nn.Conv2d(
            384, 512,
            3)  # 384 input channels, 512 output channels, 3x3 convolutions
        self.bn8 = nn.BatchNorm2d(512)  # Batch Norm layer

        self.CONV_OUTPUT_SIZE = utils.shape2size(
            utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT]))

        # FC Layers
        self.fc9 = nn.Linear(
            self.CONV_OUTPUT_SIZE, 4096
        )  # conv_output_size-dimensional input, 4096-dimensional output
        self.bn9 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.fc10 = nn.Linear(
            4096, self.NUM_CLASSES
        )  # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
Esempio n. 2
0
def gauss(x, w, sigma=None):
    # Serialized version of distance computation using less memory
    x_unf = unfold_map2d(x, w.size(2), w.size(3))
    d = torch.zeros(x_unf.size(0),
                    w.size(0),
                    x_unf.size(2),
                    x_unf.size(3),
                    device=x.device)  # batch, out-ch, height, width
    for i in range(
            w.size(0) // P.HEBB_UPD_GRP +
        (1 if w.size(0) % P.HEBB_UPD_GRP != 0 else 0)):
        start = i * P.HEBB_UPD_GRP
        end = min((i + 1) * P.HEBB_UPD_GRP, w.size(0))
        w_i = w[start:end, :, :, :]
        w_i = w_i.view(1, w_i.size(0), 1, 1,
                       -1)  # batch, out-ch, height, width, filter
        d[:, start:end, :, :] = torch.norm(
            x_unf - w_i, p=2, dim=4
        )  # w_i broadcast over x_unf batch, height and width dims, x_unf broadcast over w out_ch dim
    if sigma is None:
        return torch.exp(-d.pow(2) / (2 * utils.shape2size(tuple(w[0].size())))
                         )  # heuristic: use number of dimensions as variance
    # if sigma is None: return torch.exp(-d.pow(2) / (2 * torch.norm(w.view(w.size(0), 1, -1) - w.view(1, w.size(0), -1), p=2, dim=2).max().pow(2)/(w.size(0)**(2/(utils.shape2size(tuple(w[0].size()))))))) # heuristic: normalization condition
    # if sigma is None: return torch.exp(-d.pow(2) / (2 * d.mean().pow(2)))
    return torch.exp(-d.pow(2) / (2 * (sigma.view(1, -1, 1, 1).pow(2))))
Esempio n. 3
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)

        # Here we define the layers of our network

        # Fourth convolutional layer
        self.conv4 = nn.Conv2d(
            self.get_input_shape()[0], 256,
            3)  # 192 input channels, 256 output channels, 3x3 convolutions
        self.bn4 = nn.BatchNorm2d(256)  # Batch Norm layer

        self.CONV_OUTPUT_SIZE = utils.shape2size(
            self.get_output_fmap_shape(self.CONV_OUTPUT))

        # FC Layers
        self.fc5 = nn.Linear(
            self.CONV_OUTPUT_SIZE, 4096
        )  # conv_output_size-dimensional input, 4096-dimensional output
        self.bn5 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.fc6 = nn.Linear(
            4096, self.NUM_CLASSES
        )  # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
Esempio n. 4
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)

        # Here we define the layers of our network

        # First convolutional layer
        self.conv1 = nn.Conv2d(
            3, 96, 7)  # 3 input channels, 96 output channels, 7x7 convolutions
        self.bn1 = nn.BatchNorm2d(96)  # Batch Norm layer
        # Second convolutional layer
        self.conv2 = nn.Conv2d(
            96, 128,
            3)  # 96 input channels, 128 output channels, 3x3 convolutions
        self.bn2 = nn.BatchNorm2d(128)  # Batch Norm layer
        # Third convolutional layer
        self.conv3 = nn.Conv2d(
            128, 192,
            3)  # 128 input channels, 192 output channels, 3x3 convolutions
        self.bn3 = nn.BatchNorm2d(192)  # Batch Norm layer
        # Fourth convolutional layer
        self.conv4 = nn.Conv2d(
            192, 192,
            3)  # 192 input channels, 192 output channels, 3x3 convolutions
        self.bn4 = nn.BatchNorm2d(192)  # Batch Norm layer
        # Fifth convolutional layer
        self.conv5 = nn.Conv2d(
            192, 256,
            3)  # 192 input channels, 256 output channels, 3x3 convolutions
        self.bn5 = nn.BatchNorm2d(256)  # Batch Norm layer
        # Sixth convolutional layer
        self.conv6 = nn.Conv2d(
            256, 256,
            3)  # 256 input channels, 256 output channels, 3x3 convolutions
        self.bn6 = nn.BatchNorm2d(256)  # Batch Norm layer
        # Seventh convolutional layer
        self.conv7 = nn.Conv2d(
            256, 384,
            3)  # 256 input channels, 384 output channels, 3x3 convolutions
        self.bn7 = nn.BatchNorm2d(384)  # Batch Norm layer
        # Eightth convolutional layer
        self.conv8 = nn.Conv2d(
            384, 512,
            3)  # 384 input channels, 512 output channels, 3x3 convolutions
        self.bn8 = nn.BatchNorm2d(512)  # Batch Norm layer

        self.CONV_OUTPUT_SIZE = utils.shape2size(
            utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT]))

        # FC Layers
        self.fc9 = nn.Linear(
            self.CONV_OUTPUT_SIZE, 4096
        )  # conv_output_size-dimensional input, 4096-dimensional output
        self.bn9 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.fc10 = nn.Linear(
            4096, self.NUM_CLASSES
        )  # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
Esempio n. 5
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.INPUT_SIZE = utils.shape2size(self.get_input_shape())
        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)

        # FC Layers
        self.fc = nn.Linear(
            self.INPUT_SIZE, self.NUM_CLASSES
        )  # input_size-dimensional input, NUM_CLASSES-dimensional output (one per class)
Esempio n. 6
0
	def __init__(self, config, input_shape=None):
		super(Net, self).__init__(config, input_shape)
		
		self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][P.KEY_DS_NUM_CLASSES]
		self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)
		
		# Here we define the layers of our network
		
		# First convolutional layer
		self.conv1 = nn.Conv2d(3, 96, 5) # 3 input channels, 96 output channels, 5x5 convolutions
		self.bn1 = nn.BatchNorm2d(96) # Batch Norm layer
		
		self.CONV_OUTPUT_SIZE = utils.shape2size(utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT]))
		
		# FC Layers
		self.fc2 = nn.Linear(self.CONV_OUTPUT_SIZE, self.NUM_CLASSES) # conv_output_shape-dimensional input, 10-dimensional output (one per class)
Esempio n. 7
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)
        self.INPUT_SIZE = utils.shape2size(self.get_input_shape())

        # Here we define the layers of our network

        # FC Layers
        self.fc1 = nn.Linear(
            self.INPUT_SIZE,
            4096)  # input_size-dimensional input, 4096-dimensional output
        self.bn1 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.fc2 = nn.Linear(
            4096, self.NUM_CLASSES
        )  # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
	def __init__(self, config, input_shape=None):
		super(Net, self).__init__(config, input_shape)
		
		self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][P.KEY_DS_NUM_CLASSES]
		self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)
		self.NUM_LATENT_VARS = config.CONFIG_OPTIONS.get(PP.KEY_VAE_NUM_LATENT_VARS, 256)
		self.ELBO_BETA = config.CONFIG_OPTIONS.get(P.KEY_ELBO_BETA, 1.)
		self.ALPHA_L = config.CONFIG_OPTIONS.get(P.KEY_ALPHA_L, 1.)
		self.ALPHA_G = config.CONFIG_OPTIONS.get(P.KEY_ALPHA_G, 0.)
		
		# Here we define the layers of our network and the variables to store internal gradients
		
		# First convolutional layer
		self.conv1 = nn.Conv2d(3, 96, 5) # 3 input chennels, 96 output channels, 5x5 convolutions
		self.bn1 = nn.BatchNorm2d(96) # Batch Norm layer
		self.conv1_delta_w = torch.zeros_like(self.conv1.weight)
		self.conv1_delta_bias = torch.zeros_like(self.conv1.bias)
		self.bn1_delta_w = torch.zeros_like(self.bn1.weight)
		self.bn1_delta_bias = torch.zeros_like(self.bn1.bias)
		# Second convolutional layer
		self.conv2 = nn.Conv2d(96, 128, 3) # 96 input chennels, 128 output channels, 3x3 convolutions
		self.bn2 = nn.BatchNorm2d(128) # Batch Norm layer
		self.conv2_delta_w = torch.zeros_like(self.conv2.weight)
		self.conv2_delta_bias = torch.zeros_like(self.conv2.bias)
		self.bn2_delta_w = torch.zeros_like(self.bn2.weight)
		self.bn2_delta_bias = torch.zeros_like(self.bn2.bias)
		# Third convolutional layer
		self.conv3 = nn.Conv2d(128, 192, 3)  # 128 input chennels, 192 output channels, 3x3 convolutions
		self.bn3 = nn.BatchNorm2d(192) # Batch Norm layer
		self.conv3_delta_w = torch.zeros_like(self.conv3.weight)
		self.conv3_delta_bias = torch.zeros_like(self.conv3.bias)
		self.bn3_delta_w = torch.zeros_like(self.bn3.weight)
		self.bn3_delta_bias = torch.zeros_like(self.bn3.bias)
		# Fourth convolutional layer
		self.conv4 = nn.Conv2d(192, 256, 3)  # 192 input chennels, 256 output channels, 3x3 convolutions
		self.bn4 = nn.BatchNorm2d(256) # Batch Norm layer
		self.conv4_delta_w = torch.zeros_like(self.conv4.weight)
		self.conv4_delta_bias = torch.zeros_like(self.conv4.bias)
		self.bn4_delta_w = torch.zeros_like(self.bn4.weight)
		self.bn4_delta_bias = torch.zeros_like(self.bn4.bias)
		
		self.OUTPUT_FMAP_SHAPE = {k: utils.tens2shape(v) for k, v in self.get_dummy_fmap().items() if isinstance(v, torch.Tensor)}
		self.OUTPUT_FMAP_SIZE = {k: utils.shape2size(self.OUTPUT_FMAP_SHAPE[k]) for k in self.OUTPUT_FMAP_SHAPE.keys()}
		self.CONV_OUTPUT_SIZE = self.OUTPUT_FMAP_SIZE[self.CONV_OUTPUT]
		
		# FC Layers
		self.fc5 = nn.Linear(self.CONV_OUTPUT_SIZE, 4096) # conv_output_size-dimensional input, 4096-dimensional output
		self.bn5 = nn.BatchNorm1d(4096) # Batch Norm layer
		self.fc5_delta_w = torch.zeros_like(self.fc5.weight)
		self.fc5_delta_bias = torch.zeros_like(self.fc5.bias)
		self.bn5_delta_w = torch.zeros_like(self.bn5.weight)
		self.bn5_delta_bias = torch.zeros_like(self.bn5.bias)
		self.fc6 = nn.Linear(4096, self.NUM_CLASSES) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
		
		# Latent variable mapping layers
		self.fc_mu1 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN1], self.NUM_LATENT_VARS)  # bn1_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_var1 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN1], self.NUM_LATENT_VARS)  # bn1_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_mu1_delta_w = torch.zeros_like(self.fc_mu1.weight)
		self.fc_mu1_delta_bias = torch.zeros_like(self.fc_mu1.bias)
		self.fc_var1_delta_w = torch.zeros_like(self.fc_var1.weight)
		self.fc_var1_delta_bias = torch.zeros_like(self.fc_var1.bias)
		self.fc_mu2 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN2], self.NUM_LATENT_VARS)  # bn2_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_var2 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN2], self.NUM_LATENT_VARS)  # bn2_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_mu2_delta_w = torch.zeros_like(self.fc_mu2.weight)
		self.fc_mu2_delta_bias = torch.zeros_like(self.fc_mu2.bias)
		self.fc_var2_delta_w = torch.zeros_like(self.fc_var2.weight)
		self.fc_var2_delta_bias = torch.zeros_like(self.fc_var2.bias)
		self.fc_mu3 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN3], self.NUM_LATENT_VARS)  # bn3_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_var3 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN3], self.NUM_LATENT_VARS)  # bn3_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_mu3_delta_w = torch.zeros_like(self.fc_mu3.weight)
		self.fc_mu3_delta_bias = torch.zeros_like(self.fc_mu3.bias)
		self.fc_var3_delta_w = torch.zeros_like(self.fc_var3.weight)
		self.fc_var3_delta_bias = torch.zeros_like(self.fc_var3.bias)
		self.fc_mu4 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN4], self.NUM_LATENT_VARS)  # bn4_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_var4 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN4], self.NUM_LATENT_VARS)  # bn4_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_mu4_delta_w = torch.zeros_like(self.fc_mu4.weight)
		self.fc_mu4_delta_bias = torch.zeros_like(self.fc_mu4.bias)
		self.fc_var4_delta_w = torch.zeros_like(self.fc_var4.weight)
		self.fc_var4_delta_bias = torch.zeros_like(self.fc_var4.bias)
		self.fc_mu5 = nn.Linear(4096, self.NUM_LATENT_VARS)  # 4096-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_var5 = nn.Linear(4096, self.NUM_LATENT_VARS)  # 4096-dimensional input, NUM_LATENT_VARS-dimensional output
		self.fc_mu5_delta_w = torch.zeros_like(self.fc_mu5.weight)
		self.fc_mu5_delta_bias = torch.zeros_like(self.fc_mu5.bias)
		self.fc_var5_delta_w = torch.zeros_like(self.fc_var5.weight)
		self.fc_var5_delta_bias = torch.zeros_like(self.fc_var5.bias)
		
		# Decoding Layers
		self.dec_fc0 = nn.Linear(self.NUM_LATENT_VARS, 4096)  # NUM_LATENT_VARS-dimensional input, 4096-dimensional output
		self.dec_bn0 = nn.BatchNorm1d(4096)  # Batch Norm layer
		self.dec_fc1 = nn.Linear(4096, self.CONV_OUTPUT_SIZE)  # 4096-dimensional input, CONV_OUTPUT_SIZE-dimensional output
		self.dec_fc0_delta_w = torch.zeros_like(self.dec_fc0.weight)
		self.dec_fc0_delta_bias = torch.zeros_like(self.dec_fc0.bias)
		self.dec_bn0_delta_w = torch.zeros_like(self.dec_bn0.weight)
		self.dec_bn0_delta_bias = torch.zeros_like(self.dec_bn0.bias)
		self.dec_fc1_delta_w = torch.zeros_like(self.dec_fc1.weight)
		self.dec_fc1_delta_bias = torch.zeros_like(self.dec_fc1.bias)
		self.dec_fc2 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN4])  # NUM_LATENT_VARS-dimensional input, bn4_output_size-dimensional output
		self.dec_bn2 = nn.BatchNorm2d(256)  # Batch Norm layer
		self.dec_conv2 = nn.ConvTranspose2d(256, 192, 3) # 256 input chennels, 192 output channels, 3x3 transpose convolutions
		self.dec_fc2_delta_w = torch.zeros_like(self.dec_fc2.weight)
		self.dec_fc2_delta_bias = torch.zeros_like(self.dec_fc2.bias)
		self.dec_bn2_delta_w = torch.zeros_like(self.dec_bn2.weight)
		self.dec_bn2_delta_bias = torch.zeros_like(self.dec_bn2.bias)
		self.dec_conv2_delta_w = torch.zeros_like(self.dec_conv2.weight)
		self.dec_conv2_delta_bias = torch.zeros_like(self.dec_conv2.bias)
		self.dec_fc3 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN3])  # NUM_LATENT_VARS-dimensional input, bn3_output_size-dimensional output
		self.dec_bn3 = nn.BatchNorm2d(192)  # Batch Norm layer
		self.dec_conv3 = nn.ConvTranspose2d(192, 128, 3) # 192 input chennels, 128 output channels, 3x3 transpose convolutions
		self.dec_fc3_delta_w = torch.zeros_like(self.dec_fc3.weight)
		self.dec_fc3_delta_bias = torch.zeros_like(self.dec_fc3.bias)
		self.dec_bn3_delta_w = torch.zeros_like(self.dec_bn3.weight)
		self.dec_bn3_delta_bias = torch.zeros_like(self.dec_bn3.bias)
		self.dec_conv3_delta_w = torch.zeros_like(self.dec_conv3.weight)
		self.dec_conv3_delta_bias = torch.zeros_like(self.dec_conv3.bias)
		self.dec_fc4 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN2])  # NUM_LATENT_VARS-dimensional input, bn2_output_size-dimensional output
		self.dec_bn4 = nn.BatchNorm2d(128)  # Batch Norm layer
		self.dec_conv4 = nn.ConvTranspose2d(128, 96, 3) # 128 input chennels, 96 output channels, 3x3 transpose convolutions
		self.dec_fc4_delta_w = torch.zeros_like(self.dec_fc4.weight)
		self.dec_fc4_delta_bias = torch.zeros_like(self.dec_fc4.bias)
		self.dec_bn4_delta_w = torch.zeros_like(self.dec_bn4.weight)
		self.dec_bn4_delta_bias = torch.zeros_like(self.dec_bn4.bias)
		self.dec_conv4_delta_w = torch.zeros_like(self.dec_conv4.weight)
		self.dec_conv4_delta_bias = torch.zeros_like(self.dec_conv4.bias)
		self.dec_fc5 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN1])  # NUM_LATENT_VARS-dimensional input, bn1_output_size-dimensional output
		self.dec_bn5 = nn.BatchNorm2d(96)  # Batch Norm layer
		self.dec_conv5 = nn.ConvTranspose2d(96, 3, 5) # 96 input chennels, 3 output channels, 5x5 transpose convolutions
		self.dec_fc5_delta_w = torch.zeros_like(self.dec_fc5.weight)
		self.dec_fc5_delta_bias = torch.zeros_like(self.dec_fc5.bias)
		self.dec_bn5_delta_w = torch.zeros_like(self.dec_bn5.weight)
		self.dec_bn5_delta_bias = torch.zeros_like(self.dec_bn5.bias)
		self.dec_conv5_delta_w = torch.zeros_like(self.dec_conv5.weight)
		self.dec_conv5_delta_bias = torch.zeros_like(self.dec_conv5.bias)
		
		# Internal ELBO loss function
		self.loss = ELBOMetric(self.ELBO_BETA)
Esempio n. 9
0
def nc_base(w):
    return w.size(0)**(1 / (utils.shape2size(tuple(w[0].size()))))
Esempio n. 10
0
def weight_init_std(w):
    stdv = 1 / (utils.shape2size(w[0].size()))**0.5
    torch.nn.init.uniform_(w, -stdv, stdv)
    return w
Esempio n. 11
0
def nc_raised_ang_sim(w):
    return 1 / (-math.log(1 - 1 / (w.size(0)**
                                   (1 /
                                    (utils.shape2size(tuple(w[0].size())))))))
Esempio n. 12
0
def nc_raised_cos_sim(w):
    return 1 / (-math.log(
        (1 + torch.cos(1 / (w.size(0)**
                            (1 /
                             (utils.shape2size(tuple(w[0].size()))))))) / 2))
Esempio n. 13
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)

        # Here we define the layers of our network

        # First convolutional layer
        self.conv1 = nn.Conv2d(
            3, 96, 7)  # 3 input channels, 96 output channels, 7x7 convolutions
        self.bn1 = nn.BatchNorm2d(96)  # Batch Norm layer
        # Second convolutional layer
        self.conv2 = nn.Conv2d(
            96, 128,
            3)  # 96 input channels, 128 output channels, 3x3 convolutions
        self.bn2 = nn.BatchNorm2d(128)  # Batch Norm layer
        # Third convolutional layer
        self.conv3 = nn.Conv2d(
            128, 192,
            3)  # 128 input channels, 192 output channels, 3x3 convolutions
        self.bn3 = nn.BatchNorm2d(192)  # Batch Norm layer
        # Fourth convolutional layer
        self.conv4 = nn.Conv2d(
            192, 192,
            3)  # 192 input channels, 192 output channels, 3x3 convolutions
        self.bn4 = nn.BatchNorm2d(192)  # Batch Norm layer
        # Fifth convolutional layer
        self.conv5 = nn.Conv2d(
            192, 256,
            3)  # 192 input channels, 256 output channels, 3x3 convolutions
        self.bn5 = nn.BatchNorm2d(256)  # Batch Norm layer
        # Sixth convolutional layer
        self.conv6 = nn.Conv2d(
            256, 256,
            3)  # 256 input channels, 256 output channels, 3x3 convolutions
        self.bn6 = nn.BatchNorm2d(256)  # Batch Norm layer
        # Seventh convolutional layer
        self.conv7 = nn.Conv2d(
            256, 384,
            3)  # 256 input channels, 384 output channels, 3x3 convolutions
        self.bn7 = nn.BatchNorm2d(384)  # Batch Norm layer
        # Eightth convolutional layer
        self.conv8 = nn.Conv2d(
            384, 512,
            3)  # 384 input channels, 512 output channels, 3x3 convolutions
        self.bn8 = nn.BatchNorm2d(512)  # Batch Norm layer

        self.CONV_OUTPUT_SHAPE = self.get_output_fmap_shape(self.CONV_OUTPUT)
        self.CONV_OUTPUT_SIZE = utils.shape2size(self.CONV_OUTPUT_SHAPE)

        # FC Layers
        self.fc9 = nn.Linear(
            self.CONV_OUTPUT_SIZE, 4096
        )  # conv_output_size-dimensional input, 4096-dimensional output
        self.bn9 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.fc_mu = nn.Linear(
            4096, self.NUM_LATENT_VARS
        )  # conv_output_size-dimensional input, NUM_LATENT_VARS-dimensional output
        self.fc_var = nn.Linear(
            4096, self.NUM_LATENT_VARS
        )  # conv_output_size-dimensional input, NUM_LATENT_VARS-dimensional output

        # Decoding Layers
        self.dec_fc0 = nn.Linear(
            self.NUM_LATENT_VARS,
            4096)  # NUM_LATENT_VARS-dimensional input, 4096-dimensional output
        self.dec_bn0 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.dec_fc1 = nn.Linear(
            4096, self.CONV_OUTPUT_SIZE
        )  # 4096-dimensional input, CONV_OUTPUT_SIZE-dimensional output
        self.dec_bn1 = nn.BatchNorm1d(
            self.CONV_OUTPUT_SIZE)  # Batch Norm layer
        self.dec_conv2 = nn.ConvTranspose2d(
            512, 384, 3
        )  # 512 input channels, 384 output channels, 3x3 transpose convolutions
        self.dec_bn2 = nn.BatchNorm2d(384)  # Batch Norm layer
        self.dec_conv3 = nn.ConvTranspose2d(
            384, 256, 3
        )  # 384 input channels, 256 output channels, 3x3 transpose convolutions
        self.dec_bn3 = nn.BatchNorm2d(256)  # Batch Norm layer
        self.dec_conv4 = nn.ConvTranspose2d(
            256, 256, 3
        )  # 256 input channels, 256 output channels, 3x3 transpose convolutions
        self.dec_bn4 = nn.BatchNorm2d(256)  # Batch Norm layer
        self.dec_conv5 = nn.ConvTranspose2d(
            256, 192, 3
        )  # 256 input channels, 192 output channels, 3x3 transpose convolutions
        self.dec_bn5 = nn.BatchNorm2d(192)  # Batch Norm layer
        self.dec_conv6 = nn.ConvTranspose2d(
            192, 192, 3
        )  # 192 input channels, 192 output channels, 3x3 transpose convolutions
        self.dec_bn6 = nn.BatchNorm2d(192)  # Batch Norm layer
        self.dec_conv7 = nn.ConvTranspose2d(
            192, 128, 3
        )  # 192 input channels, 128 output channels, 3x3 transpose convolutions
        self.dec_bn7 = nn.BatchNorm2d(128)  # Batch Norm layer
        self.dec_conv8 = nn.ConvTranspose2d(
            128, 96, 3
        )  # 128 input channels, 96 output channels, 3x3 transpose convolutions
        self.dec_bn8 = nn.BatchNorm2d(96)  # Batch Norm layer
        self.dec_conv9 = nn.ConvTranspose2d(
            96, 3, 7
        )  # 96 input channels, 3 output channels, 7x7 transpose convolutions
        self.dec_bn9 = nn.BatchNorm2d(3)  # Batch Norm layer
Esempio n. 14
0
    def __init__(self, config, input_shape=None):
        super(Net, self).__init__(config, input_shape)

        self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][
            P.KEY_DS_NUM_CLASSES]
        self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5)
        self.NUM_LATENT_VARS = config.CONFIG_OPTIONS.get(
            PP.KEY_VAE_NUM_LATENT_VARS, 256)

        # Here we define the layers of our network

        # First convolutional layer
        self.conv1 = nn.Conv2d(
            3, 96, 5)  # 3 input channels, 96 output channels, 5x5 convolutions
        self.bn1 = nn.BatchNorm2d(96)  # Batch Norm layer
        # Second convolutional layer
        self.conv2 = nn.Conv2d(
            96, 128,
            3)  # 96 input channels, 128 output channels, 3x3 convolutions
        self.bn2 = nn.BatchNorm2d(128)  # Batch Norm layer
        # Third convolutional layer
        self.conv3 = nn.Conv2d(
            128, 192,
            3)  # 128 input channels, 192 output channels, 3x3 convolutions
        self.bn3 = nn.BatchNorm2d(192)  # Batch Norm layer
        # Fourth convolutional layer
        self.conv4 = nn.Conv2d(
            192, 256,
            3)  # 192 input channels, 256 output channels, 3x3 convolutions
        self.bn4 = nn.BatchNorm2d(256)  # Batch Norm layer

        self.CONV_OUTPUT_SHAPE = utils.tens2shape(
            self.get_dummy_fmap()[self.CONV_OUTPUT])
        self.CONV_OUTPUT_SIZE = utils.shape2size(self.CONV_OUTPUT_SHAPE)

        # FC Layers
        self.fc5 = nn.Linear(
            self.CONV_OUTPUT_SIZE, 4096
        )  # conv_output_size-dimensional input, 4096-dimensional output
        self.bn5 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.fc6 = nn.Linear(
            4096, self.NUM_CLASSES
        )  # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
        self.fc_mu = nn.Linear(
            4096, self.NUM_LATENT_VARS
        )  # 4096-dimensional input, NUM_LATENT_VARS-dimensional output
        self.fc_var = nn.Linear(
            4096, self.NUM_LATENT_VARS
        )  # 4096-dimensional input, NUM_LATENT_VARS-dimensional output

        # Decoding Layers
        self.dec_fc0 = nn.Linear(
            self.NUM_LATENT_VARS,
            4096)  # NUM_LATENT_VARS-dimensional input, 4096-dimensional output
        self.dec_bn0 = nn.BatchNorm1d(4096)  # Batch Norm layer
        self.dec_fc1 = nn.Linear(
            4096, self.CONV_OUTPUT_SIZE
        )  # 4096-dimensional input, CONV_OUTPUT_SIZE-dimensional output
        self.dec_bn1 = nn.BatchNorm1d(
            self.CONV_OUTPUT_SIZE)  # Batch Norm layer
        self.dec_conv2 = nn.ConvTranspose2d(
            256, 192, 3
        )  # 256 input channels, 192 output channels, 3x3 transpose convolutions
        self.dec_bn2 = nn.BatchNorm2d(192)  # Batch Norm layer
        self.dec_conv3 = nn.ConvTranspose2d(
            192, 128, 3
        )  # 192 input channels, 128 output channels, 3x3 transpose convolutions
        self.dec_bn3 = nn.BatchNorm2d(128)  # Batch Norm layer
        self.dec_conv4 = nn.ConvTranspose2d(
            128, 96, 3
        )  # 128 input channels, 96 output channels, 3x3 transpose convolutions
        self.dec_bn4 = nn.BatchNorm2d(96)  # Batch Norm layer
        self.dec_conv5 = nn.ConvTranspose2d(
            96, 3, 5
        )  # 96 input channels, 3 output channels, 5x5 transpose convolutions
        self.dec_bn5 = nn.BatchNorm2d(3)  # Batch Norm layer