Beispiel #1
0
    def pass_forward(self, inputs, train_mode=True, **kwargs):
        input_num, input_depth, input_height, input_width = inputs.shape
        self.inputs = inputs

        assert (input_height -
                self.pool_size[0]) % self.strides[0] == 0, 'Invalid height'
        assert (input_width -
                self.pool_size[1]) % self.strides[1] == 0, 'Invalid width'

        output_height, output_width = get_output_dims(input_height,
                                                      input_width,
                                                      self.pool_size,
                                                      self.strides)

        input_reshaped = inputs.reshape(input_num * input_depth, 1,
                                        input_height, input_width)
        self.input_col = im2col_indices(input_reshaped,
                                        self.pool_size[0],
                                        self.pool_size[1],
                                        padding=(self.pad_height,
                                                 self.pad_width),
                                        stride=self.strides[0])

        output, self.pool_cache = self.pool_forward(self.input_col)

        output = output.reshape(int(output_height), int(output_width),
                                input_num, input_depth)

        return output.transpose(2, 3, 0, 1)
    def output_shape(self):
        pad_height, pad_width = get_pad(self.padding, self.input_shape[1],
                                        self.input_shape[2], self.strides[0],
                                        self.strides[1], self.kernel_size[0],
                                        self.kernel_size[1])

        output_height, output_width = get_output_dims(self.input_shape[1],
                                                      self.input_shape[2],
                                                      self.kernel_size,
                                                      self.strides,
                                                      self.padding)

        return self.filters, int(output_height), int(output_width)
    def pass_forward(self, inputs, train_mode=True, **kwargs):
        self.filter_num, _, _, _ = self.weights.shape
        self.input_shape = inputs.shape
        self.inputs = inputs

        input_num, input_depth, input_height, input_width = inputs.shape

        pad_height, pad_width = get_pad(self.padding, input_height,
                                        input_width, self.strides[0],
                                        self.strides[1], self.kernel_size[0],
                                        self.kernel_size[1])

        x_padded = np.pad(self.inputs, ((0, 0), (0, 0), pad_height, pad_width),
                          mode='constant')

        # confirm dimensions
        assert (input_height + np.sum(pad_height) - self.kernel_size[0]
                ) % self.strides[0] == 0, 'height does not work'
        assert (input_width + np.sum(pad_width) - self.kernel_size[1]
                ) % self.strides[1] == 0, 'width does not work'

        # compute output_height and output_width
        output_height, output_width = get_output_dims(input_height,
                                                      input_width,
                                                      self.kernel_size,
                                                      self.strides,
                                                      self.padding)

        output = np.zeros(
            (input_num, self.filter_num, output_height, output_width))

        self.input_col = unroll_inputs(x_padded, x_padded.shape[0],
                                       x_padded.shape[1], output_height,
                                       output_width, self.kernel_size[0])

        # TODO: weights need to be rearraged in a way to have a matrix
        #       multiplication with the generated toeplitz matrix
        self.weight_col = self.weights.reshape(self.filter_num, -1)

        # calculate ouput
        output = self.weight_col @ self.input_col + self.bias
        # output = np.matmul(self.weight_col, self.input_col) + self.bias
        output = output.reshape(self.filter_num, int(output_height),
                                int(output_width), input_num)

        return output.transpose(3, 0, 1, 2)
    def pass_forward(self, inputs, train_mode=True, **kwargs):
        self.filter_num, _, _, _ = self.weights.shape
        self.input_shape = inputs.shape
        self.inputs = inputs

        input_num, input_depth, input_height, input_width = inputs.shape

        pad_height, pad_width = get_pad(self.padding, input_height,
                                        input_width, self.strides[0],
                                        self.strides[1], self.kernel_size[0],
                                        self.kernel_size[1])

        x_padded = np.pad(self.inputs, ((0, 0), (0, 0), pad_height, pad_width),
                          mode='constant')

        # confirm dimensions
        assert (input_height + np.sum(pad_height) - self.kernel_size[0]
                ) % self.strides[0] == 0, 'height does not work'
        assert (input_width + np.sum(pad_width) - self.kernel_size[1]
                ) % self.strides[1] == 0, 'width does not work'

        # compute output_height and output_width
        output_height, output_width = get_output_dims(input_height,
                                                      input_width,
                                                      self.kernel_size,
                                                      self.strides,
                                                      self.padding)

        output = np.zeros(
            (input_num, self.filter_num, output_height, output_width))

        # convolutions
        for b in np.arange(input_num):  # batch number
            for f in np.arange(self.filter_num):  # filter number
                for h in np.arange(output_height):  # output height
                    for w in np.arange(output_width):  # output width
                        h_stride, w_stride = h * self.strides[
                            0], w * self.strides[1]
                        x_patch = x_padded[b, :, h_stride:h_stride +
                                           self.kernel_size[0],
                                           w_stride:w_stride +
                                           self.kernel_size[1]]
                        output[b, f, h, w] = np.sum(
                            x_patch * self.weights[f]) + self.bias[f]

        return output
Beispiel #5
0
    def output_shape(self):
        input_channels, input_height, input_width = self.input_shape

        self.pad_height, self.pad_width = get_pad(self.padding, input_height,
                                                  input_width, self.strides[0],
                                                  self.strides[1],
                                                  self.pool_size[0],
                                                  self.pool_size[1])

        output_height, output_width = get_output_dims(input_height,
                                                      input_width,
                                                      self.pool_size,
                                                      self.strides,
                                                      self.padding)

        assert output_height % 1 == 0
        assert output_width % 1 == 0

        return input_channels, int(output_height), int(output_width)
    def pass_forward(self, inputs, train_mode=True, **kwargs):
        self.filter_num, _, _, _ = self.weights.shape
        self.input_shape = inputs.shape
        self.inputs = inputs

        input_num, input_depth, input_height, input_width = inputs.shape

        pad_height, pad_width = get_pad(self.padding, input_height,
                                        input_width, self.strides[0],
                                        self.strides[1], self.kernel_size[0],
                                        self.kernel_size[1])

        # confirm dimensions
        assert (input_height + np.sum(pad_height) - self.kernel_size[0]
                ) % self.strides[0] == 0, 'height does not work'
        assert (input_width + np.sum(pad_width) - self.kernel_size[1]
                ) % self.strides[1] == 0, 'width does not work'

        # compute output_height and output_width
        output_height, output_width = get_output_dims(input_height,
                                                      input_width,
                                                      self.kernel_size,
                                                      self.strides,
                                                      self.padding)

        # convert to columns
        self.input_col = im2col_indices(inputs,
                                        self.kernel_size[0],
                                        self.kernel_size[1],
                                        padding=(pad_height, pad_width),
                                        stride=1)

        self.weight_col = self.weights.reshape(self.filter_num, -1)

        # calculate ouput
        output = self.weight_col @ self.input_col + self.bias
        output = output.reshape(self.filter_num, int(output_height),
                                int(output_width), input_num)

        return output.transpose(3, 0, 1, 2)