Neural network 当使用多个GPU进行训练时,加载预训练模型失败

Neural network 当使用多个GPU进行训练时,加载预训练模型失败,neural-network,deep-learning,keras,backend,Neural Network,Deep Learning,Keras,Backend,我已经训练了一个网络模型,并通过checkpoint=ModelCheckpoint(filepath='weights.hdf5')回调保存了它的权重和体系结构。在培训期间,我通过调用下面的函数来使用多个GPU: def make_parallel(model, gpu_count): def get_slice(data, idx, parts): shape = tf.shape(data) size = tf.concat([ shape[:1]

我已经训练了一个网络模型,并通过
checkpoint=ModelCheckpoint(filepath='weights.hdf5')
回调保存了它的权重和体系结构。在培训期间,我通过调用下面的函数来使用多个GPU:

def make_parallel(model, gpu_count):
    def get_slice(data, idx, parts):
        shape = tf.shape(data)
        size = tf.concat([ shape[:1] // parts, shape[1:] ],axis=0)
        stride = tf.concat([ shape[:1] // parts, shape[1:]*0 ],axis=0)
        start = stride * idx
        return tf.slice(data, start, size)

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

    #Place a copy of the model on each GPU, each getting a slice of the batch
    for i in range(gpu_count):
        with tf.device('/gpu:%d' % i):
            with tf.name_scope('tower_%d' % i) as scope:

                inputs = []
                #Slice each input into a piece for processing on this GPU
                for x in model.inputs:
                    input_shape = tuple(x.get_shape().as_list())[1:]
                    slice_n = Lambda(get_slice, output_shape=input_shape, arguments={'idx':i,'parts':gpu_count})(x)
                    inputs.append(slice_n)                

                outputs = model(inputs)

                if not isinstance(outputs, list):
                    outputs = [outputs]

                #Save all the outputs for merging back together later
                for l in range(len(outputs)):
                    outputs_all[l].append(outputs[l])

    # merge outputs on CPU
    with tf.device('/cpu:0'):
        merged = []
        for outputs in outputs_all:
            merged.append(merge(outputs, mode='concat', concat_axis=0))

        return Model(input=model.inputs, output=merged)
使用测试代码:

from keras.models import Model, load_model
import numpy as np
import tensorflow as tf

model = load_model('cpm_log/deneme.hdf5')

x_test = np.random.randint(0, 255, (1, 368, 368, 3))

output = model.predict(x = x_test, batch_size=1)

print output[4].shape
我得到的错误如下:

Traceback (most recent call last):
  File "cpm_test.py", line 5, in <module>
    model = load_model('cpm_log/Jun5_1000/deneme.hdf5')
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 240, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 301, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/__init__.py", line 46, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
    list(custom_objects.items())))
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2378, in from_config
    process_layer(layer_data)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2373, in process_layer
    layer(input_tensors[0], **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 578, in __call__
    output = self.call(inputs, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 659, in call
    return self.function(inputs, **arguments)
  File "/home/muhammed/DEV_LIBS/developments/mocap/pose_estimation/training/cpm/multi_gpu.py", line 12, in get_slice
    def get_slice(data, idx, parts):
NameError: global name 'tf' is not defined
回溯(最近一次呼叫最后一次):
文件“cpm_test.py”,第5行,在
模型=负载模型('cpm\u log/Jun5\u 1000/deneme.hdf5')
文件“/usr/local/lib/python2.7/dist-packages/keras/models.py”,第240行,在load\u模型中
模型=来自配置的模型(模型配置,自定义对象=自定义对象)
文件“/usr/local/lib/python2.7/dist-packages/keras/models.py”,第301行,模型配置中的
返回层\模块。反序列化(配置,自定义\对象=自定义\对象)
文件“/usr/local/lib/python2.7/dist-packages/keras/layers/_-init__.py”,第46行,反序列化
可打印\u模块\u name='layer')
文件“/usr/local/lib/python2.7/dist packages/keras/utils/generic_utils.py”,第140行,反序列化_keras_对象
列表(自定义对象.项())
文件“/usr/local/lib/python2.7/dist packages/keras/engine/topology.py”,第2378行,from_config
处理层(层数据)
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py”,第2373行,进程层中
图层(输入_张量[0],**kwargs)
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py”,第578行,在调用中__
输出=自调用(输入,**kwargs)
文件“/usr/local/lib/python2.7/dist packages/keras/layers/core.py”,第659行,在调用中
返回self.function(输入,**参数)
文件“/home/muhammed/DEV_LIBS/developments/mocap/pose_assessment/training/cpm/multi_gpu.py”,第12行,在get_切片中
def get_切片(数据、idx、零件):
NameError:未定义全局名称“tf”

通过检查错误输出,我确定问题在于并行化代码。然而,我无法解决这个问题

您可能需要使用
自定义对象
来启用模型加载

import tensorflow as tf
model = load_model('model.h5', custom_objects={'tf': tf,})

如果在
get_slice
定义的开头添加
import tensorflow as tf
,会发生什么情况?它已被添加。一切都没有改变。