Exemplo n.º 1
0
def test_greedy_oracle_populate_doesnt_crash_with_init_hps(get_best_trials):
    hp = keras_tuner.HyperParameters()
    tf.keras.backend.clear_session()
    input_node = ak.ImageInput(shape=(32, 32, 3))
    input_node.batch_size = 32
    input_node.num_samples = 1000
    output_node = ak.ImageBlock()(input_node)
    head = ak.ClassificationHead(num_classes=10)
    head.shape = (10, )
    output_node = head(output_node)
    graph = ak.graph.Graph(inputs=input_node, outputs=output_node)
    graph.build(hp)

    oracle = greedy.GreedyOracle(
        initial_hps=task_specific.IMAGE_CLASSIFIER,
        objective="val_loss",
        seed=utils.SEED,
    )
    trial = mock.Mock()
    trial.hyperparameters = hp
    get_best_trials.return_value = [trial]

    for i in range(10):
        tf.keras.backend.clear_session()
        values = oracle.populate_space("a")["values"]
        hp = oracle.hyperparameters.copy()
        hp.values = values
        graph.build(hp)
        oracle.update_space(hp)
Exemplo n.º 2
0
    def train():
        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        # Initialize the image regressor.
        input_node = ak.ImageInput()
        output_node = ak.ImageBlock(
            # Only search ResNet architectures.
            block_type="resnet",
            # Normalize the dataset.
            normalize=False,
            # Do not do data augmentation.
            augment=False,
        )(input_node)
        output_node = ak.RegressionHead()(output_node)
        reg = ak.AutoModel(inputs=input_node,
                           outputs=output_node,
                           overwrite=True,
                           max_trials=1)

        # Feed the image regressor with training data.
        reg.fit(
            x_train,
            y_train,
            # Split the training data and use the last 15% as validation data.
            validation_split=0.15,
            epochs=2,
        )

        # Predict with the best model.
        predicted_y = reg.predict(x_test)
        print(predicted_y)

        # Evaluate the best model with testing data.
        print(reg.evaluate(x_test, y_test))
Exemplo n.º 3
0
def build_graph():
    tf.keras.backend.clear_session()
    image_input = ak.ImageInput(shape=(32, 32, 3))
    merged_outputs = ak.ImageBlock()(image_input)
    head = ak.ClassificationHead(num_classes=10)
    head.output_shape = (10, )
    classification_outputs = head(merged_outputs)
    return ak.graph.Graph(inputs=image_input, outputs=classification_outputs)
Exemplo n.º 4
0
def test_image_classifier_tuner1():
    tf.keras.backend.clear_session()
    input_node = ak.ImageInput(shape=(32, 32, 3))
    output_node = ak.ImageBlock()(input_node)
    output_node = ak.ClassificationHead(loss='categorical_crossentropy',
                                        output_shape=(10, ))(output_node)
    graph = graph_module.Graph(input_node, output_node)
    check_initial_hp(task_specific.IMAGE_CLASSIFIER[1], graph)
Exemplo n.º 5
0
def test_image_classifier_oracle():
    tf.keras.backend.clear_session()
    input_node = ak.ImageInput(shape=(32, 32, 3))
    output_node = ak.ImageBlock()(input_node)
    output_node = ak.ClassificationHead(loss='categorical_crossentropy',
                                        output_shape=(10, ))(output_node)
    graph = graph_module.Graph(input_node, output_node)
    oracle = greedy.GreedyOracle(hypermodel=graph,
                                 initial_hps=task_specific.IMAGE_CLASSIFIER,
                                 objective='val_loss')
    oracle._populate_space('0')
    hp = oracle.get_space()
    hp.values = task_specific.IMAGE_CLASSIFIER[0]
    assert len(
        set(task_specific.IMAGE_CLASSIFIER[0].keys()) -
        set(oracle.get_space().values.keys())) == 0
    oracle._populate_space('1')
    assert len(
        set(task_specific.IMAGE_CLASSIFIER[1].keys()) -
        set(oracle.get_space().values.keys())) == 0
Exemplo n.º 6
0
)
"""
## Customized Search Space
For advanced users, you may customize your search space by using AutoModel instead of
ImageClassifier. You can configure the ImageBlock for some high-level configurations,
e.g., block_type for the type of neural network to search, normalize for whether to do
data normalization, augment for whether to do data augmentation. You can also do not
specify these arguments, which would leave the different choices to be tuned
automatically. See the following example for detail.
"""

input_node = ak.ImageInput()
output_node = ak.ImageBlock(
    # Only search ResNet architectures.
    block_type="resnet",
    # Normalize the dataset.
    normalize=True,
    # Do not do data augmentation.
    augment=False,
)(input_node)
output_node = ak.ClassificationHead()(output_node)
clf = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=1)
clf.fit(x_train, y_train, epochs=10)
"""
The usage of AutoModel is similar to the functional API of Keras. Basically, you are
building a graph, whose edges are blocks and the nodes are intermediate outputs of
blocks. To add an edge from input_node to output_node with output_node =
ak.[some_block]([block_args])(input_node).

You can even also use more fine grained blocks to customize the search space even
further. See the following example.
"""
data
# encoding labels base on task(regession/classification)

# train-test split
data_train, data_test = train_test_split(data,
                                         train_size=0.8,
                                         random_state=123)

# validation/cross-validation applied
data_train, data_val = train_test_split(data_train,
                                        train_size=0.8,
                                        random_state=456)

# build AutoML (e.g. autokeras & try one from semi-scratch)
input_node = ak.ImageInput()
output_node = ak.ImageBlock()(input_node)
output_node = ak.ClassificationHead()
model = ak.AutoModel(tuner='bayesian',
                     inputs=input_node,
                     outputs=output_node,
                     max_trials=100,
                     overwrite=True,
                     seed=10)

# Train model
model.fit(x_train, y_train, epochs=200, validation_data=(x_val, y_val))

# Evaluate model
score = model.evaluate(x_test, y_test)
print(score)
Exemplo n.º 8
0
    def autokeras_train(self, ui):

        import autokeras as ak
        # simple pre-processing required of just scaling the data
        # autokeras makes a mess of reshaping input and one hot encoding label outputs by creating a custom layer
        # load the training and testing data, then scale it into the range [0, 1]

        print("[INFO] pre-processing", ui.dataset, "...")
        trainX, trainY, testX, testY = pre_processor(ui)

        for model in range(ui.ak_model):

            # train our Auto-Keras model
            print("[INFO] Training a new Autokeras model: ", model)

            # max_trials is max number of Keras Models to try.
            # search may finish before reaching the max_trials. Defaults to 100.
            # for exhaustive search set max_trials=50
            if ui.optimize == 'autokeras':

                print("Training with Autokeras ..")
                test_model = ak.ImageClassifier(max_trials=ui.ak_trial,
                                                seed=ui.my_seed)

            if ui.optimize == 'automodel':

                print("Training with Automodel ..")

                input_node = ak.ImageInput()
                output_node = ak.ImageBlock(
                    # Specify architecture subset to search
                    block_type=ui.ak_seek,
                    # Normalize the dataset
                    normalize=True,
                    # don't use data augmentation.
                    augment=False)(input_node)
                output_node = ak.ClassificationHead()(output_node)
                test_model = ak.AutoModel(inputs=input_node,
                                          outputs=output_node,
                                          max_trials=ui.ak_trial,
                                          objective="val_accuracy",
                                          seed=ui.my_seed)

            # number of epochs to train each model during search, if unspecified, defaults to max of 1000.
            # Training stops if the validation loss stops improving for 10 epochs.
            # for exhaustive search set epochs=100
            test_model.fit(trainX,
                           trainY,
                           validation_split=0.3,
                           epochs=ui.epochs,
                           verbose=2)

            print("saving the model ..")
            best_model = test_model.export_model()
            best_model.summary()

            filename = ui.dataset + '-' + 'model_number_' + str(model) + '.h5'
            print("filename: ", filename)
            best_model.save(filename)  # Save the model

        return
Exemplo n.º 9
0
num_classes = 10
shape = (28, 28, 1)
# Simple
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
auto_pipeline = ak.ImageClassifier(shape, num_classes)

# Loss, optimizer are picked automatically
auto_pipeline.fit(x_train, y_train)

# The predict function should output the labels instead of numerical vectors.
auto_pipeline.predict(x_test)

# Intermediate
inputs = ak.ImageInput(shape=...)
x = ak.ImageBlock(inputs)
head = ak.ClassificationHead(num_classes, metrics=['accuracy'])
outputs = head(x)
automodel = ak.GraphAutoModel(inputs=inputs, outputs=outputs)

# Loss, optimizer are picked automatically
automodel.fit(x_train, y_train)

# Advanced

inputs = ak.ImageInput(shape=...)
outputs1 = ak.ResNetBlock()(inputs)
outputs2 = ak.XceptionBlock()(inputs)
outputs = ak.Merge()((outputs1, outputs2))
outputs = ak.ClassificationHead(num_classes)(outputs)
automodel = ak.GraphAutoModel(inputs=inputs, outputs=outputs)