Я пытаюсь обучить модель на основе замороженной модели Inception_v3 с 3 классами в качестве выходных данных.Когда я запускаю тренировку, точность тренировки повышается, но не точность валидации, которая более или менее точно составляет 33,33%, т.е. показывает совершенно случайный прогноз.Я не могу понять, где ошибка в моем коде и / или подходе
Я пробовал различные формы вывода после ядра Inception v3 без различий.
# Model definition
# InceptionV3 frozen, flatten, dense 1024, dropout 50%, dense 1024, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, flatten, dense 1024, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, flatten, dense 1024, dense 3, lr 0.005 --> does not train
# InceptionV3 frozen, GlobalAvgPooling, dense 1024, dense 1024, dense 512, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process, batch=32 --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process, batch=32, rebalance train/val sets --> does not train
IMAGE_SIZE = 150
BATCH_SIZE = 32
def build_model(image_size):
input_tensor = tf.keras.layers.Input(shape=(image_size, image_size, 3))
inception_base = InceptionV3(include_top=False, weights='imagenet', input_tensor=input_tensor)
for layer in inception_base.layers:
layer.trainable = False
x = inception_base.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dropout(0.2)(x)
output_tensor = tf.keras.layers.Dense(3, activation="softmax")(x)
model = tf.keras.Model(inputs=input_tensor, outputs=output_tensor)
return model
model = build_model(IMAGE_SIZE)
model.compile(optimizer=RMSprop(lr=0.002), loss='categorical_crossentropy', metrics=['acc'])
# Data generators with Image augmentations
train_datagen = ImageDataGenerator(
rescale=1./255,
preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
# Do not augment validation!
validation_datagen = ImageDataGenerator(
rescale=1./255,
preprocessing_function=tf.keras.applications.inception_v3.preprocess_input)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='categorical')
validation_generator = validation_datagen.flow_from_directory(
valid_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
class_mode='categorical')
Выводэта ячейка:
Найдено 1697 изображений, относящихся к 3 классам.Найдено 712 изображений, относящихся к 3 классам.
Вывод двух последних эпох обучения:
Эпоха 19/20
23/23 [==============================] - 6 с 257 мс / шаг - потеря: 1,1930 - в соотв. 0,3174
54/54 [==============================] - 20 с 363 мс / шаг - потеря: 0,7870 - в соответствии с: 0,6912 - val_loss: 1,1930 - val_acc: 0,3174
Epoch 20/20
23/23 [====================================] - 6 с 255 мс / шаг - потеря: 1,1985 - согласно: 0,3160
54/54 [====================================] - 20 с 362 мс / шаг - потеря: 0,7819 - в соответствии с:0,7018 - val_loss: 1,1985 - val_acc: 0,3160