def __init__(self,
                 net_vlad,
                 num_cluster,
                 dim,
                 PCA_dim,
                 mode=None,
                 BatchNorm=None,
                 pca_model=None,
                 pretrained=True):
        super(AttentAlex, self).__init__()
        self.doPCA = pca_model
        self.mode = mode
        encoder = models.alexnet(pretrained=pretrained)
        layers = list(encoder.features.children())[:-2]
        if pretrained:
            for l in layers[:-1]:
                for p in l.parameters():
                    p.requires_grad = False
        layers.append(nn.BatchNorm2d(dim))
        self.base_model = nn.Sequential(*layers)
        self.WPCA = pca_model
        self.net_vlad = net_vlad

        self.ca = ChannelAttention(dim)
        self.sa = SpatialAttention()
 def discriminator_block(in_filters, out_filters, stride, normalize):
     """Returns layers of each discriminator block"""
     layers = [nn.Conv2d(in_filters, out_filters, 3, stride, 1)]
     if normalize:
         layers.append(nn.InstanceNorm2d(out_filters))
     layers.append(nn.LeakyReLU(0.2, inplace=True))
     return layers
Example #3
0
    def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
        norm_layer = self._norm_layer
        downsample = None
        previous_dilation = self.dilation
        if dilate:
            self.dilation *= stride
            stride = 1
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                conv1x1(self.inplanes, planes * block.expansion, stride),
                norm_layer(planes * block.expansion),
            )

        layers = []
        layers.append(
            block(self.inplanes, planes, stride, downsample, self.groups,
                  self.base_width, previous_dilation, norm_layer))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(
                block(self.inplanes,
                      planes,
                      groups=self.groups,
                      base_width=self.base_width,
                      dilation=self.dilation,
                      norm_layer=norm_layer))

        return nn.Sequential(*layers)
 def upsample(in_feat, out_feat, normalize=True):
     layers = [
         nn.ConvTranspose2d(in_feat, out_feat, 4, stride=2, padding=1)
     ]
     if normalize:
         layers.append(nn.BatchNorm2d(out_feat, 0.8))
     layers.append(nn.ReLU())
     return layers
Example #5
0
def fileLayers(filename):
	if filename not in fileLayerCache:
		proc = os.popen("ogrinfo '%s' -al | grep -E '  Layer \\(String\\) = .*' | sed -e 's/  Layer (String) = //' | sort | uniq" % filename, "r")
		layers = []
		for line in proc:
			layer = line.strip("\n")
			if len(layer) > 0:
				layers.append(layer)
		proc.close()
		fileLayerCache[filename] = layers
	return fileLayerCache[filename]
Example #6
0
 def to_dict(self):
     layers = []
     for layer in self._layers:
         config = layer.to_dict()
         dic = {}
         for key, value in config.iteritems():
             if isinstance(value, (int, float, str, bool, type(None), tuple, list, dict)):
                 dic[key] = value
         layers.append(dic)
     return {
         "layers": layers,
         "weight_initializer": self.weight_initializer,
         "weight_init_std": self.weight_init_std
     }
Example #7
0
	def to_dict(self):
		layers = []
		for layer in self._layers:
			config = layer.to_dict()
			dic = {}
			for key, value in config.iteritems():
				if isinstance(value, (int, float, str, bool, type(None), tuple, list, dict)):
					dic[key] = value
			layers.append(dic)
		return {
			"layers": layers,
			"weight_initializer": self.weight_initializer,
			"weight_init_std": self.weight_init_std
		}
Example #8
0
    def get_network(self):
        self._read_config()

        input_layer = None
        layers = []

        prev_layer = None
        for data in self._layers:
            if data["type"] == "input":
                input_size = self._input_size * self._input_size
                output_size = int(data["output_size"])
                layer = InputLayer(input_size, output_size)
            elif data["type"] == "dense":
                if "output_size" in data:
                    output_size = int(data["output_size"])
                else:
                    output_size = self._output_size
                activation_function_str = data["af"]
                activation_function = self._lookup_activation_function(
                    activation_function_str)
                activation_function_d = self._lookup_activation_function_d(
                    activation_function_str)
                learning_rate = float(data["la"])
                layer = DenseLayer(prev_layer.get_output_shape(), output_size,
                                   activation_function, activation_function_d,
                                   learning_rate)
            elif data["type"] == "convolution":
                if prev_layer == None:
                    input_shape = (self._input_size, self._input_size, 1)
                else:
                    input_shape = prev_layer.get_output_shape()
                kernel_n = int(data["kernel_n"])
                kernel_m = int(data["kernel_m"])
                channels_out = int(data["channels"])
                output_shape = (kernel_n, kernel_m, channels_out)
                v_stride = int(data["stride_n"])
                h_stride = int(data["stride_m"])
                padding = int(data["padding"])
                la = float(data["la"])
                layer = ConvolutionLayer(input_shape, output_shape, h_stride,
                                         v_stride, padding, la)
            if input_layer == None:
                input_layer = layer
            else:
                layers.append(layer)
            prev_layer = layer

        network = Network(input_layer, layers)
        return network
Example #9
0
	def to_dict(self):
		layers = []
		for layer in self.layers:
			config = layer.to_dict()
			dictionary = {}
			if isinstance(layer, Residual):
				dictionary["_residual"] = True
			for key, value in config.iteritems():
				if isinstance(value, (int, float, str, bool, type(None), tuple, list, dict)):
					dictionary[key] = value
			layers.append(dictionary)
		return {
			"layers": layers,
			"weight_initializer": self.weight_initializer,
			"weight_std": self.weight_std
		}
Example #10
0
 def make_layer(self, res_block, out_channels, num_blocks=2, stride=1):
     downsample = None
     if (stride != 1) or (self.in_channels != out_channels):
         downsample = nn.Sequential(
             nn.Conv2d(self.in_channels,
                       self.out_channels,
                       kernel_size=3,
                       stride=stride,
                       padding=1,
                       bias=False), nn.BatchNorm2d(out_channels))
     layers = [
         res_block(self.in_channels, out_channels, stride, downsample)
     ]
     self.in_channels = out_channels
     for i in range(1, num_blocks):
         layers.append(res_block(out_channels, out_channels))
     return nn.Sequential(*layers)
 def __init__(self,
              net_vlad,
              num_cluster,
              dim,
              PCA_dim,
              BatchNorm=None,
              pca_model=None,
              pretrained=True):
     super(VGGNet, self).__init__()
     self.doPCA = pca_model
     encoder = models.vgg16(pretrained=pretrained)
     layers = list(encoder.features.children())[:-2]
     if pretrained:
         for l in layers[:-5]:
             for p in l.parameters():
                 p.requires_grad = False
     layers.append(nn.BatchNorm2d(dim))
     self.base_model = nn.Sequential(*layers)
     self.WPCA = pca_model
Example #12
0
    def _make_layer(self, block, planes, blocks, stride=1, circpad=False):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes,
                          planes * block.expansion,
                          kernel_size=1,
                          stride=stride,
                          bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(
            block(self.inplanes, planes, stride, downsample, circpad=circpad))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, circpad=circpad))

        return nn.Sequential(*layers)
Example #13
0
 def init_glow(num_blocks, steps_per_block, name):
     layers = []
     for b in range(num_blocks):
         channel = 2**(b + 2)  # s=0, c=8; s=1, c=32; s=2, c=128
         for s in range(steps_per_block):
             if name is 'dti':
                 layers.append(
                     nn.
                     Sequential(  # there are num_blocks * steps_per_block sequential block
                         DTI_AffineCoupling(
                             inchannel=int(channel * param['dti'] //
                                           2))))
             elif name is 'odf':
                 layers.append(
                     nn.
                     Sequential(  # there are num_blocks * steps_per_block sequential block
                         ODF_AffineCoupling(inchannel=channel *
                                            param['odf'] // 2)))
             elif name is 'eig':
                 layers.append(
                     nn.
                     Sequential(  # there are num_blocks * steps_per_block sequential block
                         EIG_AffineCoupling(inchannel=channel *
                                            param['eig'] // 2)))
             else:
                 raise NotImplementedError
     return layers
    def __init__(self, channels=3):
        super(GlobalDiscriminator, self).__init__()
        self.output_shape = (24, 24)

        def discriminator_block(in_filters, out_filters, stride, normalize):
            """Returns layers of each discriminator block"""
            layers = [nn.Conv2d(in_filters, out_filters, 3, stride, 1)]
            if normalize:
                layers.append(nn.InstanceNorm2d(out_filters))
            layers.append(nn.LeakyReLU(0.2, inplace=True))
            return layers

        layers = []
        in_filters = channels
        for out_filters, stride, normalize in [(64, 2, False), (128, 2, True),
                                               (256, 2, True), (512, 1, True)]:
            layers.extend(
                discriminator_block(in_filters, out_filters, stride,
                                    normalize))
            in_filters = out_filters

        layers.append(nn.Conv2d(out_filters, 1, 3, 1, 1))

        self.model = nn.Sequential(*layers)
Example #15
0
 def toJSON(self):
     it = iter(self.start)
     layers = []
     for layer in it:
         layers.append(layer.toJSON())
     return layers
Example #16
0
 def toJSON(self):
     it = iter(self.start)
     layers = []
     for layer in it:
         layers.append(layer.toJSON())
     return layers
Example #17
0
    def forward_with_extras(self, imgs):
        """
        Performs one forward pass through the network given at initialization
        (only convolutional, linear, pooling and ReLU layer). Additionally to
        the final output the input and output to each convolutional and linear
        layer, the switches of pooling layers and the indices of the values
        that are set to zero of each ReLU layer, are returned.
        """

        output = Variable(imgs, requires_grad=False)

        layers = []
        layers_wo_bias = []
        cnt = 0
        indices = []
        switches = []

        for ind, layer in enumerate(self.backward_layers[::-1]):
            # print(layer.forward_layer)
            if layer.__class__.__name__ == "PatternConv2d":
                # save input to layer
                layers.append({})
                layers[cnt]["inputs"] = output.data
                # apply forward layer
                output, output_wo_bias = layer(output)
                # save output of layer
                layers[cnt]["outputs"] = output.data
                # save output without bias
                layers_wo_bias.append(output_wo_bias)
                cnt += 1
            elif layer.__class__.__name__ == "PatternLinear":
                # save input to layer
                layers.append({})
                layers[cnt]["inputs"] = output.data
                # apply layer
                output, output_wo_bias = layer(output)
                # save output of layer
                layers[cnt]["outputs"] = output.data
                # save output without bias
                layers_wo_bias.append(output_wo_bias)
                cnt += 1
            elif layer.__class__.__name__ == "PatternMaxPool2d":
                # set return indices to true to get the switches
                # apply layer
                output, switch = layer(output)
                # save switches
                switches.append(switch)
            elif layer.__class__.__name__ == "PatternReLU":
                # save indices smaller zero
                output, inds = layer(output)
                indices.append(inds)

            # add view between convolutional and linear sequential
            if ind == self.reshape_ind - 1:  # layer before the first linear layer
                self._reshape_size_in = output.shape
                output = output.view(-1, self.lst_layers[ind + 1].in_features)

        return (
            output,
            (layers, layers_wo_bias),
            indices[::-1],
            switches[::-1],
        )
Example #18
0
    def _build(self, z, is_training=True, test_local_stats=True, use_bn=False):
        batch_norm_args = {
            'is_training': is_training,
            'test_local_stats': test_local_stats
        }

        method = self._method
        # Cycle over the encoder shapes backwards, to build a symmetrical decoder.
        enc_conv_shapes = self._enc_conv_shapes[::-1]
        strides = self._dec_up_strides
        # We store the heights and widths of the encoder feature maps that are
        # unique, i.e., the ones right after a layer with stride != 1. These will be
        # used as a target to potentially crop the upsampled feature maps.
        unique_hw = np.unique([(el[1], el[2]) for el in enc_conv_shapes],
                              axis=0)
        unique_hw = unique_hw.tolist()[::-1]
        unique_hw.pop()  # Drop the initial shape

        # The first filter is an MLP.
        mlp_filter, conv_filters = self._filters[0], self._filters[1:]
        # The first shape is used after the MLP to go to 4D.

        layers = [z]
        # The shape of the first enc is used after the MLP to go back to 4D.
        dec_mlp = snt.nets.MLP(
            name='dec_mlp_projection',
            output_sizes=[mlp_filter,
                          np.prod(enc_conv_shapes[0][1:])],
            use_bias=not use_bn,
            activation=self._activation,
            activate_final=True)

        upsample_mlp_flat = dec_mlp(z)
        if use_bn:
            upsample_mlp_flat = snt.BatchNorm(scale=True)(upsample_mlp_flat,
                                                          **batch_norm_args)
        layers.append(upsample_mlp_flat)

        # Ignore the batch size
        enc_conv_shapes[0] = [upsample_mlp_flat.shape[0]] + \
            enc_conv_shapes[0][1:]

        upsample = tf.reshape(upsample_mlp_flat, [-1] + enc_conv_shapes[0][1:])
        layers.append(upsample)

        for i, (filter_i, stride_i) in enumerate(zip(conv_filters, strides),
                                                 1):
            if method != 'deconv' and stride_i > 1:
                upsample = tf.image.resize_images(
                    upsample,
                    [stride_i * el for el in upsample.shape.as_list()[1:3]],
                    method=method,
                    name='upsample_' + str(i))
            upsample = self._conv_layer(
                filters=filter_i,
                kernel_size=self._kernel_size,
                padding='same',
                use_bias=not use_bn,
                activation=self._activation,
                strides=stride_i if method == 'deconv' else 1,
                name='upsample_conv_' + str(i))(upsample)
            if use_bn:
                upsample = snt.BatchNorm(scale=True)(upsample,
                                                     **batch_norm_args)
            if stride_i > 1:
                hw = unique_hw.pop()
                upsample = utils.maybe_center_crop(upsample, hw)
            layers.append(upsample)

        # Final layer, no upsampling.
        x_logits = tf.layers.Conv2D(filters=self._n_c,
                                    kernel_size=self._kernel_size,
                                    padding='same',
                                    use_bias=not use_bn,
                                    activation=None,
                                    strides=1,
                                    name='logits')(upsample)
        if use_bn:
            x_logits = snt.BatchNorm(scale=True)(x_logits, **batch_norm_args)
        layers.append(x_logits)

        logging.info('%s upsampling module layer shapes', self._method_str)
        logging.info('\n'.join([str(v.shape.as_list()) for v in layers]))

        return x_logits
 def downsample(in_feat, out_feat, normalize=True):
     layers = [nn.Conv2d(in_feat, out_feat, 4, stride=2, padding=1)]
     if normalize:
         layers.append(nn.BatchNorm2d(out_feat, 0.8))
     layers.append(nn.LeakyReLU(0.2))
     return layers
Example #20
0
 def get_layers(self):
     layers = [self._input_layer]
     for layer in self._hidden_layers:
         layers.append(layer)
     return layers