基于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


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




















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值