Repository: KIT-IAI/pyWATTS

Issue:
Keras Save Load
**Describe the bug**
The path of the saved keras model distinguishes from the path saved in the json. And targets is not part of the saved keras model, if the model is fitted.


Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ec3b099..65f8588 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@
 
 ### Fixed
 * Missing call of _get_rolling in Rolling Base  ([#76](https://github.com/KIT-IAI/pyWATTS/issues/76))
+* Fix save and load of keras Wrapper  ([#91](https://github.com/KIT-IAI/pyWATTS/issues/91))
 * Import of pytorchWrapper via wrapper.__init__
 
 
diff --git a/pywatts/wrapper/keras_wrapper.py b/pywatts/wrapper/keras_wrapper.py
index 6cc5da2..266c985 100644
--- a/pywatts/wrapper/keras_wrapper.py
+++ b/pywatts/wrapper/keras_wrapper.py
@@ -26,6 +26,7 @@ class KerasWrapper(DlWrapper):
     def __init__(self, model: Union[tf.keras.Model, Tuple[tf.keras.Model, Dict[str, tf.keras.Model]]],
                  name: str = "KerasWrapper", fit_kwargs=None, compile_kwargs=None):
         self.aux_models = {}
+        self.targets = []
         if isinstance(model, tuple):
             self.aux_models = model[1]
             model = model[0]
@@ -73,14 +74,17 @@ class KerasWrapper(DlWrapper):
         :return: The path where the model is stored.
         """
         json = super().save(fm)
-        self.model.save(filepath=fm.get_path(f"{self.name}.h5"))
+        json["targets"] = self.targets
+        model_path = fm.get_path(f"{self.name}.h5")
+        self.model.save(filepath=model_path)
         aux_models = []
         for name, aux_model in self.aux_models.items():
-            aux_model.save(filepath=fm.get_path(f"{self.name}_{name}.h5"))
-            aux_models.append((name, fm.get_path(f"{self.name}_{name}.h5")))
+            aux_model_path = fm.get_path(f"{self.name}_{name}.h5")
+            aux_model.save(filepath=aux_model_path)
+            aux_models.append((name, aux_model_path))
         json.update({
             "aux_models": aux_models,
-            "model": fm.get_path(f"{self.name}.h5")
+            "model": model_path
         })
 
         return json
@@ -112,6 +116,8 @@ class KerasWrapper(DlWrapper):
         else:
             module = cls(model, name=name, **params)
         module.is_fitted = load_information["is_fitted"]
+
+        module.targets = load_information["targets"]
         return module
 
     def get_params(self) -> Dict[str, object]:


Test patch:
diff --git a/tests/unit/wrapper/test_keras_wrapper.py b/tests/unit/wrapper/test_keras_wrapper.py
index c7ad45d..282c305 100644
--- a/tests/unit/wrapper/test_keras_wrapper.py
+++ b/tests/unit/wrapper/test_keras_wrapper.py
@@ -37,7 +37,8 @@ stored_model = {
             "batch_size": 512,
             "epochs": 1
         }
-    }
+    },
+    "targets": []
 }
 
 
@@ -159,7 +160,8 @@ class TestKerasWrapper(unittest.TestCase):
                                 'model': os.path.join("new_path", "to_somewhere", "KerasWrapper.h5"),
                                 'module': 'pywatts.wrapper.keras_wrapper',
                                 'name': 'KerasWrapper',
-                                'params': {'compile_kwargs': {"test": "arg1"}, 'fit_kwargs': {"42": 24}}
+                                'params': {'compile_kwargs': {"test": "arg1"}, 'fit_kwargs': {"42": 24}},
+                                "targets":[]
                                 })
 
     @patch('pywatts.wrapper.keras_wrapper.tf.keras.models.load_model')