Machine Learning - Image Classification Study Notes

Train the neural network model

1. Basic steps

1. Feed the training data to the model. In this example, the training data is in the train_images and train_labels arrays.
2. The model learns to associate images with labels.
3. Ask the model to make predictions on the test set (in this case, the test_images array).
4. Verify that the predictions match the labels in the test_labels array.

2. Data preprocessing

Check the image information and make the label value between 0-1

#设置标签分类
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

Lower the tag value

train_images_scaled=train_images/255
test_images_scaled=test_images/255

3. Construct neuron model

%config IPCompleter.greedy=True  #Tab键代码自动提示

method one:

#构造神经元模型

model=keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    #该层视为图像中未堆叠的像素行并将其排列起来。该层没有要学习的参数,它只会重新格式化数据。
    
    keras.layers.Dense(128,activation=tf.nn.relu),
    keras.layers.Dense(10,activation=tf.nn.softmax)
    #它们是密集连接或全连接神经层
])

Method Two:

model=keras.Sequential()
model.add( keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))

The activation function will not be described in detail

4. Training and evaluation model

model.compile
loss function loss - Used to measure the accuracy of the model during training. You will want to minimize this function in order to "steer" the model in the correct direction.
optimizer - decides how the model updates based on the data it sees and its own loss function .
metrics - Used to monitor training and testing steps. The following examples use accuracy, the proportion of images that are correctly classified.

#训练评估模型

model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
#adam是常用优化方法
#loss选择sparse是因为此处是作类别输出

model.fit(train_images,train_labels,epochs=5) #训练
"""
callbacks=[callbacks]用于终止训练
model.fit(train_images_scaled,train_labels,epochs=5,callbacks=[callbacks])
"""
model.evaluate(test_images,test_labels)  #评估模型效果

Custom callback method

#自定义callbacks方法

class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self,epoch,logs()):
        if(logs.get('loss')<0.4):
            print("\nloss is low so cancelling training!")
            self.model.stop_training=True
callbacks=myCallback()

If there are too many training times, overfitting will occur.
If the loss obtained by the evaluation is too different from the loss obtained by the model, it means that overfitting has occurred.

view weight

w,b = model.get_layer(index=0).weights

predict

The model has a linear output, the logits. A softmax layer can be appended to convert the logits into more human-readable probabilities.

#预测
#附加 softmax 层
import numpy as np
probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])

#预测测试集中所有模型的标签
predictions = probability_model.predict(test_images_scaled)

#测试
np.argmax(predictions[0]) #查看置信度最大的标签
test_labels[0]

Official learning documents:
Tensorflow official learning documents

Guess you like

Origin blog.csdn.net/qq_43842886/article/details/111564707