一:数据导入和处理
1.导入相关包:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
2.下载数据
(x_train_all, y_train_all), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()# x_valid:测试的图片集 x_train:训练的图片集。y_valid:测试的目标集,y_train:训练的目标集
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]“”“
x_valid.shape:(5000,32,32,3)
x_text.shape:(45000, 32, 32, 3)
y_test.shape: (10000,1)
”“”
3.数据归一化
from sklearn.preprocessing import StandardScaler# 这里使用StandardScaler标准差标准化
scaler = StandardScaler()
# 先将数据转换为numpy中的float32类型后再改变形状
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 32, 32, 3)
x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1, 1)).reshape(-1, 32, 32, 3)
x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 32, 32, 3)
二:定义卷积神经网络
# 定义卷积神经网络.
model = tf.keras.models.Sequential()
# 2次卷积, 一次池化, 总共3层.
# 第一层
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=(32, 32, 3)))
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2))# 第二层
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', ))
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2))# 第三层
model.add(tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation='relu', ))
model.add(tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2))# 将数据展平
model.add(tf.keras.layers.Flatten())# 全连接层
model.add(tf.keras.layers.Dense(256, activation='relu'))
# 加入dropout减轻过拟合现象
model.add(tf.keras.layers.AlphaDropout(0.3))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
配置网格
# 配置网络,损失,优化器,函数
model.compile(loss='sparse_categorical_crossentropy',optimizer='sgd',metrics=['acc'])
三:训练
# 传入训练图片集,训练目标集,共迭代十次
# validation_data中传入测试图片集,测试目标集
history = model.fit(x_train_scaled, y_train, epochs=10,validation_data=(x_valid_scaled, y_valid))
画出损失看一下
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid()
plt.gca().set_ylim(0, 2)
plt.show()
总结:
对于图片分类:
1.先确定使用什么算法来对物体识别
2.将数据处理到适用于这个算法的格式
3. 构建这个算法的卷积神经网络
4.训练调参
5.预测