Exemple #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 = (input_height -
                         self.pool_size[0]) / self.strides[0] + 1
        output_width = (input_width - self.pool_size[1]) / self.strides[1] + 1

        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)
Exemple #2
0
    def pass_forward(self, inputs, train_mode=True, **kwargs):
        self.filter_num, _, _, _ = self.weights.shape
        input_num, input_depth, input_height, input_width = inputs.shape
        self.input_shape = inputs.shape
        self.inputs = inputs

        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'

        # alternate formula: [((W - KernelW + 2P) / Sw) + 1] and [((H - KernelH + 2P) / Sh) + 1]
        # output_height = ((input_height - self.kernel_size[0] + np.sum(pad_height)) / self.strides[0]) + 1
        # output_width = ((input_width - self.kernel_size[1] + np.sum(pad_width)) / self.strides[1]) + 1

        if self.padding == 'same':
            output_height = np.ceil(
                np.float32(input_height) / np.float32(self.strides[0]))
            output_width = np.ceil(
                np.float32(input_width) / np.float32(self.strides[1]))

        if self.padding == 'valid':
            output_height = np.ceil(
                np.float32(input_height - self.kernel_size[0] + 1) /
                np.float32(self.strides[0]))
            output_width = np.ceil(
                np.float32(input_width - self.kernel_size[1] + 1) /
                np.float32(self.strides[1]))

        # 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)
    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)