【问题标题】:Can not save model using model.save following multi_gpu_model in Keras无法在 Keras 中使用 multi_gpu_model 之后的 model.save 保存模型
【发布时间】:2018-04-22 23:25:44
【问题描述】:

在升级到 Keras 2.0.9 后,我一直在使用 multi_gpu_model 实用程序,但我无法使用

保存我的模型或最佳权重
model.save('path')

我得到的错误是

TypeError: can't pickle module objects

我怀疑获取模型对象的访问权存在一些问题。有解决此问题的方法吗?

【问题讨论】:

标签: tensorflow keras distributed-computing multi-gpu keras-2


【解决方案1】:

说实话,最简单的方法是使用实​​际检查多 gpu 并行模型

 parallel_model.summary()

(并行模型只是应用了multi_gpu函数后的模型)。这清楚地突出了实际模型(我认为倒数第二层 - 我现在不在我的电脑旁)。然后就可以用这个层的名字来保存模型了。

 model = parallel_model.get_layer('sequential_1)

它通常称为sequential_1,但如果您使用的是已发布的架构,它可能是“googlenet”或“alexnet”。您将从摘要中看到图层的名称。

那么保存就很简单了

 model.save()

格言方法有效,但我认为它有点矫枉过正。

Rem:您需要编译模型和并行模型。

【讨论】:

  • 另请注意,为了从模型中获取中间层的输出,您必须首先像 GhostRider 解释的那样打开模型,然后访问这些层。 model.get_layer('last_dense_layer').output 例如
【解决方案2】:

解决方法

这是一个在保存时不会失败的修补版本:

from keras.layers import Lambda, concatenate
from keras import Model
import tensorflow as tf

def multi_gpu_model(model, gpus):
  if isinstance(gpus, (list, tuple)):
    num_gpus = len(gpus)
    target_gpu_ids = gpus
  else:
    num_gpus = gpus
    target_gpu_ids = range(num_gpus)

  def get_slice(data, i, parts):
    shape = tf.shape(data)
    batch_size = shape[:1]
    input_shape = shape[1:]
    step = batch_size // parts
    if i == num_gpus - 1:
      size = batch_size - step * i
    else:
      size = step
    size = tf.concat([size, input_shape], axis=0)
    stride = tf.concat([step, input_shape * 0], axis=0)
    start = stride * i
    return tf.slice(data, start, size)

  all_outputs = []
  for i in range(len(model.outputs)):
    all_outputs.append([])

  # Place a copy of the model on each GPU,
  # each getting a slice of the inputs.
  for i, gpu_id in enumerate(target_gpu_ids):
    with tf.device('/gpu:%d' % gpu_id):
      with tf.name_scope('replica_%d' % gpu_id):
        inputs = []
        # Retrieve a slice of the input.
        for x in model.inputs:
          input_shape = tuple(x.get_shape().as_list())[1:]
          slice_i = Lambda(get_slice,
                           output_shape=input_shape,
                           arguments={'i': i,
                                      'parts': num_gpus})(x)
          inputs.append(slice_i)

        # Apply model on slice
        # (creating a model replica on the target device).
        outputs = model(inputs)
        if not isinstance(outputs, list):
          outputs = [outputs]

        # Save the outputs for merging back together later.
        for o in range(len(outputs)):
          all_outputs[o].append(outputs[o])

  # Merge outputs on CPU.
  with tf.device('/cpu:0'):
    merged = []
    for name, outputs in zip(model.output_names, all_outputs):
      merged.append(concatenate(outputs,
                                axis=0, name=name))
    return Model(model.inputs, merged)

您可以使用这个multi_gpu_model 函数,直到在 keras 中修复该错误。另外,在加载模型时,提供 tensorflow 模块对象很重要:

model = load_model('multi_gpu_model.h5', {'tf': tf})

工作原理

问题在于import tensorflow 位于multi_gpu_model 中间的行:

def multi_gpu_model(model, gpus):
  ...
  import tensorflow as tf
  ...

这会为get_slice lambda 函数创建一个闭包,其中包括 gpus(可以)和 tensorflow 模块(不可以)的数量。模型保存尝试序列化所有层,包括调用 get_slice 的层,但由于 tf 在闭包中而失败。

解决方案是将导入移出multi_gpu_model,以便tf 成为一个全局对象,尽管get_slice 仍然需要它才能工作。这解决了保存问题,但在加载时必须明确提供tf

【讨论】:

  • 感谢@Maxim 的补丁。我想知道如果我给 multi_gpu_model(model, 1) 这是否可行。
【解决方案3】:

需要通过将 multi_gpu_model 权重加载到常规模型权重来解决这个问题。 例如

#1, instantiate your base model on a cpu
with tf.device("/cpu:0"):
    model = create_model()

#2, put your model to multiple gpus, say 2
multi_model = multi_gpu_model(model, 2)

#3, compile both models
model.compile(loss=your_loss, optimizer=your_optimizer(lr))
multi_model.compile(loss=your_loss, optimizer=your_optimizer(lr))

#4, train the multi gpu model
# multi_model.fit() or multi_model.fit_generator()

#5, save weights
model.set_weights(multi_model.get_weights())
model.save(filepath=filepath)

`

参考:https://github.com/fchollet/keras/issues/8123

【讨论】:

  • 很抱歉听到这个消息..你能分享你得到的新错误或你正在使用的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
相关资源
最近更新 更多