Ejemplo n.º 1
0
def test_spatial_reduction():
    input_shape = (32, 32, 3)
    block = block_module.SpatialReduction()
    block.set_state(block.get_state())
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert common.name_in_hps('reduction_type', hp)
Ejemplo n.º 2
0
def test_spatial_reduction():
    input_shape = (32, 32, 3)
    block = reduction.SpatialReduction()
    hp = kerastuner.HyperParameters()

    block = graph_module.deserialize(graph_module.serialize(block))
    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('reduction_type', hp)
Ejemplo n.º 3
0
def test_structured_data_block():
    block = hyperblock_module.StructuredDataBlock()
    block.num_heads = 1
    block.set_state(block.get_state())
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input())

    assert common.name_in_hps('block_type', hp)
Ejemplo n.º 4
0
def test_temporal_reduction():
    input_shape = (32, 10)
    block = block_module.TemporalReduction()
    block.set_config(block.get_config())
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert common.name_in_hps('reduction_type', hp)
Ejemplo n.º 5
0
def test_structured_data_block():
    block = hyperblock_module.StructuredDataBlock()
    block.heads = [ak.ClassificationHead()]
    block.set_state(block.get_state())
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input())

    assert common.name_in_hps('module_type', hp)
Ejemplo n.º 6
0
def test_embedding_block():
    input_shape = (32, )
    block = block_module.EmbeddingBlock()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('pretraining', hp)
    assert name_in_hps('embedding_dim', hp)
Ejemplo n.º 7
0
def test_dense_block():
    input_shape = (32, )
    block = block_module.DenseBlock()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('num_layers', hp)
    assert name_in_hps('use_batchnorm', hp)
Ejemplo n.º 8
0
def test_embedding_block():
    input_shape = (32,)
    block = basic.Embedding()
    block.max_features = 100
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('pretraining', hp)
    assert utils.name_in_hps('embedding_dim', hp)
Ejemplo n.º 9
0
def test_image_block():
    block = hyperblock_module.ImageBlock(normalize=None, augment=None)
    block.set_state(block.get_state())
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input())

    assert common.name_in_hps('block_type', hp)
    assert common.name_in_hps('normalize', hp)
    assert common.name_in_hps('augment', hp)
Ejemplo n.º 10
0
def test_dense_block():
    input_shape = (32, )
    block = basic.DenseBlock()
    hp = kerastuner.HyperParameters()

    block = graph_module.deserialize(graph_module.serialize(block))
    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('num_layers', hp)
    assert utils.name_in_hps('use_batchnorm', hp)
Ejemplo n.º 11
0
def test_graph_basics():
    input_node = ak.Input(shape=(30, ))
    output_node = input_node
    output_node = ak.DenseBlock()(output_node)
    output_node = ak.RegressionHead(output_shape=(1, ))(output_node)

    graph = graph_module.HyperBuiltGraphHyperModel(input_node, output_node)
    model = graph.build(kerastuner.HyperParameters())
    assert model.input_shape == (None, 30)
    assert model.output_shape == (None, 1)
Ejemplo n.º 12
0
def test_merge(tmp_dir):
    x_train = np.random.rand(100, 32)
    y_train = np.random.rand(100)

    input_node1 = ak.Input()
    input_node2 = ak.Input()
    output_node1 = ak.DenseBlock()(input_node1)
    output_node2 = ak.DenseBlock()(input_node2)
    output_node = ak.Merge()([output_node1, output_node2])
    output_node = ak.RegressionHead()(output_node)

    graph = ak.GraphAutoModel([input_node1, input_node2],
                              output_node,
                              directory=tmp_dir,
                              max_trials=1)
    graph.fit([x_train, x_train], y_train, epochs=1, batch_size=100, verbose=False)
    result = graph.predict([x_train, x_train])

    assert result.shape == (100, 1)
Ejemplo n.º 13
0
def test_imag_augmentation():
    input_shape = (32, 32, 3)
    block = preprocessing.ImageAugmentation()
    hp = kerastuner.HyperParameters()

    block = graph_module.deserialize(graph_module.serialize(block))
    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('vertical_flip', hp)
    assert utils.name_in_hps('horizontal_flip', hp)
Ejemplo n.º 14
0
def test_input_output_disconnect(tmp_dir):
    input_node1 = ak.Input()
    output_node = input_node1
    _ = ak.DenseBlock()(output_node)

    input_node = ak.Input()
    output_node = input_node
    output_node = ak.DenseBlock()(output_node)
    output_node = ak.RegressionHead()(output_node)

    input_node.shape = (32,)
    output_node[0].shape = (1,)

    with pytest.raises(ValueError) as info:
        graph = ak.GraphAutoModel(input_node1,
                                  output_node,
                                  directory=tmp_dir)
        graph.build(kerastuner.HyperParameters())
    assert str(info.value) == 'Inputs and outputs not connected.'
Ejemplo n.º 15
0
def test_rnn_block():
    input_shape = (32, 10)
    block = block_module.RNNBlock()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('bidirectional', hp)
    assert name_in_hps('layer_type', hp)
    assert name_in_hps('num_layers', hp)
Ejemplo n.º 16
0
def test_conv_block():
    input_shape = (32, 32, 3)
    block = block_module.ConvBlock()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('kernel_size', hp)
    assert name_in_hps('num_blocks', hp)
    assert name_in_hps('separable', hp)
Ejemplo n.º 17
0
def test_segmentation():
    y = np.array(['a', 'a', 'c', 'b'])
    head = head_module.SegmentationHead(name='a')
    adapter = head.get_adapter()
    adapter.fit_transform(y)
    head.config_from_adapter(adapter)
    input_shape = (64, 64, 21)
    hp = kerastuner.HyperParameters()
    head = blocks.deserialize(blocks.serialize(head))
    head.build(hp, ak.Input(shape=input_shape).build())
Ejemplo n.º 18
0
def build_model():
    input_layer = ak.Input()
    cnn_layer = ak.ConvBlock()(input_layer)
    cnn_layer2 = ak.ConvBlock()(cnn_layer)
    dense_layer = ak.DenseBlock()(cnn_layer2)
    dense_layer2 = ak.DenseBlock()(dense_layer)
    output_layer = ak.ClassificationHead(num_classes=10)(dense_layer2)

    automodel = ak.auto_model.AutoModel(input_layer, output_layer, max_trials=20, seed=123, project_name='autoML')

    return automodel
Ejemplo n.º 19
0
def test_conv_block():
    input_shape = (32, 32, 3)
    block = basic.ConvBlock()
    hp = kerastuner.HyperParameters()

    block = graph_module.deserialize(graph_module.serialize(block))
    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('kernel_size', hp)
    assert utils.name_in_hps('num_blocks', hp)
    assert utils.name_in_hps('separable', hp)
Ejemplo n.º 20
0
def test_rnn_block():
    input_shape = (32, 10)
    block = basic.RNNBlock()
    hp = kerastuner.HyperParameters()

    block = graph_module.deserialize(graph_module.serialize(block))
    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('bidirectional', hp)
    assert utils.name_in_hps('layer_type', hp)
    assert utils.name_in_hps('num_layers', hp)
Ejemplo n.º 21
0
def test_hyper_graph_cycle(tmp_dir):
    input_node1 = ak.Input()
    input_node2 = ak.Input()
    output_node1 = ak.DenseBlock()(input_node1)
    output_node2 = ak.DenseBlock()(input_node2)
    output_node = ak.Merge()([output_node1, output_node2])
    head = ak.RegressionHead()
    output_node = head(output_node)
    head.outputs = output_node1

    input_node1.shape = (32,)
    input_node2.shape = (32,)
    output_node[0].shape = (1,)

    with pytest.raises(ValueError) as info:
        graph = ak.GraphAutoModel([input_node1, input_node2],
                                  output_node,
                                  directory=tmp_dir)
        graph.build(kerastuner.HyperParameters())
    assert str(info.value) == 'The network has a cycle.'
Ejemplo n.º 22
0
def test_resnet_block(init, build):
    input_shape = (32, 32, 3)
    block = block_module.ResNetBlock()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('version', hp)
    assert name_in_hps('pooling', hp)
    assert init.called
    assert build.called
Ejemplo n.º 23
0
def test_embedding_block():
    input_shape = (32,)
    block = block_module.EmbeddingBlock()
    block.max_features = 100
    block.set_state(block.get_state())
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert common.name_in_hps('pretraining', hp)
    assert common.name_in_hps('embedding_dim', hp)
Ejemplo n.º 24
0
def test_graph_save_load(tmp_path):
    input1 = ak.Input()
    input2 = ak.Input()
    output1 = ak.DenseBlock()(input1)
    output2 = ak.ConvBlock()(input2)
    output = ak.Merge()([output1, output2])
    output1 = ak.RegressionHead()(output)
    output2 = ak.ClassificationHead()(output)

    graph = graph_module.Graph(
        inputs=[input1, input2],
        outputs=[output1, output2],
    )
    path = os.path.join(tmp_path, "graph")
    graph.save(path)
    graph = graph_module.load_graph(path)

    assert len(graph.inputs) == 2
    assert len(graph.outputs) == 2
    assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock)
    assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock)
Ejemplo n.º 25
0
def test_resnet_block(init, build):
    input_shape = (32, 32, 3)
    block = basic.ResNetBlock()
    hp = kerastuner.HyperParameters()

    block = graph_module.deserialize(graph_module.serialize(block))
    block.build(hp, ak.Input(shape=input_shape).build())

    assert utils.name_in_hps('version', hp)
    assert utils.name_in_hps('pooling', hp)
    assert init.called
    assert build.called
Ejemplo n.º 26
0
def test_graph_save_load(tmp_path):
    input1 = ak.Input()
    input2 = ak.Input()
    output1 = ak.DenseBlock()(input1)
    output2 = ak.ConvBlock()(input2)
    output = ak.Merge()([output1, output2])
    output1 = ak.RegressionHead()(output)
    output2 = ak.ClassificationHead()(output)

    graph = graph_module.Graph(
        inputs=[input1, input2],
        outputs=[output1, output2],
        override_hps=[hp_module.Choice('dense_block_1/num_layers', [6], default=6)])
    config = graph.get_config()
    graph = graph_module.Graph.from_config(config)

    assert len(graph.inputs) == 2
    assert len(graph.outputs) == 2
    assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock)
    assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock)
    assert isinstance(graph.override_hps[0], hp_module.Choice)
Ejemplo n.º 27
0
def CreateSupergraph(output_dir, hp_tuner):
    input_node = ak.Input()
    conv2d_1 = ak.ConvBlock(num_blocks=1, num_layers=3,
                            max_pooling=True, dropout=0)(input_node)
    dense_1 = ak.DenseBlock(dropout=0)(conv2d_1)
    output_node = ak.ClassificationHead(num_classes=4,
                                        metrics=['accuracy'])(dense_1)

    automodel = ak.AutoModel(inputs=input_node, outputs=output_node,
                             max_trials=3, directory=output_dir, project_name="autoML",
                             tuner=hp_tuner, seed=123)

    return automodel
Ejemplo n.º 28
0
def test_xception_block(init, build):
    input_shape = (32, 32, 3)
    block = block_module.XceptionBlock()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('activation', hp)
    assert name_in_hps('initial_strides', hp)
    assert name_in_hps('num_residual_blocks', hp)
    assert name_in_hps('pooling', hp)
    assert init.called
    assert build.called
Ejemplo n.º 29
0
def test_segmentation():
    dataset = np.array(["a", "a", "c", "b"])
    head = head_module.SegmentationHead(name="a", shape=(1,))
    adapter = head.get_adapter()
    dataset = adapter.adapt(dataset, batch_size=32)
    analyser = head.get_analyser()
    for data in dataset:
        analyser.update(data)
    analyser.finalize()
    head.config_from_analyser(analyser)
    head.build(
        keras_tuner.HyperParameters(),
        ak.Input(shape=(32,)).build_node(keras_tuner.HyperParameters()),
    )
Ejemplo n.º 30
0
def test_graph_save_load(tmp_path):
    input1 = ak.Input()
    input2 = ak.Input()
    output1 = ak.DenseBlock()(input1)
    output2 = ak.ConvBlock()(input2)
    output = ak.Merge()([output1, output2])
    output1 = ak.RegressionHead()(output)
    output2 = ak.ClassificationHead()(output)

    graph = graph_module.Graph(
        inputs=[input1, input2],
        outputs=[output1, output2],
        override_hps=[hp_module.Choice("dense_block_1/num_layers", [6], default=6)],
    )
    path = os.path.join(tmp_path, "graph")
    graph.save(path)
    graph = graph_module.load_graph(path)

    assert len(graph.inputs) == 2
    assert len(graph.outputs) == 2
    assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock)
    assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock)
    assert isinstance(graph.override_hps[0], hp_module.Choice)