Ejemplo n.º 1
0
def load_multi_model(models_dir, custom_objects=None):
    '''
    Loads multiple models stored in `models_path`.

    Args:
       models_path: A string indicating the directory were models are stored.
       custom_objects: Dict mapping class names (or function names) of custom (non-Keras) objects to class/functions.

    Returns: List of models, list of model_specs

    '''

    models = []
    model_specs = []
    num_models = 0
    model_extensions = ['.h5', '.hdf5']

    for dirpath, dirnames, files in os.walk(models_dir):
        for dir in dirnames:
            files = os.listdir(os.path.join(dirpath, dir))
            for filename in files:
                if filename.endswith(tuple(model_extensions)):
                    print('Loading model ', filename)
                    model, model_spec = load_model(os.path.join(dirpath, dir, filename), custom_objects=custom_objects)
                    models.append(model)
                    model_specs.append(model_spec)
                    num_models += 1

    print('Models loaded: ', num_models)
    return models, model_specs
Ejemplo n.º 2
0
 def define_my_vgg(self):
     vgg = VGG19(include_top=False,
                 input_shape=(self.image_row, self.image_row, 3))
     vgg.trainable = False
     models = []
     for i in [1, 2, 4, 5, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20]:
         model = Model(inputs=vgg.input, outputs=vgg.layers[i].output)
         model.trainable = False
         models.append(model)
     return models
Ejemplo n.º 3
0
def load_trained_models_1(input_shape, directory):
    n_folds = 5
    models = list()
    for i in range(n_folds):
        # define model
        model = CapsNet_1(input_shape=input_shape, n_class=2, routings=3)
        weight_file = directory + '/fold_%d' % (i) + '/best_model.h5'
        model.load_weights(weight_file)
        print('load weight from ', weight_file)
        models.append(model)

    return models
Ejemplo n.º 4
0
def get_models(models_dir):
    print("loading models from '%s'" % models_dir)
    custom_objects = {'PositionEncode': juliet_memnet.PositionEncode}
    models = []
    for fname in MODEL_FNAMES:
        path = os.path.join(models_dir, fname)
        if not os.path.exists(path):
            print("Warning: could not find model file '%s'" % path)
            continue
        model = keras.models.load_model(path, custom_objects=custom_objects)
        models.append(model)

    return models
Ejemplo n.º 5
0
def compareModelAccuracy(a,b,c,d):   
    print('\nCompare Multiple Classifiers:')
    print('\nK-Fold Cross-Validation Accuracy:\n')
    models = []
    models.append(('LR', LogisticRegression()))
    models.append(('RF', RandomForestClassifier()))
#    models.append(('SVM', SVC())) # takes hours to run!
#    models.append(('LSVM', LinearSVC()))
#    models.append(('GNB', GaussianNB()))
    models.append(('DTC', DecisionTreeClassifier()))
    models.append(('XGB', XGBClassifier()))
    resultsAccuracy = []
    names = []
    for name, model in models:
        model.fit(a,b)
        kfold = model_selection.KFold(n_splits=10, random_state=7)
        accuracy_results = model_selection.cross_val_score(model, a,b, cv=kfold, scoring='accuracy')
        resultsAccuracy.append(accuracy_results)
        names.append(name)
        accuracyMessage = "%s: %f (%f)" % (name, accuracy_results.mean(), accuracy_results.std())
        print(accuracyMessage)
   
    # boxplot algorithm comparison
    fig = plt.figure()
    fig.suptitle('Algorithm Comparison: Accuracy')
    ax = fig.add_subplot(111)
    plt.boxplot(resultsAccuracy)
    ax.set_xticklabels(names)
    ax.set_ylabel('Cross-Validation: Accuracy Score')
    plt.show()
    return
Ejemplo n.º 6
0
def load_models(paths):
    """
    Load keras models from the paths, returning a list of
    models.
    """

    models = []

    for i, path in enumerate(paths, start=1):
        # Note: compile=False improves model load times (by about 3x)
        model = keras.models.load_model(path, compile=False)
        model.name = "model{}".format(i)
        models.append(model)

    return models
Ejemplo n.º 7
0
    def get_models(self):
        models = []
        models.append(self.predictions_1d_conv_output_1)

        # models.append(self.predictions_1d_conv_decrease)
        # models.append(self.predictions_1d_conv_double)
        # models.append(self.predictions_1d_conv_double_dropout)
        # models.append(self.predictions_1d_conv_activ_tanh)
        # models.append(self.predictions_1d_conv_activ_sig)
        # models.append(self.predictions_1d_conv_activ_linear)
        # models.append(self.predictions_1d_conv_activ_exp)
        # models.append(self.predictions_1d_conv_double_deep_unif)
        # models.append(self.predictions_1d_conv_double_deep_mixed)

        return models
Ejemplo n.º 8
0
    return modelA, modelB


def cut_model(model, cut_point):

    modelA, modelB = cut_model_functional(model, cut_point)

    return modelA, modelB


models = []
for c in valid_cut_points:
    try:
        modelA, modelB = cut_model(model, c)
        models.append(modelB)
    except Exception as e:
        models.append(None)
        continue

    warmup_start = time.time()

    if model_name == 'lenet':
        image = load_img('images/' + images[0] + '.jpg',
                         color_mode='grayscale',
                         target_size=(28, 28))
    else:
        image = load_img('images/' + images[0] + '.jpg',
                         target_size=(224, 224))

    # convert the image pixels to a numpy array
Ejemplo n.º 9
0
def predictor_results_to_csv(wav_name, wav_duration):
    """
    :param wav_name: file's name
    :param wav_duration: file's duration
    :return: writes one row to wav_path with extracted features

    """
    """
    # search for the models the needed to be loaded by the types of labels we trained for - models list
    also save their sizes in order to insert the right input later on
    """
    print("the chunk " + wav_name + " is being examined for disterss")
    directory_path = 'train/positive'
    list_of_files = listdir(directory_path)
    models = []
    models_mfcc_sizes = []
    for i in list_of_files:
        model_path = str(f'{clfGlobals.bestModelsPath}/{i}_clf_mfcc_12.h5')
        if os.path.exists(model_path):
            # print(f"loading model from {model_path}")
            models_mfcc_sizes.append(12)
            models.append(load_model(model_path))
            continue
        model_path = str(f'{clfGlobals.bestModelsPath}/{i}_clf_mfcc_15.h5')
        if os.path.exists(model_path):
            # print(f"loading model from {model_path}")
            models_mfcc_sizes.append(15)
            models.append(load_model(model_path))
            continue
        model_path = str(f'{clfGlobals.bestModelsPath}/{i}_clf_mfcc_20.h5')
        if os.path.exists(model_path):
            # print(f"loading model from {model_path}")
            models_mfcc_sizes.append(20)
            models.append(load_model(model_path))
            continue
    """
    # search for the right row in each csv file of the features and load it to dict for further use
    """
    # input file name you want to search
    name = wav_name
    # read csv, and split on "," the line
    csv_file_12 = csv.reader(open('csv/test/data_test_mfcc_12.csv', "r"),
                             delimiter=",")
    csv_file_15 = csv.reader(open('csv/test/data_test_mfcc_15.csv', "r"),
                             delimiter=",")
    csv_file_20 = csv.reader(open('csv/test/data_test_mfcc_20.csv', "r"),
                             delimiter=",")
    # loop through csv list
    dict = {}
    for row in csv_file_12:
        # if current rows 2nd value is equal to input, save that row
        if name == row[0]:
            dict[12] = row
            dict[12].pop(0)
            dict[12].pop()
    for row in csv_file_15:
        # if current rows 2nd value is equal to input, save that row
        if name == row[0]:
            dict[15] = row
            dict[15].pop(0)
            dict[15].pop()
    for row in csv_file_20:
        # if current rows 2nd value is equal to input, save that row
        if name == row[0]:
            dict[20] = row
            dict[20].pop(0)
            dict[20].pop()
    """
        # the first features to be written in the predictor csv is the file name date and length of the file
    """
    now = datetime.datetime.now()
    to_append = f'{str(str(now.day)+"-"+str(now.month)+"-"+str(now.year)+":"+str(now.hour)+"-"+str(now.minute)+"-"+str(now.second))} {str(wav_name)} {str(wav_duration)} '
    """
        # for each label insert the correct input , append the result to the row string and count the number of positive
         predictions found so far (count_positives)
    """
    count_positives = 0
    labels_that_appeared = list_of_files
    for i, m in enumerate(models, 0):
        # the input that is right for the model
        X = np.array(dict[models_mfcc_sizes[i]])
        scaler = StandardScaler()
        # the input after being normalized - an input with 1 column and 18,21,26 rows is recieved
        X_test_scaled = scaler.fit_transform(X[:, np.newaxis])
        # in order the insert the input to the model we nedd to inverse it to a row with 18,21,26 inputs
        X_inverse = X_test_scaled.transpose()
        # print(np.max(X_test))  # 2590
        # print(np.max(X_inverse))  # 5
        # this bring a 0/1 answer
        # prediction=m.predict(X_inverse)
        # this bring a probability- float number between 0 to 1
        # the next line is the one who gets the results of the label classifier
        prediction = m.predict_proba(X_inverse)
        result1 = np.sum(prediction[0])
        # result2=np.argmax(prediction[0])
        # print(result1)
        # print(result2)
        # current_label_score2=m.predict_classes(input_for_label)
        # print(current_label_score[0][0])
        # if the probabilty is over 0.5 than round to 1 otherwise 0
        current_label_score = 1 if result1 >= 0.5 else 0
        # current_label_score = prediction[0]
        # current_label_score2=current_label_score2.shape[0]
        to_append += f' {current_label_score}'
        count_positives += current_label_score
        # remove the label from the label list if its not found positive from that label
        labels_that_appeared[
            i] = 0 if current_label_score == 0 else labels_that_appeared[i]
    # sum of the labels results
    to_append += f' {count_positives}'
    # if we found at leaset one positive label than we categorize the sound as a distress
    to_append += f' {(count_positives>0)}'

    #  save to csv (append new lines)
    file = open('predictor_data.csv', 'a', newline='')
    with file:
        writer = csv.writer(file)
        writer.writerow(to_append.split())

    # return the chunk result in order to sum all the results of the chunks of the file
    return (count_positives > 0), [
        str(label) for label in labels_that_appeared
    ]
Ejemplo n.º 10
0
# Set the network speed to maximum, in case it's already limited due to previous runs.
set_network_speed(50)

# Initialise variables
cut_points = []
network_speed = 50
weights = [0, 0, 0, 0, 0]
models = []
valid_cut_points = get_valid_cut_points(model, model_name)
valid_cut_points_iter = iter(valid_cut_points)

# Load the pre-partitioned Edge models into the 'models' list
for c in valid_cut_points:

    if c == -1:
        models.append(None)
        break

    json_file = open(
        'edge_models/edge_models_' + str(model_name) + '/model_' + str(c) +
        '.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    modelA = model_from_json(loaded_model_json)
    models.append(modelA)

    print('model layers:', len(modelA.layers))

    time.sleep(1)
    print('done sleep')
def ALLModels(opts):
    models = []
    #models.append(XGB(opts))    
    #models.append(DTree(opts))
    #models.append(LR(opts))
    #models.append(SVMSVC(opts))
    #models.append(LINSVC(opts))
    #models.append(NBMULTI(opts))
    #models.append(NBBonuli(opts))
    X_train, y_train = data_process(opts)
    #y_train = to_categorical(y_train, num_classes = nclass)
    X_train = normalize(X_train, norm = 'max', axis=0, copy = True, return_norm = False)
    X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.3, random_state=42)
    X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)      
    FOLDER = 'clean_vpn12_rf'    
    param_grid = {
        'critire': ['gini', 'gain'],
        'n_estimators': [200, 700],
        'max_features': ['auto', 'sqrt', 'log2'],
        'min_samples_split': np.arange(2, 30, 2),
        'max_depth':np.arange(2, 31)         
    }

    classifier = RandomForestClassifier(n_jobs=-1, oob_score=True)
    rf = ML_Model("Random Forest", classifier, param_grid)
    rf.model_path = FOLDER
    models.append(rf)
    
    FOLDER = 'clean_vpn12_lr'
    classifier = LogisticRegression(multi_class='ovr', penalty='l2')
    param_grid = dict(C=[0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000])
    lr = ML_Model("Log. Regression", classifier, param_grid)    
    lr.model_path = FOLDER
    models.append(lr)
    
    classifier = BernoulliNB()        
    FOLDER = 'clean_vpn12_NB-Bonulina'         
    nb = ML_Model("NB-Bernoulli", classifier, None)
    nb.model_path = FOLDER
    models.append(nb)
    
    FOLDER = 'clean_vpn12_xgb'
    xgb = XGBClassifier(
            learning_rate=0.1,
            n_estimators=1000,
            objective='multi:softmax',
            nthread=4,
            scale_pos_weight=1,
            seed=27,
            num_classes = 12)
    param_grid = {
            'max_depth': range(3, 10, 2),
            'min_child_weight': range(1, 6, 2),
            'gamma': [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.5, 2, 5],
            'subsample': [i / 10.0 for i in range(5, 11)],
            'colsample_bytree': [i / 10.0 for i in range(5, 11)],
            'reg_alpha': [1e-5, 1e-2, 0.1, 1, 100]
        }    
   
    xgb = ML_Model('XGBoost', xgb, param_grid)
    xgb.model_path = FOLDER
    models.append(xgb)
    
    FOLDER = 'clean_vpn12_svc'
    classifier = svm.SVC()
    C_range = np.logspace(-2, 10, 13)    
    gamma_range = np.logspace(-9, 3, 13)
    param_grid = dict(gamma=gamma_range, C=C_range)  
 
    svmsvc = ML_Model('SVM-SVC', classifier, param_grid)
    svmsvc.model_path = FOLDER 
    models.append(svmsvc)
    
    final_train(models, X_train, y_train, X_test, y_test, opts.sets)
    ML_Model.models_metric_summary(models)
Ejemplo n.º 12
0
def NNTRegressor(I_data_path, I_Model_path, k, num_epochs, batch_Size, Verbose, NNTList):
    
    import numpy as np
    import pandas as pd
    import pickle
    import matplotlib.pyplot as plt
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import train_test_split
    import time
    import os

    import warnings
    warnings.filterwarnings('ignore') 
    
    from sklearn.ensemble import RandomForestRegressor
    from sklearn.metrics import accuracy_score
    from sklearn.metrics import mean_absolute_error
    
    import keras
    from keras.models import Sequential
    from keras import models
    from keras import layers
    get_ipython().run_line_magic('matplotlib', 'inline')
    from joblib import dump 

    
    #################################### Functions ####################################
    
    # Normalise
    def Normalise(Data, Target):
        sc = StandardScaler()
        dfX = Data.drop([Target], axis=1)
        sc.fit(dfX)
        X = sc.transform(dfX)
        os.makedirs(I_Model_path + 'temp', exist_ok=True)
        print('save scaler to '+ I_Model_path + 'temp/scaler.pickle')
        with open(I_Model_path + 'temp/scaler.pickle', 'wb') as f:
            pickle.dump(sc, f)
        dump(sc, I_Model_path + 'temp/std_scaler.bin', compress=True)
        y = Data.values[:, Data.columns.get_loc(Target)]
        return X, y
    
        
    #Validation
    def validation(train_data, train_targets, num_val_samples, i):
        val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
        val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]
        return val_data, val_targets

    def partial_train(train_data, train_targets, num_val_samples, i):
        partial_train_data = np.concatenate([train_data[:i * num_val_samples], train_data[(i + 1) * num_val_samples:]],
        axis=0)
        partial_train_targets = np.concatenate([train_targets[:i * num_val_samples], train_targets[(i + 1) * num_val_samples:]],
        axis=0)
        return partial_train_data, partial_train_targets    

    #Models
    def build_model1(titles):
        red = Sequential()
        red.add(layers.Dense(64, activation='relu', input_shape=(n,)))
        red.add(layers.Dense(128, activation='relu'))
        red.add(layers.Dense(64, activation='relu'))
        red.add(layers.Dense(1))
        red.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
        title = 'Three hidden layers (relu: 64, 128, 64) and rmsprop optimizer'
        titles.append(title)
        return red

    def build_model2(titles):
        red = Sequential()
        red.add(layers.Dense(16, activation='relu', input_shape=(n,)))
        red.add(layers.Dense(16, activation='relu'))
        red.add(layers.Dense(1))
        red.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
        title = 'Two hidden layers (relu: 16) and rmsprop optimizer'
        titles.append(title)
        return red

    def build_model3(titles):    
        red = Sequential()
        red.add(layers.Dense(512, activation='relu', input_shape=(n,)))
        red.add(layers.Dense(1))
        red.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
        title = 'One hidden layers (relu: 512) and rmsprop optimizer'
        titles.append(title)
        return red

    def build_model4(title):    
        red = Sequential()
        red.add(layers.Dense(64, activation='relu', input_shape=(n,)))
        red.add(layers.Dense(64, activation='relu'))   
        red.add(layers.Dense(64, activation='softmax'))
        red.add(layers.Dense(1))
        red.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
        title = 'Three hidden layers (relu: 64) and rmsprop optimizer'
        titles.append(title)
        return red

    def build_model5(title):    
        red = Sequential()
        red.add(layers.Dense(32, activation='relu', input_shape=(n,)))
        red.add(layers.Dense(64, activation='relu'))   
        red.add(layers.Dense(128, activation='relu'))
        red.add(layers.Dense(128, activation='relu'))
        red.add(layers.Dense(512, activation='relu'))
        red.add(layers.Dense(1))
        red.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
        title = 'One hidden layers (sigmoid: 16) and rmsprop optimizer'
        titles.append(title)
        return red
  
    def build_model6(titles):
        red = Sequential()
        red.add(layers.Dense(64, activation='relu', input_shape=(train_data.shape[1],)))
        red.add(layers.Dense(64, activation='relu'))
        red.add(layers.Dense(64, activation='relu'))
        red.add(layers.Dense(64, activation='relu'))
        red.add(layers.Dense(1))
        red.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
        title = 'Four hidden layers (relu: 64) and rmsprop optimizer'
        titles.append(title)
        return red  
    

    
    #MAE in k
    def average_mae(train_all_scores, val_all_scores, num_epochs):
        training = [np.mean([x[k] for x in train_all_scores]) for k in range(num_epochs)]
        validation = [np.mean([x[k] for x in val_all_scores]) for k in range(num_epochs)]
        return training, validation
    
    #Visuals
    def plot_Mae(average_mae_training, average_mae_validation, title, i):
        my_dpi=96
        plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=my_dpi)
        plt.plot(range(1, len(average_mae_training) + 1), average_mae_training, 'o', label='Training')
        plt.plot(range(1, len(average_mae_validation) + 1), average_mae_validation, 'lightseagreen', label='Validation')
        plt.title(title, size=18)
        plt.xlabel('Epochs', size=18)
        plt.ylabel('Mean Absolute Error', size=18)
        plt.legend(loc='best',prop={'size': 16}) 
        os.makedirs(I_Model_path + 'visuals', exist_ok=True)
        plt.savefig(I_Model_path + 'visuals/MAE Model' + str(NNTList[i]) + '.png') 
        
        
    # Timer
    def Timer(Model_Step, last_time, k):
        Time_list = []
        now = time.time() - last_time
        if now < 60:
            print('   Time of '+ Model_Step+str(k)+': '+ str(round(now))+'s')
            Time_list.append('Time of '+ Model_Step +str(k)+': '+ str(round(now))+'s')
        elif 60<now<3600:
            now_m = now/60
            minutes = int(now_m)
            secs = int((now_m - minutes)*60)
            print('   Time of '+ Model_Step+str(k)+': ' +str(minutes)+'m and '+str(secs)+'s')
            Time_list.append('   Time of '+ Model_Step+str(k)+': ' +str(minutes)+'m and '+str(secs)+'s')
            del now_m
            del minutes
            del secs
        elif now>3600:
            now_h = now/3600
            hours = int(now_h)
            now_m = (now_h - hours)*60
            minutes = int(now_m)
            secs = int((now_m - minutes)*60)
            print('   Time of '+ Model_Step+str(k)+': ' +str(hours)+'h, '+str(minutes)+'m and '+str(secs)+'s')
            Time_list.append('   Time of '+ Model_Step+str(k)+': ' +str(hours)+'h, '+str(minutes)+'m and '+str(secs)+'s')
            del now_h
            del hours
            del now_m
            del minutes
            del secs
            
    ############################################################################################################
    
    # Define inputs
    Master_path = I_data_path    
    os.makedirs(I_Model_path, exist_ok=True)
    
    ## Load data:
    print('Loading the data...')
    data = pd.read_csv(Master_path)

    
    #Creating a dataset copy
    Data = data.copy()
    print('Working dataset has: ' +  str(Data.shape[0])+ ' rows and ' + str(Data.shape[1])+ ' columns')
    

    # Normalise
    print('Normalising...')
    X, y = Normalise(Data,'CANTIDADSALIDA')
    print('Completed')
    
    #Split in train test
    print('Spliting in train and test set')
    train_data, test_data, train_targets, test_targets = train_test_split(X, y, 
                                                                          train_size=0.80, test_size=0.20, random_state=0)

    
    
    ### Save as csv:
    os.makedirs(I_Model_path + 'temp', exist_ok=True)
    dfX = Data.drop('CANTIDADSALIDA', axis=1)
    
    print('Saving training feature set to ' + I_Model_path + 'temp/train_X.csv')
    train_data_df = pd.DataFrame(train_data, columns= dfX.columns )
    train_data_df.to_csv(I_Model_path + 'temp/train_X.csv')
    with open(I_Model_path + 'temp/train_X.pickle', 'wb') as f:
        pickle.dump(train_data, f)    
    
    print('Saving training labels to '+I_Model_path + 'temp/train_Y.csv')
    train_targets_df = pd.DataFrame(train_targets, columns= ['CANTIDADSALIDA'])
    train_targets_df.to_csv(I_Model_path + 'temp/train_Y.csv')
    with open(I_Model_path + 'temp/train_Y.pickle', 'wb') as f:
        pickle.dump(train_targets, f) 

    print('Saving test feature set to '+ I_Model_path + 'temp/test_X.csv')
    test_data_df = pd.DataFrame(test_data, columns= dfX.columns )
    test_data_df.to_csv(I_Model_path + 'temp/test_X.csv')
    with open(I_Model_path + 'temp/test_X.pickle', 'wb') as f:
        pickle.dump(test_data, f)    
    
    print('Saving test labels to '+I_Model_path + '/temp/test_Y.csv')
    test_targets_df = pd.DataFrame(test_targets, columns= ['CANTIDADSALIDA'])
    test_targets_df.to_csv(I_Model_path + 'temp/test_Y.csv')
    with open(I_Model_path + 'temp/test_Y.pickle', 'wb') as f:
        pickle.dump(test_targets, f)
        

    
    train_data_df["CANTIDADSALIDA"] = train_targets_df["CANTIDADSALIDA"]
    train_data_df['CANTIDADSALIDA'] = train_data_df['CANTIDADSALIDA'].astype(int)
    
    test_data_df["CANTIDADSALIDA"] = test_targets_df["CANTIDADSALIDA"]
    test_data_df['CANTIDADSALIDA'] = test_data_df['CANTIDADSALIDA'].astype(int)
    
        
    # Parameters
    #A) Lists:
    models = []
    titles = [] 
    
    #B) Temps
    n = train_data.shape[1] # number of features
    num_val_samples = len(train_data) // k

    #Model
    if 1 in NNTList:
        models.append(build_model1(titles)) 
    if 2 in NNTList:
        models.append(build_model2(titles)) 
    if 3 in NNTList:
        models.append(build_model3(titles)) 
    if 4 in NNTList:
        models.append(build_model4(titles)) 
    if 5 in NNTList:
        models.append(build_model5(titles)) 
    if 6 in NNTList:
        models.append(build_model6(titles)) 
              
    # Training K-Cross Neural Network:
    print('- Starting to train the model:')
    final_time = time.time()
    for i in range(len(models)):
        last_time = time.time()

        print('Model '+ str(NNTList[i]) + ':')
        # NNT model 
        red = models[i]

        # Lists    
        train_all_scores_byModel = []
        val_all_scores_byModel = []

        #K-Cross-validation
        for j in range(k):

            last_time2 = time.time()
            print("   Processing fold #", j)
            
            # Validation
            val_data, val_targets = validation(train_data, train_targets, num_val_samples, j)
            partial_train_data, partial_train_targets = partial_train(train_data, train_targets, num_val_samples, j)

            # Training
            training = red.fit(partial_train_data, partial_train_targets, validation_data = (val_data, val_targets),
                                epochs = num_epochs, batch_size = batch_Size, verbose=Verbose)

            #Mean absolute error
            train_mae_training = training.history['mae']
            val_mae_training = training.history['val_mae']

            # Adding to the list
            train_all_scores_byModel.append(train_mae_training)
            val_all_scores_byModel.append(val_mae_training)

            #timer
            Timer('Step ', last_time2, j)

            del val_data, val_targets

        #Saving the model 
        print('Saving temps')  
        
        os.makedirs(I_Model_path + 'models', exist_ok=True)
        os.makedirs(I_Model_path + 'temp', exist_ok=True)
        
        red.save(I_Model_path + 'models/Model' +str(NNTList[i])+ '.h5')  
            
        with open(I_Model_path + 'models/NNTModel' +str(NNTList[i])+ '.pickle', 'wb') as f:
            pickle.dump(red, f)
        #timer
        print('   ' + str(Timer('Model ', last_time, NNTList[i])))

        # Evaluate
        test_mse_score, test_mae_score = red.evaluate(test_data, test_targets,verbose=0)
        print("      Mean Absolute Error model "+ str(NNTList[i])+ ":"  "= {:.2f}".format(test_mae_score))
        average_mae_training, average_mae_validation =  average_mae(train_all_scores_byModel, val_all_scores_byModel, num_epochs)

        with open(I_Model_path + 'temp/NNT_error' +str(NNTList[i])+ '.pickle', 'wb') as f:
            pickle.dump(test_mae_score, f)        
        
        # Visual 
        plot_Mae(average_mae_training, average_mae_validation, titles[i], i)  
        plt.rcdefaults()
    print('      ' + str(Timer('Global', final_time, 0)))   
    



    return  train_data_df, train_targets_df, test_data_df, test_targets_df
Ejemplo n.º 13
0
except IndexError:
    print("There are no models currently saved in the top_models folder.")
else:
    for folder in folders:
        mlp = None
        try:
            mlp = load_model(model_dir + "/" + folder + "/model")

            holdout_data = pd.read_csv(model_dir + "/" + folder +
                                       "/holdout_data.csv")
            training_data = pd.read_csv(model_dir + "/" + folder +
                                        "/training_data.csv")
            score_df = pd.read_csv(model_dir + "/" + folder +
                                   "/learning_scores.csv",
                                   sep='\s*,\s*')
            models.append([mlp, holdout_data, training_data, score_df])

            predictions = mlp.predict(X)
            model_mae = mean_absolute_error(Y, predictions)
            model_r2 = r2_score(Y, predictions)

            if disp_testing:
                holdout_X_test = holdout_data.iloc[:, 1:-2].values
                holdout_Y_test = holdout_data.loc[:, "κref."].values
                predictions = mlp.predict(holdout_X_test)
                model_mae = mean_absolute_error(holdout_Y_test, predictions)
                model_r2 = r2_score(holdout_Y_test, predictions)
                model_rmse = math.sqrt(
                    mean_squared_error(holdout_Y_test, predictions))
            else:
                predictions = mlp.predict(X)
Ejemplo n.º 14
0
def create_3():
    ''' Create ensemble_3
    Returns:
        ensemble_model: ensemble_3
    '''
    model_input = Input(shape=(28, 28, 1))
    models = []
    models.append(load_hdf5('model/emnist-letters_200epoch_lenet_1_1'))
    models.append(load_hdf5('model/emnist-letters_200epoch_lenet_1_2'))
    models.append(load_hdf5('model/emnist-letters_200epoch_lenet_1_5'))
    models.append(load_hdf5('model/emnist-letters_200epoch_lenet_1_6'))
    models.append(load_hdf5('model/emnist-letters_300epoch_lenet_2_2'))
    models.append(load_hdf5('model/emnist-letters_300epoch_lenet_2_1'))
    models.append(load_hdf5('model/emnist-letters_300epoch_lenet_2_4'))
    ensemble_model = ensemble_models(models, model_input)

    ensemble_model.compile(loss='categorical_crossentropy',
                           optimizer='adadelta',
                           metrics=['accuracy'])

    return ensemble_model