Пример #1
0
def test_model_acc():
    correct = 0
    for test_batch_in, test_batch_out in test_batches:
        test_output = my_model(Variable(test_batch_in)).data
        correct += np.sum(np.argmax(test_output, axis=1) == np.argmax(test_batch_out, axis=1))

    my_acc = correct / len(test_dataset)
    return my_acc
Пример #2
0
    def __init__(self, num_input, num_output):
        super().__init__()

        self.num_input = num_input
        self.num_output = num_output

        self.weights = Variable(
            np.random.normal(0, 0.05, (self.num_input, self.num_output)))
        self.biases = variable.zeros(self.num_output, np.float32)

        self.register_variables(('weight', self.weights),
                                ('bias', self.biases))
Пример #3
0
    def __init__(self,
                 input_channels,
                 output_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 add_bias=True):
        super().__init__()

        if isinstance(kernel_size, int):
            kernel_size = (kernel_size, kernel_size)

        if isinstance(stride, int):
            stride = (stride, stride)

        if isinstance(padding, int):
            padding = (padding, padding)

        self.input_channels = input_channels
        self.output_channels = output_channels
        self.kernel_size = kernel_size
        self.padding = padding
        self.stride = stride

        self.weights = Variable(
            np.random.normal(0, 0.05,
                             (self.output_channels, self.input_channels,
                              self.kernel_size[0], self.kernel_size[1])))

        self.add_bias = add_bias
        if self.add_bias:
            self.biases = variable.zeros(output_channels)
            self.register_variables(('weight', self.weights),
                                    ('bias', self.biases))
        else:
            self.register_variables(('weight', self.weights))

        self.img2col = Img2Col(self.kernel_size, self.stride)
Пример #4
0
    for test_batch_in, test_batch_out in test_batches:
        test_output = my_model(Variable(test_batch_in)).data
        correct += np.sum(np.argmax(test_output, axis=1) == np.argmax(test_batch_out, axis=1))

    my_acc = correct / len(test_dataset)
    return my_acc


finished = False
for it in range(iterations):
    if finished:
        break
    train_batches.shuffle()
    start = time()
    for i_b, (batch_in, batch_out) in enumerate(train_batches):
        model_input = Variable(batch_in)
        good_output = Variable(batch_out)
        model_output = my_model(model_input)

        err = loss(good_output, model_output)

        optimizer.zero_grad()
        err.backward()
        optimizer.step()

        if i_b % 100 == 0:
            print(i_b)

    print("epoch time: {:.2f} seconds".format(time() - start))
    acc = test_model_acc()
    print("model accuracy: {}".format(acc))
Пример #5
0
class Conv2d(Module):
    def __init__(self,
                 input_channels,
                 output_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 add_bias=True):
        super().__init__()

        if isinstance(kernel_size, int):
            kernel_size = (kernel_size, kernel_size)

        if isinstance(stride, int):
            stride = (stride, stride)

        if isinstance(padding, int):
            padding = (padding, padding)

        self.input_channels = input_channels
        self.output_channels = output_channels
        self.kernel_size = kernel_size
        self.padding = padding
        self.stride = stride

        self.weights = Variable(
            np.random.normal(0, 0.05,
                             (self.output_channels, self.input_channels,
                              self.kernel_size[0], self.kernel_size[1])))

        self.add_bias = add_bias
        if self.add_bias:
            self.biases = variable.zeros(output_channels)
            self.register_variables(('weight', self.weights),
                                    ('bias', self.biases))
        else:
            self.register_variables(('weight', self.weights))

        self.img2col = Img2Col(self.kernel_size, self.stride)

    # https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/content/making_faster.html
    def forward(self, input_variable: Variable) -> Variable:
        """
        Performs 2D convolution on input_variable.
        Args:
            input_variable (np.array): input image allowed shapes:
                                [N, C, H, W], [C, H, W]
                                N - batches,
                                C - channels,
                                H - height,
                                W - width
        """

        img2col = self.img2col(input_variable)
        unformed_res = self.weights.reshape(self.weights.shape[0],
                                            -1) @ img2col

        img_w = input_variable.shape[-1]
        img_h = input_variable.shape[-2]
        # new image width
        new_w = (img_w - self.kernel_size[0]) // self.stride[0] + 1

        # new image height
        new_h = (img_h - self.kernel_size[1]) // self.stride[1] + 1

        batch_input = len(input_variable.shape) == 4

        if batch_input:
            output_shape = (input_variable.shape[0], self.output_channels,
                            new_h, new_w)
        else:
            output_shape = (self.output_channels, new_h, new_w)

        if self.add_bias:
            unformed_res = (unformed_res.swap_axes(-1, -2) +
                            self.biases).swap_axes(-1, -2)

        return unformed_res.reshape(*output_shape)