Tensorflow.keras笔记-卷积神经网络
cifar10数据集
1. 载入cifar10数据集
2. 处理数据,归一化
3. 数据扩增
4. 建模
5. 断点续训
6. 给图识物
#载入包import tensorflow as tfimport osimport numpy as npfrom matplotlib import pyplot as pltfrom tensorflow.keras.layers import Conv2D,BatchNormalization,Activation,MaxPool2D,Dropout,Flatten,Densefrom tensorflow.keras import Modelnp.set_printoptions(threshold=np.inf)
1-2:载入cifar10数据集,并进行归一化处理
cifar10数据集共有60000张彩色图像,这些图像是32*32,分为10个类,每类6000张图。这里面有50000张用于训练,另外10000用于测试。
#数据导入处理cifar10 = tf.keras.datasets.cifar10(x_train,y_train),(x_test,y_test) = cifar10.load_data()x_train,x_test = x_train / 255.0, x_test / 255.0
3:建模,卷积神经网络核心是应用卷积层对图片进行特征提取,而后在用全连接层进行神经网络分析。卷积层包含五部分:卷积层(Conv2D),批标准化(BN),激活(Activation),池化层(Pooling)和Dropout。卷积就是采用多个‘CBAPD’提取图片特征。
#建模class Baseline(Model): #继承Model def __init__(self): super(Baseline,self).__init__() self.c1 = Conv2D(filters=6, kernel_size=(5,5), padding='same') #卷积 self.b1 = BatchNormalization() #批标准化 self.a1 = Activation('relu') #激活函数 self.p1 = MaxPool2D(pool_size=(2,2), strides=2,padding='same') #最大池化 self.d1 = Dropout(0.2) #随机沉默参数 self.flatten = Flatten() #全连接 self.f1 = Dense(128, activation='relu') self.d2 = Dropout(0.2) self.f2 = Dense(10, activation='softmax') def call(self, x): x = self.c1(x) x = self.b1(x) x = self.a1(x) x = self.p1(x) x = self.d1(x) x = self.flatten(x) x = self.f1(x) x = self.d2(x) y = self.f2(x) return ymodel = Baseline()
4:设置参数,断点续训,数据增强,模型训练。数据增强函数
数据增强采用keras自带的ImageDataGenerator函数
tf.keras.preprocessing.image.ImageDataGenerator(rescale = 所有数据将乘以该数值rotation_range = 随机旋转角度数范围width_shift_range = 随机宽度偏移量height_shift_ragne = 随机高度偏移量horizontal_flip = 是否随机水平翻转zoom_range = 随机缩放的范围[1-n, 1+n])image_gen_train.fit(x_train)model.fit(image_gen_train.flow(x_train,y_train,batch_size=32),...)
断点续训采用回调函数Checkpoint保存模型
#设置参数model.compile(optimizer = 'adam', loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy'])cp_callback = tf.keras.callbacks.ModelCheckpoint( filepath='D:/Python/cifar10/check.skpt', save_weight_only=True, save_best_only=True)#执行model.fit(x_train, y_train,batch_size=32, epochs=5, validation_data=(x_test,y_test), validation_freq=1, callbacks=[cp_callback])
5. 给图识物:采用model.predict()函数识别图片,并输出结果
import matplotlib as pltfrom PIL import Imageclothes = ['T恤', '长裤', '长袖', '裙子', '外套', '凉鞋', '衬衫', '休闲鞋', '包包', '鞋子']for i in range(10): path = 'D:/1_数据/Python/Tensorflow学习/数据/exam_fashion/' + str(i) + '.jpg' img = Image.open(path) img = img.resize((28,28),Image.ANTIALIAS) img = img.convert('L') img = np.array(img) img = 255-img img = img / 255.0 x_predict = img[tf.newaxis, ...,tf.newaxis] result = model.predict(x_predict) pred = tf.argmax(result, axis=1) num = int(np.array(pred)) print('这件是:{0}_{1}'.format(num,clothes[num]))
学习于↓
《TensorFlow 2.0深度学习算法实战》
中国大学Mooc链接:
https://www.icourse163.org/course/PKU-1002536002
曹健 北京大学 软件与微电子学院