Esempio n. 1
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        # raise Exception("Not implemented!")

        self.out_classes = n_output_classes
        image_width, image_height, in_channels = input_shape

        self.layers = [
            ConvolutionalLayer(in_channels, conv1_channels, 3, 1),
            ReLULayer(),
            MaxPoolingLayer(4, 4),
            ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1),
            ReLULayer(),
            MaxPoolingLayer(4, 4),
            Flattener(),
            FullyConnectedLayer(4 * conv2_channels, n_output_classes)
        ]
Esempio n. 2
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        width, height, channels = input_shape
        self.conv1 = ConvolutionalLayer(in_channels=3,
                                        out_channels=3,
                                        filter_size=conv1_channels)
        self.relu1 = ReLULayer()
        self.maxpool1 = MaxPoolingLayer(4, 4)
        self.conv2 = ConvolutionalLayer(in_channels=3,
                                        out_channels=3,
                                        filter_size=conv2_channels)
        self.relu2 = ReLULayer()
        self.maxpool2 = MaxPoolingLayer(4, 4)
        self.flatten = Flattener()
        self.f_connected = FullyConnectedLayer(3, n_output_classes)
Esempio n. 3
0
    def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.height = input_shape[0]
        self.width = input_shape[1]
        self.input_channels = input_shape[2]
        self.n_output_classes = n_output_classes
        self.conv1_channels = conv1_channels
        self.conv2_channels = conv2_channels

        self.conv1_layer = ConvolutionalLayer(self.input_channels, self.conv1_channels, 3, 1)
        self.relu1 = ReLULayer()
        self.maxpool1 = MaxPoolingLayer(4, 4)

        self.conv2_layer = ConvolutionalLayer(self.conv1_channels, self.conv2_channels, 3, 1)
        self.relu2 = ReLULayer()
        self.maxpool2 = MaxPoolingLayer(4, 4)

        self.flattener = Flattener()
        self.fc_layer = FullyConnectedLayer(2*2*self.conv2_channels, self.n_output_classes)
Esempio n. 4
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        input_width, input_height, input_channels = input_shape
        self.conv1 = ConvolutionalLayer(input_channels,
                                        conv1_channels,
                                        filter_size=3,
                                        padding=1)
        self.relu1 = ReLULayer()
        self.maxpool1 = MaxPoolingLayer(pool_size=4, stride=4)
        self.conv2 = ConvolutionalLayer(conv1_channels,
                                        conv2_channels,
                                        filter_size=3,
                                        padding=1)
        self.relu2 = ReLULayer()
        self.maxpool2 = MaxPoolingLayer(pool_size=4, stride=4)
        self.flattener = Flattener()
        self.fc = FullyConnectedLayer(
            input_width * input_height * conv2_channels // (4**4),
            n_output_classes)
Esempio n. 5
0
    def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        width, height, n_channels = input_shape
        conv = 3
        pad = 1
        stride = 4
        pool = 4
        
        hidden_layer_size = (width // stride // stride) * (height // stride // stride) * conv2_channels
        
        self.layers = [
            ConvolutionalLayer(n_channels, conv1_channels, conv, pad),
            ReLULayer(),
            MaxPoolingLayer(pool, stride),
            ConvolutionalLayer(conv1_channels, conv2_channels, conv, pad),
            ReLULayer(),
            MaxPoolingLayer(pool, stride),
            Flattener(),
            FullyConnectedLayer(hidden_layer_size, n_output_classes)        
        ]
Esempio n. 6
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network
        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        width, height, channels = input_shape
        filter_size = 3
        padding = 1
        pool_size = 4
        pool_stride = 4

        self.Conv1 = ConvolutionalLayer(channels, conv1_channels, filter_size,
                                        padding)
        self.ReLU1 = ReLULayer()
        self.MaxPool1 = MaxPoolingLayer(pool_size, pool_stride)

        self.Conv2 = ConvolutionalLayer(conv1_channels, conv2_channels,
                                        filter_size, padding)
        self.ReLU2 = ReLULayer()
        self.MaxPool2 = MaxPoolingLayer(pool_size, pool_stride)

        left_width = width // pool_stride // pool_stride
        left_height = height // pool_stride // pool_stride

        self.Flat = Flattener()
        self.FullyConnected = FullyConnectedLayer(
            left_width * left_height * conv2_channels, n_output_classes)
Esempio n. 7
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        self.input_shape = input_shape
        self.n_output_classes = n_output_classes

        self.layer1 = ConvolutionalLayer(3, conv1_channels, 3,
                                         1)  #32x32x3xconv1_channels
        self.layer2 = ReLULayer()
        self.layer3 = MaxPoolingLayer(4, 4)  #8x8x3xconv1_channels
        self.layer4 = ConvolutionalLayer(
            conv1_channels, conv2_channels, 3,
            1)  #8x8x3x conv1_channels x conv2_channels
        self.layer5 = ReLULayer()
        self.layer6 = MaxPoolingLayer(
            4, 4)  #2x2x3 conv1_channels x conv2_channels
        self.layer7 = Flattener()
        self.layer8 = FullyConnectedLayer(conv1_channels * conv2_channels * 2,
                                          n_output_classes)
Esempio n. 8
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        self.layers = [
            ConvolutionalLayer(input_shape[2], conv1_channels, 3, 0),
            ReLULayer(),
            MaxPoolingLayer(4, 4),
            ConvolutionalLayer(conv1_channels, conv2_channels, 3, 0),
            ReLULayer(),
            MaxPoolingLayer(4, 4),
            Flattener(),
            FullyConnectedLayer(
                int(input_shape[0] * input_shape[1] * conv2_channels / 256),
                n_output_classes)
        ]
Esempio n. 9
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        filter_size = 3
        pool_size = 4
        self.conv1 = ConvolutionalLayer(input_shape[2],
                                        conv1_channels,
                                        filter_size,
                                        padding=1)
        self.relu1 = ReLULayer()
        self.max_pool1 = MaxPoolingLayer(pool_size, stride=pool_size)
        self.conv2 = ConvolutionalLayer(conv1_channels,
                                        conv2_channels,
                                        filter_size,
                                        padding=1)
        self.relu2 = ReLULayer()
        self.max_pool2 = MaxPoolingLayer(pool_size, stride=pool_size)
        self.flatten = Flattener()
        self.fc = FullyConnectedLayer(n_input=4 * conv2_channels,
                                      n_output=n_output_classes)
Esempio n. 10
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers

        self.out_classes = n_output_classes
        image_width, image_height, in_channels = input_shape

        self.Conv1 = ConvolutionalLayer(in_channels, conv1_channels, 3, 1)
        self.ReLU1 = ReLULayer()
        self.MaxPool1 = MaxPoolingLayer(4, 4)
        self.Conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1)
        self.ReLU2 = ReLULayer()
        self.MaxPool2 = MaxPoolingLayer(4, 4)
        self.Flatten = Flattener()
        self.FC = FullyConnectedLayer(4 * conv2_channels, n_output_classes)
Esempio n. 11
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        width, height, n_channels = input_shape

        self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, 3, 1)
        self.relu1 = ReLULayer()
        self.maxpool1 = MaxPoolingLayer(4, 4)
        self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1)
        self.relu2 = ReLULayer()
        self.maxpool2 = MaxPoolingLayer(4, 4)
        self.flatten = Flattener()

        self.fc = FullyConnectedLayer(
            (height // 4 // 4) * (width // 4 // 4) * conv2_channels,
            n_output_classes)

        self.conv1_params = self.conv1.params()
        self.conv2_params = self.conv2.params()
        self.fc_params = self.fc.params()
Esempio n. 12
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.model = [
            ConvolutionalLayer(in_channels=input_shape[2],
                               out_channels=conv1_channels,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(2, 2),
            ConvolutionalLayer(in_channels=conv1_channels,
                               out_channels=conv2_channels,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(2, 2),
            Flattener(),
            FullyConnectedLayer(n_input=int(input_shape[0] / 4)**2 *
                                conv2_channels,
                                n_output=n_output_classes)
        ]
Esempio n. 13
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        #raise Exception("Not implemented!")

        image_width, image_height, n_channels = input_shape

        self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, 3, 1)
        self.relu1 = ReLULayer()
        self.maxp1 = MaxPoolingLayer(4, 4)
        self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1)
        self.relu2 = ReLULayer()
        self.maxp2 = MaxPoolingLayer(4, 4)
        self.flatn = Flattener()

        fc_input = int(image_width * image_height * conv2_channels / pow(4, 4))
        self.fc = FullyConnectedLayer(fc_input, n_output_classes)
Esempio n. 14
0
    def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO_ Create necessary layers
        # raise Exception("Not implemented!")

        weight, height, cannels = input_shape
        filter_size = 3
        pool_size = 4
        padding = 1
        stride = pool_size

        self.conv1 = ConvolutionalLayer(cannels, conv1_channels, filter_size, padding)
        self.relu1 = ReLULayer()
        self.maxpool1 = MaxPoolingLayer(pool_size, stride)
        self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding)
        self.relu2 = ReLULayer()
        self.maxpool2 = MaxPoolingLayer(pool_size, stride)
        self.flatten = Flattener()
        n_fc_input = int(height / pool_size / pool_size * weight / pool_size / pool_size * conv2_channels)
        self.fc = FullyConnectedLayer(n_fc_input, n_output_classes)
Esempio n. 15
0
    def __init__(self, n_input, n_output, conv1_size, conv2_size, reg):
        """
        Initializes the neural network

        Arguments:
        n_input, int - dimension of the model input
        n_output, int - number of classes to predict
        conv1_size, int - number of filters in the 1st conv layer
        conv2_size, int - number of filters in the 2nd conv layer
        reg, float - L2 regularization strength
        """
        self.reg = reg
        height, width, input_channels = n_input

        self.L = [
            ConvolutionalLayer(in_channels=input_channels,
                               out_channels=conv1_size,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(4, 4),
            ConvolutionalLayer(in_channels=conv1_size,
                               out_channels=conv2_size,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(4, 4),
            Flattener(),
            FullyConnectedLayer(8, n_output)
        ]
Esempio n. 16
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.layers = [
            ConvolutionalLayer(in_channels=input_shape[2],
                               out_channels=input_shape[2],
                               filter_size=conv1_channels,
                               padding=2),
            ReLULayer(),
            MaxPoolingLayer(pool_size=4, stride=2),  #2
            ConvolutionalLayer(in_channels=input_shape[2],
                               out_channels=input_shape[2],
                               filter_size=conv2_channels,
                               padding=2),
            ReLULayer(),
            MaxPoolingLayer(pool_size=4, stride=2),  #2
            Flattener(),
            FullyConnectedLayer(n_input=192, n_output=n_output_classes)
        ]  #192
Esempio n. 17
0
    def __init__(self,
                 input_shape,
                 n_output_classes,
                 conv1_channels,
                 conv2_channels,
                 filter_size=3):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.input_shape = input_shape
        self.n_output_classes = n_output_classes
        self.conv1_channels = conv1_channels
        self.conv2_channels = conv2_channels
        self.filter_size = filter_size
        self.padding = 1

        c1 = int(
            (input_shape[0] - self.filter_size + 2 * self.padding) / 1) + 1
        mp1 = int((c1 - 4) / 4) + 1
        c2 = int((mp1 - self.filter_size + 2 * self.padding) / 1) + 1
        self.size_after_2maxpool = int((c2 - 4) / 4) + 1

        self.RL1 = ReLULayer()
        self.RL2 = ReLULayer()
        self.MaxPool1 = MaxPoolingLayer(pool_size=4, stride=4)
        self.MaxPool2 = MaxPoolingLayer(pool_size=4, stride=4)
        self.Flatten = Flattener()
        self.Conv1 = ConvolutionalLayer(in_channels=self.input_shape[-1],
                                        out_channels=conv1_channels,
                                        filter_size=self.filter_size,
                                        padding=self.padding)
        self.Conv2 = ConvolutionalLayer(in_channels=conv1_channels,
                                        out_channels=conv2_channels,
                                        filter_size=self.filter_size,
                                        padding=self.padding)
        self.FC = FullyConnectedLayer(n_input=conv2_channels *
                                      self.size_after_2maxpool**2,
                                      n_output=self.n_output_classes)
Esempio n. 18
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        width, height, im_channels = input_shape

        out_height_conv1 = int((height - 3 + 2 * 1) / 1 + 1)
        out_width_conv1 = int((width - 3 + 2 * 1) / 1 + 1)

        out_height_maxpool1 = int((out_height_conv1 - 4) / 2 + 1)
        out_width_maxpool1 = int((out_width_conv1 - 4) / 2 + 1)

        out_height_conv2 = int((out_height_maxpool1 - 3 + 2 * 1) / 1 + 1)
        out_width_conv2 = int((out_width_maxpool1 - 3 + 2 * 1) / 1 + 1)

        out_height_maxpool2 = int((out_height_conv2 - 4) / 2 + 1)
        out_width_maxpool2 = int((out_width_conv2 - 4) / 2 + 1)

        neurons_in_fc = out_height_maxpool2 * out_width_maxpool2 * conv2_channels
        self.sequential = [
            ConvolutionalLayer(im_channels,
                               conv1_channels,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(pool_size=4, stride=2),
            ConvolutionalLayer(conv1_channels,
                               conv2_channels,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(pool_size=4, stride=2),
            Flattener(),
            FullyConnectedLayer(neurons_in_fc, n_output_classes)
        ]
Esempio n. 19
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        # padding=0, stride=1
        self.padding = 0
        self.stride = 1
        self.filter_size = 3
        self.pool_size = 4

        self.conv1_out_size = int((input_shape[0] - self.filter_size +
                                   2 * self.padding) / self.stride + 1)
        self.max_pool1_out_size = int((self.conv1_out_size - self.pool_size) /
                                      self.stride + 1)
        self.conv2_out_size = int((self.max_pool1_out_size - self.filter_size +
                                   2 * self.padding) / self.stride + 1)
        self.max_pool2_out_size = int((self.conv2_out_size - self.pool_size) /
                                      self.stride + 1)

        # padding=0, stride=1
        self.sequence = [
            ConvolutionalLayer(input_shape[2], conv1_channels,
                               self.filter_size, self.padding),
            ReLULayer(),
            MaxPoolingLayer(self.pool_size, self.stride),
            ConvolutionalLayer(conv1_channels, conv2_channels,
                               self.filter_size, self.padding),
            ReLULayer(),
            MaxPoolingLayer(self.pool_size, self.stride),
            Flattener(),
            FullyConnectedLayer(
                self.max_pool2_out_size * self.max_pool2_out_size *
                conv2_channels, n_output_classes)
        ]
Esempio n. 20
0
 def __init__(self, input_shape, n_output_classes, conv1_channels,
              conv2_channels):
     self.layers = [ConvolutionalLayer(in_channels=input_shape[2], \
                                       out_channels=input_shape[2], \
                                       filter_size=conv1_channels, \
                                       padding=2),
                    ReLULayer(),
                    MaxPoolingLayer(pool_size=4, \
                                    stride=2),
                    ConvolutionalLayer(in_channels=input_shape[2], \
                                       out_channels=input_shape[2], \
                                       filter_size=conv2_channels, \
                                       padding=2),
                    ReLULayer(),
                    MaxPoolingLayer(pool_size=4, \
                                    stride=2),
                    Flattener(),
                    FullyConnectedLayer(n_input=192, \
                                        n_output=n_output_classes)]
Esempio n. 21
0
    def __init__(self, input_shape, n_input_classes, n_output_classes,
                 conv1_channels, conv2_channels, reg):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        stride = 1
        conv1_channels, int - number of filters in the 1st conv layer, padding=1
        after first conv  - shape (2, 32, 32, 2)
        after first max pooling  - shape (2, 8, 8, 2)
        conv2_channels, int - number of filters in the 2nd conv layer, padding=1
        after second conv  - shape (2, 8, 8, 2)
        after second max poolling - width =2, height = 2 channels = 2
        for convolutional layer in_channels = 8
        """
        # TODO Create necessary layers
        layers = []
        image_width, image_height, channels = input_shape
        conv1_layer = ConvolutionalLayer(in_channels=channels,
                                         out_channels=conv1_channels,
                                         filter_size=3,
                                         padding=1)
        layers.append(conv1_layer)
        layers.append(ReLULayer())
        layers.append(MaxPoolingLayer(4, 4))

        conv2_layer = ConvolutionalLayer(in_channels=conv1_channels,
                                         out_channels=conv2_channels,
                                         filter_size=3,
                                         padding=1)

        layers.append(conv2_layer)
        layers.append(ReLULayer())
        layers.append(MaxPoolingLayer(4, 4))
        layers.append(Flattener())
        layers.append(FullyConnectedLayer(n_input_classes, n_output_classes))

        self.layers = layers
        self.reg = reg
Esempio n. 22
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.conv1 = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1)
        self.reLu1 = ReLULayer()
        self.mxPl1 = MaxPoolingLayer(4, 4)
        self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1)
        self.reLu2 = ReLULayer()
        self.mxPl2 = MaxPoolingLayer(4, 4)
        self.flat = Flattener()
        self.fCL = FullyConnectedLayer(4 * conv2_channels, n_output_classes)
Esempio n. 23
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        #raise Exception("Not implemented!")

        image_width, image_height, image_channels = input_shape

        maxpool1_size = 4
        maxpool2_size = 4

        flattener_width = int(image_width / (maxpool1_size * maxpool2_size))
        flattener_height = int(image_width / (maxpool1_size * maxpool2_size))

        self.layers = [
            ConvolutionalLayer(in_channels=image_channels,
                               out_channels=conv1_channels,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(maxpool1_size, maxpool1_size),
            ConvolutionalLayer(in_channels=conv1_channels,
                               out_channels=conv2_channels,
                               filter_size=3,
                               padding=1),
            ReLULayer(),
            MaxPoolingLayer(maxpool2_size, maxpool2_size),
            Flattener(),
            FullyConnectedLayer(
                flattener_width * flattener_height * conv2_channels,
                n_output_classes)
        ]
Esempio n. 24
0
    def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        #raise Exception("Not implemented!")
        self.layers = {"conv1": ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1),
                  "relu1": ReLULayer(),
                  "maxpool1": MaxPoolingLayer(4, 4),
                  "conv2": ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1),
                  "relu2": ReLULayer(),
                  "maxpool2": MaxPoolingLayer(4, 4),
                  "flatten": Flattener(),
                  "fc": FullyConnectedLayer((input_shape[0]//16)*(input_shape[1]//16)*conv2_channels, n_output_classes)}
Esempio n. 25
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.layer_1 = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1)
        self.layer_2 = ReLULayer()
        self.layer_3 = MaxPoolingLayer(4, 4)
        self.layer_4 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1)
        self.layer_5 = ReLULayer()
        self.layer_6 = MaxPoolingLayer(4, 4)
        self.layer_7 = Flattener()
        self.layer_8 = FullyConnectedLayer(
            (input_shape[0] * input_shape[1] * conv2_channels) // (16**2),
            n_output_classes)
Esempio n. 26
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels, reg):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        self.reg = reg

        self.conv1 = ConvolutionalLayer(in_channels=input_shape[-1],
                                        out_channels=conv1_channels,
                                        filter_size=3,
                                        padding=1)
        self.relu1 = ReLULayer()
        self.maxpool1 = MaxPoolingLayer(pool_size=4, stride=4)
        self.conv2 = ConvolutionalLayer(in_channels=conv1_channels,
                                        out_channels=conv2_channels,
                                        filter_size=3,
                                        padding=1)
        self.relu2 = ReLULayer()
        self.maxpool2 = MaxPoolingLayer(pool_size=4, stride=4)
        self.flattener = Flattener()

        ## n_input = 4*conv2_channels - hard coding here, because of constant picture size 32 32 3

        self.fullyconlayer = FullyConnectedLayer(n_input=4 * conv2_channels,
                                                 n_output=n_output_classes)

        self.W_fc_layer = None
        self.B_fc_layer = None
        self.W_con1_layer = None
        self.B_con1_layer = None
        self.W_con2_layer = None
        self.B_con2_layer = None
Esempio n. 27
0
 def __init__(self, input_shape, n_output_classes, conv1_channels,
              conv2_channels):
     """
     Initializes the neural network
     :param input_shape: tuple of 3 ints - image_width, image_height, n_channels
     :param n_output_classes: int - number of classes to predict
     :param conv1_channels: int - number of filters in the 1st conv layer
     :param conv2_channels: int - number of filters in the 2nd conv layer
     """
     self.convolution_one = ConvolutionalLayer(input_shape[2],
                                               conv1_channels, 3, 1)
     self.relu_one = ReLULayer()
     self.maxpool_one = MaxPoolingLayer(4, 4)
     self.convolution_two = ConvolutionalLayer(conv1_channels,
                                               conv2_channels, 3, 1)
     self.relu_two = ReLULayer()
     self.maxpool_two = MaxPoolingLayer(4, 4)
     self.flattener = Flattener()
     height = ((input_shape[0] + 2 * 1 - 3 + 1) // 4 + 2 * 1 - 3 + 1) // 4
     width = ((input_shape[1] + 2 * 1 - 3 + 1) // 4 + 2 * 1 - 3 + 1) // 4
     self.fc = FullyConnectedLayer(width * height * conv2_channels,
                                   n_output_classes)
Esempio n. 28
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        filter_size = 3
        padding = 1
        pool_size = 4
        stride = 4
        width, height, n_channels = input_shape
        assert ((height + 2 * padding - filter_size + 1) % pool_size == 0)
        assert ((width + 2 * padding - filter_size + 1) % pool_size == 0)
        height = (height + 2 * padding - filter_size + 1) // pool_size
        width = (width + 2 * padding - filter_size + 1) // pool_size
        assert ((height + 2 * padding - filter_size + 1) % pool_size == 0)
        assert ((width + 2 * padding - filter_size + 1) % pool_size == 0)
        height = (height + 2 * padding - filter_size + 1) // pool_size
        width = (width + 2 * padding - filter_size + 1) // pool_size

        # TODO Create necessary layers
        self.Conv_1 = ConvolutionalLayer(n_channels, conv1_channels,
                                         filter_size, padding)
        self.Relu_1 = ReLULayer()
        self.Maxpool_1 = MaxPoolingLayer(pool_size, stride)
        self.Conv_2 = ConvolutionalLayer(conv1_channels, conv2_channels,
                                         filter_size, padding)
        self.Relu_2 = ReLULayer()
        self.Maxpool_2 = MaxPoolingLayer(pool_size, stride)
        self.Flattener = Flattener()
        self.FC = FullyConnectedLayer(height * width * conv2_channels,
                                      n_output_classes)
Esempio n. 29
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        width, height, n_input_channels = input_shape
        kernel_size = 3
        padding = 1
        conv_stride = 1
        pooling_stride = 4
        filter_size = 4

        conv1_output = (width - kernel_size + 2 * padding) / conv_stride + 1
        pooling1_output = (conv1_output - filter_size) / pooling_stride + 1
        conv2_output = (pooling1_output - kernel_size +
                        2 * padding) / conv_stride + 1
        pooling2_output = (conv2_output - filter_size) / pooling_stride + 1
        fc_input = int(pooling2_output * pooling2_output * conv2_channels)

        self.Sequential = [
            ConvolutionalLayer(n_input_channels, conv1_channels, kernel_size,
                               padding),
            ReLULayer(),
            MaxPoolingLayer(filter_size, pooling_stride),
            ConvolutionalLayer(conv1_channels, conv2_channels, kernel_size,
                               padding),
            ReLULayer(),
            MaxPoolingLayer(filter_size, pooling_stride),
            Flattener(),
            FullyConnectedLayer(fc_input, n_output_classes)
        ]
Esempio n. 30
0
    def __init__(self, input_shape, n_output_classes, conv1_channels,
                 conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        self.layers = []

        image_width, image_height, n_channels = input_shape
        filter_size = 3
        pool_size = 4
        stride = pool_size
        padding = 1
        fc_input = (image_height //
                    (pool_size**2)) * (image_width //
                                       (pool_size**2)) * conv2_channels

        self.layers.append(
            ConvolutionalLayer(n_channels, conv1_channels, filter_size,
                               padding))
        self.layers.append(ReLULayer())
        self.layers.append(MaxPoolingLayer(pool_size, stride))

        self.layers.append(
            ConvolutionalLayer(conv1_channels, conv2_channels, filter_size,
                               padding))
        self.layers.append(ReLULayer())
        self.layers.append(MaxPoolingLayer(pool_size, stride))

        self.layers.append(Flattener())
        self.layers.append(FullyConnectedLayer(fc_input, n_output_classes))