2016-12-27 73 views
0

關卡片斷產生:Keras:加載檢查點的權重由HDF5在多重GPU

checkpointer = ModelCheckpoint(filepath=os.path.join(savedir, "mid/weights.{epoch:02d}.hd5"), monitor='val_loss', verbose=1, save_best_only=False, save_weights_only=False) 
hist = model.fit_generator(
    gen.generate(batch_size = batch_size, nb_classes=nb_classes), samples_per_epoch=593920, nb_epoch=nb_epoch, verbose=1, callbacks=[checkpointer], validation_data = gen.vld_generate(VLD_PATH, batch_size = 64, nb_classes=nb_classes), nb_val_samples=10000 
) 

我訓練的轉儲在HDF5格式mid文件多GPU主機上我的模型。當我與keras.load_weights('mid')他們裝單GPU機器上,有人提出了一個錯誤:

Using TensorFlow backend. 
Traceback (most recent call last): 
    File "server.py", line 171, in <module> 
    model = load_model_and_weights('zhch.yml', '7_weights.52.hd5') 
    File "server.py", line 16, in load_model_and_weights 
    model.load_weights(os.path.join('model', weights_name)) 
    File "/home/lz/code/ProjectGo/meta/project/libpolicy-server/.virtualenv/lib/python3.5/site-packages/keras/engine/topology.py", line 2701, in load_weights 
    self.load_weights_from_hdf5_group(f) 
    File "/home/lz/code/ProjectGo/meta/project/libpolicy-server/.virtualenv/lib/python3.5/site-packages/keras/engine/topology.py", line 2753, in load_weights_from_hdf5_group 
    str(len(flattened_layers)) + ' layers.') 
ValueError: You are trying to load a weight file containing 1 layers into a model with 21 layers. 

有什麼辦法可以載入多GPU的單GPU計算機上生成檢查點的權重?似乎沒有問題的凱拉斯討論這個問題,因此任何幫助將不勝感激。

+0

你能否在同一個多GPU機器中加載?錯誤消息說有一些層不匹配。模型有多少層? –

+0

@YaoZhang Weights可以在源機器上加載'model.load_weights()'。 multi-GPU機器上的'model.yml'和model都有21層。 – lz96

回答

2

您可以在單個GPU這樣加載模型:

from keras.models import load_model 

multi_gpus_model = load_model('mid') 
origin_model = multi_gpus_model.layers[-2] # you can use multi_gpus_model.summary() to see the layer of the original model 
origin_model.save_weights('single_gpu_model.hdf5') 

「single_gpu_model.hdf5」是,你可以加載到單GPU機型的文件。