Esempio n. 1
0
    def test_loadmodel(self):
        """ Test whether a dummy model can be save and then loaded """
        model = create_dummy_model()
        storage.savemodel(model, self.path, self.modelname)

        loaded_model = storage.loadmodel(self.path, self.modelname)

        assert hasattr(loaded_model, 'fit')
Esempio n. 2
0
    def test_savemodel(self):
        """ Test whether a dummy model is saved """
        model = create_dummy_model()

        storage.savemodel(model, self.path, self.modelname)

        assert os.path.isfile(
            self.architecture_json_file_name) and os.path.isfile(
                self.weights_file_name)
Esempio n. 3
0
 def test_loadmodel(self):
     """ Test whether a dummy model can be save and then loaded """
     best_model = create_dummy_model()
     filepath = os.getcwd() + '/'
     modelname = 'teststorage'
     storage.savemodel(best_model, filepath, modelname)
     filename1 = filepath + modelname + '_architecture.json'
     filename2 = filepath + modelname + '_weights.npy'
     model_loaded = storage.loadmodel
     test = hasattr(best_model, 'fit')
     if test is True:
         os.remove(filename1)
         os.remove(filename2)
     assert test
Esempio n. 4
0
 def test_loadmodel(self):
     """ Test whether a dummy model can be save and then loaded """
     best_model = create_dummy_model()
     filepath = os.getcwd() + '/'
     modelname = 'teststorage'
     storage.savemodel(best_model, filepath, modelname)
     filename1 = filepath + modelname + '_architecture.json'
     filename2 = filepath + modelname + '_weights.npy'
     model_loaded = storage.loadmodel
     test = hasattr(best_model, 'fit')
     if test is True:
         os.remove(filename1)
         os.remove(filename2)
     assert test
Esempio n. 5
0
 def test_savemodel(self):
     """ Test whether a dummy model is saved """
     best_model = create_dummy_model()
     filepath = os.getcwd() + '/'
     modelname = 'teststorage'
     storage.savemodel(best_model, filepath, modelname)
     filename1 = filepath + modelname + '_architecture.json'
     filename2 = filepath + modelname + '_weights.npy'
     test1 = os.path.isfile(filename1)
     test2 = os.path.isfile(filename2)
     test = test1 == True and test2 == True
     if test is True:
         os.remove(filename1)
         os.remove(filename2)
     assert test
Esempio n. 6
0
 def test_savemodel(self):
     """ Test whether a dummy model is saved """
     best_model = create_dummy_model()
     filepath = os.getcwd() + '/'
     modelname = 'teststorage'
     storage.savemodel(best_model, filepath, modelname)
     filename1 = filepath + modelname + '_architecture.json'
     filename2 = filepath + modelname + '_weights.npy'
     test1 = os.path.isfile(filename1)
     test2 = os.path.isfile(filename2)
     test = test1 == True and test2 == True
     if test is True:
         os.remove(filename1)
         os.remove(filename2)
     assert test
Esempio n. 7
0
    X_val,
    y_val_binary,
    models,
    nr_epochs=nr_epochs,
    subset_size=subset_size,
    verbose=True,
    outputfile=outputfile)
print('Details of the training process were stored in ', outputfile)

# # Inspect model performance (table)
modelcomparisons = pd.DataFrame({
    'model': [str(params) for model, params, model_types in models],
    'train_acc': [history.history['acc'][-1] for history in histories],
    'train_loss': [history.history['loss'][-1] for history in histories],
    'val_acc': [history.history['val_acc'][-1] for history in histories],
    'val_loss': [history.history['val_loss'][-1] for history in histories]
})
modelcomparisons.to_csv(os.path.join(resultpath, 'modelcomparisons.csv'))

modelcomparisons

# # Choose the best model and save it

best_model_index = np.argmax(val_accuracies)
best_model, best_params, best_model_types = models[best_model_index]
print('Model type and parameters of the best model:')
print(best_model_types)
print(best_params)
modelname = 'my_bestmodel'
storage.savemodel(best_model, resultpath, modelname)
# First step is to create a model architecture. As we do not know what architecture is best for our data we will create a set of models to investigate which architecture is most suitable for our data and classification task. You will need to specificy how many models you want to create with argument 'number_of_models', the type of model which can been 'CNN' or 'DeepConvLSTM', and maximum number of layers per modeltype. See for a full overview of the optional arguments the function documentation of modelgen.generate_models

# In[16]:

num_classes = ys[0].shape[1]
np.random.seed(123)
models = modelgen.generate_models(Xs[0].shape,
                                  number_of_classes=num_classes,
                                  number_of_models=nr_models)

# In[ ]:

# In[19]:

for i, (model, params, model_type) in enumerate(models):
    storage.savemodel(model, resultpath, "model_" + str(i))

# ## Compare models
# Now that the model architectures have been generated it is time to compare the models by training them in a subset of the training data and evaluating the models in the validation subset. This will help us to choose the best candidate model. Performance results are stored in a json file.

# In[25]:


def split_train_test(X_list, y_list, j):
    X_train = np.concatenate(X_list[0:j] + X_list[j + 1:])
    X_test = X_list[j]
    y_train = np.concatenate(y_list[0:j] + y_list[j + 1:])
    y_test = y_list[j]
    return X_train, y_train, X_test, y_test