2021-10-29 Tensorflow 2 Tensorflow model establishment and training-Multilayer Perceptron MLP

About the Fashion-MNIST dataset

Zalando replaced the MNIST data set with the Fashion-MNIST data set and used Fashion-MINST as a machine learning data set.
The Fashion-MNIST data set consists of a total of 70,000 pictures of clothing, shoes and bags. Each picture has a pixel size of 28x28 and is displayed in grayscale. Among them, 60,000 images are used for training and 10,000 images are used for testing.
The Fashion data set can be divided into ten categories, expressed in Chinese as follows:
class_name = ["T-shirt", "Pants", "Hoodie", "Dress", "Coat", "Sandals", "Shirt", " sneakers”, “bag”, “boots”]

Load the Fashion dataset

The version of Tensorflow used in this application is 2.3.0

  • Import the necessary libraries
    import tensorflow as tf
    import numpy as np
    from tensorflow.keras.layers import Dense, Flatten
    from tensorflow.keras import Model
    import matplotlib.pyplot as plt
  • Load data set
    #Read the input features and labels for training
    fashion = tf.keras.datasets.fashion_mnist
    (x_train, y_train), (x_test, y_test) = fashion.load_data()
    Description:
    x_train: indicates loading the feature data training set, That is, the 60,000 picture training set
    y_train: represents the label training set corresponding to the picture classification
    x_test: represents the loaded feature data test set, that is, the test set containing 10,000 pictures
    y_test: represents the label training set corresponding to the picture classification
    #In order to achieve input feature classification 1. The value is [0, 1], and the amount of calculation is reduced. The picture is defaulted to a number between 0 and 255, which facilitates the neural network to absorb
    x_train, x_test = x_train/255.0, x_test/255.0
    class_name = ["T-shirt ","pants","hoodie","dress","jacket","sandals","shirt","sneakers","bag","boots"]

Build a neural network

Define neural network class

Define the neural network model class FashionModel, which is a subclass of tf.keras.Model

class FashionModel(Model):
   # 定义网络结构
	def __init__(self):
    	super(FashionModel, self).__init__()
    	#将数据拉直 [28,28]->[784,1]
   		self.flatten = Flatten()
    	#指定激活函数
    	self.d1 = Dense(128, activation="relu") 
    	#指定激活函数,设置为[0,1]之间的数字
    	self.d2 = Dense(10, activation="softmax")
	def call(self, inputs, training=None, mask=None):
    	x = self.flatten(inputs)
    	x = self.d1(x)
    	y = self.d2(x)
   		return y

The above network is divided into three layers: flattened layer, fully connected layer d1, and second fully connected layer d2

Define and call the neural network, passing in the training set and test set as parameters:

def generate_nn(x_train,y_train,x_test,y_test):
	# 声明神经网络对象
 	model = FashionModel()
	# 配置训练方法(优化器,损失函数,评测指标)
	model.compile(optimizer=tf.keras.optimizers.Adam(),
              	  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              	  metrics=[tf.keras.metrics.sparse_categorical_accuracy])
# 执行训练过程
	model.fit(x_train, y_train,
              batch_size=32, epochs=10,
              validation_data=(x_test, y_test),
              validation_freq=1)
# 打印网络结构和参数
	model.summary()
	return model

Image display processing

def show_image(img_arr):
	"""
	显示图片
	Parameters
	----------
	img_arr : numpy.ndarray
     表示图片
	"""
	plt.imshow(img_arr,cmap="binary")
	plt.show()

Predict the classification of ten pictures

Location of the picture:
The extension of the picture is jpeg
Insert image description here

#创建模型
model = generate_nn(x_train,y_train,x_test,y_test)
#定义图片的存储位置
path = "./test_data/exam_fashion/exam_fashion/"
#图片的文件名,形如1.jpeg
images = ["%d.jpeg"%i for i in range(0,10)]
#生成28x28的图片数组
matrix = np.full(784,255.0).reshape(28,28)
images_data=[]
for imgfile in images:
    #设置要访问的图片文件名
	img_name = "%s%s"%(path,imgfile)
	#加载图片
	img = tf.keras.preprocessing.image.load_img(img_name,color_mode="grayscale",target_size=(28,28))
	img_data = tf.keras.preprocessing.image.img_to_array(img)   #将图片数据转换成numpy数组
	img_data = matrix-img_data.reshape(28,28)                             #图片反相处理,黑变白,白变黑,将数组设置为28x28 
	show_image(img_data)                                                              #显示图片
	images_data.append(img_data)
	
def main():
	images_data = np.array(images_data)
	y_pred = model.predict(images_data)                                            #预测的结果
	index = np.argmax(y_pred,axis=1)                                                 #获取预测的结果,返回二位数组的	列坐标对应的分类
	for i in index:                                                                                   #显示最后的结果 
   		 print(i,class_name[i])             
   		                            
if __name__=="__main__":
	main()                         

Test results:
6 shirt
1 trousers
4 jacket
3 dress
4 jacket
5 sandals
0 T-shirt
7 sneakers
8 bags
9 boots

Guess you like

Origin blog.csdn.net/userhu2012/article/details/121041107
Recommended