問題一 :直接利用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
本博客僅作爲個人學習筆記記錄。