Ejemplo n.º 1
0
 def test_save_tf(self):
     path = os.path.join(self.get_temp_dir(), 'model')
     with self.assertRaisesRegexp(
             NotImplementedError,
             'Saving the model as SavedModel is still in experimental stages.'
     ):
         save.save_model(self.model, path, save_format='tf')
Ejemplo n.º 2
0
 def test_save_format_defaults_pathlib(self):
     if sys.version_info < (3, 6):
         self.skipTest(
             'pathlib is only available for python version >= 3.6')
     path = pathlib.Path(self.get_temp_dir()) / 'model_path'
     save.save_model(self.model, path)
     self.assert_saved_model(path)
Ejemplo n.º 3
0
 def test_save_load_hdf5_pathlib(self):
     if sys.version_info < (3, 6):
         self.skipTest(
             'pathlib is only available for python version >= 3.6')
     path = pathlib.Path(self.get_temp_dir()) / 'model'
     save.save_model(self.model, path, save_format='h5')
     save.load_model(path)
Ejemplo n.º 4
0
def save_model(model, path, optimizer=True):

    if isinstance(path, str):
        if path.endswith(".h5") or path.endswith(".hdf5"):
            filepath = path
        else:
            raise ValueError("file must be .h5 file")
    else:
        raise TypeError("file must be type `str`")

    save.save_model(model.train_model, path, include_optimizer=optimizer)

    with h5py.File(filepath, "r+") as h5file:

        train_generator = model.train_generator

        h5file.attrs["train_generator_config"] = json.dumps(
            {
                "class_name": train_generator.__class__.__name__,
                "config": train_generator.get_config(),
            },
            default=get_json_type,
        ).encode("utf8")

        h5file.attrs["pose_model_config"] = json.dumps(
            {
                "class_name": model.__class__.__name__,
                "config": model.get_config()
            },
            default=get_json_type,
        ).encode("utf8")
Ejemplo n.º 5
0
 def test_save_hdf5(self):
   path = os.path.join(self.get_temp_dir(), 'model')
   save.save_model(self.model, path, save_format='h5')
   self.assert_h5_format(path)
   with self.assertRaisesRegex(
       NotImplementedError,
       'requires the model to be a Functional model or a Sequential model.'):
     save.save_model(self.subclassed_model, path, save_format='h5')
Ejemplo n.º 6
0
    def test_layer_saving_with_h5(self):
        vocab_data = ["earth", "wind", "and", "fire"]

        input_data = keras.Input(shape=(None, ), dtype=dtypes.string)
        layer = get_layer_class()(max_tokens=10)
        layer.set_vocabulary(vocab_data)
        int_data = layer(input_data)
        model = keras.Model(inputs=input_data, outputs=int_data)
        path = os.path.join(self.get_temp_dir(), "model")
        with self.assertRaisesRegex(NotImplementedError,
                                    "Save or restore weights that is not.*"):
            save.save_model(model, path, save_format="h5")
Ejemplo n.º 7
0
 def test_load_weights(self):
     """
 test loading model weights
 """
     model = testing_utils.get_small_functional_mlp(
         3, 2, 3, ["fc", "output", "input"])
     model.compile(loss=keras.losses.MSE,
                   optimizer=keras.optimizers.RMSprop(lr=0.0001),
                   metrics=[keras.metrics.categorical_accuracy])
     path = os.path.join(self.get_temp_dir(), 'model')
     save.save_model(model, path, save_format='h5')
     del model
     model = testing_utils.get_small_functional_mlp(
         2, 2, 3, ["fc", "output", "input"])
     model.load_weights(path, by_name=True, skip_mismatch=True)
     with self.assertRaises(ValueError):
         model.load_weights(path, by_name=True, skip_mismatch=False)
     del model
Ejemplo n.º 8
0
 def test_save_tf(self):
   path = os.path.join(self.get_temp_dir(), 'model')
   save.save_model(self.model, path, save_format='tf')
   self.assert_saved_model(path)
   with self.assertRaisesRegex(ValueError, 'input shapes have not been set'):
     save.save_model(self.subclassed_model, path, save_format='tf')
   self.subclassed_model.predict(np.random.random((3, 5)))
   save.save_model(self.subclassed_model, path, save_format='tf')
   self.assert_saved_model(path)
Ejemplo n.º 9
0
 def test_save_format_defaults(self):
   path = os.path.join(self.get_temp_dir(), 'model_path')
   save.save_model(self.model, path)
   self.assert_saved_model(path)
Ejemplo n.º 10
0
 def test_save_load_tf_string(self):
   path = os.path.join(self.get_temp_dir(), 'model')
   save.save_model(self.model, path, save_format='tf')
   save.load_model(path)
Ejemplo n.º 11
0
 def test_save_load_tf_pathlib(self):
     if sys.version_info >= (3, 4):
         path = pathlib.Path(self.get_temp_dir()) / 'model'
         save.save_model(self.model, path, save_format='tf')
         save.load_model(path)
Ejemplo n.º 12
0
    def test_save_format_defaults(self):
        path = os.path.join(self.get_temp_dir(), 'model_path')

        # The default is currently HDF5 no matter what the filepath is.
        save.save_model(self.model, path)
        self.assert_h5_format(path)
Ejemplo n.º 13
0
    def test_save_hdf5(self):
        path = os.path.join(self.get_temp_dir(), 'model')
        save.save_model(self.model, path, save_format='h5')

        self.assert_h5_format(path)
Ejemplo n.º 14
0
 def test_save_format_defaults_pathlib(self):
   path = pathlib.Path(self.get_temp_dir()) / 'model_path'
   save.save_model(self.model, path)
   self.assert_saved_model(path)
Ejemplo n.º 15
0
  def test_save_format_defaults(self):
    path = os.path.join(self.get_temp_dir(), 'model_path')

    # The default is currently HDF5 no matter what the filepath is.
    save.save_model(self.model, path)
    self.assert_h5_format(path)
Ejemplo n.º 16
0
 def test_save_tf(self):
   path = os.path.join(self.get_temp_dir(), 'model')
   with self.assertRaisesRegexp(
       NotImplementedError,
       'Saving the model as SavedModel is still in experimental stages.'):
     save.save_model(self.model, path, save_format='tf')
Ejemplo n.º 17
0
  def test_save_hdf5(self):
    path = os.path.join(self.get_temp_dir(), 'model')
    save.save_model(self.model, path, save_format='h5')

    self.assert_h5_format(path)
Ejemplo n.º 18
0
 def _save_model(self, model, saved_dir):
     save.save_model(model, saved_dir, save_format='tf')
Ejemplo n.º 19
0
 def test_save_load_hdf5_pathlib(self):
   path = pathlib.Path(self.get_temp_dir()) / 'model'
   save.save_model(self.model, path, save_format='h5')
   save.load_model(path)