AlexNet模型结构,卷积池化参数计算以及应用

AlexNet显示模型参数

pytorch版本

# 代码测试各网络层参数情况
import torch.nn as nn
from torchsummary import summary
import torch.nn.functional as F


class testNet(nn.Module):
    def __init__(self):
        # nn.Module的子类函数必须在构造函数中执行父类的构造函数
        super(testNet, self).__init__()  # 等价于nn.Module.__init__()

        # 在此处写入网络层
        self.conv1 = nn.Conv2d(3, 48, (11, 11), 4, 0)  # inputsize :227*227*3
        self.conv2 = nn.Conv2d(48, 128, (5, 5), 1, 2)
        self.conv3 = nn.Conv2d(128, 192, (3, 3), 1, 1)
        self.conv4 = nn.Conv2d(192, 192, (3, 3), 1, 1)
        self.conv5 = nn.Conv2d(192, 128, (3, 3), 1, 1)
        self.fc1 = nn.Linear(4608, 2048)
        self.fc2 = nn.Linear(2048, 2048)
        self.fc3 = nn.Linear(2048, 1000)

    def forward(self, x):
        x = self.conv1(x)
        print("conv1:", x.size())
        x = F.max_pool2d(x, (3, 3), stride=2)
        print("max_pool1:", x.size())
        x = self.conv2(x)
        x = F.max_pool2d(x, (3, 3), stride=2)  # F.max_pool2d的返回值是一个Variable 默认零填充
        print('max_pool2: ', x.size())
        x = self.conv3(x)
        print('conv3: ', x.size())
        x = self.conv4(x)
        print('conv4: ', x.size())
        x = self.conv5(x)
        print('conv5: ', x.size())
        x = F.max_pool2d(x, (3, 3), stride=2)
        print('max_pool3: ', x.size())
        x = x.view(x.size()[0], -1)  # reshape 成一维
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        x = F.softmax(x)


if __name__ == '__main__':

    net = testNet()
    summary(net, input_size=(3, 227, 227), batch_size=1, device='cuda')


keras版本

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten


img_shape = (227, 227, 3)  # inputsize 227*227*3

model = Sequential()
model.add(Conv2D(48, kernel_size=11, strides=4, input_shape=img_shape, padding="VALID"))  # VALID是不填充
# 因为观察网络参数与各层结构,这里忽略激活函数和BN操作
model.add(MaxPool2D(pool_size=(3, 3), strides=2))
model.add(Conv2D(128, kernel_size=5, strides=1, padding="SAME"))
# model.add(MaxPool2D(pool_size=(3, 3), strides=2))
# model.add(Conv2D(192, kernel_size=3, strides=1, padding="SAME"))
# model.add(Conv2D(192, kernel_size=3, strides=1, padding="SAME"))
# model.add(Conv2D(128, kernel_size=3, strides=1, padding="SAME"))
# model.add(MaxPool2D(pool_size=(3, 3), strides=2))
# model.add(Flatten())  # 相当于reshape
# model.add(Dense(2048))
# # 同样这里忽略dropout
# model.add(Dense(2048))
# model.add(Dense(1000, activation='softmax'))
model.summary()


应用到cifar10数据集上进行分类

模型训练

# 基于keras的Alexnet做cifar10数据集分类
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation
from tensorflow.keras.layers import MaxPool2D, Flatten, Dense, Dropout
import os
from matplotlib import pyplot as plt

cifar10 = tf.keras.datasets.cifar10
(x_train, y_trian), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train/255.0, x_test/255.0  # 归一化处理

# 完成了数据集的导入


class AlexNet8(Model):
    def __init__(self):
        super(AlexNet8, self).__init__()

        self.c1 = Conv2D(filters=96, kernel_size=(3, 3))
        self.b1 = BatchNormalization()  # 将每一个batch进行归一化
        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()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              # 稀疏分类交叉熵损失函数
              metrics=['sparse_categorical_accuracy'])
              # 系数分类准确率指标

checkpoint_save_path = "./checkpoint/AlexNet8.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_trian, batch_size=32, epochs=5, validation_data=(x_test, y_test),
                    validation_freq=1,  # 每一轮之后验证集验证
                    callbacks=[cp_callback]
                    )
# fit函数 返回的是history对象,history.history 记录损失函数和其他指标的数值变化(epoch)
# callback在GAN中不是很适合, save , save_weights

model.summary()

####
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()

应用分类

import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense
from tensorflow.keras import Model
# 模型存储路径
model_save_path = './checkpoint/AlexNet8.ckpt'


# 加载网络模型
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()
# 使用断点参数
model.load_weights(model_save_path)
# 利用已经训练好的网络模型

# cafir10数据集 32*32大小 3通道 即:32*32*3 输入特征需要batch*32*32*3
# 将图片加载为指定像素
test_image = image.load_img('马.jpg', target_size=(32, 32))
# 转换成array格式 32*32*3
test_image = image.img_to_array(test_image)
# 将3维array格式数据增加一维变成4维 与所需要输入一致
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image/255.
# 输入图片进行预测
prediction = model.predict(test_image)
#预测结果为独热码,符合概率分布,因此 选出最大概率
pred = tf.argmax(prediction, axis=1)  # 含batchsize的维度
tf.print("预测类别为", pred)



链接: B站视频教学课程.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plus_left

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值