Keras: Load checkpoint weights HDF5 generated by multiple GPUs

前端 未结 2 415
长情又很酷
长情又很酷 2021-01-19 23:16

Checkpoint snippet:

checkpointer = ModelCheckpoint(filepath=os.path.join(savedir, \"mid/weights.{epoch:02d}.hd5\"), monitor=\'val_loss\', verbose=1, save_bes         


        
2条回答
  •  别那么骄傲
    2021-01-20 00:12

    Try this function:

    def keras_model_reassign_weights(model_cpu,model_gpu):
        weights_temp ={}
        print('_'*5,'Collecting weights from GPU model','_'*5)
        for layer in model_gpu.layers:
            try:
                for layer_unw in layer.layers:
                    #print('Weights extracted for: ',layer_unw.name)
                    weights_temp[layer_unw.name] = layer_unw.get_weights()
                break
            except:
                print('Skipped: ',layer.name)
        print('_'*5,'Writing weights to CPU model','_'*5)
        for layer in model_cpu.layers:
            try:
                layer.set_weights(weights_temp[layer.name])
                #print(layer.name,'Done!')
            except:
                print(layer.name,'weights does not set for this layer!')
        return model_cpu
    

    But you need to load weights to your gpu model first:

    #load or initialize your keras multi-gpu model
    model_gpu = None 
    #load or initialize your keras model with the same structure, without using keras.multi_gpu function
    model_cpu = None 
    #load weights into multigpu model
    model_gpu.load_weights(r'gpu_model_best_checkpoint.hdf5') 
    #execute function
    model_cpu = keras_model_reassign_weights(model_cpu,model_gpu)
    #save obtained weights for cpu model
    model_cpu.save_weights(r'CPU_model.hdf5')
    

    After transferring you can use weights with a single GPU or CPU model.

提交回复
热议问题