Tensorflow2.0 VGG13与cifar100

VGG13 网络
在这里插入图片描述

具体代码实现 准确率 37%左右

"""
    cifar100数据集处理
    使用 VGG13 网络层
"""
import os

import tensorflow as tf
from tensorflow.keras import layers, Sequential, datasets, optimizers

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
tf.random.set_seed(2345)

# GPU设置
gpu_lst = tf.config.experimental.list_physical_devices("GPU")
print("GPU:{}个".format(len(gpu_lst)))

for gpu in gpu_lst:
    tf.config.experimental.set_memory_growth(gpu, True)


# 1.load data_sets   数据集的加载
def pre_process(x_data, y_data):
    # 数据范围 (0-255) ==> (0-1])
    x_data = tf.cast(x_data, dtype=tf.float32) / 255.
    # 数据类型 tf.int32  y:(0-99)
    y_data = tf.cast(y_data, dtype=tf.int32)
    return x_data, y_data


# 加载默认数据集:cifar-100
(x, y), (x_test, y_test) = datasets.cifar100.load_data()
# y (50000,1) ==>(50000,)
y = tf.squeeze(y, axis=1)

# y_test (10000,1) ==>(10000,)
y_test = tf.squeeze(y_test, axis=1)

print(x.shape, y.shape, x_test.shape, y_test.shape)

# 训练集 dataset 数据集生成(做简单的预处理 + batch)
train_db = tf.data.Dataset.from_tensor_slices((x, y))
train_db = train_db.shuffle(1000).map(pre_process).batch(128)

# 测试集 dataset 数据集生成(做简单的预处理 + batch)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_db = test_db.map(pre_process).batch(64)

# 数据维度确认
sample = next(iter(train_db))
# x,y的维度   x的最小值与最大值
print("sample:", sample[0].shape, sample[1].shape,
      tf.reduce_min(sample[0]), tf.reduce_max(sample[0]))

# 2.build network 构建网络结构与创建

vgg13 = [  # 5 unit of conf + max pooling
    # unit 1 [b, 32, 32, 3]
    layers.Conv2D(64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.Conv2D(64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="same"),
    # [b, 16, 16, 64]

    # unit 2
    layers.Conv2D(128, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.Conv2D(128, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="same"),
    # [b, 8, 8, 128]

    # unit 3
    layers.Conv2D(256, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.Conv2D(256, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="same"),
    # [b, 4, 4, 256]

    # unit 4
    layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="same"),
    # [b, 2, 2, 512]

    # unit 5
    layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.Conv2D(512, kernel_size=[3, 3], padding="same", activation=tf.nn.relu),
    layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="same"),
    # [b, 1, 1, 512]

    # [b, 1, 1, 512] ==> [b, 512]
    layers.Flatten(),

    # 全连接
    # [b, 512] ==> [b, 512]
    layers.Dense(512, activation=tf.nn.relu),
    layers.Dropout(0.5),

    # [b, 256] ==> [b, 256]
    layers.Dense(256, activation=tf.nn.relu),
    layers.Dropout(0.5),

    # [b, 256] ==> [b, 100]
    layers.Dense(100, activation=None)
]


def main():
    # 创建网络结构
    vgg13_net = Sequential(vgg13)
    # x = tf.random.normal([4, 32, 32, 3])

    # vgg13 网络的创建
    vgg13_net.build(input_shape=[None, 32, 32, 3])
    # out = vgg13_net(x)
    # print(out.shape)

    # 设置优化器
    optimizer = optimizers.Adam(lr=1e-4)
    # 设置可训练参数
    variables = vgg13_net.trainable_variables

    # 3.Train  循环训练
    for epoch in range(50):
        for step, (x, y) in enumerate(train_db):

            with tf.GradientTape() as tape:
                # 前向传播
                # [b, 32, 32,3] => [b, 100]
                y_prd = vgg13_net(x)

                # [b,] => [b, 100]
                y_true = tf.one_hot(y, depth=100)

                # 损失函数计算
                loss = tf.losses.categorical_crossentropy(y_true, y_prd, from_logits=True)
                loss = tf.reduce_mean(loss)
            # 梯度计算
            grads = tape.gradient(loss, variables)
            # 优化器更新参数
            optimizer.apply_gradients(zip(grads, variables))

            # 打印结果
            if step % 100 == 0:
                print(epoch, step, "loss:", float(loss))

        # 4.Test 计算正确率
        total_num = 0
        total_correct = 0
        for x, y in test_db:
            # 前向传播
            y_prd = vgg13_net(x)
            prob = tf.nn.softmax(y_prd, axis=1)
            pred = tf.argmax(prob, axis=1)
            pred = tf.cast(pred, dtype=tf.int32)

            # 计算正确数
            # [b, bool] ==> [b, int32]
            correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
            correct = tf.reduce_sum(correct)

            # 累加总数与正确数
            total_num += x.shape[0]
            total_correct += int(correct)

        # 计算正确率
        acc = total_correct / total_num
        print(epoch, "acc:", acc)


if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

廷益--飞鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值