在keras中保存模型的非类型错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在keras中保存模型的非类型错误相关的知识,希望对你有一定的参考价值。

错误:TypeError:Fetch参数None具有无效类型

我认为当我在回调模型检查点中保存模型时会发生错误。在搜索错误时,this出现但我不能使用这个答案,因为我使用keras因此我没有在tensorflow中显式调用sess.run()。这个时代也是完美无瑕的训练,只有当它被保存时才会弹出错误。

码:

完整的模型在这里链接的kaggle笔记本:https://www.kaggle.com/aevinq/cnn-batchnormalization-0-1646/

弹出错误的相关代码是:

early_stopping = EarlyStopping(monitor='val_loss', patience=5, mode='min')
mcp_save = ModelCheckpoint('md.hdf5', save_best_only=True, monitor='val_loss', mode='min')
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, epsilon=1e-4, mode='min')
history = model.fit(train_X, train_y, batch_size=32, epochs=20, verbose=1, validation_split=0.25, callbacks=[early_stopping, reduce_lr_loss, mcp_save])

错误:

Train on 4413 samples, validate on 1471 samples
Epoch 1/20
4384/4413 [============================>.] - ETA: 1s - loss: 0.5157 - acc: 0.7696
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-97f0757a1e9c> in <module>()
      2 mcp_save = ModelCheckpoint('md.hdf5', save_best_only=True, monitor='val_loss', mode='min')
      3 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, epsilon=1e-4, mode='min')
----> 4 history = model.fit(train_X, train_y, batch_size=32, epochs=20, verbose=1, validation_split=0.25, callbacks=[early_stopping, reduce_lr_loss, mcp_save])

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    970                               initial_epoch=initial_epoch,
    971                               steps_per_epoch=steps_per_epoch,
--> 972                               validation_steps=validation_steps)
    973 
    974     def evaluate(self, x=None, y=None,

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1655                               initial_epoch=initial_epoch,
   1656                               steps_per_epoch=steps_per_epoch,
-> 1657                               validation_steps=validation_steps)
   1658 
   1659     def evaluate(self, x=None, y=None,

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
   1231                             for l, o in zip(out_labels, val_outs):
   1232                                 epoch_logs['val_' + l] = o
-> 1233             callbacks.on_epoch_end(epoch, epoch_logs)
   1234             if callback_model.stop_training:
   1235                 break

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/callbacks.py in on_epoch_end(self, epoch, logs)
     71         logs = logs or {}
     72         for callback in self.callbacks:
---> 73             callback.on_epoch_end(epoch, logs)
     74 
     75     def on_batch_begin(self, batch, logs=None):

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/callbacks.py in on_epoch_end(self, epoch, logs)
    413                             self.model.save_weights(filepath, overwrite=True)
    414                         else:
--> 415                             self.model.save(filepath, overwrite=True)
    416                     else:
    417                         if self.verbose > 0:

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/engine/topology.py in save(self, filepath, overwrite, include_optimizer)
   2563         """
   2564         from ..models import save_model
-> 2565         save_model(self, filepath, overwrite, include_optimizer)
   2566 
   2567     def save_weights(self, filepath, overwrite=True):

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/models.py in save_model(model, filepath, overwrite, include_optimizer)
    145                 if symbolic_weights:
    146                     optimizer_weights_group = f.create_group('optimizer_weights')
--> 147                     weight_values = K.batch_get_value(symbolic_weights)
    148                     weight_names = []
    149                     for i, (w, val) in enumerate(zip(symbolic_weights,

/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/backend/tensorflow_backend.py in batch_get_value(ops)
   2208     """
   2209     if ops:
-> 2210         return get_session().run(ops)
   2211     else:
   2212         return []

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    887     try:
    888       result = self._run(None, fetches, feed_dict, options_ptr,
--> 889                          run_metadata_ptr)
    890       if run_metadata:
    891         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1103     # Create a fetch handler to take care of the structure of fetches.
   1104     fetch_handler = _FetchHandler(
-> 1105         self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
   1106 
   1107     # Run request and get response.

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds, feed_handles)
    412     """
    413     with graph.as_default():
--> 414       self._fetch_mapper = _FetchMapper.for_fetch(fetches)
    415     self._fetches = []
    416     self._targets = []

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    232     elif isinstance(fetch, (list, tuple)):
    233       # NOTE(touts): This is also the code path for namedtuples.
--> 234       return _ListFetchMapper(fetch)
    235     elif isinstance(fetch, dict):
    236       return _DictFetchMapper(fetch)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in __init__(self, fetches)
    339     """
    340     self._fetch_type = type(fetches)
--> 341     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    342     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    343 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in <listcomp>(.0)
    339     """
    340     self._fetch_type = type(fetches)
--> 341     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    342     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    343 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    229     if fetch is None:
    230       raise TypeError('Fetch argument %r has invalid type %r' %
--> 231                       (fetch, type(fetch)))
    232     elif isinstance(fetch, (list, tuple)):
    233       # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>
答案

这是Keras的一个错误。最近更新后None中有model.optimizer.weights值,在模型保存期间调用K.batch_get_value时会导致错误。

我打开了一个PR来修复它并将它合并。您可以在Github上安装最新的Keras来修复它。

以上是关于在keras中保存模型的非类型错误的主要内容,如果未能解决你的问题,请参考以下文章

Keras - LeakyReLU 保存模型时没有属性名称错误

如何使用 keras 加载保存的模型? (错误: : TypeError: __init__() 得到了一个意外的关键字参数“可训练”)

微调 keras .h5 模型到 C++ 中使用的 .pb 模型会导致运行模型失败错误

保存包裹在 Keras 中的模型

在 keras 中保存最佳模型

错误 "IndexError: 如何在Keras中使用训练好的模型预测输入图像?