競(jìng)賽介紹:Kaggle Dogs vs. Cats (https://www.kaggle.com/c/dogs-vs-cats)
要點(diǎn):
1. 用kaggle API下載數(shù)據(jù)后汗茄,train文件夾下的貓狗圖片須分別歸入2個(gè)文件夾,即cat和dog侥蒙,否則flow_from_directory會(huì)報(bào)錯(cuò)
2. 由于該競(jìng)賽項(xiàng)目已經(jīng)結(jié)束甘穿,本示例沒(méi)有對(duì)test文件夾下的圖片進(jìn)行分類渤涌,而是用train文件夾下的圖片進(jìn)行訓(xùn)練和驗(yàn)證
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
# 設(shè)置文件路徑
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监憎,若驗(yàn)證數(shù)據(jù)集的精度在2個(gè)epoch后不再改進(jìn)庆揩,則停止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膳叨,同時(shí)考慮圖像增強(qiáng)颓影;如需將train數(shù)據(jù)集劃分為訓(xùn)練和驗(yàn)證兩個(gè)子集,需在此設(shè)置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根據(jù)label進(jìn)行設(shè)置诡挂,class_mode根據(jù)應(yīng)用場(chǎng)景設(shè)置(二分類為binary),subset根據(jù)用途分別設(shè)置為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.
# 訓(xùn)練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
訓(xùn)練數(shù)據(jù)集精度為0.9785临谱,驗(yàn)證數(shù)據(jù)集精度為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()