我正在尝试使用 VGG16 通过迁移学习来训练模型,但即使随着学习时期的进展,准确性也没有增加。会不会是输入有问题?
def build_model():
input_tensor = layers.Input(shape=(32, 32, 3))
resize_layer = layers.Lambda(lambda image: tf.image.resize(image, (224, 224)))(input_tensor)
base_model = VGG16(weights='imagenet', include_top=False, input_tensor=resize_layer)
base_model.trainable = False
x = layers.Flatten()(base_model.output)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(256)(x)
x = layers.Dropout(0.5)(x)
output_tensor = layers.Dense(10, activation='softmax')(x)
model = tf.keras.models.Model(inputs=input_tensor, outputs=output_tensor)
return model
def cutout(img):
mask_size = 16
img_height, img_width, _ = img.shape
x = np.random.randint(0, img_height)
y = np.random.randint(0, img_width)
x1 = np.clip(x - mask_size // 2, 0, img_width)
x2 = np.clip(x + mask_size // 2, 0, img_width)
y1 = np.clip(y - mask_size // 2, 0, img_height)
y2 = np.clip(y + mask_size // 2, 0, img_height)
img[y1:y2, x1:x2, :] = 0
return img
def preprocessing(img):
if np.random.rand() <= 0.5:
img = cutout(img)
return preprocess_input(img)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = utils.to_categorical(y_train, 10)
y_test = utils.to_categorical(y_test, 10)
x_train, x_val = x_train[:40000], x_train[40000:]
y_train, y_val = y_train[:40000], y_train[40000:]
datagen = ImageDataGenerator(
rotation_range=15, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True,
preprocessing_function = preprocessing)
train_gen = datagen.flow(x_train, y_train, batch_size=200)
model = build_model()
model.compile(optimizer=SGD(momentum = 0.9), loss = 'categorical_crossentropy', metrics = ['accuracy'])
history1 = model.fit(train_gen, validation_data=(x_val, y_val), epochs=5)
model.trainable = True
history2 = model.fit(train_gen, validation_data=(x_val, y_val), epochs=10)`
输出:
Epoch 1/5
200/200 [==============================] - 67s 284ms/step - loss: 162.0445 - accuracy: 0.0984 - val_loss: 63.9608 - val_accuracy: 0.1016
Epoch 2/5
200/200 [==============================] - 55s 273ms/step - loss: 59.8545 - accuracy: 0.0995 - val_loss: 30.2987 - val_accuracy: 0.0980
Epoch 3/5
200/200 [==============================] - 54s 272ms/step - loss: 59.9274 - accuracy: 0.0998 - val_loss: 79.5693 - val_accuracy: 0.0997
Epoch 4/5
200/200 [==============================] - 54s 272ms/step - loss: 58.7012 - accuracy: 0.0974 - val_loss: 25.4963 - val_accuracy: 0.0952
Epoch 5/5
200/200 [==============================] - 54s 272ms/step - loss: 55.8720 - accuracy: 0.1001 - val_loss: 57.3340 - val_accuracy: 0.0977
该模型中使用的SGD优化器的初始学习率为0.01,似乎有点大了。通过稍微减小这个值,训练过程变得更加顺利。
model.compile(optimizer=SGD(leraning_rate = 0.001, momentum = 0.9), loss='categorical_crossentropy', metrics = ['accuracy'])
它的工作