Tensorflow2.0 Resnet18与cifar100

Resnet结构
Tensorflow2.0 Resnet18与cifar100_第1张图片
详细结构

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

网络结构代码:

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


# 基础卷积单元网络
# conv + bn + relu
class BasicBlock(layers.Layer):
    def __init__(self, filter_num, stride=1):
        """
        :param filter_num: 卷积核的个数
        :param stride: 步长
        """
        super(BasicBlock, self).__init__()

        # 卷积层 1 可能会做 下采样
        self.conv1 = layers.Conv2D(filter_num, (3, 3),
                                   strides=stride, padding="same")
        self.bn1 = layers.BatchNormalization()
        self.relu = layers.Activation("relu")

        # 卷积层2 不做下采样
        self.conv2 = layers.Conv2D(filter_num, (3, 3),
                                   strides=1, padding="same")
        self.bn2 = layers.BatchNormalization()

        if stride != 1:  # 是否使用 下采样
            # 下采样
            self.down_sample = Sequential()
            # 可能会做下采样
            self.down_sample.add(layers.Conv2D(filter_num, (1, 1), strides=stride))
            self.down_sample.add(layers.BatchNormalization())
        else:
            # 不做下采样
            self.down_sample = lambda x: x

        self.stride = stride  # 步长

    def call(self, inputs, training=None):
        # 残差边 根据stride(下采样 或 不做处理)
        identity = self.down_sample(inputs)

        # 卷积边
        # 层1 卷积 + 正则化 + relu层
        out = self.conv1(inputs)  # 根据stride(下采样 或 不做处理)
        out = self.bn1(out)
        out = self.relu(out)

        # 层2 卷积 + 正则化
        out = self.conv2(out)
        out = self.bn2(out)

        # 卷积边 + 残差边
        out_put = layers.add([out, identity])
        # relu 函数
        out_put = tf.nn.relu(out_put)

        return out_put


# ResNet 网络的创建
class ResNet(tf.keras.Model):
    def __init__(self, layer_dims, num_classes=100):  # [2, 2, 2, 2]
        # layer_dims 层的维度  18:[2 * res_block, 2 * res_block, 2 * res_block, 2 * res_block]
        # 34:[3, 4, 6, 3]
        super(ResNet, self).__init__()
        self.stem = Sequential([layers.Conv2D(64, (3, 3), strides=(1, 1)),
                                layers.BatchNormalization(),
                                layers.Activation("relu"),
                                layers.MaxPool2D(pool_size=(2, 2), strides=(1, 1), padding="same")
                                ])
        # 2个block
        self.layer1 = self.build_res_block(64, layer_dims[0])
        # 2个block 下采样一次
        self.layer2 = self.build_res_block(128, layer_dims[1], stride=2)
        # 2个block 下采样一次
        self.layer3 = self.build_res_block(256, layer_dims[2], stride=2)
        # 2个block 下采样一次
        self.layer4 = self.build_res_block(512, layer_dims[3], stride=2)

        # output:[b, 512, h, w] ==> 全局均值池化
        self.avg_pool = layers.GlobalAveragePooling2D()
        # 全链接层
        self.fc = layers.Dense(num_classes)

    def call(self, inputs, training=None):
        # 前向运算
        # 初步处理
        x = self.stem(inputs)
        # 四层的叠加
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        # [b, c] 全局均值池化
        x = self.avg_pool(x)
        # [b, 100] 全连接层
        x = self.fc(x)
        return x

    # 基本单元块
    @staticmethod
    def build_res_block(filter_num, blocks, stride=1):
        res_blocks = Sequential()

        # 可能下采样
        res_blocks.add(BasicBlock(filter_num, stride))

        for _ in range(1, blocks):
            # 不做下采样
            res_blocks.add(BasicBlock(filter_num, stride=1))
        return res_blocks


# resnet 18 层
def resnet18():
    return ResNet([2, 2, 2, 2])


# resnet 34 层
def resnet34():
    return ResNet([3, 4, 6, 3])

网络训练与测试

"""
    cifar100数据集处理
    使用 Resnet18、Resnet34 网络层
"""
import os

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

from resnet import resnet18

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)  # GPU自增长


# 1.load data_sets   数据集的加载
def pre_process(x_data, y_data):
    # 数据范围 (0-255) ==> (0-1])
    x_data = 2 * tf.cast(x_data, dtype=tf.float32) / 255. - 1
    # 数据类型 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(10000).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 构建网络结构与创建
def main():
    # 创建网络结构
    model = resnet18()

    # x = tf.random.normal([4, 32, 32, 3])
    # resnet18 网络的创建
    model.build(input_shape=(None, 32, 32, 3))

    # 设置优化器
    optimizer = optimizers.Adam(lr=1e-4)
    model.summary()

    # 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 = model(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, model.trainable_variables)
            # 优化器更新参数
            optimizer.apply_gradients(zip(grads, model.trainable_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 = model(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()

你可能感兴趣的:(TensorFlow)