From bc2381a98d2d0dcad5df0710ffef1097bba2e7cf Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:29:07 +0200 Subject: [PATCH 01/17] Moved tests out for CNN and ConvLSTM models --- tests/test_modelgen.py | 124 --------------------------- tests/tests_models/test_cnn.py | 98 +++++++++++++++++++++ tests/tests_models/test_conv_lstm.py | 51 +++++++++++ 3 files changed, 149 insertions(+), 124 deletions(-) create mode 100755 tests/tests_models/test_cnn.py create mode 100755 tests/tests_models/test_conv_lstm.py diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index 8604ef2..a96d22a 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -51,131 +51,7 @@ def _generate_train_data(self, x_shape, nr_classes): y_train = np.random.randint(0, 1, size=(1, nr_classes)) return X_train, y_train - # Tests for CNN model: - def test_cnn_starts_with_batchnorm(self): - """ CNN models should always start with a batch normalization layer. """ - model_type = CNN((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32], - "fc_hidden_nodes": 100}) - assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' - - def test_cnn_fc_nodes(self): - """ CNN model should have number of dense nodes defined by user. """ - fc_hidden_nodes = 101 - model_type = CNN((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32], - "fc_hidden_nodes": fc_hidden_nodes}) - - dense_layer = [l for l in model.layers if 'Dense' in str(l)][0] - assert dense_layer.output_shape[1] == fc_hidden_nodes, 'Wrong number of fc nodes.' - - def test_cnn_batchnorm_dim(self): - """"The output shape of the batchnorm should be (None, nr_timesteps, nr_filters)""" - model_type = CNN((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32], - "fc_hidden_nodes": 100}) - - batchnormlay = model.layers[2] - assert batchnormlay.output_shape == (None, 20, 32) - - def test_cnn_enough_batchnorm(self): - """CNN model should contain as many batch norm layers as it has activations layers""" - model_type = CNN((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32], - "fc_hidden_nodes": 100}) - - batch_norm_layers = len([l for l in model.layers if 'BatchNormalization' in str(l)]) - activation_layers = len([l for l in model.layers if 'Activation' in str(l)]) - assert batch_norm_layers == activation_layers - - def test_cnn_metrics(self): - """CNN model should be compiled with the metrics that we give it""" - metrics = ['accuracy', 'mae'] - x_shape = (None, 20, 3) - nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) - - model_type = CNN(x_shape, nr_classes, metrics=metrics) - model = model_type.create_model(**{"filters": [32, 32], - "fc_hidden_nodes": 100}) - model.fit(X_train, y_train, epochs=1) - - model_metrics = [m.name for m in model.metrics] - for metric in metrics: - assert metric in model_metrics - - def test_CNN_hyperparameters_nrlayers(self): - """ Number of Conv layers from range [4, 4] should be 4. """ - custom_settings = get_default() - kwargs = {'cnn_min_layers': 4, - 'cnn_max_layers': 4} - # Replace default parameters with input - for key, value in kwargs.items(): - if key in custom_settings: - custom_settings[key] = value - - model_type = CNN(None, None, **custom_settings) - hyperparams = model_type.generate_hyperparameters() - - assert len(hyperparams.get('filters')) == 4 - - def test_CNN_hyperparameters_fcnodes(self): - """ Number of fc nodes from range [123, 123] should be 123. """ - custom_settings = get_default() - kwargs = {'cnn_min_fc_nodes': 123, - 'cnn_max_fc_nodes': 123} - # Replace default parameters with input - for key, value in kwargs.items(): - if key in custom_settings: - custom_settings[key] = value - - model_type = CNN(None, None, **custom_settings) - hyperparams = model_type.generate_hyperparameters() - - assert hyperparams.get('fc_hidden_nodes') == 123 - - # Tests for DeepconvLSTM model: - def test_deepconvlstm_batchnorm_dim(self): - """The output shape of the batchnorm should be (None, nr_timesteps, nr_channels, nr_filters)""" - model_type = ConvLSTM((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32], - "lstm_dims": [32, 32]}) - - batchnormlay = model.layers[3] - assert batchnormlay.output_shape == (None, 20, 3, 32) - - def test_deepconvlstm_enough_batchnorm(self): - """LSTM model should contain as many batch norm layers as it has activations layers""" - model_type = ConvLSTM((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32, 32], - "lstm_dims": [32, 32, 32]}) - - batch_norm_layers = len([l for l in model.layers if 'BatchNormalization' in str(l)]) - activation_layers = len([l for l in model.layers if 'Activation' in str(l)]) - assert batch_norm_layers == activation_layers - - def test_DeepConvLSTM_hyperparameters_nrconvlayers(self): - """ Number of Conv layers from range [4, 4] should be 4. """ - custom_settings = get_default() - kwargs = {'deepconvlstm_min_conv_layers': 4, - 'deepconvlstm_max_conv_layers': 4} - # Replace default parameters with input - for key, value in kwargs.items(): - if key in custom_settings: - custom_settings[key] = value - - model_type = ConvLSTM(None, None, **custom_settings) - hyperparams = model_type.generate_hyperparameters() - - assert len(hyperparams.get('filters')) == 4 - - def test_deepconvlstm_starts_with_batchnorm(self): - """ DeepConvLSTM models should always start with a batch normalization layer. """ - model_type = ConvLSTM((None, 20, 3), 2) - model = model_type.create_model(**{"filters": [32, 32], - "lstm_dims": [32, 32]}) - assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' # Tests for ResNet model: def test_ResNet_starts_with_batchnorm(self): diff --git a/tests/tests_models/test_cnn.py b/tests/tests_models/test_cnn.py new file mode 100755 index 0000000..2897d26 --- /dev/null +++ b/tests/tests_models/test_cnn.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- +import unittest +from mcfly.models import CNN +from tests.test_modelgen import get_default + + +class CNNSuite(unittest.TestCase): + """ + Test cases for CNN models. + """ + + def test_cnn_starts_with_batchnorm(self): + """ CNN models should always start with a batch normalization layer. """ + model_type = CNN((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32], + "fc_hidden_nodes": 100}) + assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' + + + def test_cnn_fc_nodes(self): + """ CNN model should have number of dense nodes defined by user. """ + fc_hidden_nodes = 101 + model_type = CNN((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32], + "fc_hidden_nodes": fc_hidden_nodes}) + + dense_layer = [l for l in model.layers if 'Dense' in str(l)][0] + assert dense_layer.output_shape[1] == fc_hidden_nodes, 'Wrong number of fc nodes.' + + + def test_cnn_batchnorm_dim(self): + """"The output shape of the batchnorm should be (None, nr_timesteps, nr_filters)""" + model_type = CNN((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32], + "fc_hidden_nodes": 100}) + + batchnormlay = model.layers[2] + assert batchnormlay.output_shape == (None, 20, 32) + + + def test_cnn_enough_batchnorm(self): + """CNN model should contain as many batch norm layers as it has activations layers""" + model_type = CNN((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32], + "fc_hidden_nodes": 100}) + + batch_norm_layers = len([l for l in model.layers if 'BatchNormalization' in str(l)]) + activation_layers = len([l for l in model.layers if 'Activation' in str(l)]) + assert batch_norm_layers == activation_layers + + + def test_cnn_metrics(self): + """CNN model should be compiled with the metrics that we give it""" + metrics = ['accuracy', 'mae'] + x_shape = (None, 20, 3) + nr_classes = 2 + X_train, y_train = self._generate_train_data(x_shape, nr_classes) + + model_type = CNN(x_shape, nr_classes, metrics=metrics) + model = model_type.create_model(**{"filters": [32, 32], + "fc_hidden_nodes": 100}) + model.fit(X_train, y_train, epochs=1) + + model_metrics = [m.name for m in model.metrics] + for metric in metrics: + assert metric in model_metrics + + + def test_CNN_hyperparameters_nrlayers(self): + """ Number of Conv layers from range [4, 4] should be 4. """ + custom_settings = get_default() + kwargs = {'cnn_min_layers': 4, + 'cnn_max_layers': 4} + # Replace default parameters with input + for key, value in kwargs.items(): + if key in custom_settings: + custom_settings[key] = value + + model_type = CNN(None, None, **custom_settings) + hyperparams = model_type.generate_hyperparameters() + + assert len(hyperparams.get('filters')) == 4 + + + def test_CNN_hyperparameters_fcnodes(self): + """ Number of fc nodes from range [123, 123] should be 123. """ + custom_settings = get_default() + kwargs = {'cnn_min_fc_nodes': 123, + 'cnn_max_fc_nodes': 123} + # Replace default parameters with input + for key, value in kwargs.items(): + if key in custom_settings: + custom_settings[key] = value + + model_type = CNN(None, None, **custom_settings) + hyperparams = model_type.generate_hyperparameters() + + assert hyperparams.get('fc_hidden_nodes') == 123 diff --git a/tests/tests_models/test_conv_lstm.py b/tests/tests_models/test_conv_lstm.py new file mode 100755 index 0000000..9e9fa2f --- /dev/null +++ b/tests/tests_models/test_conv_lstm.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +import unittest +from mcfly.models import ConvLSTM +from tests.test_modelgen import get_default + +class ConvLSTMSuite(unittest.TestCase): + """ + Tests cases for DeepconvLSTM models. + """ + + def test_deepconvlstm_batchnorm_dim(self): + """The output shape of the batchnorm should be (None, nr_timesteps, nr_channels, nr_filters)""" + model_type = ConvLSTM((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32], + "lstm_dims": [32, 32]}) + + batchnormlay = model.layers[3] + assert batchnormlay.output_shape == (None, 20, 3, 32) + + def test_deepconvlstm_enough_batchnorm(self): + """LSTM model should contain as many batch norm layers as it has activations layers""" + model_type = ConvLSTM((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32, 32], + "lstm_dims": [32, 32, 32]}) + + batch_norm_layers = len([l for l in model.layers if 'BatchNormalization' in str(l)]) + activation_layers = len([l for l in model.layers if 'Activation' in str(l)]) + assert batch_norm_layers == activation_layers + + def test_DeepConvLSTM_hyperparameters_nrconvlayers(self): + """ Number of Conv layers from range [4, 4] should be 4. """ + custom_settings = get_default() + kwargs = {'deepconvlstm_min_conv_layers': 4, + 'deepconvlstm_max_conv_layers': 4} + # Replace default parameters with input + for key, value in kwargs.items(): + if key in custom_settings: + custom_settings[key] = value + + model_type = ConvLSTM(None, None, **custom_settings) + hyperparams = model_type.generate_hyperparameters() + + assert len(hyperparams.get('filters')) == 4 + + def test_deepconvlstm_starts_with_batchnorm(self): + """ DeepConvLSTM models should always start with a batch normalization layer. """ + model_type = ConvLSTM((None, 20, 3), 2) + model = model_type.create_model(**{"filters": [32, 32], + "lstm_dims": [32, 32]}) + + assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' From f9bae2dd59619a2825caa6f21da4396e880d8582 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:32:14 +0200 Subject: [PATCH 02/17] Moved function to a public spot --- tests/test_modelgen.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index a96d22a..9ee5603 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -43,15 +43,17 @@ def get_default(): 'high_reg': 4} return settings -class ModelGenerationSuite(unittest.TestCase): - """Basic test cases.""" - def _generate_train_data(self, x_shape, nr_classes): +# TODO: Move this to an utils file? +def generate_train_data(x_shape, nr_classes): X_train = np.random.rand(1, *x_shape[1:]) y_train = np.random.randint(0, 1, size=(1, nr_classes)) return X_train, y_train +class ModelGenerationSuite(unittest.TestCase): + """Basic test cases.""" + # Tests for ResNet model: def test_ResNet_starts_with_batchnorm(self): @@ -100,7 +102,7 @@ def test_ResNet_metrics(self): metrics = ['accuracy', 'mae'] x_shape = (None, 20, 3) nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) + X_train, y_train = generate_train_data(x_shape, nr_classes) model_type = ResNet(x_shape, nr_classes, metrics=metrics) model = model_type.create_model(16, 20) @@ -177,7 +179,7 @@ def test_InceptionTime_metrics(self): metrics = ['accuracy', 'mae'] x_shape = (None, 20, 3) nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) + X_train, y_train = generate_train_data(x_shape, nr_classes) model_type = InceptionTime(x_shape, nr_classes, metrics=metrics) model = model_type.create_model(16) @@ -216,7 +218,7 @@ def test_generate_models_metrics(self): """ Test if correct number of models is generated and if metrics is correct. """ x_shape = (None, 20, 10) nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) + X_train, y_train = generate_train_data(x_shape, nr_classes) n_models = 5 models = modelgen.generate_models(x_shape, nr_classes, n_models) @@ -234,7 +236,7 @@ def test_generate_models_pass_model_object(self): """ Test if model class can be passed as model_types input.""" x_shape = (None, 20, 10) nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) + X_train, y_train = generate_train_data(x_shape, nr_classes) n_models = 4 models = modelgen.generate_models(x_shape, nr_classes, n_models, @@ -250,7 +252,7 @@ def test_generate_models_exception(self): """ Test expected generate_models exception.""" x_shape = (None, 20, 10) nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) + X_train, y_train = generate_train_data(x_shape, nr_classes) n_models = 2 with pytest.raises(NameError, match="Unknown model name, 'wrong_entry'."): From 5d126091d2da7e418e943ea2f9bc13f630a4fb78 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:35:44 +0200 Subject: [PATCH 03/17] Moved out tests for ResNet --- tests/test_modelgen.py | 79 --------------------------- tests/tests_models/test_resnet.py | 89 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 79 deletions(-) create mode 100755 tests/tests_models/test_resnet.py diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index 9ee5603..e915d05 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -55,85 +55,6 @@ class ModelGenerationSuite(unittest.TestCase): """Basic test cases.""" - # Tests for ResNet model: - def test_ResNet_starts_with_batchnorm(self): - """ ResNet models should always start with a batch normalization layer. """ - model_type = ResNet((None, 20, 3), 2) - model = model_type.create_model(16, 20) - - assert 'BatchNormalization' in str(type(model.layers[1])), 'Wrong layer type.' - - - def test_ResNet_first_sandwich_layers(self): - """ ResNet models should always start with a residual module. """ - model_type = ResNet((None, 20, 3), 2) - model = model_type.create_model(16, 20) - - assert 'Conv1D' or 'Convolution1D' in str(type(model.layers[2])), 'Wrong layer type.' - assert 'BatchNormalization' in str(type(model.layers[3])), 'Wrong layer type.' - assert 'ReLU' in str(type(model.layers[4])), 'Wrong layer type.' - - - def test_ResNet_depth(self): - """ ResNet model should have depth (number of residual modules) as defined by user. """ - depths = 2 - - model_type = ResNet((None, 20, 3), 2) - model = model_type.create_model(16, 20, network_depth=depths) - - add_layers = [str(type(l)) for l in model.layers if 'Add' in str(type(l))] - assert len(add_layers) == depths, 'Wrong number of residual modules (network depths).' - - - def test_ResNet_first_module_dim(self): - """"The output shape throughout the first residual module should be (None, nr_timesteps, min_filters_number)""" - min_filters_number = 16 - - model_type = ResNet((None, 30, 5), 2) - model = model_type.create_model(min_filters_number, 20) - - firstConvlayer = model.layers[2] - firstAddlayer = model.layers[12] - assert firstConvlayer.output_shape == (None, 30, min_filters_number) - assert firstAddlayer.output_shape == (None, 30, min_filters_number) - - def test_ResNet_metrics(self): - """ResNet model should be compiled with the metrics that we give it""" - metrics = ['accuracy', 'mae'] - x_shape = (None, 20, 3) - nr_classes = 2 - X_train, y_train = generate_train_data(x_shape, nr_classes) - - model_type = ResNet(x_shape, nr_classes, metrics=metrics) - model = model_type.create_model(16, 20) - model.fit(X_train, y_train, epochs=1) - - model_metrics = [m.name for m in model.metrics] - for metric in metrics: - assert metric in model_metrics - - def test_ResNet_hyperparameters(self): - """ Network depth from range [4,4] should be 4. - Maximum kernal size from range [10, 10] should be 10. - Minimum filter number from range [16, 16] should be 16. """ - custom_settings = get_default() - kwargs = {'resnet_min_network_depth': 4, - 'resnet_mmax_network_depth': 4, - 'resnet_min_max_kernel_size': 10, - 'resnet_max_max_kernel_size': 10, - 'resnet_min_filters_number': 16, - 'resnet_max_filters_number': 16} - # Replace default parameters with input - for key, value in kwargs.items(): - if key in custom_settings: - custom_settings[key] = value - - model_type = ResNet(None, None, **custom_settings) - hyperparams = model_type.generate_hyperparameters() - - assert hyperparams.get('network_depth') == 4, 'Wrong network depth' - assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' - assert hyperparams.get('min_filters_number') == 16, 'Wrong filter number' # Tests for InceptionTime model: def test_InceptionTime_starts_with_batchnorm(self): diff --git a/tests/tests_models/test_resnet.py b/tests/tests_models/test_resnet.py new file mode 100755 index 0000000..31ebe37 --- /dev/null +++ b/tests/tests_models/test_resnet.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- + +import unittest +from mcfly.models import ResNet +from tests.test_modelgen import get_default, generate_train_data + +class ResNetSuite(unittest.TestCase): + """ + Test cases for ResNet models. + """ + + def test_ResNet_starts_with_batchnorm(self): + """ ResNet models should always start with a batch normalization layer. """ + model_type = ResNet((None, 20, 3), 2) + model = model_type.create_model(16, 20) + + assert 'BatchNormalization' in str(type(model.layers[1])), 'Wrong layer type.' + + + def test_ResNet_first_sandwich_layers(self): + """ ResNet models should always start with a residual module. """ + model_type = ResNet((None, 20, 3), 2) + model = model_type.create_model(16, 20) + + assert 'Conv1D' or 'Convolution1D' in str(type(model.layers[2])), 'Wrong layer type.' + assert 'BatchNormalization' in str(type(model.layers[3])), 'Wrong layer type.' + assert 'ReLU' in str(type(model.layers[4])), 'Wrong layer type.' + + + def test_ResNet_depth(self): + """ ResNet model should have depth (number of residual modules) as defined by user. """ + depths = 2 + + model_type = ResNet((None, 20, 3), 2) + model = model_type.create_model(16, 20, network_depth=depths) + + add_layers = [str(type(l)) for l in model.layers if 'Add' in str(type(l))] + assert len(add_layers) == depths, 'Wrong number of residual modules (network depths).' + + + def test_ResNet_first_module_dim(self): + """"The output shape throughout the first residual module should be (None, nr_timesteps, min_filters_number)""" + min_filters_number = 16 + + model_type = ResNet((None, 30, 5), 2) + model = model_type.create_model(min_filters_number, 20) + + firstConvlayer = model.layers[2] + firstAddlayer = model.layers[12] + assert firstConvlayer.output_shape == (None, 30, min_filters_number) + assert firstAddlayer.output_shape == (None, 30, min_filters_number) + + def test_ResNet_metrics(self): + """ResNet model should be compiled with the metrics that we give it""" + metrics = ['accuracy', 'mae'] + x_shape = (None, 20, 3) + nr_classes = 2 + X_train, y_train = generate_train_data(x_shape, nr_classes) + + model_type = ResNet(x_shape, nr_classes, metrics=metrics) + model = model_type.create_model(16, 20) + model.fit(X_train, y_train, epochs=1) + + model_metrics = [m.name for m in model.metrics] + for metric in metrics: + assert metric in model_metrics + + def test_ResNet_hyperparameters(self): + """ Network depth from range [4,4] should be 4. + Maximum kernal size from range [10, 10] should be 10. + Minimum filter number from range [16, 16] should be 16. """ + custom_settings = get_default() + kwargs = {'resnet_min_network_depth': 4, + 'resnet_mmax_network_depth': 4, + 'resnet_min_max_kernel_size': 10, + 'resnet_max_max_kernel_size': 10, + 'resnet_min_filters_number': 16, + 'resnet_max_filters_number': 16} + # Replace default parameters with input + for key, value in kwargs.items(): + if key in custom_settings: + custom_settings[key] = value + + model_type = ResNet(None, None, **custom_settings) + hyperparams = model_type.generate_hyperparameters() + + assert hyperparams.get('network_depth') == 4, 'Wrong network depth' + assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' + assert hyperparams.get('min_filters_number') == 16, 'Wrong filter number' From 284fafd056f0886b25cdefb5a61b1c152ba25aa5 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:38:43 +0200 Subject: [PATCH 04/17] InceptionTime was named as ResNet in some comments --- tests/test_modelgen.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index e915d05..c3c0633 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -55,7 +55,6 @@ class ModelGenerationSuite(unittest.TestCase): """Basic test cases.""" - # Tests for InceptionTime model: def test_InceptionTime_starts_with_batchnorm(self): """ InceptionTime models should always start with a batch normalization layer. """ @@ -64,6 +63,7 @@ def test_InceptionTime_starts_with_batchnorm(self): assert 'BatchNormalization' in str(type(model.layers[1])), 'Wrong layer type.' + def test_InceptionTime_first_inception_module(self): """ Test layers of first inception module. """ model_type = InceptionTime((None, 20, 3), 2) @@ -73,8 +73,9 @@ def test_InceptionTime_first_inception_module(self): assert 'MaxPooling1D' in str(type(model.layers[3])), 'Wrong layer type.' assert 'Concatenate' in str(type(model.layers[8])), 'Wrong layer type.' + def test_InceptionTime_depth(self): - """ ResNet model should have depth (number of residual modules) as defined by user. """ + """ InceptionTime model should have depth (number of residual modules) as defined by user. """ depths = 3 model_type = InceptionTime((None, 20, 3), 2) @@ -83,6 +84,7 @@ def test_InceptionTime_depth(self): concat_layers = [str(type(l)) for l in model.layers if 'concatenate' in str(type(l)).lower()] assert len(concat_layers) == depths, 'Wrong number of inception modules (network depths).' + def test_InceptionTime_first_module_dim(self): """"The output shape throughout the first residual module should be (None, nr_timesteps, min_filters_number)""" min_filters_number = 16 @@ -95,8 +97,9 @@ def test_InceptionTime_first_module_dim(self): assert secondConvlayer.output_shape == (None, 30, min_filters_number) assert firstConcatlayer.output_shape == (None, 30, min_filters_number * 4) + def test_InceptionTime_metrics(self): - """ResNet model should be compiled with the metrics that we give it""" + """InceptionTime model should be compiled with the metrics that we give it""" metrics = ['accuracy', 'mae'] x_shape = (None, 20, 3) nr_classes = 2 @@ -110,6 +113,7 @@ def test_InceptionTime_metrics(self): for metric in metrics: assert metric in model_metrics + def test_InceptionTime_hyperparameters(self): """ Network depth from range [5,5] should be 5. Maximum kernal size from range [12, 12] should be 12. @@ -134,6 +138,7 @@ def test_InceptionTime_hyperparameters(self): assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('filters_number') == 32, 'Wrong filter number' + # Tests for general mcfly functionality: def test_generate_models_metrics(self): """ Test if correct number of models is generated and if metrics is correct. """ From a3084a7906e0940788abba600610d5fe2052cd5a Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:42:34 +0200 Subject: [PATCH 05/17] Moved out InceptionTime tests --- tests/test_modelgen.py | 85 --------------------- tests/tests_models/test_inception_time.py | 93 +++++++++++++++++++++++ 2 files changed, 93 insertions(+), 85 deletions(-) create mode 100755 tests/tests_models/test_inception_time.py diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index c3c0633..3fe6960 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -54,91 +54,6 @@ def generate_train_data(x_shape, nr_classes): class ModelGenerationSuite(unittest.TestCase): """Basic test cases.""" - - # Tests for InceptionTime model: - def test_InceptionTime_starts_with_batchnorm(self): - """ InceptionTime models should always start with a batch normalization layer. """ - model_type = InceptionTime((None, 20, 3), 2) - model = model_type.create_model(16) - - assert 'BatchNormalization' in str(type(model.layers[1])), 'Wrong layer type.' - - - def test_InceptionTime_first_inception_module(self): - """ Test layers of first inception module. """ - model_type = InceptionTime((None, 20, 3), 2) - model = model_type.create_model(16) - - assert 'Conv1D' or 'Convolution1D' in str(type(model.layers[2])), 'Wrong layer type.' - assert 'MaxPooling1D' in str(type(model.layers[3])), 'Wrong layer type.' - assert 'Concatenate' in str(type(model.layers[8])), 'Wrong layer type.' - - - def test_InceptionTime_depth(self): - """ InceptionTime model should have depth (number of residual modules) as defined by user. """ - depths = 3 - - model_type = InceptionTime((None, 20, 3), 2) - model = model_type.create_model(16, network_depth=depths) - - concat_layers = [str(type(l)) for l in model.layers if 'concatenate' in str(type(l)).lower()] - assert len(concat_layers) == depths, 'Wrong number of inception modules (network depths).' - - - def test_InceptionTime_first_module_dim(self): - """"The output shape throughout the first residual module should be (None, nr_timesteps, min_filters_number)""" - min_filters_number = 16 - - model_type = InceptionTime((None, 30, 5), 2) - model = model_type.create_model(min_filters_number) - - secondConvlayer = model.layers[5] - firstConcatlayer = model.layers[8] - assert secondConvlayer.output_shape == (None, 30, min_filters_number) - assert firstConcatlayer.output_shape == (None, 30, min_filters_number * 4) - - - def test_InceptionTime_metrics(self): - """InceptionTime model should be compiled with the metrics that we give it""" - metrics = ['accuracy', 'mae'] - x_shape = (None, 20, 3) - nr_classes = 2 - X_train, y_train = generate_train_data(x_shape, nr_classes) - - model_type = InceptionTime(x_shape, nr_classes, metrics=metrics) - model = model_type.create_model(16) - model.fit(X_train, y_train) - - model_metrics = [m.name for m in model.metrics] - for metric in metrics: - assert metric in model_metrics - - - def test_InceptionTime_hyperparameters(self): - """ Network depth from range [5,5] should be 5. - Maximum kernal size from range [12, 12] should be 12. - Minimum filter number from range [32, 32] should be 32. """ - custom_settings = get_default() - x_shape = (None, 20, 3) - kwargs = {'IT_min_network_depth': 5, - 'IT_max_network_depth': 5, - 'IT_min_max_kernel_size': 10, - 'IT_max_max_kernel_size': 10, - 'IT_min_filters_number': 32, - 'IT_max_filters_number': 32} - # Replace default parameters with input - for key, value in kwargs.items(): - if key in custom_settings: - custom_settings[key] = value - - model_type = InceptionTime(x_shape, None, **custom_settings) - hyperparams = model_type.generate_hyperparameters() - - assert hyperparams.get('network_depth') == 5, 'Wrong network depth' - assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' - assert hyperparams.get('filters_number') == 32, 'Wrong filter number' - - # Tests for general mcfly functionality: def test_generate_models_metrics(self): """ Test if correct number of models is generated and if metrics is correct. """ diff --git a/tests/tests_models/test_inception_time.py b/tests/tests_models/test_inception_time.py new file mode 100755 index 0000000..3137c79 --- /dev/null +++ b/tests/tests_models/test_inception_time.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- + +import unittest +from mcfly.models import InceptionTime +from tests.test_modelgen import get_default, generate_train_data + +class InceptionTimeSuite(unittest.TestCase): + """ + Test cases for InceptionTime models. + """ + + # Tests for InceptionTime model: + def test_InceptionTime_starts_with_batchnorm(self): + """ InceptionTime models should always start with a batch normalization layer. """ + model_type = InceptionTime((None, 20, 3), 2) + model = model_type.create_model(16) + + assert 'BatchNormalization' in str(type(model.layers[1])), 'Wrong layer type.' + + + def test_InceptionTime_first_inception_module(self): + """ Test layers of first inception module. """ + model_type = InceptionTime((None, 20, 3), 2) + model = model_type.create_model(16) + + assert 'Conv1D' or 'Convolution1D' in str(type(model.layers[2])), 'Wrong layer type.' + assert 'MaxPooling1D' in str(type(model.layers[3])), 'Wrong layer type.' + assert 'Concatenate' in str(type(model.layers[8])), 'Wrong layer type.' + + + def test_InceptionTime_depth(self): + """ InceptionTime model should have depth (number of residual modules) as defined by user. """ + depths = 3 + + model_type = InceptionTime((None, 20, 3), 2) + model = model_type.create_model(16, network_depth=depths) + + concat_layers = [str(type(l)) for l in model.layers if 'concatenate' in str(type(l)).lower()] + assert len(concat_layers) == depths, 'Wrong number of inception modules (network depths).' + + + def test_InceptionTime_first_module_dim(self): + """"The output shape throughout the first residual module should be (None, nr_timesteps, min_filters_number)""" + min_filters_number = 16 + + model_type = InceptionTime((None, 30, 5), 2) + model = model_type.create_model(min_filters_number) + + secondConvlayer = model.layers[5] + firstConcatlayer = model.layers[8] + assert secondConvlayer.output_shape == (None, 30, min_filters_number) + assert firstConcatlayer.output_shape == (None, 30, min_filters_number * 4) + + + def test_InceptionTime_metrics(self): + """InceptionTime model should be compiled with the metrics that we give it""" + metrics = ['accuracy', 'mae'] + x_shape = (None, 20, 3) + nr_classes = 2 + X_train, y_train = generate_train_data(x_shape, nr_classes) + + model_type = InceptionTime(x_shape, nr_classes, metrics=metrics) + model = model_type.create_model(16) + model.fit(X_train, y_train) + + model_metrics = [m.name for m in model.metrics] + for metric in metrics: + assert metric in model_metrics + + + def test_InceptionTime_hyperparameters(self): + """ Network depth from range [5,5] should be 5. + Maximum kernal size from range [12, 12] should be 12. + Minimum filter number from range [32, 32] should be 32. """ + custom_settings = get_default() + x_shape = (None, 20, 3) + kwargs = {'IT_min_network_depth': 5, + 'IT_max_network_depth': 5, + 'IT_min_max_kernel_size': 10, + 'IT_max_max_kernel_size': 10, + 'IT_min_filters_number': 32, + 'IT_max_filters_number': 32} + # Replace default parameters with input + for key, value in kwargs.items(): + if key in custom_settings: + custom_settings[key] = value + + model_type = InceptionTime(x_shape, None, **custom_settings) + hyperparams = model_type.generate_hyperparameters() + + assert hyperparams.get('network_depth') == 5, 'Wrong network depth' + assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' + assert hyperparams.get('filters_number') == 32, 'Wrong filter number' From 827713b62177c6a6768c009fcc5476756e1fbbd8 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:44:07 +0200 Subject: [PATCH 06/17] Removed unused imports --- tests/test_modelgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index 3fe6960..9285a1a 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -3,7 +3,7 @@ import pytest from tensorflow.keras.models import Model from mcfly import modelgen -from mcfly.models import CNN, ConvLSTM, ResNet, InceptionTime +from mcfly.models import ResNet # TODO: Move this to an utils file, or obtain it from other source? From e2ef96161f2ed9404c78a884a8ff6f35a5c0c13d Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:50:16 +0200 Subject: [PATCH 07/17] Test to guarantee that Travis is picking up these files --- tests/tests_models/test_cnn.py | 4 ++++ tests/tests_models/test_conv_lstm.py | 6 ++++++ tests/tests_models/test_inception_time.py | 6 ++++++ tests/tests_models/test_resnet.py | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/tests/tests_models/test_cnn.py b/tests/tests_models/test_cnn.py index 2897d26..f10df6d 100755 --- a/tests/tests_models/test_cnn.py +++ b/tests/tests_models/test_cnn.py @@ -96,3 +96,7 @@ def test_CNN_hyperparameters_fcnodes(self): hyperparams = model_type.generate_hyperparameters() assert hyperparams.get('fc_hidden_nodes') == 123 + + def test_cnn_travis(self): + """Not sure if Travis's pytest is picking up these files""" + assert False diff --git a/tests/tests_models/test_conv_lstm.py b/tests/tests_models/test_conv_lstm.py index 9e9fa2f..ae1a952 100755 --- a/tests/tests_models/test_conv_lstm.py +++ b/tests/tests_models/test_conv_lstm.py @@ -49,3 +49,9 @@ def test_deepconvlstm_starts_with_batchnorm(self): "lstm_dims": [32, 32]}) assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' + + + + def test_deepconvlstm_travis(self): + """Not sure if Travis's pytest is picking up these files""" + assert False diff --git a/tests/tests_models/test_inception_time.py b/tests/tests_models/test_inception_time.py index 3137c79..0f9bd0c 100755 --- a/tests/tests_models/test_inception_time.py +++ b/tests/tests_models/test_inception_time.py @@ -91,3 +91,9 @@ def test_InceptionTime_hyperparameters(self): assert hyperparams.get('network_depth') == 5, 'Wrong network depth' assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('filters_number') == 32, 'Wrong filter number' + + + def test_InceptionTime_travis(self): + """Not sure if Travis's pytest is picking up these files""" + assert False + diff --git a/tests/tests_models/test_resnet.py b/tests/tests_models/test_resnet.py index 31ebe37..d377517 100755 --- a/tests/tests_models/test_resnet.py +++ b/tests/tests_models/test_resnet.py @@ -87,3 +87,9 @@ def test_ResNet_hyperparameters(self): assert hyperparams.get('network_depth') == 4, 'Wrong network depth' assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('min_filters_number') == 16, 'Wrong filter number' + + + def test_ResNet_travis(self): + """Not sure if Travis's pytest is picking up these files""" + assert False + From ae99b38e1558a88b664dd0010df005cd9152d54d Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:57:01 +0200 Subject: [PATCH 08/17] Revert "Test to guarantee that Travis is picking up these files" This reverts commit e2ef96161f2ed9404c78a884a8ff6f35a5c0c13d. Previous commit proved unnecessary. --- tests/tests_models/test_cnn.py | 4 ---- tests/tests_models/test_conv_lstm.py | 6 ------ tests/tests_models/test_inception_time.py | 6 ------ tests/tests_models/test_resnet.py | 6 ------ 4 files changed, 22 deletions(-) diff --git a/tests/tests_models/test_cnn.py b/tests/tests_models/test_cnn.py index f10df6d..2897d26 100755 --- a/tests/tests_models/test_cnn.py +++ b/tests/tests_models/test_cnn.py @@ -96,7 +96,3 @@ def test_CNN_hyperparameters_fcnodes(self): hyperparams = model_type.generate_hyperparameters() assert hyperparams.get('fc_hidden_nodes') == 123 - - def test_cnn_travis(self): - """Not sure if Travis's pytest is picking up these files""" - assert False diff --git a/tests/tests_models/test_conv_lstm.py b/tests/tests_models/test_conv_lstm.py index ae1a952..9e9fa2f 100755 --- a/tests/tests_models/test_conv_lstm.py +++ b/tests/tests_models/test_conv_lstm.py @@ -49,9 +49,3 @@ def test_deepconvlstm_starts_with_batchnorm(self): "lstm_dims": [32, 32]}) assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' - - - - def test_deepconvlstm_travis(self): - """Not sure if Travis's pytest is picking up these files""" - assert False diff --git a/tests/tests_models/test_inception_time.py b/tests/tests_models/test_inception_time.py index 0f9bd0c..3137c79 100755 --- a/tests/tests_models/test_inception_time.py +++ b/tests/tests_models/test_inception_time.py @@ -91,9 +91,3 @@ def test_InceptionTime_hyperparameters(self): assert hyperparams.get('network_depth') == 5, 'Wrong network depth' assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('filters_number') == 32, 'Wrong filter number' - - - def test_InceptionTime_travis(self): - """Not sure if Travis's pytest is picking up these files""" - assert False - diff --git a/tests/tests_models/test_resnet.py b/tests/tests_models/test_resnet.py index d377517..31ebe37 100755 --- a/tests/tests_models/test_resnet.py +++ b/tests/tests_models/test_resnet.py @@ -87,9 +87,3 @@ def test_ResNet_hyperparameters(self): assert hyperparams.get('network_depth') == 4, 'Wrong network depth' assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('min_filters_number') == 16, 'Wrong filter number' - - - def test_ResNet_travis(self): - """Not sure if Travis's pytest is picking up these files""" - assert False - From 35818a9a5b1baf55ccf39c0b584b9216ba2dbf89 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 17:59:55 +0200 Subject: [PATCH 09/17] Moved files out of subfolder because they failed imports --- ...arameter_generator.py => test_base_hyperparameter_generator.py | 0 tests/test_find_architecture.py => test_find_architecture.py | 0 tests/test_integration.py => test_integration.py | 0 tests/test_modelgen.py => test_modelgen.py | 0 tests/test_storage.py => test_storage.py | 0 tests/test_tools.py => test_tools.py | 0 {tests/tests_models => tests_models}/test_cnn.py | 0 {tests/tests_models => tests_models}/test_conv_lstm.py | 0 {tests/tests_models => tests_models}/test_inception_time.py | 0 {tests/tests_models => tests_models}/test_resnet.py | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename tests/test_base_hyperparameter_generator.py => test_base_hyperparameter_generator.py (100%) rename tests/test_find_architecture.py => test_find_architecture.py (100%) rename tests/test_integration.py => test_integration.py (100%) rename tests/test_modelgen.py => test_modelgen.py (100%) rename tests/test_storage.py => test_storage.py (100%) rename tests/test_tools.py => test_tools.py (100%) rename {tests/tests_models => tests_models}/test_cnn.py (100%) rename {tests/tests_models => tests_models}/test_conv_lstm.py (100%) rename {tests/tests_models => tests_models}/test_inception_time.py (100%) rename {tests/tests_models => tests_models}/test_resnet.py (100%) diff --git a/tests/test_base_hyperparameter_generator.py b/test_base_hyperparameter_generator.py similarity index 100% rename from tests/test_base_hyperparameter_generator.py rename to test_base_hyperparameter_generator.py diff --git a/tests/test_find_architecture.py b/test_find_architecture.py similarity index 100% rename from tests/test_find_architecture.py rename to test_find_architecture.py diff --git a/tests/test_integration.py b/test_integration.py similarity index 100% rename from tests/test_integration.py rename to test_integration.py diff --git a/tests/test_modelgen.py b/test_modelgen.py similarity index 100% rename from tests/test_modelgen.py rename to test_modelgen.py diff --git a/tests/test_storage.py b/test_storage.py similarity index 100% rename from tests/test_storage.py rename to test_storage.py diff --git a/tests/test_tools.py b/test_tools.py similarity index 100% rename from tests/test_tools.py rename to test_tools.py diff --git a/tests/tests_models/test_cnn.py b/tests_models/test_cnn.py similarity index 100% rename from tests/tests_models/test_cnn.py rename to tests_models/test_cnn.py diff --git a/tests/tests_models/test_conv_lstm.py b/tests_models/test_conv_lstm.py similarity index 100% rename from tests/tests_models/test_conv_lstm.py rename to tests_models/test_conv_lstm.py diff --git a/tests/tests_models/test_inception_time.py b/tests_models/test_inception_time.py similarity index 100% rename from tests/tests_models/test_inception_time.py rename to tests_models/test_inception_time.py diff --git a/tests/tests_models/test_resnet.py b/tests_models/test_resnet.py similarity index 100% rename from tests/tests_models/test_resnet.py rename to tests_models/test_resnet.py From 2983d2a6675d31e5edccd6c2b0807ae9137d9089 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:04:34 +0200 Subject: [PATCH 10/17] Revert "Moved files out of subfolder because they failed imports" This reverts commit 35818a9a5b1baf55ccf39c0b584b9216ba2dbf89. Moved wrong files --- .../test_base_hyperparameter_generator.py | 0 test_find_architecture.py => tests/test_find_architecture.py | 0 test_integration.py => tests/test_integration.py | 0 test_modelgen.py => tests/test_modelgen.py | 0 test_storage.py => tests/test_storage.py | 0 test_tools.py => tests/test_tools.py | 0 {tests_models => tests/tests_models}/test_cnn.py | 0 {tests_models => tests/tests_models}/test_conv_lstm.py | 0 {tests_models => tests/tests_models}/test_inception_time.py | 0 {tests_models => tests/tests_models}/test_resnet.py | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename test_base_hyperparameter_generator.py => tests/test_base_hyperparameter_generator.py (100%) rename test_find_architecture.py => tests/test_find_architecture.py (100%) rename test_integration.py => tests/test_integration.py (100%) rename test_modelgen.py => tests/test_modelgen.py (100%) rename test_storage.py => tests/test_storage.py (100%) rename test_tools.py => tests/test_tools.py (100%) rename {tests_models => tests/tests_models}/test_cnn.py (100%) rename {tests_models => tests/tests_models}/test_conv_lstm.py (100%) rename {tests_models => tests/tests_models}/test_inception_time.py (100%) rename {tests_models => tests/tests_models}/test_resnet.py (100%) diff --git a/test_base_hyperparameter_generator.py b/tests/test_base_hyperparameter_generator.py similarity index 100% rename from test_base_hyperparameter_generator.py rename to tests/test_base_hyperparameter_generator.py diff --git a/test_find_architecture.py b/tests/test_find_architecture.py similarity index 100% rename from test_find_architecture.py rename to tests/test_find_architecture.py diff --git a/test_integration.py b/tests/test_integration.py similarity index 100% rename from test_integration.py rename to tests/test_integration.py diff --git a/test_modelgen.py b/tests/test_modelgen.py similarity index 100% rename from test_modelgen.py rename to tests/test_modelgen.py diff --git a/test_storage.py b/tests/test_storage.py similarity index 100% rename from test_storage.py rename to tests/test_storage.py diff --git a/test_tools.py b/tests/test_tools.py similarity index 100% rename from test_tools.py rename to tests/test_tools.py diff --git a/tests_models/test_cnn.py b/tests/tests_models/test_cnn.py similarity index 100% rename from tests_models/test_cnn.py rename to tests/tests_models/test_cnn.py diff --git a/tests_models/test_conv_lstm.py b/tests/tests_models/test_conv_lstm.py similarity index 100% rename from tests_models/test_conv_lstm.py rename to tests/tests_models/test_conv_lstm.py diff --git a/tests_models/test_inception_time.py b/tests/tests_models/test_inception_time.py similarity index 100% rename from tests_models/test_inception_time.py rename to tests/tests_models/test_inception_time.py diff --git a/tests_models/test_resnet.py b/tests/tests_models/test_resnet.py similarity index 100% rename from tests_models/test_resnet.py rename to tests/tests_models/test_resnet.py From 3828386a400ca009d5ec1f94421f7c3f66cb319a Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:06:35 +0200 Subject: [PATCH 11/17] Moved model tests out from subfolder because of failed imports in Travis --- tests/{tests_models => }/test_cnn.py | 0 tests/{tests_models => }/test_conv_lstm.py | 0 tests/{tests_models => }/test_inception_time.py | 0 tests/{tests_models => }/test_resnet.py | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename tests/{tests_models => }/test_cnn.py (100%) rename tests/{tests_models => }/test_conv_lstm.py (100%) rename tests/{tests_models => }/test_inception_time.py (100%) rename tests/{tests_models => }/test_resnet.py (100%) diff --git a/tests/tests_models/test_cnn.py b/tests/test_cnn.py similarity index 100% rename from tests/tests_models/test_cnn.py rename to tests/test_cnn.py diff --git a/tests/tests_models/test_conv_lstm.py b/tests/test_conv_lstm.py similarity index 100% rename from tests/tests_models/test_conv_lstm.py rename to tests/test_conv_lstm.py diff --git a/tests/tests_models/test_inception_time.py b/tests/test_inception_time.py similarity index 100% rename from tests/tests_models/test_inception_time.py rename to tests/test_inception_time.py diff --git a/tests/tests_models/test_resnet.py b/tests/test_resnet.py similarity index 100% rename from tests/tests_models/test_resnet.py rename to tests/test_resnet.py From 24284ecc2db6dce48c834f66d8f734bb5a1e78aa Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:07:22 +0200 Subject: [PATCH 12/17] Corrected import --- tests/test_cnn.py | 2 +- tests/test_conv_lstm.py | 2 +- tests/test_inception_time.py | 2 +- tests/test_resnet.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_cnn.py b/tests/test_cnn.py index 2897d26..d20f59d 100755 --- a/tests/test_cnn.py +++ b/tests/test_cnn.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import unittest from mcfly.models import CNN -from tests.test_modelgen import get_default +from test_modelgen import get_default class CNNSuite(unittest.TestCase): diff --git a/tests/test_conv_lstm.py b/tests/test_conv_lstm.py index 9e9fa2f..9d0ba93 100755 --- a/tests/test_conv_lstm.py +++ b/tests/test_conv_lstm.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import unittest from mcfly.models import ConvLSTM -from tests.test_modelgen import get_default +from test_modelgen import get_default class ConvLSTMSuite(unittest.TestCase): """ diff --git a/tests/test_inception_time.py b/tests/test_inception_time.py index 3137c79..a7b28b3 100755 --- a/tests/test_inception_time.py +++ b/tests/test_inception_time.py @@ -2,7 +2,7 @@ import unittest from mcfly.models import InceptionTime -from tests.test_modelgen import get_default, generate_train_data +from test_modelgen import get_default, generate_train_data class InceptionTimeSuite(unittest.TestCase): """ diff --git a/tests/test_resnet.py b/tests/test_resnet.py index 31ebe37..044b41c 100755 --- a/tests/test_resnet.py +++ b/tests/test_resnet.py @@ -2,7 +2,7 @@ import unittest from mcfly.models import ResNet -from tests.test_modelgen import get_default, generate_train_data +from test_modelgen import get_default, generate_train_data class ResNetSuite(unittest.TestCase): """ From e1bf3f142e0aa8bf0e1e9dcda7b5efc77a793ec6 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:11:52 +0200 Subject: [PATCH 13/17] Added main to execute test locally --- tests/test_cnn.py | 5 +++++ tests/test_conv_lstm.py | 4 ++++ tests/test_inception_time.py | 4 ++++ tests/test_resnet.py | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/tests/test_cnn.py b/tests/test_cnn.py index d20f59d..cb366e9 100755 --- a/tests/test_cnn.py +++ b/tests/test_cnn.py @@ -96,3 +96,8 @@ def test_CNN_hyperparameters_fcnodes(self): hyperparams = model_type.generate_hyperparameters() assert hyperparams.get('fc_hidden_nodes') == 123 + + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_conv_lstm.py b/tests/test_conv_lstm.py index 9d0ba93..2e62f6d 100755 --- a/tests/test_conv_lstm.py +++ b/tests/test_conv_lstm.py @@ -49,3 +49,7 @@ def test_deepconvlstm_starts_with_batchnorm(self): "lstm_dims": [32, 32]}) assert 'BatchNormalization' in str(type(model.layers[0])), 'Wrong layer type.' + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_inception_time.py b/tests/test_inception_time.py index a7b28b3..62d8d4a 100755 --- a/tests/test_inception_time.py +++ b/tests/test_inception_time.py @@ -91,3 +91,7 @@ def test_InceptionTime_hyperparameters(self): assert hyperparams.get('network_depth') == 5, 'Wrong network depth' assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('filters_number') == 32, 'Wrong filter number' + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_resnet.py b/tests/test_resnet.py index 044b41c..290c5dc 100755 --- a/tests/test_resnet.py +++ b/tests/test_resnet.py @@ -87,3 +87,7 @@ def test_ResNet_hyperparameters(self): assert hyperparams.get('network_depth') == 4, 'Wrong network depth' assert hyperparams.get('max_kernel_size') == 10, 'Wrong kernel' assert hyperparams.get('min_filters_number') == 16, 'Wrong filter number' + + +if __name__ == '__main__': + unittest.main() From 07cf115cad793259c299dd5a8f39e8b3ea036953 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:12:45 +0200 Subject: [PATCH 14/17] Import generate train data --- tests/test_cnn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_cnn.py b/tests/test_cnn.py index cb366e9..3506d05 100755 --- a/tests/test_cnn.py +++ b/tests/test_cnn.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import unittest from mcfly.models import CNN -from test_modelgen import get_default +from test_modelgen import get_default, generate_train_data class CNNSuite(unittest.TestCase): @@ -54,7 +54,7 @@ def test_cnn_metrics(self): metrics = ['accuracy', 'mae'] x_shape = (None, 20, 3) nr_classes = 2 - X_train, y_train = self._generate_train_data(x_shape, nr_classes) + X_train, y_train = generate_train_data(x_shape, nr_classes) model_type = CNN(x_shape, nr_classes, metrics=metrics) model = model_type.create_model(**{"filters": [32, 32], From 4823927d13fdd904efe37f0260a78a648466254c Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:28:42 +0200 Subject: [PATCH 15/17] Fixed typo --- tests/test_resnet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_resnet.py b/tests/test_resnet.py index 290c5dc..f654416 100755 --- a/tests/test_resnet.py +++ b/tests/test_resnet.py @@ -71,7 +71,7 @@ def test_ResNet_hyperparameters(self): Minimum filter number from range [16, 16] should be 16. """ custom_settings = get_default() kwargs = {'resnet_min_network_depth': 4, - 'resnet_mmax_network_depth': 4, + 'resnet_max_network_depth': 4, 'resnet_min_max_kernel_size': 10, 'resnet_max_max_kernel_size': 10, 'resnet_min_filters_number': 16, From d772162d354033e605a85efca6e231d9ada152e2 Mon Sep 17 00:00:00 2001 From: breixo Date: Thu, 30 Jul 2020 18:36:30 +0200 Subject: [PATCH 16/17] Fixed extra tab --- tests/test_modelgen.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index 9285a1a..a0563a2 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -46,9 +46,9 @@ def get_default(): # TODO: Move this to an utils file? def generate_train_data(x_shape, nr_classes): - X_train = np.random.rand(1, *x_shape[1:]) - y_train = np.random.randint(0, 1, size=(1, nr_classes)) - return X_train, y_train + X_train = np.random.rand(1, *x_shape[1:]) + y_train = np.random.randint(0, 1, size=(1, nr_classes)) + return X_train, y_train class ModelGenerationSuite(unittest.TestCase): From 5ae59002b2f07aa2c78fca50fcca2c8c0798fcfd Mon Sep 17 00:00:00 2001 From: breixo Date: Fri, 31 Jul 2020 11:38:18 +0200 Subject: [PATCH 17/17] Removed todos --- tests/test_modelgen.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_modelgen.py b/tests/test_modelgen.py index a0563a2..31a3889 100644 --- a/tests/test_modelgen.py +++ b/tests/test_modelgen.py @@ -6,7 +6,6 @@ from mcfly.models import ResNet -# TODO: Move this to an utils file, or obtain it from other source? def get_default(): """ "Define mcflu default parameters as dictionary. """ settings = {'metrics': ['accuracy'], @@ -44,7 +43,6 @@ def get_default(): return settings -# TODO: Move this to an utils file? def generate_train_data(x_shape, nr_classes): X_train = np.random.rand(1, *x_shape[1:]) y_train = np.random.randint(0, 1, size=(1, nr_classes))