From 2404827cb49812c13c9b340a5e5640ddd2e80366 Mon Sep 17 00:00:00 2001 From: Felix Kleinert <f.kleinert@fz-juelich.de> Date: Tue, 3 Mar 2020 10:46:36 +0100 Subject: [PATCH] update raise conditions for invalid data formats --- src/model_modules/advanced_paddings.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/model_modules/advanced_paddings.py b/src/model_modules/advanced_paddings.py index f5a386fb..6450752a 100644 --- a/src/model_modules/advanced_paddings.py +++ b/src/model_modules/advanced_paddings.py @@ -27,7 +27,11 @@ class PadUtils: """ if strides != 1: raise NotImplementedError("Strides other than 1 not implemented!") + if not all(isinstance(k, int) for k in kernel_size): + raise ValueError(f"The `kernel_size` argument must have a tuple of integers. Got: {kernel_size}") + ks = np.array(kernel_size, dtype=np.int64) + if any(k <= 0 for k in ks): raise ValueError(f"All values of kernel_size must be > 0. Got: {kernel_size} ") @@ -36,8 +40,9 @@ class PadUtils: # convert numpy int to base int pad = [np.asscalar(v) for v in pad] return tuple(pad) + # return tuple(PadUtils.check_padding_format(pad)) else: - raise NotImplementedError("even kernel size not implemented") + raise NotImplementedError(f"even kernel size not implemented. Got {kernel_size}") @staticmethod def spatial_2d_padding(padding=((1, 1), (1, 1)), data_format=None): @@ -74,6 +79,16 @@ class PadUtils: if len(padding) != 2: raise ValueError('`padding` should have two elements. ' 'Found: ' + str(padding)) + for idx_pad, sub_pad in enumerate(padding): + if isinstance(sub_pad, str): + raise ValueError(f'`padding[{idx_pad}]` is str but must be int') + if hasattr(sub_pad, '__len__'): + if len(sub_pad) != 2: + raise ValueError(f'`padding[{idx_pad}]` should have one or two elements. ' + f'Found: {padding[idx_pad]}') + if not all(isinstance(sub_k, int) for sub_k in padding[idx_pad]): + raise ValueError(f'`padding[{idx_pad}]` should have one or two elements of type int. ' + f"Found:{padding[idx_pad]} of type {[type(sub_k) for sub_k in padding[idx_pad]]}") height_padding = conv_utils.normalize_tuple(padding[0], 2, '1st entry of padding') if not all(k >= 0 for k in height_padding): @@ -239,17 +254,18 @@ class SymmetricPadding2D(_ZeroPadding): if __name__ == '__main__': from keras.models import Model from keras.layers import Conv2D, Flatten, Dense, Input + kernel_1 = (3, 3) kernel_2 = (5, 5) x = np.array(range(2000)).reshape(-1, 10, 10, 1) y = x.mean(axis=(1, 2)) x_input = Input(shape=x.shape[1:]) - pad1 = PadUtils.get_padding_for_same(kernel_1) + pad1 = PadUtils.get_padding_for_same(kernel_size=kernel_1) x_out = ReflectionPadding2D(padding=pad1, name="RefPAD")(x_input) x_out = Conv2D(5, kernel_size=kernel_1, activation='relu')(x_out) - pad2 = PadUtils.get_padding_for_same(kernel_2) + pad2 = PadUtils.get_padding_for_same(kernel_size=kernel_2) x_out = SymmetricPadding2D(padding=pad2, name="SymPAD")(x_out) x_out = Conv2D(2, kernel_size=kernel_2, activation='relu')(x_out) x_out = Flatten()(x_out) @@ -258,5 +274,6 @@ if __name__ == '__main__': model = Model(inputs=x_input, outputs=x_out) model.compile('adam', loss='mse') model.summary() + model.fit(x, y, epochs=10) -- GitLab