无法使用Colab在Tensorflow中保存/加载Keras模型

时间:2018-09-29 15:14:09

标签: python tensorflow keras

我正在使用GettingStarted tutorial关注Tensorflow的Colab notebooks

成功遵循它之后,我决定尝试一下保存模型。我试图将其保存起来,但效果很好。 (感谢this的回答)

# Saving Model to Drive

!pip install -U -q PyDrive

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive 
client.auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Save Keras Model or weights on google drive
model.save('getting-started-wtih-tf.h5')  
model_file = drive.CreateFile({'title' : 'getting-started-wtih-tf.h5'})
model_file.SetContentFile('getting-started-wtih-tf.h5')
model_file.Upload()

# download to google drive
drive.CreateFile({'id': model_file.get('id')})

但是几周后,它因出现此异常而开始失败。

NotImplementedErrorTraceback (most recent call last)
<ipython-input-28-9fcdb77fc96c> in <module>()
     15 # Save Keras Model or weights on google drive
     16 # tf.keras.models.save_model(model, 'getting-started-wtih-tf.h5')
---> 17 model.save('getting-started-wtih-tf.h5')
     18 model_file = drive.CreateFile({'title' : 'getting-started-wtih-tf.h5'})
     19 model_file.SetContentFile('getting-started-wtih-tf.h5')

/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/network.pyc in save(self, filepath, overwrite, include_optimizer)
   1356     """
   1357     if not self._is_graph_network:
-> 1358       raise NotImplementedError
   1359 
   1360     from tensorflow.python.keras.models import save_model  #pylint: disable=g-import-not-at-top

NotImplementedError: 

我试图通过以下方法克服此问题:

model.save('getting-started-wtih-tf.h5')

指向面向TF的保存语句的行:

tf.keras.models.save_model(model, 'getting-started-wtih-tf.h5')

但是现在,我无法使用以下这些加载相同的模型:

# Loading Model and Reavaluating
new_model = tf.keras.models.load_model('getting-started-wtih-tf.h5')
new_model.summary()

new_model.evaluate(x_test_gray_scale, y_test)

因为我得到了:

AttributeErrorTraceback (most recent call last)
<ipython-input-43-f7b789f2dd31> in <module>()
----> 1 new_model = tf.keras.models.load_model('getting-started-wtih-tf.h5')
      2 new_model.summary()
      3 
      4 new_model.evaluate(x_test_gray_scale, y_test)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/saving.pyc in load_model(filepath, custom_objects, compile)
    260           weighted_metrics=weighted_metrics,
    261           loss_weights=loss_weights,
--> 262           sample_weight_mode=sample_weight_mode)
    263 
    264       # Set optimizer weights.

/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/checkpointable/base.py in _method_wrapper(self, *args, **kwargs)
    424     self._setattr_tracking = False  # pylint: disable=protected-access
    425     try:
--> 426       method(self, *args, **kwargs)
    427     finally:
    428       self._setattr_tracking = previous_value  # pylint: disable=protected-access

/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/training.pyc in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, distribute, **kwargs)
    523     # One list per output of the model.
    524     self.nested_metrics = training_utils.collect_metrics(
--> 525         metrics, self.output_names)
    526     self.nested_weighted_metrics = training_utils.collect_metrics(
    527         weighted_metrics, self.output_names)

AttributeError: 'Sequential' object has no attribute 'output_names'

简而言之,我的问题是,如何在Tensorflow中保存/加载keras模型?

此外,this is the notebook我正在努力。

0 个答案:

没有答案