train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')# Note that the validation data should not be augmented!
validation_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(# This is the target directory
train_dir,# All images will be resized to 150x150
target_size=(210,210),
batch_size=61,# Since we use binary_crossentropy loss, we need binary labels
class_mode='categorical')
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(210,210),
batch_size=61,
class_mode='categorical')
Found 549 images belonging to 61 classes.
Found 61 images belonging to 61 classes.
from keras.callbacks import ModelCheckpoint
from matplotlib import pyplot as plt
import numpy as np
checkpointer = ModelCheckpoint(filepath='TJUFACE.augmentation.model.weights.best.hdf5', verbose=1,
save_best_only=True)
before =0
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(210,210),
batch_size=61,
class_mode='categorical')
xx =[]
yy =[]for i inrange(200):
xx +=[i]print('The ',i+1,'times:')
history = model.fit_generator(
train_generator,
steps_per_epoch=9,
epochs=1,
validation_data=validation_generator,
validation_steps=1,
callbacks=[checkpointer],
verbose=1)# model.load_weights('TJUFACE.augmentation.model.weights.best.hdf5')
test_loss, test_acc = model.evaluate_generator(test_generator, steps=1)
yy +=[test_acc]if test_acc > before:print('-------------------------------------------------------------------------')print('epochs = ',i+1)print('Test_acc:', test_acc)print('-------------------------------------------------------------------------')
before = test_acc
print()print('The highest test_acc :',before)
Found 61 images belonging to 61 classes.
The 1 times:
Epoch 1/1
9/9 [==============================] - 14s 2s/step - loss: 4.1443 - acc: 0.0109 - val_loss: 4.0953 - val_acc: 0.0984
Epoch 00001: val_loss improved from inf to 4.09529, saving model to TJUFACE.augmentation.model.weights.best.hdf5
-------------------------------------------------------------------------
epochs = 1
Test_acc: 0.09836065769195557
-------------------------------------------------------------------------
The 2 times:
Epoch 1/1
9/9 [==============================] - 11s 1s/step - loss: 4.1036 - acc: 0.0328 - val_loss: 4.0612 - val_acc: 0.0656
Epoch 00001: val_loss improved from 4.09529 to 4.06118, saving model to TJUFACE.augmentation.model.weights.best.hdf5
The 3 times:
Epoch 1/1
9/9 [==============================] - 11s 1s/step - loss: 4.0701 - acc: 0.0510 - val_loss: 3.9763 - val_acc: 0.0656
……
……
……
Epoch 00001: val_loss did not improve from 0.33823
The 199 times:
Epoch 1/1
9/9 [==============================] - 11s 1s/step - loss: 0.4557 - acc: 0.8743 - val_loss: 0.8803 - val_acc: 0.8361
Epoch 00001: val_loss did not improve from 0.33823
The 200 times:
Epoch 1/1
9/9 [==============================] - 11s 1s/step - loss: 0.5764 - acc: 0.8452 - val_loss: 0.4710 - val_acc: 0.8852
Epoch 00001: val_loss did not improve from 0.33823
The highest test_acc : 0.9508196711540222
plt.figure(figsize=(10,5),dpi=200)
plt.title('Test_acc')
plt.xlabel('Epochs')
plt.xticks(np.arange(0,205,10))
plt.ylabel('Test_acc')
plt.yticks(np.arange(0,1,0.1))defsmooth_curve(points, factor=0.8):
smoothed_points =[]for point in points:if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor + point *(1- factor))else:
smoothed_points.append(point)return smoothed_points
plt.plot(xx,smooth_curve(yy),label='Final',color='m',marker=',',linestyle='-')# 16进制颜色码
plt.legend()
plt.show()