有没有一种方法,我们可以加载网络的架构,然后在Keras中从头开始训练它?
是,假设你想从头开始使用"ResNet50v2"为2个类和255x255x3输入训练分类器,你所要做的就是导入没有最后一个softmax层的架构,添加你的自定义层,并用"无"初始化权重。
from keras.applications.resnet_v2 import ResNet50V2
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
input_shape = (255,255,3)
n_class = 2
base_model = ResNet50V2(weights=None,input_shape=input_shape,include_top=False)
# Add Custom layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
# ADD a fully-connected layer
x = Dense(1024, activation='relu')(x)
# Softmax Layer
predictions = Dense(n_class, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
# Compile Model
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
# Train
model.fit(X_train,y_train,epochs=20,batch_size=50,validation_data=(X_val,y_val))
同样,要使用其他架构,如EfficienNet,请参阅Keras文档。对于EfficientNet,你也可以点击这个链接