Today, I started to classify images on the basis of the previous ones. If there is something you don’t understand, please read the previous articles.
Table of contents
Before starting, first import the packages we need to use, most of which have been introduced before:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
import os
from glob import glob
from tensorflow.keras import datasets,layers,optimizers,losses,models
from PIL import Image
image processing
The purpose of making an image classification model is to let the machine recognize the characteristics of the memory image, so the input image must be preprocessed first, and the uniform size and format should be adjusted.
First write a function to convert each picture into the array format we need:
def read_image(img_path,shape):
plt_img = Image.open(img_path)
np_img = np.array(plt_img,dtype=np.uint8)
np_img = cv2.resize(np_img,shape)
return np_img
Then we read the pictures under the picture path, and use read_image to process each picture:
folders = os.listdir("seg_train")//文件路径
type_dict = {}
imgs = []
labs = []
for i,type_name in enumerate(folders)://处理图片与标签
type_dict[i] = type_name
images = glob(os.path.join('seg_train',type_name,'*.jpg'))
labels = [i] * len(images)
imgs = [*imgs,*images]
labs = [*labs,*labels]
//X是图片矩阵,Y是对应的标签数值
X_train = np.zeros([len(imgs),50,50,3])
for index,img_path in enumerate(imgs):
X_train[index,:,:,:] = read_image(img_path,(50,50))
Y_train = np.zeros([len(labs),len(folders)])
for i in range(len(labs)):
Y_train[i,labs[i]] = 1.0
X_train =np.reshape(X_train,[X_train.shape[0],50*50*3])
operation result:
model building
To build a model, there are not many skills, and all the fully connected layers are used.
The result of the last layer 6: Correspondingly, I have 6 image types
The specific meaning has been explained before, here is the code directly:
model = tf.keras.Sequential([
# layers.Dense(67500, activation='relu'),
# layers.Dense(50000,activation='relu'),
# layers.Dense(25000, activation='relu'),
# layers.Dense(12500,activation='relu'),
# layers.Dense(7500, activation='relu'),
layers.Dense(2500,activation='relu'),
layers.Dense(1500,activation='relu'),
layers.Dense(700,activation='relu'),
layers.Dense(300,activation='relu'),
layers.Dense(100,activation='relu'),
layers.Dense(6,activation='softmax')
])
//定义优化、损失
opt = optimizers.SGD(learning_rate=0.001)
loss = losses.CategoricalCrossentropy()
model.build(input_shape=[None,7500])
model.compile(optimizer=opt,loss=loss,metrics=['accuracy'])
model.fit(x=X_train,y=Y_train,batch_size=100,epochs=25,shuffle=True)
model.save('model.h5')
Run it, and save a model by the way, so we can use it later:
This computer doesn't have a GPU and either won't work or is just too slow.
model use
It is also very simple to use, we load back the saved model, enter a photo, and then we can judge
model = tf.keras.models.load_model('model.h5')
# model = tf.keras.models.load_model
tmp_img = np.reshape(img,[1,-1])
pr_lab = model(tmp_img,training=False)
pr_lab = np.array(pr_lab)
pr_lab = np.argmax(pr_lab)
print(type_dict[pr_lab])
plt.figure()
plt.imshow(img)
plt.title(type_dict[pr_lab])
plt.show()
Take a look at the results:
Let's see if this photo is actually a mountain:
It seems that the effect is not bad.