TensorFlow2.0以上版本的图像分类

简介: TensorFlow2.0以上版本的图像分类

目录


摘要


网络详解


训练部分


1、导入依赖


2、设置全局参数


3、加载数据


4、定义模型


5、切割训练集和验证集


6、数据增强


7、设置callback函数


8、训练并保存模型


9、保存训练历史数据


完整代码:


测试部分


1、导入依赖


2、设置全局参数


3、加载模型


4、处理图片


5、预测类别


完整代码


摘要

本篇文章采用CNN实现图像的分类,图像选取了猫狗大战数据集的1万张图像(猫狗各5千)。模型采用自定义的CNN网络,版本是TensorFlow 2.0以上的版本。通过本篇文章,你可以学到图像分类常用的手段,包括:


1、图像增强


2、训练集和验证集切分


3、使用ModelCheckpoint保存最优模型


4、使用ReduceLROnPlateau调整学习率。


5、打印loss结果生成jpg图片。


网络详解

训练部分

1、导入依赖

import os

import numpy as np

from tensorflow import keras

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Dropout,BatchNormalization,Flatten

from tensorflow.keras.layers import Conv2D, MaxPooling2D,GlobalAveragePooling2D

import cv2

from tensorflow.keras.preprocessing.image import img_to_array

from sklearn.model_selection import train_test_split

from tensorflow.python.keras import Input

from tensorflow.python.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau

from tensorflow.python.keras.layers import PReLU, Activation

from tensorflow.python.keras.models import Model


2、设置全局参数

norm_size=100#输入到网络的图像尺寸,单位是像素。
datapath='train'#图片的根目录
EPOCHS =100#训练的epoch个数
INIT_LR = 1e-3#初始学习率
labelList=[]#标签
dicClass={'cat':0,'dog':1}#类别
labelnum=2#类别个数
batch_size = 4


3、加载数据

def loadImageData():
    imageList = []
    listImage=os.listdir(datapath)#获取所有的图像
    for img in listImage:#遍历图像
        labelName=dicClass[img.split('.')[0]]#获取label对应的数字
        print(labelName)
        labelList.append(labelName)
        dataImgPath=os.path.join(datapath,img)
        print(dataImgPath)
        image = cv2.imdecode(np.fromfile(dataImgPath, dtype=np.uint8), -1)
        # load the image, pre-process it, and store it in the data list
        image = cv2.resize(image, (norm_size, norm_size), interpolation=cv2.INTER_LANCZOS4)
        image = img_to_array(image)
        imageList.append(image)
    imageList = np.array(imageList, dtype="int") / 255.0#归一化图像
    return imageList
print("开始加载数据")
imageArr=loadImageData()
labelList = np.array(labelList)
print("加载数据完成")
print(labelList)

4、定义模型

def bn_prelu(x):
    x = BatchNormalization(epsilon=1e-5)(x)
    x = PReLU()(x)
    return x
def build_model(out_dims, input_shape=(norm_size, norm_size, 3)):
    inputs_dim = Input(input_shape)
    x = Conv2D(32, (3, 3), strides=(2, 2), padding='same')(inputs_dim)
    x = bn_prelu(x)
    x = Conv2D(32, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = GlobalAveragePooling2D()(x)
    dp_1 = Dropout(0.5)(x)
    fc2 = Dense(out_dims)(dp_1)
    fc2 = Activation('softmax')(fc2) #此处注意,为sigmoid函数
    model = Model(inputs=inputs_dim, outputs=fc2)
    return model
model=build_model(labelnum)#生成模型
optimizer = Adam(lr=INIT_LR)#加入优化器,设置优化器的学习率。
model.compile(optimizer =optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

5、切割训练集和验证集

trainX,valX,trainY,valY = train_test_split(imageArr,labelList, test_size=0.3, random_state=42)


6、数据增强

from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)
val_datagen = ImageDataGenerator()     #验证集不做图片增强
train_generator = train_datagen.flow(trainX,trainY,batch_size=batch_size,shuffle=True)
val_generator = val_datagen.flow(valX,valY,batch_size=batch_size,shuffle=True)


7、设置callback函数


checkpointer = ModelCheckpoint(filepath='weights_best_simple_model.hdf5',
                            monitor='val_accuracy',verbose=1, save_best_only=True, mode='max')
reduce = ReduceLROnPlateau(monitor='val_accuracy',patience=10,
                                            verbose=1,
                                            factor=0.5,
                                            min_lr=1e-6)

8、训练并保存模型

history = model.fit_generator(train_generator,
       steps_per_epoch=trainX.shape[0]/batch_size,
       validation_data = val_generator,
       epochs=EPOCHS,
       validation_steps=valX.shape[0]/batch_size,
       callbacks=[checkpointer,reduce],
       verbose=1,shuffle=True)
model.save('my_model_.h5')

9、保存训练历史数据


import os
loss_trend_graph_path = r"WW_loss.jpg"
acc_trend_graph_path = r"WW_acc.jpg"
import matplotlib.pyplot as plt
print("Now,we start drawing the loss and acc trends graph...")
# summarize history for accuracy
fig = plt.figure(1)
plt.plot(history.history["accuracy"])
plt.plot(history.history["val_accuracy"])
plt.title("Model accuracy")
plt.ylabel("accuracy")
plt.xlabel("epoch")
plt.legend(["train", "test"], loc="upper left")
plt.savefig(acc_trend_graph_path)
plt.close(1)
# summarize history for loss
fig = plt.figure(2)
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
plt.title("Model loss")
plt.ylabel("loss")
plt.xlabel("epoch")
plt.legend(["train", "test"], loc="upper left")
plt.savefig(loss_trend_graph_path)
plt.close(2)
print("We are done, everything seems OK...")
# #windows系统设置10关机
os.system("shutdown -s -t 10")

tt.png

tt.png

完整代码:

import os
import numpy as np
from tensorflow import keras
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout,BatchNormalization,Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D,GlobalAveragePooling2D
import cv2
from tensorflow.keras.preprocessing.image import img_to_array
from sklearn.model_selection import train_test_split
from tensorflow.python.keras import Input
from tensorflow.python.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from tensorflow.python.keras.layers import PReLU, Activation
from tensorflow.python.keras.models import Model
norm_size=100
datapath='train'
EPOCHS =100
INIT_LR = 1e-3
labelList=[]
dicClass={'cat':0,'dog':1}
labelnum=2
batch_size = 4
def loadImageData():
    imageList = []
    listImage=os.listdir(datapath)
    for img in listImage:
        labelName=dicClass[img.split('.')[0]]
        print(labelName)
        labelList.append(labelName)
        dataImgPath=os.path.join(datapath,img)
        print(dataImgPath)
        image = cv2.imdecode(np.fromfile(dataImgPath, dtype=np.uint8), -1)
        # load the image, pre-process it, and store it in the data list
        image = cv2.resize(image, (norm_size, norm_size), interpolation=cv2.INTER_LANCZOS4)
        image = img_to_array(image)
        imageList.append(image)
    imageList = np.array(imageList, dtype="int") / 255.0
    return imageList
print("开始加载数据")
imageArr=loadImageData()
labelList = np.array(labelList)
print("加载数据完成")
print(labelList)
def bn_prelu(x):
    x = BatchNormalization(epsilon=1e-5)(x)
    x = PReLU()(x)
    return x
def build_model(out_dims, input_shape=(norm_size, norm_size, 3)):
    inputs_dim = Input(input_shape)
    x = Conv2D(32, (3, 3), strides=(2, 2), padding='same')(inputs_dim)
    x = bn_prelu(x)
    x = Conv2D(32, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)
    x = bn_prelu(x)
    x = GlobalAveragePooling2D()(x)
    dp_1 = Dropout(0.5)(x)
    fc2 = Dense(out_dims)(dp_1)
    fc2 = Activation('softmax')(fc2) #此处注意,为sigmoid函数
    model = Model(inputs=inputs_dim, outputs=fc2)
    return model
model=build_model(labelnum)
optimizer = Adam(lr=INIT_LR)
model.compile(optimizer =optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
trainX,valX,trainY,valY = train_test_split(imageArr,labelList, test_size=0.3, random_state=42)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)
val_datagen = ImageDataGenerator()     #验证集不做图片增强
train_generator = train_datagen.flow(trainX,trainY,batch_size=batch_size,shuffle=True)
val_generator = val_datagen.flow(valX,valY,batch_size=batch_size,shuffle=True)
checkpointer = ModelCheckpoint(filepath='weights_best_simple_model.hdf5',
                            monitor='val_accuracy',verbose=1, save_best_only=True, mode='max')
reduce = ReduceLROnPlateau(monitor='val_accuracy',patience=10,
                                            verbose=1,
                                            factor=0.5,
                                            min_lr=1e-6)
history = model.fit_generator(train_generator,
       steps_per_epoch=trainX.shape[0]/batch_size,
       validation_data = val_generator,
       epochs=EPOCHS,
       validation_steps=valX.shape[0]/batch_size,
       callbacks=[checkpointer,reduce],
       verbose=1,shuffle=True)
model.save('my_model_.h5')
print(history)
import os
loss_trend_graph_path = r"WW_loss.jpg"
acc_trend_graph_path = r"WW_acc.jpg"
import matplotlib.pyplot as plt
print("Now,we start drawing the loss and acc trends graph...")
# summarize history for accuracy
fig = plt.figure(1)
plt.plot(history.history["accuracy"])
plt.plot(history.history["val_accuracy"])
plt.title("Model accuracy")
plt.ylabel("accuracy")
plt.xlabel("epoch")
plt.legend(["train", "test"], loc="upper left")
plt.savefig(acc_trend_graph_path)
plt.close(1)
# summarize history for loss
fig = plt.figure(2)
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
plt.title("Model loss")
plt.ylabel("loss")
plt.xlabel("epoch")
plt.legend(["train", "test"], loc="upper left")
plt.savefig(loss_trend_graph_path)
plt.close(2)
print("We are done, everything seems OK...")
# #windows系统设置10关机
os.system("shutdown -s -t 10")

测试部分

1、导入依赖

import cv2

import numpy as np

from tensorflow.keras.preprocessing.image import img_to_array

from  tensorflow.keras.models import load_model

import time


2、设置全局参数


norm_size=100

imagelist=[]

emotion_labels = {

   0: 'cat',

   1: 'dog'

}


3、加载模型


emotion_classifier=load_model("my_model_.h5")

t1=time.time()


4、处理图片


image = cv2.imdecode(np.fromfile('test/8.jpg', dtype=np.uint8), -1)

# load the image, pre-process it, and store it in the data list

image = cv2.resize(image, (norm_size, norm_size), interpolation=cv2.INTER_LANCZOS4)

image = img_to_array(image)

imagelist.append(image)

imageList = np.array(imagelist, dtype="float") / 255.0


5、预测类别


pre=np.argmax(emotion_classifier.predict(imageList))

emotion = emotion_labels[pre]

t2=time.time()

print(emotion)

t3=t2-t1

print(t3)


完整代码

import cv2
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array
from  tensorflow.keras.models import load_model
import time
norm_size=100
imagelist=[]
emotion_labels = {
    0: 'cat',
    1: 'dog'
}
emotion_classifier=load_model("my_model_.h5")
t1=time.time()
image = cv2.imdecode(np.fromfile('test/8.jpg', dtype=np.uint8), -1)
# load the image, pre-process it, and store it in the data list
image = cv2.resize(image, (norm_size, norm_size), interpolation=cv2.INTER_LANCZOS4)
image = img_to_array(image)
imagelist.append(image)
imageList = np.array(imagelist, dtype="float") / 255.0
pre=np.argmax(emotion_classifier.predict(imageList))
emotion = emotion_labels[pre]
t2=time.time()
print(emotion)
t3=t2-t1
print(t3)
目录
相关文章
|
7月前
|
TensorFlow 算法框架/工具
个错误可能是由于TensorFlow版本不兼容导致的
个错误可能是由于TensorFlow版本不兼容导致的
134 6
|
7月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
PYTHON TENSORFLOW 2二维卷积神经网络CNN对图像物体识别混淆矩阵评估|数据分享
PYTHON TENSORFLOW 2二维卷积神经网络CNN对图像物体识别混淆矩阵评估|数据分享
|
7月前
|
JSON TensorFlow 算法框架/工具
Windows下安装Anaconda5.3.1+Python3.8+TensorFlow2.13.0-CPU版本总结
Windows下安装Anaconda5.3.1+Python3.8+TensorFlow2.13.0-CPU版本总结
419 0
|
2月前
|
数据采集 TensorFlow 算法框架/工具
【大作业-03】手把手教你用tensorflow2.3训练自己的分类数据集
本教程详细介绍了如何使用TensorFlow 2.3训练自定义图像分类数据集,涵盖数据集收集、整理、划分及模型训练与测试全过程。提供完整代码示例及图形界面应用开发指导,适合初学者快速上手。[教程链接](https://www.bilibili.com/video/BV1rX4y1A7N8/),配套视频更易理解。
52 0
【大作业-03】手把手教你用tensorflow2.3训练自己的分类数据集
|
1月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
利用Python和TensorFlow构建简单神经网络进行图像分类
利用Python和TensorFlow构建简单神经网络进行图像分类
56 3
|
26天前
|
机器学习/深度学习 数据采集 TensorFlow
利用TensorFlow实现简单的图像分类模型
利用TensorFlow实现简单的图像分类模型
26 0
|
2月前
|
机器学习/深度学习 数据可视化 TensorFlow
使用TensorFlow构建一个简单的图像分类模型
【10月更文挑战第18天】使用TensorFlow构建一个简单的图像分类模型
69 1
|
2月前
|
机器学习/深度学习 TensorFlow API
使用 TensorFlow 和 Keras 构建图像分类器
【10月更文挑战第2天】使用 TensorFlow 和 Keras 构建图像分类器
|
4月前
|
并行计算 TensorFlow 算法框架/工具
Window安装TensorFlow-GPU版本
Window安装TensorFlow-GPU版本
66 0
|
4月前
|
机器学习/深度学习 监控 数据可视化

热门文章

最新文章