如何在 Keras 中从头开始训练模型(如 EfficientNet、Resnet)?

How can I train a model ( like EfficientNet, Resnet ) from scratch in Keras?

有没有办法加载网络架构,然后在 Keras 中从头开始训练它?

,假设您想使用 "ResNet50v2" 从头开始​​为 2 类 和 255x255x3 输入训练分类器,您所要做的就是导入没有最后一个 softmax 层的架构,添加自定义层并使用 "None".

初始化权重
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 Documention。 具体EfficientNet,也可以按照这个link