TensorFlow人马图像分类(卷积神经网络)
Posted _睿智_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TensorFlow人马图像分类(卷积神经网络)相关的知识,希望对你有一定的参考价值。
目录
基础理论
1、sigmoid激活函数
由于是二分类,所以最后会用到sigmoid激活函数(0、1两值)。
2、聚类&&分类
聚类(Clustering):是指把相似的数据划分到一起,具体划分的时候并不关心这一类的标签,目标就是把相似的数据聚合到一起,聚类是一种无监督学习(Unsupervised Learning)方法。
(聚类:不按标签把相似数据聚合到一起)
分类(Classification):是把不同的数据划分开,其过程是通过训练数据集获得一个分类器,再通过分类器去预测未知数据,分类是一种监督学习(Supervised Learning)方法。
一、准备数据
数据下载地址:
https://storage.googleapis.com/laurencemoroney-blog.appspot.com/horse-or-human.zip \\
-O /tmp/horse-or-human.zip
# 1、准备数据
from tensorflow.keras.preprocessing.image import ImageDataGenerator
1、创建两个数据生成器
# 1-1、创建两个数据生成器
train_data_gen = ImageDataGenerator(rescale=1/255)
test_data_gen = ImageDataGenerator(rescale=1/255)
2、创建训练数据与测试数据生成器
训练数据生成器
# 训练数据生成器(指向训练数据文件夹)
train_generator = train_data_gen.flow_from_directory(
'D:\\\\Study\\\\AI\\\\OpenCV\\\\horse-human-data\\\\horse-or-human',
target_size = (150,150), batch_size = 32, class_mode = 'binary'
# 目标大小 一批的数量 二分类
)
测试数据生成器
# 测试数据生成器(指向测试数据文件夹)
test_generator = test_data_gen.flow_from_directory(
'D:\\\\Study\\\\AI\\\\OpenCV\\\\horse-human-data\\\\validation-horse-or-human',
target_size = (150,150), batch_size = 32, class_mode = 'binary'
)
二、构建神经网络
# 2、构建神经网络
import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
# 神经网络模型
model = tf.keras.models.Sequential([
# 第一层CNN
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
# 第二层CNN
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# 第三层CNN
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# 第四层CNN
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# 第五层CNN
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# 输入层
tf.keras.layers.Flatten(),
# 隐层
tf.keras.layers.Dense(512, activation='relu'),
# 输出层:用sigmoid激活函数二分类(只有一个神经元,结果只有0和1,分别对应人和马)
tf.keras.layers.Dense(1, activation='sigmoid')
])
# 可视化
model.summary()
Model: "sequential_3" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_15 (Conv2D) (None, 148, 148, 16) 448 _________________________________________________________________ max_pooling2d_15 (MaxPooling (None, 74, 74, 16) 0 _________________________________________________________________ conv2d_16 (Conv2D) (None, 72, 72, 32) 4640 _________________________________________________________________ max_pooling2d_16 (MaxPooling (None, 36, 36, 32) 0 _________________________________________________________________ conv2d_17 (Conv2D) (None, 34, 34, 64) 18496 _________________________________________________________________ max_pooling2d_17 (MaxPooling (None, 17, 17, 64) 0 _________________________________________________________________ conv2d_18 (Conv2D) (None, 15, 15, 64) 36928 _________________________________________________________________ max_pooling2d_18 (MaxPooling (None, 7, 7, 64) 0 _________________________________________________________________ conv2d_19 (Conv2D) (None, 5, 5, 64) 36928 _________________________________________________________________ max_pooling2d_19 (MaxPooling (None, 2, 2, 64) 0 _________________________________________________________________ flatten_3 (Flatten) (None, 256) 0 _________________________________________________________________ dense_6 (Dense) (None, 512) 131584 _________________________________________________________________ dense_7 (Dense) (None, 1) 513 ================================================================= Total params: 229,537 Trainable params: 229,537 Non-trainable params: 0 _________________________________________________________________
三、编译、训练
# 3、编译&&训练
model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.001), metrics=['acc'])
model.fit(train_generator, epochs=5, validation_data=test_generator)
# 训练集 迭代次数 测试集
Epoch 1/10 33/33 [==============================] - 18s 544ms/step - loss: 0.5144 - acc: 0.7420 - val_loss: 0.6703 - val_acc: 0.8438 Epoch 2/10 33/33 [==============================] - 18s 548ms/step - loss: 0.1962 - acc: 0.9299 - val_loss: 0.7632 - val_acc: 0.8594 Epoch 3/10 33/33 [==============================] - 18s 558ms/step - loss: 0.1640 - acc: 0.9445 - val_loss: 0.7594 - val_acc: 0.8594 Epoch 4/10 33/33 [==============================] - 18s 551ms/step - loss: 0.0940 - acc: 0.9640 - val_loss: 1.0639 - val_acc: 0.8398 Epoch 5/10 33/33 [==============================] - 18s 544ms/step - loss: 0.0851 - acc: 0.9747 - val_loss: 1.4272 - val_acc: 0.8633 Epoch 6/10 33/33 [==============================] - 18s 551ms/step - loss: 0.0774 - acc: 0.9698 - val_loss: 1.0516 - val_acc: 0.8516 Epoch 7/10 33/33 [==============================] - 19s 562ms/step - loss: 0.0113 - acc: 0.9981 - val_loss: 2.2403 - val_acc: 0.7734 Epoch 8/10 33/33 [==============================] - 18s 549ms/step - loss: 0.0689 - acc: 0.9786 - val_loss: 1.3702 - val_acc: 0.8672 Epoch 9/10 33/33 [==============================] - 18s 549ms/step - loss: 0.0043 - acc: 1.0000 - val_loss: 2.0496 - val_acc: 0.8594 Epoch 10/10 33/33 [==============================] - 18s 556ms/step - loss: 0.0572 - acc: 0.9873 - val_loss: 1.8541 - val_acc: 0.8633
[12]:
<tensorflow.python.keras.callbacks.History at 0x7fa1b05ddcd0>
四、预测(单图预测)
1、待预测图像处理
1-1、读取图像
import cv2
import matplotlib.pyplot as plt
import numpy as np
# (1) 读取图像
# img1:马 img2:人
img1 = cv2.imread('tensorflow_datasets/validation-horse-or-human/horses/horse1-000.png')
img1 = cv2.resize(img1, (150, 150))
img2 = cv2.imread('tensorflow_datasets/validation-horse-or-human/humans/valhuman01-01.png')
img2 = cv2.resize(img2, (150, 150))
1-2、BGR转RGB
BGR转RGB(opencv的色彩空间是BGR,plt色彩空间是RGB)。
# (2) BGR转RGB
# OpenCV中图片像素按照BGR方式排列;而Matpoltlib中图片按照RGB方式排序
b,g,r = cv2.split(img1) #分离
img1 = cv2.merge([r,g,b]) #合并
b,g,r = cv2.split(img2) #分离
img2 = cv2.merge([r,g,b]) #合并
1-3、显示图像
# (3) 显示图像
f, ax = plt.subplots(1, 2)
ax[0].imshow(img1)
ax[0].set_title("0")
ax[1].imshow(img2)
ax[1].set_title("1")
plt.show()
如果不进行BGR转RGB的操作,就会出现这样色彩错乱的情况:
(来自冥界的人马)
2、图像数据转换
这里model.predict()的预测需要更高维度的图像数据,进行一下转换。
# 图像转数据(用来做预测)
Img1 = np.expand_dims(img1, axis=0) #转三维数据(二维转三维)
Img2 = np.expand_dims(img2, axis=0) #转三维数据(二维转三维)
img:
Img:
3、预测
# 4、预测(分别对两个图进行预测)
for i in range(2):
if i==0:
# 对图像1做预测
classes = model.predict(Img1, batch_size=10)
print(f'{i}号图片预测结果为:', int(classes[0][0]), '马')
elif i==1:
# 对图像2做预测
classes = model.predict(Img2, batch_size=10)
print(f'{i}号图片预测结果为:', int(classes[0][0]), '人')
总代码
# 1、准备数据
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# 1-1、创建两个数据生成器
train_data_gen = ImageDataGenerator(rescale=1/255)
test_data_gen = ImageDataGenerator(rescale=1/255)
# 1-2、创建训练数据与测试数据生成器
# 训练数据生成器(指向训练数据文件夹)
train_generator = train_data_gen.flow_from_directory(
'tensorflow_datasets/horse-or-human/',
target_size = (150,150), batch_size = 32, class_mode = 'binary'
# 目标大小 一批的数量 二分类
)
# 测试数据生成器(指向测试数据文件夹)
test_generator = test_data_gen.flow_from_directory(
'tensorflow_datasets/validation-horse-or-human/',
target_size = (150,150), batch_size = 32, class_mode = 'binary'
)
# 2、构建神经网络
import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
# 神经网络模型
model = tf.keras.models.Sequential([
# 第一层CNN
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
# 第二层CNN
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
# 第三层CNN
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
# 第四层CNN
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
# 第五层CNN
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
# 输入层
tf.keras.layers.Flatten(),
# 隐层
tf.keras.layers.Dense(512, activation='relu'),
# 输出层:用sigmoid激活函数二分类(只有一个神经元,结果只有0和1,分别对应人和马)
tf.keras.layers.Dense(1, activation='sigmoid')
])
# 可视化
model.summary()
# 3、编译&&训练
model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.001), metrics=['acc'])
model.fit(train_generator, epochs=10, validation_data=test_generator)
# 训练集 迭代次数 测试集
# 4、预测
import cv2
import matplotlib.pyplot as plt
import numpy as np
# (1) 读取图像
# img1:马 img2:人
img1 = cv2.imread('tensorflow_datasets/validation-horse-or-human/horses/horse1-000.png')
img1 = cv2.resize(img1, (150, 150))
img2 = cv2.imread('tensorflow_datasets/validation-horse-or-human/humans/valhuman01-01.png')
img2 = cv2.resize(img2, (150, 150))
# (2) BGR转RGB
# OpenCV中图片像素按照BGR方式排列;而Matpoltlib中图片按照RGB方式排序
b,g,r = cv2.split(img1) #分离
img1 = cv2.merge([r,g,b]) #合并
b,g,r = cv2.split(img2) #分离
img2 = cv2.merge([r,g,b]) #合并
# (3) 显示图像
f, ax = plt.subplots(1, 2)
ax[0].imshow(img1)
ax[1].imshow(img2)
plt.show()
# 图像转数据(用来做预测)
Img1 = np.expand_dims(img1, axis=0) #转三维数据(二维转三维)
Img2 = np.expand_dims(img2, axis=0) #转三维数据(二维转三维)
# 4、预测(分别对两个图进行预测)
for i in range(2):
if i==0:
# 对图像1做预测
classes = model.predict(Img1, batch_size=10)
print(f'{i}号图片预测结果为:', int(classes[0][0]), '马')
elif i==1:
# 对图像2做预测
classes = model.predict(Img2, batch_size=10)
print(f'{i}号图片预测结果为:', int(classes[0][0]), '人')
以上是关于TensorFlow人马图像分类(卷积神经网络)的主要内容,如果未能解决你的问题,请参考以下文章