在多GPU环境中保存和加载keras模型

时间:2019-07-12 05:21:55

标签: tensorflow keras

使用的API: 汇率== 2.2.4 Tensorflow = 1.13.1

问题陈述: 加载保存的keras模型进行再训练后,它会更改输入和输出张量名称。

要复制的代码:

from keras.applications import MobileNet
from keras.layers import Input
from keras.utils import multi_gpu_model
from keras.models import model_from_json, load_model

input_tensor = Input(shape=(224, 224, 3), name="input")
pre_trained_model = MobileNet(input_shape=(224,224,3),
                                       alpha=1,
                                       depth_multiplier=1,
                                       dropout=1e-3,
                                       include_top=True,
                                       weights='imagenet',
                                       input_tensor=self.input_tensor,
                                       classes=1000)
last_layer = pre_trained_model.layers[-1].output
output_tensor = Dense(10, activation='softmax', name='output')(last_layer)
model = Model(inputs=input_tensor, outputs=output_tensor)
model = multi_gpu_model(model, gpus=4)
model.compile(
            optimizer='adam',
            loss='binary_crossentropy',
            metrics='accuracy',
        )

---- Training ----

architecture_file="model_arch.json"
weight_file="model_weights.h5"
model.save_weights(weight_file)
with open(architecture_file, 'w') as f:
     f.write(model.to_json())

After the first training, when I froze the model and loaded into the testing environment, I could feed to input node and get output from output

Then reload and train again

 with open(architecture_file, 'r') as f:
      model = model_from_json(f.read())
      model.load_weights(weight_file, by_name=True)

model.compile(
            optimizer='adam',
            loss='binary_crossentropy',
            metrics='accuracy',
        )

---- training ----
architecture_file="model_arch.json"
weight_file="model_weights.h5"
model.save_weights(weight_file)
with open(architecture_file, 'w') as f:
     f.write(model.to_json())
After the second training from the saved model, when I froze the model and loaded into the testing environment,
I could feed to input to input node the input node has changed to input_1

输出张量名称发生了相同的事情,所有输出变为output_1。

有人可以帮助我解决此问题吗?

0 个答案:

没有答案