tensorflow2.0 picture classification of combat --- fashion-mnist data set classification

In fact, the idea of ​​writing this blog is mainly described some of tf2.0 common api usage and how to quickly and easily build a neural network using the tf.keras

1. First talk about tf.keras, with which we can easily build network models they want to build, fight like building blocks, layer by layer network stack up. But deep gradient network will disappear and so on, so just be able to build a network model to model results also need some knowledge of other ways to optimize.

To introduce fashion-mnist data sets can take a look at the links below
describes the on Github fashion-mnist

2. Then talk about the general classification commonly used for image optimization

  • 1. Image data normalization (standardization): accelerate network convergence, specific principles can imagine in a concentric gradient along the fastest to reach the center without formal graphical reach the center will be twists and turns along the gradient
    Here Insert Picture Description
  • 2. Data feature enhancements: Links
  • 3. Network super search parameters: get the best model parameters, mainly grid search, random search, genetic algorithms, heuristic search
  • Application 4.dropout, earlystopping, regularization and other methods: by adding a layer of oblivion, regularization and early stop to prevent over-fitting model

3. The implementation code and results section

#先导入一些常用库,后续用到再增加
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
import sklearn
import os
import sys

#看一下版本,确认是2.0
print(tf.__version__)

Here Insert Picture Description

#使用keras自带的模块导入数据,并且切分训练集、验证集、测试集,对训练数据进行标准化处理
fashion_mnist=keras.datasets.fashion_mnist
(x_train_all,y_train_all),(x_test,y_test)=fashion_mnist.load_data()
print(x_train_all.shape)
print(y_train_all.shape)
print(x_test.shape)
print(y_test.shape)

#切分训练集和验证集
x_train,x_valid=x_train_all[5000:],x_train_all[:5000]
y_train,y_valid=y_train_all[5000:],y_train_all[:5000]

print(x_train.shape)
print(y_train.shape)
print(x_valid.shape)
print(y_valid.shape)


#标准化
from sklearn.preprocessing import StandardScaler

scaler=StandardScaler()
x_train_scaled=scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
x_valid_scaled=scaler.fit_transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
x_test_scaled=scaler.fit_transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
#可视化一下图片以及对应的标签
#展示多张图片
def show_imgs(n_rows,n_cols,x_data,y_data,class_names):
    assert len(x_data)==len(y_data)#判断输入数据的信息是否对应一致
    assert n_rows*n_cols<=len(x_data)#保证不会出现数据量不够
    plt.figure(figsize=(n_cols*2,n_rows*1.6))
    for row in range(n_rows):
        for col in range(n_cols):
            index=n_cols*row+col   #得到当前展示图片的下标
            plt.subplot(n_rows,n_cols,index+1)
            plt.imshow(x_data[index],cmap="binary",interpolation="nearest")
            plt.axis("off")
            plt.title(class_names[y_data[index]])
    plt.show()
    
class_names=['t-shirt', 'trouser', 'pullover', 'dress', 'coat', 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
show_imgs(5,5,x_train,y_train,class_names)

Here Insert Picture Description

#搭建网络模型

model=keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
model.add(keras.layers.Dense(300,activation="relu"))
model.add(keras.layers.Dense(100,activation="relu"))
model.add(keras.layers.Dense(10,activation="softmax"))
model.compile(loss="sparse_categorical_crossentropy",optimizer="adam",metrics=["acc"])
model.summary()

Here Insert Picture Description
Here params digital network information in how to do?
W = Y X + b and according to the rules from the matrix multiplication (None, 784) to (None, 300) is an intermediate matrix (784,300) and the magnitude of the bias term b is 300, so 784 300 + 300 = 235,500, this is a small detail just mention.

#训练,并且保存最好的模型、训练的记录以及使用早停防止过拟合
import datetime
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
logdir = os.path.join('logs', current_time)
output_model=os.path.join(logdir,"fashionmnist_model.h5")
callbacks=[
    keras.callbacks.TensorBoard(log_dir=logdir),
    keras.callbacks.ModelCheckpoint(output_model,save_best_only=True),
    keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)
          ]


history=model.fit(x_train_scaled,y_train,epochs=30,validation_data=(x_valid_scaled,y_valid),callbacks=callbacks)

Here Insert Picture Description
Before I used to own a folder named using TensorBoard and ModelCheckpoint run to be wrong, I found a bit like a bug on windows, above which is a solution, and then open tensorboard look.
Here Insert Picture Description
Here Insert Picture Description
The best model is also saved as a file h5, easy call

def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8,5))
    plt.grid()
    plt.gca().set_ylim(0,1)
    plt.show()

plot_learning_curves(history)

This is a time to draw their own changes in training, and almost above
Here Insert Picture Description

#最后在测试集上的准确率
loss,acc=model.evaluate(x_test_scaled,y_test,verbose=0)
print("在测试集上的损失为:",loss)
print("在测试集上的准确率为:",acc)

Here Insert Picture Description

#得到测试集上的预测标签,可视化和真实标签的区别
y_pred=model.predict(x_test_scaled)
predict = np.argmax(y_pred,axis=1) 

show_imgs(3,5,x_test,predict,class_names)
show_imgs(3,5,x_test,y_test,class_names)

The results predicted
Here Insert Picture Description
real results
Here Insert Picture Description

4. Summary:

Read the above example, the use of tf.keras construction model is written

model=keras.models.Sequential()
model.add(...)
model.add(...)
...
model.compile(...)
model.fit(...)

#当然也可以写成
model=keras.models.Sequential([
	...
	...
	...
])
#这两者差别不大


#还有函数式的写法
inputs=...
hidden1=...(inputs)
....
#子类的写法
class ...:
	...

But for the model parameters, such as selection ( "sparse_categorical_crossentropy" and "categorical_crossentropy" or "binary_crossentropy") loss function of what kind of loss when you need to use the most appropriate function, select the activation function of each layer of the network, the optimizer choice ...... need to understand the meaning of which can be used in appropriate circumstances, I did not give examples here use super search parameters optimal model parameters, the next should write about a super example of parameters of the search.

Published 85 original articles · won praise 55 · views 20000 +

Guess you like

Origin blog.csdn.net/shelgi/article/details/103276140