model.save() model. save_weights ()
model.save_weights('./saved_models/8.h5')
===========================================================================
model.save()保存了模型的图结构和模型的参数,保存模型的后缀是.hdf5。
model. save_weights ()只保存了模型的参数,并没有保存模型的图结构,保存模型的后缀使用.h5。
所以使用save_weights保存的模型比使用save() 保存的模型的大小要小。同时加载模型时的方法也不同。model.save()保存了模型的图结构,直接使用load_model()方法就可加载模型然后做测试,例:
from tensorflow.keras.models import load_model
model=load_model("my_model_.hdf5")
加载save_weights保存的模型就稍微复杂了一些,还需要再次描述模型结构信息才能加载模型。例:
def bn_prelu(x):
x = BatchNormalization(epsilon=1e-5)(x)
x = PReLU()(x)
return x
def build_model(out_dims, input_shape=(norm_size, norm_size, 3)):
inputs_dim = Input(input_shape)
x = Conv2D(32, (3, 3), strides=(2, 2), padding='same')(inputs_dim)
x = bn_prelu(x)
x = Conv2D(32, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
x = bn_prelu(x)
x = GlobalAveragePooling2D()(x)
dp_1 = Dropout(0.5)(x)
fc2 = Dense(out_dims)(dp_1)
fc2 = Activation('softmax')(fc2) #此处注意,为sigmoid函数
model = Model(inputs=inputs_dim, outputs=fc2)
return model
model=build_model(labelnum)
model. load_weights(“my_model_.h5”);
————————————————
原文链接:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/109557652