Пример #1
0
def Basic_Block(input_size,
                inplanes=None,
                planes=10,
                stride=1,
                Z=None,
                downsample=None):
    expansion = 1
    __constants__ = ['downsample']

    layers = []

    if downsample is not None:
        layers.append(downsample)

    base_kernel = kernels.SquaredExponential(input_dim=3 * 3 * input_size[2],
                                             lengthscales=2.0)
    layer = ConvLayer(input_size,
                      patch_size=3,
                      stride=stride,
                      base_kernel=base_kernel,
                      Z=Z,
                      feature_maps_out=planes,
                      pad='SAME',
                      ltype='Residua-1')
    input_size = (layer.patch_extractor.out_image_height,
                  layer.patch_extractor.out_image_width, planes)
    layers.append(layer)

    base_kernel = kernels.SquaredExponential(input_dim=3 * 3 * input_size[2],
                                             lengthscales=2.0)
    layer = ConvLayer(input_size,
                      patch_size=3,
                      stride=1,
                      base_kernel=base_kernel,
                      Z=Z,
                      feature_maps_out=planes,
                      pad='SAME',
                      ltype='Residual-2')
    input_size = (layer.patch_extractor.out_image_height,
                  layer.patch_extractor.out_image_width, planes)
    layers.append(layer)

    return layers, input_size
Пример #2
0
    def _add_layers(self, input_size, Z, layers_strcut, X):
        k = 1
        #Z_1 = compute_z_inner(X, self.M, self.feature_maps, 1)
        for i in layers_strcut:
            for j in range(i):
                base_kernel = self.kernel(input_dim=3 * 3 * input_size[2],
                                          lengthscales=2.0)
                print('filter_size 3/1')
                layer = ConvLayer(input_size,
                                  patch_size=3,
                                  stride=1,
                                  base_kernel=base_kernel,
                                  Z=Z,
                                  feature_maps_out=input_size[2],
                                  pad='SAME',
                                  ltype='Residual')
                input_size = (layer.patch_extractor.out_image_height,
                              layer.patch_extractor.out_image_width,
                              input_size[2])
                self.Reslayers.append(layer)

            k = k + self.expansion_factor
            base_kernel = self.kernel(input_dim=1 * 1 * input_size[2],
                                      lengthscales=2.0)
            print(f'filter_size 1/2-{k}')
            output_featuers = k * self.feature_maps
            Z = compute_z_inner(X, self.M, output_featuers,
                                3) if self.expansion_factor >= 1 else Z
            layer = ConvLayer(input_size,
                              patch_size=1,
                              stride=2,
                              base_kernel=base_kernel,
                              Z=Z,
                              feature_maps_out=output_featuers,
                              pad='VALID',
                              ltype='Plain')
            input_size = (layer.patch_extractor.out_image_height,
                          layer.patch_extractor.out_image_width,
                          output_featuers)
            self.Reslayers.append(layer)

        return input_size
Пример #3
0
 def _add_first_layer(self, Z, input_size):
     base_kernel = self.kernel(input_dim=7 * 7 * input_size[2],
                               lengthscales=2.0)
     print('filter_size 7/1')
     layer = ConvLayer(input_size,
                       patch_size=7,
                       stride=1,
                       base_kernel=base_kernel,
                       Z=Z,
                       feature_maps_out=self.feature_maps,
                       pad='VALID',
                       ltype='Plain')
     self.Reslayers.append(layer)
     input_size = (layer.patch_extractor.out_image_height,
                   layer.patch_extractor.out_image_width, self.feature_maps)
     return input_size
Пример #4
0
    def _make_layer(self,
                    input_size,
                    block,
                    planes,
                    blocks,
                    Z,
                    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 * 1:  #block.expansion: stop expnation for now and set it to 1

            base_kernel = kernels.SquaredExponential(input_dim=1 * 1 *
                                                     input_size[2],
                                                     lengthscales=2.0)
            downsample = ConvLayer(input_size,
                                   patch_size=1,
                                   stride=stride,
                                   base_kernel=base_kernel,
                                   Z=Z,
                                   feature_maps_out=planes,
                                   pad='VALID',
                                   ltype='downsample')
            input_size = (downsample.patch_extractor.out_image_height,
                          downsample.patch_extractor.out_image_width, planes)

        layers = []

        layers_block, input_size = block(input_size, self.inplanes, planes,
                                         stride, Z, downsample)

        layers += layers_block

        self.inplanes = planes * 1  #block.expansion stop expnation for now and set it to 1

        for _ in range(1, blocks):
            layers_block, input_size = block(input_size, self.inplanes, planes,
                                             stride, Z, downsample)
            layers += layers_block

        return layers, input_size
Пример #5
0
    def __init__(self,
                 block,
                 layers,
                 num_classes=1000,
                 input_size=input_size,
                 zero_init_residual=False,
                 groups=1,
                 width_per_group=64,
                 replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResCGPNet, self).__init__()

        #         if norm_layer is None:
        #             norm_layer = nn.BatchNorm2d

        self._norm_layer = norm_layer

        self.inplanes = 8
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(
                                 replace_stride_with_dilation))
        self.groups = groups
        self.base_width = width_per_group
        input_size = Xtrain.shape[1:]
        self.Reslayers = []
        # current impelemtation with fixed output feature maps for all alyers to user inputed argument 10
        # need to replace it with self.inplanes but that requirs artucheture search which isn't valid for now
        Z = conv_utils.cluster_patches(Xtrain, flags.M, self.inplanes)
        base_kernel = kernels.SquaredExponential(input_dim=7 * 7 *
                                                 input_size[2],
                                                 lengthscales=2.0)
        layer = ConvLayer(input_size,
                          patch_size=7,
                          stride=2,
                          base_kernel=base_kernel,
                          Z=Z,
                          feature_maps_out=self.inplanes,
                          pad=3,
                          ltype='Plain')  # change stride 2-> 1 for cifar-10
        input_size = (layer.patch_extractor.out_image_height,
                      layer.patch_extractor.out_image_width, self.inplanes)
        self.Reslayers.append(layer)

        #self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) # need to compensate for the pooling calulate the size and adjuest the conGP acoordingly
        Z = compute_z_inner(Xtrain, flags.M, 8)
        layers_, input_size = self._make_layer(input_size, block, 8, layers[0],
                                               Z)
        self.Reslayers += layers_
        #         set_trace()
        Z = compute_z_inner(Xtrain, flags.M, 16)
        layers_, input_size = self._make_layer(
            input_size,
            block,
            16,
            layers[1],
            Z,
            stride=2,
            dilate=replace_stride_with_dilation[0])
        self.Reslayers += layers_
        Z = compute_z_inner(Xtrain, flags.M, 32)
        layers_, input_size = self._make_layer(
            input_size,
            block,
            32,
            layers[2],
            Z,
            stride=2,
            dilate=replace_stride_with_dilation[1])
        self.Reslayers += layers_
        Z = compute_z_inner(Xtrain, flags.M, 64)
        layers_, input_size = self._make_layer(
            input_size,
            block,
            64,
            layers[3],
            Z,
            stride=2,
            dilate=replace_stride_with_dilation[2])
        self.Reslayers += layers_
        #         Z = compute_z_inner(Xtrain, flags.M, 16)
        rbf = kernels.SquaredExponential(
            input_dim=input_size[0] * input_size[1] * flags.feature_maps,
            lengthscales=2.0
        )  # filter_size is equal to all input size to memic the Linear layer
        patch_extractor = PatchExtractor(input_size,
                                         filter_size=input_size[0],
                                         feature_maps=num_classes,
                                         stride=stride)
        conv_kernel = ConvKernel(rbf, patch_extractor)
        layer = Layer(conv_kernel, num_classes, Z)
        self.Reslayers.append(layer)
Пример #6
0
    if layer != flags.layers - 1:

        base_kernel = kernel(input_dim=filter_size * filter_size *
                             input_size[2],
                             lengthscales=2.0)
        print('filter_size ', filter_size)
        if filter_size == 3:
            pad = 'SAME'
            ltype = 'Residual'
        else:
            pad = 'VALID'
            ltype = 'Plain'
        layer = ConvLayer(input_size,
                          patch_size=filter_size,
                          stride=stride,
                          base_kernel=base_kernel,
                          Z=Z,
                          feature_maps_out=flags.feature_maps,
                          pad=pad,
                          ltype=ltype)

        input_size = (layer.patch_extractor.out_image_height,
                      layer.patch_extractor.out_image_width,
                      flags.feature_maps)
    else:
        rbf = kernel(input_dim=filter_size * filter_size * flags.feature_maps,
                     lengthscales=2.0)
        patch_extractor = PatchExtractor(input_size,
                                         filter_size=filter_size,
                                         feature_maps=10,
                                         stride=stride)
        conv_kernel = ConvKernel(rbf, patch_extractor)