예제 #1
0
def net_arguments():
    np.random.seed(SEED)

    layer1 = joey.MaxPooling(kernel_size=(2, 2),
                             input_size=(1, 2, 4, 4),
                             generate_code=False)
    layer2 = joey.Conv(kernel_size=(2, 2, 2),
                       input_size=(1, 2, 3, 3),
                       activation=ReLU(),
                       generate_code=False)
    layer_flat = joey.Flat(input_size=(1, 2, 2, 2), generate_code=False)
    layer3 = joey.FullyConnectedSoftmax(weight_size=(3, 8),
                                        input_size=(8, 1),
                                        generate_code=False)

    layers = [layer1, layer2, layer_flat, layer3]

    net = joey.Net(layers)

    pytorch_net = Net()
    pytorch_net.double()

    with torch.no_grad():
        pytorch_net.conv.weight[:] = torch.from_numpy(layer2.kernel.data)
        pytorch_net.conv.bias[:] = torch.from_numpy(layer2.bias.data)

        pytorch_net.fc.weight[:] = torch.from_numpy(layer3.kernel.data)
        pytorch_net.fc.bias[:] = torch.from_numpy(layer3.bias.data)

    return (net, pytorch_net, layers)
예제 #2
0
def test_max_pooling():
    layer = joey.MaxPooling(kernel_size=(2, 2), input_size=(2, 2, 3, 3))
    output = layer.execute(
        np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, 14, 15], [16, 17, 18]]],
                  [[[19, 20, 21], [22, 23, 24], [25, 26, 27]],
                   [[28, 29, 30], [31, 32, 33], [34, 35, 36]]]]))

    assert (np.array_equal(output,
                           [[[[5, 6], [8, 9]], [[14, 15], [17, 18]]],
                            [[[23, 24], [26, 27]], [[32, 33], [35, 36]]]]))
예제 #3
0
def test_max_pooling_relu():
    layer = joey.MaxPooling(kernel_size=(2, 2),
                            input_size=(2, 2, 3, 3),
                            activation=ReLU())
    output = layer.execute(
        np.array([[[[-1, -2, 3], [-4, -5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, -14, -15], [16, -17, -18]]],
                  [[[19, -20, -21], [22, -23, -24], [25, 26, 27]],
                   [[28, 29, 30], [31, 32, 33], [34, 35, 36]]]]))

    assert (np.array_equal(output,
                           [[[[0, 6], [8, 9]], [[13, 12], [16, 0]]],
                            [[[22, 0], [26, 27]], [[32, 33], [35, 36]]]]))
예제 #4
0
def net_arguments():
    np.random.seed(SEED)

    # Six 3x3 filters, activation RELU
    layer1 = joey.Conv(kernel_size=(6, 3, 3),
                       input_size=(BATCH_SIZE, 1, 32, 32),
                       activation=joey.activation.ReLU(),
                       generate_code=False)
    # 2x2 max pooling
    layer2 = joey.MaxPooling(kernel_size=(2, 2),
                             input_size=(BATCH_SIZE, 6, 30, 30),
                             stride=(2, 2),
                             generate_code=False)
    # Sixteen 3x3 filters, activation RELU
    layer3 = joey.Conv(kernel_size=(16, 3, 3),
                       input_size=(BATCH_SIZE, 6, 15, 15),
                       activation=joey.activation.ReLU(),
                       generate_code=False)
    # 2x2 max pooling
    layer4 = joey.MaxPooling(kernel_size=(2, 2),
                             input_size=(BATCH_SIZE, 16, 13, 13),
                             stride=(2, 2),
                             strict_stride_check=False,
                             generate_code=False)
    # Full connection (16 * 6 * 6 -> 120), activation RELU
    layer5 = joey.FullyConnected(weight_size=(120, 576),
                                 input_size=(576, BATCH_SIZE),
                                 activation=joey.activation.ReLU(),
                                 generate_code=False)
    # Full connection (120 -> 84), activation RELU
    layer6 = joey.FullyConnected(weight_size=(84, 120),
                                 input_size=(120, BATCH_SIZE),
                                 activation=joey.activation.ReLU(),
                                 generate_code=False)
    # Full connection (84 -> 10), output layer
    layer7 = joey.FullyConnectedSoftmax(weight_size=(10, 84),
                                        input_size=(84, BATCH_SIZE),
                                        generate_code=False)
    # Flattening layer necessary between layer 4 and 5
    layer_flat = joey.Flat(input_size=(BATCH_SIZE, 16, 6, 6),
                           generate_code=False)

    layers = [
        layer1, layer2, layer3, layer4, layer_flat, layer5, layer6, layer7
    ]

    net = joey.Net(layers)

    pytorch_net = Net()
    pytorch_net.double()

    with torch.no_grad():
        pytorch_net.conv1.weight[:] = torch.from_numpy(layer1.kernel.data)
        pytorch_net.conv1.bias[:] = torch.from_numpy(layer1.bias.data)

        pytorch_net.conv2.weight[:] = torch.from_numpy(layer3.kernel.data)
        pytorch_net.conv2.bias[:] = torch.from_numpy(layer3.bias.data)

        pytorch_net.fc1.weight[:] = torch.from_numpy(layer5.kernel.data)
        pytorch_net.fc1.bias[:] = torch.from_numpy(layer5.bias.data)

        pytorch_net.fc2.weight[:] = torch.from_numpy(layer6.kernel.data)
        pytorch_net.fc2.bias[:] = torch.from_numpy(layer6.bias.data)

        pytorch_net.fc3.weight[:] = torch.from_numpy(layer7.kernel.data)
        pytorch_net.fc3.bias[:] = torch.from_numpy(layer7.bias.data)

    return (net, pytorch_net, layers)