天天看点

基于keras的多GPU深度学习网络模型及参数保存-笔记

问题一 :直接利用keras的Callbacks(回调函数)无法保存多GPU的训练模型?

原因有以下几点:

1. 即使利用多GPU模型进行训练,但实际保存仍然必须是原模型;

eg:mgpu_model = multi_gpu_model(autoencoder_model, gpus=num_gpu)

       autoencoder_model.save('model.h5')  # 直接保存原始模型及参数

2. 多GPU模型训练时,系统需要指定CPU进行参数合成运算,此时无法实现模型及参数保存;

问题二 : “TypeError: ('Not JSON Serializable:', tf.float32)”异常报错?

1. 原因:模型保存时,只能识别以序列形式的Sequential()方式add()的网络模型。

2. 解决方法:

1)报错系统网络添加方法

# build the model

input_img = Input(shape=[depth, width, height, 1],dtype=tf.float32)

x = Conv3D(30, (5, 5, 5), activation='relu', padding='same')(input_img)

encoded = MaxPooling3D((2, 2, 2), padding='same')(x)

x = UpSampling3D((2, 2, 2))(encoded)

decoded = Conv3D(1, (5, 5, 5), activation='relu', padding='same')(x) # decoded

autoencoder_model = Model(inputs=input_img, outputs=decoded)

autoencoder_model.summary()

2)修改后网络模型

autoencoder_model = Sequential()

autoencoder_model.add(Conv3D(30, input_img, (5, 5, 5), activation='relu', padding='same')) # encoded

autoencoder_model.add(MaxPooling3D((2, 2, 2), padding='same'))

autoencoder_model.add(UpSampling3D((2, 2, 2)))

autoencoder_model.add(Conv3D(1, (5, 5, 5), activation='relu', padding='same')) #decoded

autoencoder_model.summary()

mgpu_model = multi_gpu_model(autoencoder_model, gpus=num_gpu)

mgpu_model.compile(loss='binary_crossentropy', optimizer='adadelta')

mgpu_model.fit() # fit

autoencoder_model.save('model_test.h5') # save model

注意:import h5dy

推荐博客学习:https://blog.csdn.net/cymy001/article/details/78647640

本博客仅作为个人学习笔记记录。

继续阅读