Я пытаюсь провести мультиклассовую классификацию для 26 классов. Кажется, моя модель не проверяет данные, хотя я написал генератор данных проверки помимо генератора обучающих данных. Вот код:
datagen = ImageDataGenerator(
rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
horizontal_flip=True,
fill_mode='nearest',
)
batch_size = 8
train_generator = datagen.flow(
train_images,
train_labels,
batch_size=batch_size,
shuffle=True,
subset='training',
seed=42)
valid_generator = datagen.flow(
val_images,
val_labels,
batch_size=batch_size,
shuffle=True,
subset='validation',
seed=42)
Вот модель:
img_rows = 256
img_cols = 256
def get_net():
inputs = Input((img_rows, img_cols, 1))
print("inputs shape:",inputs.shape)
#Convolution layers
conv1 = Conv2D(24, 3, strides=(2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
print("conv1 shape:",conv1.shape)
conv2 = Conv2D(24, 3, strides=(2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
print("conv2 shape:",conv2.shape)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv2)
print("pool1 shape:",pool1.shape)
drop1 = Dropout(0.25)(pool1)
conv3 = Conv2D(36, 3, strides=(2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop1)
print("conv3 shape:",conv3.shape)
conv4 = Conv2D(36, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
print("conv4 shape:",conv4.shape)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv4)
print("pool2 shape:",pool2.shape)
drop2 = Dropout(0.25)(pool2)
conv5 = Conv2D(48, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop2)
print("conv5 shape:",conv5.shape)
conv6 = Conv2D(48, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
print("conv6 shape:",conv6.shape)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv6)
print("pool3 shape:",pool3.shape)
drop3 = Dropout(0.25)(pool3)
#Flattening
flat = Flatten()(drop3)
#Fully connected layers
dense1 = Dense(128, activation = 'relu', use_bias=True, kernel_initializer = 'he_normal')(flat)
print("dense1 shape:",dense1.shape)
drop4 = Dropout(0.5)(dense1)
dense2 = Dense(128, activation = 'relu', use_bias=True, kernel_initializer = 'he_normal')(drop4)
print("dense2 shape:",dense2.shape)
drop5 = Dropout(0.5)(dense2)
dense4 = Dense(26, activation = 'softmax', use_bias=True, kernel_initializer = 'he_normal')(drop5)
print("dense4 shape:",dense4.shape)
#drop7 = Dropout(0.25)(dense4)
model = Model(input = inputs, output = dense4)
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=0.00000001, decay=0.0)
model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'])
return model
Вот код для обучения:
def train():
model = get_net()
print("got model")
model.summary()
model_checkpoint = ModelCheckpoint('seqnet.hdf5', monitor='loss',verbose=1, save_best_only=True)
print('Fitting model...')
#model.fit_generator(train_generator, validation_data=validation_generator, steps_per_epoch=len(train_generator), epochs=2)
history = model.fit_generator(
train_generator,
steps_per_epoch = len(train_generator) // batch_size,
validation_data = valid_generator,
validation_steps = len(valid_generator) // batch_size,
epochs = 50)
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
return model
model = train()
Журнал тренировок с несколькими последними эпохами - примечание он печатает информацию только о точности обучения и потере обучения, никакой информации о проверке.
Epoch 48/50
18/18 [==============================] - 12s 639ms/step - loss: 1.8327 - acc: 0.3125
Epoch 49/50
18/18 [==============================] - 11s 604ms/step - loss: 1.7274 - acc: 0.3840
Epoch 50/50
18/18 [==============================] - 11s 609ms/step - loss: 1.5989 - acc: 0.3542
dict_keys(['acc', 'loss'])
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-72-387ae82a9daf> in <module>()
39 return model
40
---> 41 model = train()
<ipython-input-72-387ae82a9daf> in train()
21 # summarize history for accuracy
22 plt.plot(history.history['acc'])
---> 23 plt.plot(history.history['val_acc'])
24 plt.title('model accuracy')
25 plt.ylabel('accuracy')
KeyError: 'val_acc'
Вот неполная кривая (согласно моему коду должны были быть созданы два графика - один - точность обучения против точности проверки, другой потеря обучения или потеря проверки):
введите описание изображения здесь