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)
def test_conv(): layer = joey.Conv(kernel_size=(2, 2, 2), input_size=(2, 2, 3, 3)) layer.kernel.data[:] = [[[[1, -2], [3, 4]], [[5, 6], [7, 8.5]]], [[[-9.5, 10], [11, 12]], [[13, -14], [-15, 16]]]] output = layer.execute( np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1], [0, 1, 0], [0, 0, 2]]], [[[-1, -2, -3], [4, 6, 8], [11, 0, 2]], [[9, 8, 7], [6, 5, 4], [3, 2, 1]]]]), [0, 0]) assert (np.array_equal( output, [[[[37.5, 48], [53, 75]], [[130.5, 109], [171, 253.5]]], [[[216.5, 205], [123, 69.5]], [[100.5, 146], [138, 42]]]]))
def test_conv_relu(): layer = joey.Conv(kernel_size=(2, 2, 2), input_size=(2, 2, 3, 3), activation=ReLU()) layer.kernel.data[:] = [[[[1, -2], [3, 4]], [[5, 6], [7, 8.5]]], [[[-9.5, 10], [11, 12]], [[13, -14], [-15, 16]]]] output = layer.execute( np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1], [0, 1, 0], [0, 0, 2]]], [[[-1, -2, -3], [4, 6, 8], [11, 0, 2]], [[9, 8, 7], [6, 5, 4], [3, 2, 1]]]]), [-50, -79.75]) assert (np.array_equal( output, [[[[0, 0], [3, 25]], [[50.75, 29.25], [91.25, 173.75]]], [[[166.5, 155], [73, 19.5]], [[20.75, 66.25], [58.25, 0]]]]))
def test_conv_larger_stride(): layer = joey.Conv(kernel_size=(2, 2, 2), input_size=(2, 2, 3, 3), stride=(2, 1), strict_stride_check=False) layer.kernel.data[:] = [[[[1, -2], [3, 4]], [[5, 6], [7, 8.5]]], [[[-9.5, 10], [11, 12]], [[13, -14], [-15, 16]]]] output = layer.execute( np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1], [0, 1, 0], [0, 0, 2]]], [[[-1, -2, -3], [4, 6, 8], [11, 0, 2]], [[9, 8, 7], [6, 5, 4], [3, 2, 1]]]]), [0, 0]) assert (np.array_equal( output, [[[[37.5, 48]], [[130.5, 109]]], [[[216.5, 205]], [[100.5, 146]]]]))
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)