如何用Vgg-16神经网络训练cifar-10

如何用Vgg-16神经网络训练cifar-10

由于vgg-16的输入是224* 224* 3,而cifar-10的输入是32* 32* 3(经转换后得到的)故应该对vgg-16模型进行修改

vgg-16架构

训练输入:固定尺寸224* 224的RGB图像。
预处理:每个像素值减去训练集上的RGB均值。
卷积核:一系列3* 3卷积核堆叠,步长为1,采用padding保持卷积后图像空间分辨率不变。
空间池化:紧随卷积“堆”的最大池化,为2*2滑动窗口,步长为2。
全连接层:特征提取完成后,接三个全连接层,前两个为4096通道,第三个为1000通道,最后是一个soft-max层,输出概率。所有隐藏层都用非线性修正ReLu
vgg-16模型图

对应cifar-10的vgg-16模型参数

这里我们只采用A方法,共11层
由于224* 224* 3的图像第一层的卷积核个数为64,所以等比缩放后32* 32* 3的图像第一层卷积核个数为16。
同理,后面第2,3,4,5层卷积核个数分别为32,64,128,128
由于vgg-16标准模型输出大小为1000,而cifar-10输出大小为10,所以将全连接三层分别设为100,40,10

修改后的vgg-16模型

import tensorflow as tf
import numpy as np
IMAGE_SIZE=32#每张图片分辨率
NUM_CHANNELS=3#输入图片通道数
vCONV_SIZE=3
vCONV_KERNEL_NUM=[None,16,32,64,128,128]
vFC_SIZE_1=100
vFC_SIZE_2=40
def get_weight(shape,regularizer):#带有正则化
    w=tf.Variable(tf.random_normal(shape=shape,stddev=0.1),dtype=tf.float32)
    if regularizer!=None :
        tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
    return w
def get_bias(shape):
    b=tf.Variable(tf.zeros(shape=shape))
    return b
def conv2d(x,w):
    return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")
  
def max_pool_2x2_pad0(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
#前向传播
def con_relu(x,index_ceng=int,regularizer=float,conv_time=int):
    conv=x
    for i in range(conv_time):
        shape_x=int(np.shape(conv)[-1])
        conv_w = get_weight(shape=(vCONV_SIZE, vCONV_SIZE,shape_x, vCONV_KERNEL_NUM[index_ceng]), regularizer=regularizer)
        conv_b = get_bias(shape=vCONV_KERNEL_NUM[index_ceng])
        conv=conv2d(conv,conv_w)
        conv=tf.nn.relu(tf.nn.bias_add(conv,conv_b))
    return conv
def forward(x,train,regularizer):
    conv1=con_relu(x,index_ceng=1,regularizer=regularizer,conv_time=1)
    pool1=max_pool_2x2_pad0(conv1)

    conv2=con_relu(pool1,index_ceng=2,regularizer=regularizer,conv_time=1)
    pool2=max_pool_2x2_pad0(conv2)

    conv3=con_relu(pool2,index_ceng=3,regularizer=regularizer,conv_time=2)
    pool3 = max_pool_2x2_pad0(conv3)

    conv4 = con_relu(pool3, index_ceng=4, regularizer=regularizer,conv_time=2)
    pool4 = max_pool_2x2_pad0(conv4)

    conv5 = con_relu(pool4, index_ceng=5, regularizer=regularizer,conv_time=2)
    pool5 = max_pool_2x2_pad0(conv5)

    pool_shape = pool5.get_shape().as_list()
    nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]  # 从 list 中依次取出矩阵的长宽及深度,并求三者的乘积,得到矩阵被拉长后的长度
    reshaped_x = tf.reshape(pool5, [pool_shape[0], nodes])  # 将 pool2 转换为一个 batch 的向量再传入后续的全连接

    fc1_w = get_weight([nodes, vFC_SIZE_1], regularizer)
    fc1_b = get_bias([vFC_SIZE_1])
    fc1 = tf.nn.relu(tf.matmul(reshaped_x, fc1_w) + fc1_b)
    if train: fc1 = tf.nn.dropout(fc1, 0.5)

    fc2_w = get_weight([vFC_SIZE_1, vFC_SIZE_2], regularizer)
    fc2_b = get_bias([vFC_SIZE_2])
    fc2 = tf.nn.relu(tf.matmul(fc1, fc2_w) + fc2_b)
    if train: fc2 = tf.nn.dropout(fc2, 0.5)

    fc3_w = get_weight([vFC_SIZE_2, OUTPUT_NOOD], regularizer)
    fc3_b = get_bias(OUTPUT_NOOD)
    y = tf.matmul(fc2, fc3_w) + fc3_b
    return y

训练结果

设batch_size=100,经20000次训练后,测试集准确率64%
经40000次训练后,测试集准确率81%

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很高兴听到你对基于深度学习的AI审图系统的设计与实现过程的兴趣。下面是对这个话题的完整性论述。 设计与实现过程 1. 数据准备模块 数据准备模块是AI审图系统的重要组成部分,用于收集和预处理CAD图纸数据。在数据准备模块中,需要进行以下步骤: - 收集CAD图纸数据:收集大量的CAD图纸数据,包括平面图、立体图、工程图等。 - 数据清洗:对收集到的数据进行清洗和去重,确保数据的质量和准确性。 - 数据预处理:对清洗后的数据进行预处理,包括图像缩放、裁剪、旋转等操作,以便于后续训练和测试。 2. 模型设计 在AI审图系统中,使用了VGG-16神经网络算法进行图像分类。VGG-16是一种深度卷积神经网络模型,由牛津大学视觉几何组(Visual Geometry Group)的研究者提出。其主要特点是采用了非常小的卷积核(3x3),但却有很深的网络结构,通过不断堆叠多个卷积层和池化层来实现图像的特征提取和分类。 模型设计的具体步骤如下: - 输入层:输入CAD图纸数据。 - 卷积层:使用3x3的卷积核进行特征提取。 - 池化层:使用最大池化方法进行下采样。 - 卷积层:再次使用3x3的卷积核进行特征提取。 - 池化层:再次使用最大池化方法进行下采样。 - 卷积层:第三次使用3x3的卷积核进行特征提取。 - 池化层:第三次使用最大池化方法进行下采样。 - 全连接层:将卷积层的输出展开成一维向量,并进行全连接操作。 - 输出层:输出图像分类结果。 使用TensorFlow框架可以方便地实现VGG-16神经网络算法。 3. 训练和测试 在训练和测试阶段,需要进行以下步骤: - 划分数据集:将数据集划分为训练集和测试集。 - 数据增强:对训练集进行数据增强,包括随机旋转、平移、翻转等操作,增加模型的鲁棒性和泛化能力。 - 训练模型:使用训练训练模型,计算损失函数并进行反向传播更新模型参数。 - 测试模型:使用测试集测试模型的准确率和效果。 - 调整模型:根据测试结果调整模型的超参数和结构,提高模型的准确率和效果。 代码实现 以下是使用TensorFlow框架和VGG-16神经网络算法实现AI审图系统的部分代码: ```python import tensorflow as tf from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense # 定义VGG-16神经网络模型 def vgg16_model(input_shape=(224, 224, 3), num_classes=10): model = tf.keras.Sequential() model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same', input_shape=input_shape)) model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', padding='same')) model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dense(4096, activation='relu')) model.add(Dense(num_classes, activation='softmax')) return model # 加载数据集并进行预处理 (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) # 创建VGG-16神经网络模型并进行训练和测试 model = vgg16_model() model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=64, epochs=10, validation_data=(x_test, y_test)) ``` 以上就是基于深度学习的AI审图系统的设计与实现过程的完整性论述及代码示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值