我使用 Keras 来微调现有的 VGG16 模型,并使用 fit_generator 来训练最后 4 层。这是我正在使用的相关代码:
# Create the model
model = models.Sequential()
# Add the vgg convolutional base model
model.add(vgg_conv)
# Add new layers
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(5, activation='softmax'))
# Show a summary of the model. Check the number of trainable params
model.summary()
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
validation_datagen = ImageDataGenerator(rescale=1./255)
#Change the batchsize according to the system RAM
train_batchsize = 100
val_batchsize = 10
train_dir='training_data/train'
validation_dir='training_data/validation' …
Run Code Online (Sandbox Code Playgroud)