tensorflow学习笔记 (五) (卷积神经网络)

文章目录

卷积神经网络

由于在实际应用场景中,需要优化的参数可能很多,容易导致模型的过拟合,因此可以先从原始图像中提取多层特征,而卷积计算是一种有效的特征提取方法。

[En]

Because in the real application scene, there may be many parameters to be optimized, which can easily lead to over-fitting of the model, we can first extract several layers of features from the original image, and convolution calculation is an effective feature extraction method.

一、卷积计算过程

1.单通道的卷积计算

tensorflow学习笔记 (五) (卷积神经网络)

; 2.三通道的卷积计算

tensorflow学习笔记 (五) (卷积神经网络)

3. 卷积计算过程动图

卷积神经网络动图

二、卷积相关

tensorflow学习笔记 (五) (卷积神经网络)

; 2.1 两种卷积核的比较与感受野

tensorflow学习笔记 (五) (卷积神经网络)
红色箭头指向的两个小方块的接受字段都是5,因为它们都描述了原始5×5正方形的像素。
[En]

The receptive fields of the two small squares pointed to by the red arrow are both 5 because they both describe the pixels of the original 5-by-5 square.

tensorflow学习笔记 (五) (卷积神经网络)

2.2 全零填充

即在输入特征图周围填充0

tensorflow学习笔记 (五) (卷积神经网络)
tensorflow学习笔记 (五) (卷积神经网络)

; 2.3 批标准化

主要是弄归一化的
我暂时不想谈这件事。当我做完项目回来的时候,我会补上的。

[En]

I don’t feel like going into it for the time being. I’ll make it up when I come back after the project.

2.4 池化

池化用于减少特征数据量,最大池化可以提取图像纹理,均值池化可以保留背景特征。

[En]

Pooling is used to reduce the amount of feature data, maximum pooling can extract image texture, and mean pooling can retain background features.

tensorflow学习笔记 (五) (卷积神经网络)
上图上方的小方块为最大值池,下方的小方块为平均值池。
[En]

The upper small square in the above picture is the maximum value pool, and the lower small square is the mean pool.

model = tf.keras.models.Sequential([
    Conv2D(filters=6, kernel_size=(5, 5), padding='valid'),
    BatchNormalization(),
    Activation('relu'),
    MaxPool2D(pool_size=(2,2),strides=2,padding='same'),
    Dropout(0.2),
])

2.5 舍弃

tensorflow学习笔记 (五) (卷积神经网络)
tensorflow学习笔记 (五) (卷积神经网络)
0.2表示随机舍弃掉20%的神经元。

; 2.6 总结

tensorflow学习笔记 (五) (卷积神经网络)
八股口诀:CBAPD

2.7 Example1

CIFAR-10 是由 Hinton 的学生 Alex Krizhevsky 和 Ilya Sutskever 整理的一个用于识别普适物体的小型数据集。一共包含 10 个类别的 RGB 彩色图 片:飞机( a叩lane )、汽车( automobile )、鸟类( bird )、猫( cat )、鹿( deer )、狗( dog )、蛙类( frog )、马( horse )、船( ship )和卡车( truck )。图片的尺寸为 32×32 ,数据集中一共有 50000 张训练圄片和 10000 张测试图片。

example1就是对cifar10进行十分类。
下载数据:

import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
cifar10 = tf.keras.datasets.cifar10
(x_train,y_train),(x_test,y_test) = cifar10.load_data()

plt.imshow(x_train[0])
plt.show()

然后构建一个神经网络,因为这是一个10级分类的问题,所以我们需要一个包含10个神经元的输出层:

[En]

Then build a neural network, because it is a 10-classification problem, so we need an output layer containing ten neurons:

tensorflow学习笔记 (五) (卷积神经网络)
实现代码:
import tensorflow as tf
import os
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense
from tensorflow.keras import Model

np.set_printoptions(threshold=np.inf)
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

class Baseline(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 y
model = Baseline()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

checkpoint_save_path = "./checkpoint/Baseline.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                 save_weights_only=True,
                                                 save_best_only=True)

history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
                    callbacks=[cp_callback])
model.summary()

file = open('./weights.txt', 'w')
for v in model.trainable_variables:
    file.write(str(v.name) + '\n')
    file.write(str(v.shape) + '\n')
    file.write(str(v.numpy()) + '\n')
file.close()

acc = history.history['sparse_categorical_accuracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()

实现结果:

tensorflow学习笔记 (五) (卷积神经网络)
tensorflow学习笔记 (五) (卷积神经网络)

三、经典网络复现

以前,一个未知的卷积神经网络用于10个分类,现在复制各种经典网络来观察它们在10个分类问题上的结果的差异。

[En]

Previously, an unknown convolution neural network was used for 10 classification, and now all kinds of classical networks are reproduced to observe their differences in the results of 10 classification problems.

3.1 LeNet5

tensorflow学习笔记 (五) (卷积神经网络)
class LeNet5(Model):
    def __init__(self):
        super(LeNet5, self).__init__()
        self.c1 = Conv2D(filters=6,kernel_size=(5,5))

        self.a1 = Activation('sigmoid')
        self.p1 = MaxPool2D(pool_size=(2,2),strides=2)

        self.c2 = Conv2D(filters=16,kernel_size=(5,5),
                         activation='sigmoid')
        self.p2 = MaxPool2D(pool_size=(2,2),strides=2)

        self.flatten = Flatten()
        self.f1 = Dense(120,activation='sigmoid')
        self.f2 = Dense(84,activation='sigmoid')
        self.f3 = Dense(10,activation='softmax')

    def call(self,x):
        x = self.c1(x)
        x = self.a1(x)
        x = self.p1(x)
        x = self.c2(x)
        x = self.p2(x)
        x = self.flatten(x)
        x = self.f1(x)
        x = self.f2(x)
        y = self.f3(x)
        return y

结果:

tensorflow学习笔记 (五) (卷积神经网络)

tensorflow学习笔记 (五) (卷积神经网络)

3.2 AlexNet8

tensorflow学习笔记 (五) (卷积神经网络)
class AlexNet8(Model):
    def __init__(self):
        super(AlexNet8, self).__init__()
        self.c1 = Conv2D(filters=96, kernel_size=(3, 3))
        self.b1 = BatchNormalization()
        self.a1 = Activation('relu')
        self.p1 = MaxPool2D(pool_size=(3, 3), strides=2)

        self.c2 = Conv2D(filters=256, kernel_size=(3, 3))
        self.b2 = BatchNormalization()
        self.a2 = Activation('relu')
        self.p2 = MaxPool2D(pool_size=(3, 3), strides=2)

        self.c3 = Conv2D(filters=384, kernel_size=(3, 3), padding='same',
                         activation='relu')

        self.c4 = Conv2D(filters=384, kernel_size=(3, 3), padding='same',
                         activation='relu')

        self.c5 = Conv2D(filters=256, kernel_size=(3, 3), padding='same',
                         activation='relu')
        self.p3 = MaxPool2D(pool_size=(3, 3), strides=2)

        self.flatten = Flatten()
        self.f1 = Dense(2048, activation='relu')
        self.d1 = Dropout(0.5)
        self.f2 = Dense(2048, activation='relu')
        self.d2 = Dropout(0.5)
        self.f3 = 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.c2(x)
        x = self.b2(x)
        x = self.a2(x)
        x = self.p2(x)

        x = self.c3(x)

        x = self.c4(x)

        x = self.c5(x)
        x = self.p3(x)

        x = self.flatten(x)
        x = self.f1(x)
        x = self.d1(x)
        x = self.f2(x)
        x = self.d2(x)
        y = self.f3(x)
        return y

model = AlexNet8()

注:在改变模型的时候,应先把生成的文件删除后再训练,不然会报错,因为训练完后的tensor维度和最开始需要的tensor维度不同

tensorflow学习笔记 (五) (卷积神经网络)

tensorflow学习笔记 (五) (卷积神经网络)

3.3 VGGNet

3.4 InceptionNet

3.5 ResNet

3.6 总结

VGGNet,InceptionNet ,resnet 这三个网络均较为复杂,电脑配置低运行起来花费时间长,等以后有时间再详细研究,目前只是先粗略过一下tensorflow。

感觉神经网络的设计过程和参数调整过程是一个形而上的过程,可能需要大量的研究才能学会调整参数。

[En]

The design process and parameter adjustment process of sensory neural network is a metaphysical process, which may require a lot of study before we can learn to adjust parameters.

四、参考

https://www.bilibili.com/video/BV1B7411L7Qt?p=42&spm_id_from=pageDriver

Original: https://blog.csdn.net/qq_45933509/article/details/124486124
Author: cqust_qilin02811
Title: tensorflow学习笔记 (五) (卷积神经网络)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/497608/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球