содержание
2. Установите глобальные параметры
5. Вырезать набор для обучения и набор для проверки
7. Установите функцию обратного вызова
9. Сохраните данные истории тренировок
2. Установите глобальные параметры
Резюме
Эта статья использует CNN для классификации изображений.Изображения выбраны из 10 000 изображений набора данных о битвах кошек и собак (5 000 кошек и собак). Модель использует пользовательскую сеть CNN, а версия — TensorFlow 2.0 или более поздняя. В этой статье вы можете изучить общие методы классификации изображений, в том числе:
1. Улучшение изображения
2. Разделить набор для обучения и набор для проверки
3. Используйте ModelCheckpoint, чтобы сохранить оптимальную модель
4. Используйте ReduceLROnPlateau, чтобы настроить скорость обучения.
5. Распечатайте результат потери, чтобы создать изображение в формате 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. Установите функцию обратного вызова
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")
Полный код:
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)