參考:keras預訓練模型應用(4):fine-turn InceptionV3 https://www.jianshu.com/p/23295376c44d
keras調用自己訓練的模型,並去掉全連接層 https://blog.csdn.net/qq_29462849/article/details/83010854
from keras.models import load_model from keras.utils import plot_model from keras.models import Model from keras.layers import Activation, Conv2D from keras.layers import GlobalAveragePooling2D
pre_model = load_model(load_model_path) # mini_XCEPTION pre_model.summary() plot_model(pre_model, to_file='mini_XCEPTION_7.png') # 畫出模型結構圖,並保存成圖片 base_model = Model(inputs=pre_model.input, outputs=pre_model.get_layer('add_4').output) base_model.summary() # 【1】增加一個卷積層 x = base_model.output x = Conv2D(num_classes, (3, 3), # kernel_regularizer=regularization, padding='same', name='conv2d_7')(x) # 【2】增加一個空域全局平均池化層 x = GlobalAveragePooling2D()(x) # 【3】增加一個激活層 output = Activation('softmax', name='predictions')(x) i = 0 # 【3】合併層,構建一個待 fine-turn 的新模型 model = Model(inputs=base_model.input, outputs=output) plot_model(model, to_file='mini_XCEPTION_3.png') # 畫出模型結構圖,並保存成圖片