示例#1
0
def plot_base_and_best_models():
    """Plot a demo model."""
    space_base_demo_to_plot = {
        'lr_rate_mult': 1.0,
        'l2_weight_reg_mult': 1.0,
        'batch_size': 300,
        'optimizer': 'Nadam',
        'coarse_labels_weight': 0.2,
        'conv_dropout_drop_proba': 0.175,
        'fc_dropout_drop_proba': 0.3,
        'use_BN': True,
        'first_conv': 4,
        'residual': 4,
        'conv_hiddn_units_mult': 1.0,
        'nb_conv_pool_layers': 3,
        'conv_pool_res_start_idx': 0.0,
        'pooling_type': 'inception',
        'conv_kernel_size': 3.0,
        'res_conv_kernel_size': 3.0,
        'fc_units_1_mult': 1.0,
        'one_more_fc': 1.0,
        'activation': 'elu'
    }
    space_best_model = {
        "activation": "elu",
        "batch_size": 320.0,
        "coarse_labels_weight": 0.3067103474295116,
        "conv_dropout_drop_proba": 0.25923531175521264,
        "conv_hiddn_units_mult": 1.5958302613876916,
        "conv_kernel_size": 3.0,
        "conv_pool_res_start_idx": 0.0,
        "fc_dropout_drop_proba": 0.4322253354921089,
        "fc_units_1_mult": 1.3083964454436132,
        "first_conv": 3,
        "l2_weight_reg_mult": 0.41206755600055983,
        "lr_rate_mult": 0.6549347353077412,
        "nb_conv_pool_layers": 3,
        "one_more_fc": None,
        "optimizer": "Nadam",
        "pooling_type": "avg",
        "res_conv_kernel_size": 2.0,
        "residual": 3.0,
        "use_BN": True
    }

    model = build_model(space_base_demo_to_plot)
    plot_model(model, to_file='model_demo.png', show_shapes=True)
    print("Saved base model visualization to model_demo.png.")
    K.clear_session()
    del model

    model = build_model(space_best_model)
    plot_model(model, to_file='model_best.png', show_shapes=True)
    print("Saved best model visualization to model_best.png.")
    K.clear_session()
    del model
def plot(hyperspace, file_name_prefix):
    """Plot a model from it's hyperspace."""
    model = build_model(hyperspace)
    plot_model(model,
               to_file='{}.png'.format(file_name_prefix),
               show_shapes=True)
    print("Saved model visualization to {}.png.".format(file_name_prefix))
    K.clear_session()
    del model
示例#3
0
def plot(hyperspace, file_name_prefix):
    """Plot a model from it's hyperspace."""
    if PLOT_FOLDER_PATH:
        if not os.path.exists(PLOT_FOLDER_PATH):
            os.makedirs(PLOT_FOLDER_PATH)
        filename = "{}/{}.png".format(PLOT_FOLDER_PATH, file_name_prefix)
    else:
        filename = "{}.png".format(file_name_prefix)
    model = build_model(hyperspace)
    plot_model(model, to_file=filename, show_shapes=True)

    K.clear_session()
    del model
示例#4
0
import numpy as np
from keras import backend as K

import time
import os


# Dimensions of the generated pictures for each filter.
img_width = 32
img_height = 32
weight_file = "{}/f37d5.hdf5".format(WEIGHTS_DIR)
LAYERS_DIR = "layers"

# Load model in test phase mode: no dropout, and use fixed BN
K.set_learning_phase(0)
model = build_model(load_best_hyperspace())
model.load_weights(weight_file)

print('Model loaded.')
model.summary()


def normalize(x):
    """Utility function to normalize a tensor by its L2 norm."""
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)


def deprocess_image(x):
    """Utility function to convert a tensor into a valid image."""
    # Normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
示例#5
0
            jsons.append(j)
    else:
        results_folder_path = "../results"
        jsons = load_jsons()

    num_jsons = len(jsons)

    for json_file in jsons:
        print("Reevaluating model %d/" % i + str(num_jsons))
        print("Model UUID: " + json_file['model_uuid'])

        model_uuid = json_file["model_uuid"]
        space = json_file["space"]

        # build model
        model = build_model(space)

        # load saved final weights
        if do_w_retrained_models:
            weights_path = "../weights-retrained/%s.hdf5" % model_uuid
        else:
            weights_path = "../weights/%s.hdf5" % model_uuid

        model.load_weights(weights_path)

        # evaluate euclidean distance
        metric_distance = euclidean_distance_metric_individual(model)
        json_file["euclidean_distance_error"] = metric_distance

        # update euclidean distance metric in json file
        file_name = json_file["model_name"] + '.txt.json'
示例#6
0
import numpy as np
from neural_net import build_model
from numerical_model.params import params

nx = int(params.nx)

# Load raw training data
raw_training_data = np.loadtxt('training_data.txt')

# Get number of training pairs
print(f"Training with {raw_training_data.shape[0]} training pairs")

# Extract training data into inputs and outputs
x_train = raw_training_data[:, :nx]
y_train = raw_training_data[:, nx:]

# Renormalise input data
max_train = 30.0
min_train = -20.0
x_train = 2.0 * (x_train - min_train) / (max_train - min_train) - 1.0

# Build model
model = build_model(nx, nx)

# Train!
model.fit(x_train, y_train, epochs=200, batch_size=128, validation_split=0.2)

model.save_weights("weights")