竞赛介绍:Kaggle Dogs vs. Cats (https://www.kaggle.com/c/dogs-vs-cats)
要点:
1. 用kaggle API下载数据后,train文件夹下的猫狗图片须分别归入2个文件夹,即cat和dog,否则flow_from_directory会报错
2. 由于该竞赛项目已经结束,本示例没有对test文件夹下的图片进行分类,而是用train文件夹下的图片进行训练和验证
3. train文件夹下共有25000张图片,其中猫狗各有12500张
代码部分:
# 加载libraries
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.figure as fig
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# 设置文件路径
dir = os.getcwd()
train_dir = os.path.join(dir, 'train')
# 显示train文件夹下的猫狗图片
fig = plt.gcf()
fig.set_size_inches(10,10)
for i in range(9):
plt.subplot(330 + 1 + i)
file_name = train_dir + '\\dog\\dog.' + str(i) + '.jpg'
im = plt.imread(file_name)
plt.imshow(im)
fig = plt.gcf()
fig.set_size_inches(10,10)
for i in range(9):
plt.subplot(330 + 1 + i)
file_name = train_dir + '\\cat\\cat.' + str(i) + '.jpg'
im = plt.imread(file_name)
plt.imshow(im)
# 定义earlystopping,若验证数据集的精度在2个epoch后不再改进,则停止model fit
monitor_val_acc = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2)
# 定义model
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu', input_shape = (150,150,3)),
tf.keras.layers.MaxPooling2D(pool_size = (2,2)),
tf.keras.layers.Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(pool_size = (2,2)),
tf.keras.layers.Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(pool_size = (2,2)),
tf.keras.layers.Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(pool_size = (2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units = 512, activation = 'relu'),
tf.keras.layers.Dense(units = 1, activation = 'sigmoid')
])
# 编译model
model.compile(loss = 'binary_crossentropy',optimizer = 'adam', metrics = ['accuracy'])
# 定义ImageDataGenerator,同时考虑图像增强;如需将train数据集划分为训练和验证两个子集,需在此设置validation_split
train_datagen = ImageDataGenerator(rescale = 1./255,
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',
validation_split=0.2
)
# 定义train_generator和validate_generator,classes根据label进行设置,class_mode根据应用场景设置(二分类为binary),subset根据用途分别设置为training和validation
train_generator = train_datagen.flow_from_directory(directory = train_dir,
target_size = (150,150),
classes = ['cat','dog'],
batch_size = 20,
class_mode = 'binary',
subset = 'training')
validate_generator = train_datagen.flow_from_directory(directory = train_dir,
target_size = (150,150),
classes = ['cat','dog'],
batch_size = 20,
class_mode = 'binary',
subset = 'validation')
Found 20000 images belonging to 2 classes.
Found 5000 images belonging to 2 classes.
# 训练model
history = model.fit_generator(generator = train_generator,
steps_per_epoch = 1000,
epochs = 20,
validation_data = validate_generator,
validation_steps = 250,
callbacks = [monitor_val_acc],
verbose = 2)
Epoch 1/20
1000/1000 - 795s - loss: 0.5794 - accuracy: 0.6880 - val_loss: 0.4907 - val_accuracy: 0.7618
Epoch 2/20
1000/1000 - 786s - loss: 0.4575 - accuracy: 0.7836 - val_loss: 0.3896 - val_accuracy: 0.8212
Epoch 3/20
1000/1000 - 804s - loss: 0.3608 - accuracy: 0.8391 - val_loss: 0.3579 - val_accuracy: 0.8384
Epoch 4/20
1000/1000 - 772s - loss: 0.2954 - accuracy: 0.8714 - val_loss: 0.3543 - val_accuracy: 0.8448
Epoch 5/20
1000/1000 - 765s - loss: 0.2313 - accuracy: 0.9015 - val_loss: 0.3222 - val_accuracy: 0.8662
Epoch 6/20
1000/1000 - 780s - loss: 0.1746 - accuracy: 0.9313 - val_loss: 0.3112 - val_accuracy: 0.8724
Epoch 7/20
1000/1000 - 797s - loss: 0.1204 - accuracy: 0.9523 - val_loss: 0.3935 - val_accuracy: 0.8784
Epoch 8/20
1000/1000 - 789s - loss: 0.0882 - accuracy: 0.9669 - val_loss: 0.4920 - val_accuracy: 0.8692
Epoch 9/20
1000/1000 - 800s - loss: 0.0594 - accuracy: 0.9785 - val_loss: 0.4468 - val_accuracy: 0.8770
训练数据集精度为0.9785,验证数据集精度为0.8770
# 绘制learning curves图
loss = history.history['loss']
val_loss = history.history['val_loss']
accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']
epoch = range(len(loss))
plt.style.use('ggplot')
plt.plot(epoch, loss, color = 'blue', label = 'training loss')
plt.plot(epoch, val_loss, color = 'red', label = 'validation loss')
plt.title('model loss', size = 20)
plt.legend()
plt.figure()
plt.plot(epoch, accuracy, color = 'blue', label = 'training accuracy')
plt.plot(epoch, val_accuracy, color = 'red', label = 'validation accuracy')
plt.title('model accuracy', size = 20)
plt.legend()
网友评论