(一)选题背景:
鸟类是野生动物的重要组成部分,是自然界的一项重要资源动物,也是生态系统中的重要组成部分。鸟类是可更新的自然资源,它在商业、旅游、美学、文化、科学和生态上都有重要价值。国内近年来鸟类系统发育与分类、分布的研究不断取得新的成果,这些对于研究我国的鸟类分类区系和系统发育、地理分布以及 生物多样性保护,都能提供很有价值的信息。机器人如何来识别不同的鸟类,由此设计了一套程序来识别不同的鸟类物种
(二)机器学习设计案例:
从网站中下载相关的数据集,对数据集进行整理,在python的环境中,给数据集中的文件打上标签,"train"和 "test"文件用于训练和测试,对数据进行预处理,利用keras和tensorflow,通过构建输入层,隐藏层,输出层建立训练模型,导入图片测试模型。
数据集来源:kaggle,网址:https://www.kaggle.com/
(三)机器学习的实现步骤
1.数据集下载
2.导入需要用到的库
import numpy as np
import pandas as pd
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array #从数据库里导入所需函数
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, GlobalAveragePooling2D, Dropout, Flatten
from tensorflow.keras.applications import VGG16
import matplotlib.pyplot as plt
import seaborn as sns
train_ds="./lty/bird-species/train"#设置训练集所在的路径
test_ds="./lty/bird-species/test"#设置测试集所在的路径
validation_ds="./lty/bird-species/valid"#设置验证集所在路径
3.遍历得到train的目录下的文件
path="C:/Users/z/lty/bird-species/train"
dirs = os.listdir( path )#返回train训练集文件夹列表
for file in dirs:
print (file)
IMAGE_DIM = (224,224)#设置图片尺寸
BATCH_SIZE = 32 #设置batch数据为32
CLASSES = 525
4.得到每个文件中图片的数量并进行图像预处理
train_datagen = ImageDataGenerator(rescale=1./255,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2
)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(directory="./lty/bird-species/train",
target_size=IMAGE_DIM, #图像尺寸224*224
batch_size=BATCH_SIZE, #batch数据,默认大小32
class_mode='categorical', #设置参数
shuffle=True )
validation_generator = val_datagen.flow_from_directory(directory="./lty/bird-species/valid",
target_size=IMAGE_DIM,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=False ) #不混洗数据
5.可视化训练数据的一批数据,并查看经过处理的图片以及它的标签
labels = [k for k in train_generator.class_indices]
sample_generate = train_generator.__next__()
images = sample_generate[0]
titles = sample_generate[1]
plt.figure(figsize = (15 , 15))
#创建一个子图矩阵
for i in range(20):
plt.subplot(5 , 5 , i+1)
plt.subplots_adjust(hspace = 0.3 , wspace = 0.3)
plt.imshow(images[i])
plt.suptitle("Sample Training Images", fontsize=20)
plt.title(f'Class: {labels[np.argmax(titles[i],axis=0)]}')
plt.axis("off")
8.构建神经网络并对模型进行训练
import numpy as np
from keras.layers import Dense,Flatten,Conv2D,Activation,Dropout
from keras import backend as K
import keras
from keras.models import Sequential, Model
from keras.models import load_model
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping,ModelCheckpoint
from keras.layers import MaxPool2D
from keras.layers import Dense
from keras.optimizers import Adam
#构建模型,
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
#将输入层的数据压成一维的数据
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=4096,activation="relu"),
tf.keras.layers.Dense(units=4096,activation="relu"),
tf.keras.layers.Dense(units=525, activation="softmax")
])
# 模型编译
model.compile(optimizer=opt,
loss=keras.losses.categorical_crossentropy,
metrics=['accuracy'])
9.总结模型
#输出模型各层的参数状况
model.summary()
from keras.optimizers import Adam
optimizer_Adam= Adam(lr=0.00001)#优化器
#利用批量生成器训练模型
history =model.fit(train_generator,
epochs=15,
validation_data=test_generator
)
#保存模型
9.绘制损失曲线和精度曲线图
#绘制损失曲线
plt.figure(figsize=(12,5))
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('Epochs')
plt.legend(['Train','Val'], loc= 'upper right')
plt.show()
#绘制精度曲线图
plt.figure(figsize=(12,5))
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend(['Train','Val'], loc = 'lower right')
plt.show()
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(directory="./lty/bird-species/test",
target_size=IMAGE_DIM,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=False )
10.测试模型
#对测试集的分类性能进行评估,及输出分类
y_test_1 = test_generator.classes #将测试集的样本标签存储到y_test_1
y_pred_1 =model.predict(test_generator) #预测结果存到y_pred_1
y_pred_1 = np.argmax(y_pred_1,axis=1)
results =model.evaluate(test_generator)
#鸟类分类报告
from sklearn.metrics import classification_report #引用库,用于输出分类报告
print(classification_report(y_test_1, y_pred_1))
11.鸟类分类预测类别
#循环遍历预测结果,
for i in range(len(images)):
predicted_class = np.argmax(predictions[i])
class_probability = predictions[i, predicted_class]
print(f'Predicted class for {i+1}.jpg : {labels[predicted_class]}') #输出预测结果
print('Class probability:', class_probability) #输出预测结果对应的概率
#输出单张图片的预测结果,包括预测类别和置信度
from PIL import Image
import os
import numpy as np
import matplotlib.pyplot as plt
image_directory = './lty/bird-species/valid/AMERICAN BITTERN'
IMAGE_DIM=( 224,224)
images = [ ]
for filename in os.listdir(image_directory):
path = os.path.join(image_directory, filename)
img = Image.open(path)
img = img.resize(IMAGE_DIM)
images.append(img)
images = np.array([np.array(img) for img in images])
images = images / 255.0
predictions = model.predict(images)
img_index = 0
class_label = np.argmax(predictions[img_index])#获取预测结果存于class_label
plt.imshow(images[img_index])
plt.axis('off')
plt.title('Predicted class: ' + str(class_label)) #预测类别
plt.show()
(四)收获:
本次的程序设计主要内容是机器学习,通过本次课程设计,让我对机器学习的理解更加深刻,同时,使我掌握了如何进行机器学习,导入数据并查看数据,构建数据集。学会了如何利用卷积层神经网络进行鸟类识别,使用Keras框架和Tensorflow库构建模型 (Adam优化器加快模型训练,)利用EarlyStopping和ModelCheckpoint回调函数来提高模型的性能,以及使用图像分类的机器学习程序。
(五)全代码
import numpy as np
import pandas as pd
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array #从数据库里导入所需函数
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, GlobalAveragePooling2D, Dropout, Flatten
from tensorflow.keras.applications import VGG16
import matplotlib.pyplot as plt
import seaborn as sns
train_ds="./lty/bird-species/train"#设置训练集所在的路径
test_ds="./lty/bird-species/test"#设置测试集所在的路径
validation_ds="./lty/bird-species/valid"#设置验证集所在路径
path="C:/Users/z/lty/bird-species/train"
dirs = os.listdir( path )#返回train训练集文件夹列表
for file in dirs:
print (file)
IMAGE_DIM = (224,224)#设置图片尺寸
BATCH_SIZE = 32 #设置batch数据为32
CLASSES = 525
train_datagen = ImageDataGenerator(rescale=1./255,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2
)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(directory="./lty/bird-species/train",
target_size=IMAGE_DIM, #图像尺寸224*224
batch_size=BATCH_SIZE, #batch数据,默认大小32
class_mode='categorical', #设置参数
shuffle=True )
validation_generator = val_datagen.flow_from_directory(directory="./lty/bird-species/valid",
target_size=IMAGE_DIM,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=False ) #不混洗数据
labels = [k for k in train_generator.class_indices]
sample_generate = train_generator.__next__()
images = sample_generate[0]
titles = sample_generate[1]
plt.figure(figsize = (15 , 15))
#创建一个子图矩阵
for i in range(20):
plt.subplot(5 , 5 , i+1)
plt.subplots_adjust(hspace = 0.3 , wspace = 0.3)
plt.imshow(images[i])
plt.suptitle("Sample Training Images", fontsize=20)
plt.title(f'Class: {labels[np.argmax(titles[i],axis=0)]}')
plt.axis("off")
import numpy as np
from keras.layers import Dense,Flatten,Conv2D,Activation,Dropout
from keras import backend as K
import keras
from keras.models import Sequential, Model
from keras.models import load_model
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping,ModelCheckpoint
from keras.layers import MaxPool2D
from keras.layers import Dense
from keras.optimizers import Adam
#构建模型,
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
#将输入层的数据压成一维的数据
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=4096,activation="relu"),
tf.keras.layers.Dense(units=4096,activation="relu"),
tf.keras.layers.Dense(units=525, activation="softmax")
])
# 模型编译
model.compile(optimizer=opt,
loss=keras.losses.categorical_crossentropy,
metrics=['accuracy'])
#输出模型各层的参数状况
model.summary()
from keras.optimizers import Adam
optimizer_Adam= Adam(lr=0.00001)#优化器
#利用批量生成器训练模型
history =model.fit(train_generator,
epochs=15,
validation_data=test_generator
)
#绘制损失曲线
plt.figure(figsize=(12,5))
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('Epochs')
plt.legend(['Train','Val'], loc= 'upper right')
plt.show()
#绘制精度曲线图
plt.figure(figsize=(12,5))
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend(['Train','Val'], loc = 'lower right')
plt.show()
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(directory="./lty/bird-species/test",
target_size=IMAGE_DIM,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=False )
#对测试集的分类性能进行评估,及输出分类
y_test_1 = test_generator.classes #将测试集的样本标签存储到y_test_1
y_pred_1 =model.predict(test_generator) #预测结果存到y_pred_1
y_pred_1 = np.argmax(y_pred_1,axis=1)
results =model.evaluate(test_generator)
from sklearn.metrics import classification_report #引用库,用于输出分类报告
print(classification_report(y_test_1, y_pred_1))
for i in range(len(images)):
predicted_class = np.argmax(predictions[i])
class_probability = predictions[i, predicted_class]
print(f'Predicted class for {i+1}.jpg : {labels[predicted_class]}') #输出预测结果
print('Class probability:', class_probability) #输出预测结果对应的概率
#输出单张图片的预测结果,包括预测类别和置信度
from PIL import Image
import os
import numpy as np
import matplotlib.pyplot as plt
image_directory = './lty/bird-species/valid/AMERICAN BITTERN'
IMAGE_DIM=( 224,224)
images = [ ]
for filename in os.listdir(image_directory):
path = os.path.join(image_directory, filename)
img = Image.open(path)
img = img.resize(IMAGE_DIM)
images.append(img)
images = np.array([np.array(img) for img in images])
images = images / 255.0
predictions = model.predict(images)
img_index = 0
class_label = np.argmax(predictions[img_index])#获取预测结果存于class_label
plt.imshow(images[img_index])
plt.axis('off')
plt.title('Predicted class: ' + str(class_label)) #预测类别
plt.show()
标签:layers,plt,鸟类,keras,Python,tf,import,识别,size From: https://www.cnblogs.com/ketolty/p/17461435.html