Esempio n. 1
0
def test_graph_attention_block():
    """Test the graph conv block"""

    in_dict = build_input_dict(dims=[100, 5, 7])
    adj_matrix_batch = construct_pre_processing_matrix_adj_bar()[0].repeat(
        [100, 1, 1])
    in_dict['adj_matrix'] = adj_matrix_batch
    net: GraphAttentionBlock = GraphAttentionBlock(
        in_keys=list(in_dict.keys()),
        out_keys='out_key',
        in_shapes=[value.shape[1:] for value in in_dict.values()],
        hidden_features=[11, 13],
        non_lins=[nn.ReLU, nn.Identity],
        attention_alpha=0.2,
        n_heads=[3, 1],
        attention_dropout=0,
        avg_last_head_attentions=False)

    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_features == 13
    assert out_dict[net.out_keys[0]].shape[-1] == net.output_features
    assert out_dict[net.out_keys[0]].shape == (100, 5, 13)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-2:]]
    assert net.get_num_of_parameters() == (7 * 11 + 11 * 2) * 3 + (33 * 13 +
                                                                   13 * 2)

    net.apply(make_module_init_normc(1.0))
Esempio n. 2
0
def test_graph_cnn_layer():
    """Test the graph conv layer"""

    feat_input = build_input_dict(dims=[100, 5, 7])['in_key']
    adj_matrix_batch = construct_pre_processing_matrix()[0].repeat(
        [100, 1, 1]).to(torch.float32)
    graph_conv_layer = GraphConvLayer(in_features=7,
                                      out_features=11,
                                      bias=False)
    assert graph_conv_layer.weight.requires_grad is True
    assert graph_conv_layer.weight.shape == torch.Size([7, 11])
    assert graph_conv_layer.bias is None
    str(graph_conv_layer)
    out = graph_conv_layer(feat_input, adj_matrix_batch)
    assert out.shape == torch.Size([100, 5, 11])

    graph_conv_layer = GraphConvLayer(in_features=7,
                                      out_features=11,
                                      bias=True)
    assert graph_conv_layer.weight.requires_grad is True
    assert graph_conv_layer.weight.shape == torch.Size([7, 11])
    assert graph_conv_layer.bias is not None
    assert graph_conv_layer.bias.requires_grad is True
    assert graph_conv_layer.bias.shape == torch.Size([11])
    out = graph_conv_layer(feat_input, adj_matrix_batch)
    assert out.shape == torch.Size([100, 5, 11])
    str(graph_conv_layer)
Esempio n. 3
0
def test_graph_attention_layer():
    """Test the graph conv layer"""

    feat_input = build_input_dict(dims=[100, 5, 7])['in_key']
    adj_matrix_batch = construct_pre_processing_matrix_adj_bar()[0].repeat(
        [100, 1, 1]).to(torch.float32)
    graph_conv_layer = GraphAttentionLayer(in_features=7,
                                           out_features=11,
                                           alpha=0.2,
                                           dropout=0.0)
    assert graph_conv_layer.weight.requires_grad is True
    assert graph_conv_layer.weight.shape == torch.Size([7, 11])
    str(graph_conv_layer)
    out = graph_conv_layer(feat_input, adj_matrix_batch)
    assert out.shape == torch.Size([100, 5, 11])

    graph_conv_layer = GraphAttentionLayer(in_features=7,
                                           out_features=11,
                                           alpha=.2,
                                           dropout=0.0)
    assert graph_conv_layer.weight.requires_grad is True
    assert graph_conv_layer.weight.shape == torch.Size([7, 11])
    out = graph_conv_layer(feat_input, adj_matrix_batch)
    assert out.shape == torch.Size([100, 5, 11])
    str(graph_conv_layer)
Esempio n. 4
0
def test_graph_cnn_block_trainable_self_importance():
    """Test the graph conv block with a trainable self importance scalar"""

    in_dict = build_input_dict(dims=[100, 5, 7])
    adj_matrix_batch = construct_pre_processing_matrix()[0].repeat([100, 1, 1])
    in_dict['adj_matrix'] = adj_matrix_batch
    net: GraphConvBlock = GraphConvBlock(
        in_keys=list(in_dict.keys()),
        out_keys='out_key',
        in_shapes=[value.shape[1:] for value in in_dict.values()],
        hidden_features=[11, 13],
        bias=[False, True],
        non_lins=[nn.ReLU, 'torch.nn.Identity'],
        node_self_importance=1.0,
        trainable_node_self_importance=True,
        preprocess_adj=True)

    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_features == 13
    assert out_dict[net.out_keys[0]].shape[-1] == net.output_features
    assert out_dict[net.out_keys[0]].shape == (100, 5, 13)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-2:]]
    assert net.get_num_of_parameters() == (7 * 11) + (11 * 13) + 13 + 1

    net.apply(make_module_init_normc(1.0))
Esempio n. 5
0
def test_global_avg_pooling_block():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 3, 64, 64])
    net: GlobalAveragePoolingBlock = GlobalAveragePoolingBlock(
        in_keys="in_key", out_keys="out_key", in_shapes=[(3, 64, 64)])
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert out_dict[net.out_keys[0]].shape == (100, 3)
Esempio n. 6
0
def test_functional_block_single_arg():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 64, 1])
    net: FunctionalBlock = FunctionalBlock(in_keys="in_key",
                                           out_keys="out_key",
                                           in_shapes=(100, 64, 1),
                                           func=torch.squeeze)
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert out_dict[net.out_keys[0]].shape == (100, 64)
Esempio n. 7
0
def test_flatten_block():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 3, 64, 64])
    net: FlattenBlock = FlattenBlock(in_keys="in_key",
                                     out_keys="out_key",
                                     in_shapes=[(3, 64, 64)],
                                     num_flatten_dims=3)
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert out_dict[net.out_keys[0]].shape == (100, 3 * 64 * 64)
Esempio n. 8
0
def test_feed_forward_convolution_block():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 3, 64, 64])
    net: VGGConvolutionBlock = VGGConvolutionBlock(in_keys="in_key", out_keys="out_key",
                                                   in_shapes=(3, 64, 64), hidden_channels=[4, 8, 16], non_lin=nn.ReLU)
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_channels == 16
    assert out_dict[net.out_keys[0]].shape[-3] == net.output_channels
    assert out_dict[net.out_keys[0]].shape == (100, 16, 8, 8)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-3:]]
Esempio n. 9
0
def test_slice_block():
    """ perception test """
    for dims in [[32, 16], [100, 32, 16], [100, 5, 32, 16]]:
        in_dict = build_input_dict(dims=dims)

        net = SliceBlock(in_keys="in_key",
                         out_keys="out_key",
                         in_shapes=(32, 16),
                         slice_dim=-2,
                         slice_idx=-1)
        str(net)
        out_dict = net(in_dict)

        assert isinstance(out_dict, Dict)
        assert net.out_shapes() == [out_dict["out_key"].shape[-1:]]
Esempio n. 10
0
def test_graph_multi_head_layer():
    """Test the graph conv layer"""

    feat_input = build_input_dict(dims=[100, 5, 7])['in_key']
    adj_matrix_batch = construct_pre_processing_matrix_adj_bar()[0].repeat(
        [100, 1, 1]).to(torch.float32)
    graph_conv_layer = GraphMultiHeadAttentionLayer(in_features=7,
                                                    out_features=11,
                                                    alpha=0.2,
                                                    n_heads=3,
                                                    dropout=0,
                                                    avg_out=False)
    str(graph_conv_layer)
    out = graph_conv_layer(feat_input, adj_matrix_batch)
    assert out.shape == torch.Size([100, 5, 33])
Esempio n. 11
0
def test_self_attention_sequential():
    """test_self_attention_sequential"""
    in_dict = build_input_dict(dims=(2, 7, 10))

    self_attn_block = SelfAttentionSeqBlock(in_keys='in_key',
                                            out_keys='self_attention',
                                            in_shapes=(7, 10),
                                            num_heads=10,
                                            dropout=0.0,
                                            bias=False,
                                            add_input_to_output=False)
    str(self_attn_block)
    out_dict = self_attn_block(in_dict)
    assert self_attn_block.get_num_of_parameters() == 401
    assert len(out_dict.keys()) == len(self_attn_block.out_keys) == 1
    assert out_dict[self_attn_block.out_keys[0]].shape == (2, 7, 10)
Esempio n. 12
0
def test_feed_forward_flatten_dense_block():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 3, 64, 64])
    net: FlattenDenseBlock = FlattenDenseBlock(in_keys="in_key",
                                               out_keys="out_key",
                                               in_shapes=(3, 64, 64),
                                               num_flatten_dims=3,
                                               hidden_units=[32, 64],
                                               non_lin=nn.ReLU)
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert out_dict[net.out_keys[0]].shape == (100, 64)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-1:]]
Esempio n. 13
0
def test_lstm_last_step_block():
    """ perception test """
    for dims in [[32, 16], [100, 32, 16], [100, 5, 32, 16]]:
        in_dict = build_input_dict(dims=dims)

        net = LSTMLastStepBlock(in_keys="in_key",
                                out_keys="out_key",
                                in_shapes=(32, 16),
                                hidden_size=64,
                                num_layers=1,
                                bidirectional=True,
                                non_lin=nn.ReLU)
        str(net)
        out_dict = net(in_dict)

        assert isinstance(out_dict, Dict)
        assert net.out_shapes() == [out_dict["out_key"].shape[-1:]]
        assert list(out_dict["out_key"].shape[:-1]) == dims[:-2]
        assert out_dict["out_key"].ndim == len(dims) - 1
Esempio n. 14
0
def test_linear_output_block():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 16])
    net: LinearOutputBlock = LinearOutputBlock(in_keys="in_key",
                                               out_keys="out_key",
                                               in_shapes=(16, ),
                                               output_units=10)
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_units == 10
    assert out_dict[net.out_keys[0]].shape[-1] == net.output_units
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-1:]]

    # test bias setting
    net.set_bias(bias=1)
    assert np.allclose(net.net.bias.detach().numpy(), 1)

    net.set_bias(bias=np.full(10, fill_value=2, dtype=np.float32))
    assert np.allclose(net.net.bias.detach().numpy(), 2)
Esempio n. 15
0
def test_strided_convolution_block_3d():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 2, 16, 16, 16])
    net: StridedConvolutionBlock = StridedConvolutionBlock(
        in_keys="in_key",
        out_keys="out_key",
        in_shapes=(2, 16, 16, 16),
        hidden_channels=[4],
        hidden_strides=[1],
        hidden_kernels=[4],
        non_lin=nn.ReLU,
        convolution_dimension=3,
        hidden_dilations=[1],
        hidden_padding=[0],
        padding_mode=None)
    str(net)
    out_dict = net(in_dict)
    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_channels == 4
    assert out_dict[net.out_keys[0]].shape[-4] == net.output_channels
    assert out_dict[net.out_keys[0]].shape == (100, 4, 13, 13, 13)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-4:]]
Esempio n. 16
0
def test_strided_convolution_block_2d():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 3, 64, 64])
    net: StridedConvolutionBlock = StridedConvolutionBlock(
        in_keys="in_key",
        out_keys="out_key",
        in_shapes=(3, 64, 64),
        hidden_channels=[4, 8, 16],
        hidden_strides=[2, 2, 1],
        hidden_kernels=[3, 3, 5],
        non_lin=nn.ReLU,
        convolution_dimension=2,
        hidden_dilations=None,
        hidden_padding=[1, 1, 1],
        padding_mode='reflect')
    str(net)
    out_dict = net(in_dict)

    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_channels == 16
    assert out_dict[net.out_keys[0]].shape[-3] == net.output_channels
    assert out_dict[net.out_keys[0]].shape == (100, 16, 14, 14)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-3:]]
Esempio n. 17
0
def test_strided_convolution_block_1d():
    """ perception test """
    in_dict = build_input_dict(dims=[100, 1, 32])
    net: StridedConvolutionBlock = StridedConvolutionBlock(
        in_keys="in_key",
        out_keys="out_key",
        in_shapes=(1, 32),
        hidden_channels=[4],
        hidden_strides=[1],
        hidden_kernels=[8],
        non_lin=nn.ReLU,
        convolution_dimension=1,
        hidden_dilations=[2],
        hidden_padding=[0],
        padding_mode=None)
    str(net)
    out_dict = net(in_dict)
    _ = net.get_num_of_parameters()
    assert isinstance(out_dict, Dict)
    assert set(net.out_keys).issubset(set(out_dict.keys()))
    assert net.output_channels == 4
    assert out_dict[net.out_keys[0]].shape[-2] == net.output_channels
    assert out_dict[net.out_keys[0]].shape == (100, 4, 18)
    assert net.out_shapes() == [out_dict[net.out_keys[0]].shape[-2:]]