1、准备工作
本文将迁移经典模型Resnet进行花的识别,花数据集下载地址https://frenzy86.s3.eu-west-2.amazonaws.com/IFAO/flowers.zip。Resnet使用介绍网址https://keras.io/api/applications/resnet/,可查看一下图像数据的输入大小和维度。
导入所要用的包,解压数据集
from tensorflow.keras.layers import Conv2D,Dense,MaxPool2D,BatchNormalization,GlobalAveragePooling2D
from tensorflow.keras.applications.resnet50 import preprocess_input,decode_predictions
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.optimizers import Adam,Adadelta
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Sequential, Model, load_model
import matplotlib.pyplot as plt
import numpy as np
##pip install splitfolders
import splitfolders
##使用splitfolders对鲜花数据集拆分成训练集和测试集
input_folder = 'flowers/'
output_folder = 'processed_data/'
train_data_dir = 'processed_data/train'
validation_data_dir = 'processed_data/val'
test_data_dir = 'processed_data/test'
splitfolders.ratio(input_folder,output_folder,seed=667,ratio=(.6,.2,.2))
2、使用ImageDataGenerator类进行图像数据集处理
img_height, img_width = (224,224) ##Resnet所需的输入数据大小
batch_size = 16
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.4
)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height,img_width),
batch_size = batch_size,
class_mode = 'categorical',
subset='training') # 训练集
valid_generator = train_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height,img_width),
batch_size = batch_size,
class_mode = 'categorical',
subset='validation') # 验证集
test_generator = train_datagen.flow_from_directory(
test_data_dir,
target_size = (img_height,img_width),
batch_size = 1,
class_mode = 'categorical',
subset='validation') # 测试集
3、调用Resnet进行模型训练
##训练标签数据
X,y = test_generator.next()
base_model = ResNet50(include_top=False,weights='imagenet')
X = base_model.output
X = GlobalAveragePooling2D()(X)
X = Dense(1024, activation='relu')(X)
###num_classes 多少类别 5个
predictions = Dense(train_generator.num_classes, activation='softmax')(X)
model = Model(inputs=base_model.input,outputs=predictions)
model.compile(optimizer=Adam(lr=0.0001),loss='categorical_crossentropy',metrics = ['accuracy'])
history = model.fit(train_generator,
epochs=10,
shuffle = True,
validation_data=valid_generator,
#validation_steps=2,
)
4、模型的保存验证测试
model.save('ResNet50_flowers.h5')
test_loss, test_acc = model.evaluate(test_generator, verbose=2)
print('\nTest accuracy ', test_acc)
import pandas as pd
import seaborn as sns
model = load_model('ResNet50_flowers.h5')
filenames = test_generator.filenames
nb_samples = len(test_generator)
y_prob =[]
y_act = []
test_generator.reset()
for _ in range (nb_samples):
X_test,y_test = test_generator.next()
y_prob.append(model.predict(X_test))
y_act.append(y_test)
predicted_class = [list(test_generator.class_indices.keys())[i.argmax()] for i in y_prob]
actual_class = [list(test_generator.class_indices.keys())[i.argmax()] for i in y_act]
test_df = pd.DataFrame(np.vstack([predicted_class,actual_class]).T,
columns=['predicted_class','actual_class'])
5 、测试新图片
测试图片下载地址https://frenzy86.s3.eu-west-2.amazonaws.com/IFAO/test_images.zip
import os
lista = os.listdir('test_images/') #['rose.jpg', 'sunflower.jpg', 'dandelion.jpg', 'tulip.jpg', 'daisy.jpg']
# {'daisy': 0, 'dandelion': 1, 'rose': 2, 'sunflower': 3, 'tulip': 4}
classes = {0:"daisy",
1:"dandelion",
2:"rose",
3:"sunflower",
4:"tulip",
}
finale=[]
name = []
res = []
for i in lista:
path = 'test_images/' + i
img = image.load_img(path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
pred = model.predict(images, batch_size=10)
print(pred)
result = np.argmax(pred, axis=-1)[0]
print(result)
name.append(i)
finale.append(result)
res.append(classes[result])
finale
print(name)
print(res)
完整代码如下
from tensorflow.keras.layers import Conv2D,Dense,MaxPool2D,BatchNormalization,GlobalAveragePooling2D
from tensorflow.keras.applications.resnet50 import preprocess_input,decode_predictions
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.optimizers import Adam,Adadelta
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Sequential, Model, load_model
import matplotlib.pyplot as plt
import numpy as np
##pip install splitfolders
import splitfolders
##使用splitfolders对鲜花数据集拆分成训练集和测试集
input_folder = 'flowers/'
output_folder = 'processed_data/'
train_data_dir = 'processed_data/train'
validation_data_dir = 'processed_data/val'
test_data_dir = 'processed_data/test'
splitfolders.ratio(input_folder,output_folder,seed=667,ratio=(.6,.2,.2))
img_height, img_width = (224,224) ##Resnet所需的输入数据大小
batch_size = 16
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.4
)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height,img_width),
batch_size = batch_size,
class_mode = 'categorical',
subset='training') # 训练集
valid_generator = train_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height,img_width),
batch_size = batch_size,
class_mode = 'categorical',
subset='validation') # 验证集
test_generator = train_datagen.flow_from_directory(
test_data_dir,
target_size = (img_height,img_width),
batch_size = 1,
class_mode = 'categorical',
subset='validation') # 测试集
X,y = test_generator.next()
base_model = ResNet50(include_top=False,weights='imagenet')
X = base_model.output
X = GlobalAveragePooling2D()(X)
X = Dense(1024, activation='relu')(X)
predictions = Dense(train_generator.num_classes, activation='softmax')(X)
model = Model(inputs=base_model.input,outputs=predictions)
model.compile(optimizer=Adam(lr=0.0001),loss='categorical_crossentropy',metrics = ['accuracy'])
history = model.fit(train_generator,
epochs=10,
shuffle = True,
validation_data=valid_generator,
#validation_steps=2,
)
model.save('ResNet50_flowers.h5')
test_loss, test_acc = model.evaluate(test_generator, verbose=2)
print('\nTest accuracy ', test_acc)
import pandas as pd
import seaborn as sns
model = load_model('ResNet50_flowers.h5')
filenames = test_generator.filenames
nb_samples = len(test_generator)
y_prob =[]
y_act = []
test_generator.reset()
for _ in range (nb_samples):
X_test,y_test = test_generator.next()
y_prob.append(model.predict(X_test))
y_act.append(y_test)
predicted_class = [list(test_generator.class_indices.keys())[i.argmax()] for i in y_prob]
actual_class = [list(test_generator.class_indices.keys())[i.argmax()] for i in y_act]
test_df = pd.DataFrame(np.vstack([predicted_class,actual_class]).T,
columns=['predicted_class','actual_class'])
import os
lista = os.listdir('test_images/') #['rose.jpg', 'sunflower.jpg', 'dandelion.jpg', 'tulip.jpg', 'daisy.jpg']
# {'daisy': 0, 'dandelion': 1, 'rose': 2, 'sunflower': 3, 'tulip': 4}
classes = {0:"daisy",
1:"dandelion",
2:"rose",
3:"sunflower",
4:"tulip",
}
finale=[]
name = []
res = []
for i in lista:
path = 'test_images/' + i
img = image.load_img(path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
pred = model.predict(images, batch_size=10)
print(pred)
result = np.argmax(pred, axis=-1)[0]
print(result)
name.append(i)
finale.append(result)
res.append(classes[result])
finale
print(name)
print(res)