(附代码)ResNet网络架构搭建以及基于数据集CIFAR-10对ResNet进行训练

本文介绍了ResNet网络架构,并通过CIFAR-10数据集进行训练,详细讲解了数据集下载、预训练权重获取及ResNet的两种残差结构。实操部分展示了一个resnet-20模型的训练过程,达到了预期的精度。
摘要由CSDN通过智能技术生成

 ​​​​​​为什么ResNet深度残差网络广受好评呢?理论部分参考

一、数据集下载

CIFAR-10 and CIFAR-100 datasets (toronto.edu)

二、预训练权重下载

'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',

 三、Demo介绍

构造一个resnet-20,在cifar-10上参考论文中的设置进行训练,达到论文给出的精度。

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


if __name__ == "__main__":

    # config
    train_dir = os.path.join("..", "data", "cifar-10",  "cifar10_train")
    test_dir = os.path.join("..", "data", "cifar-10", "cifar10_test")

    now_time = datetime.now()
    time_str = datetime.strftime(now_time, '%m-%d_%H-%M')
    log_dir = os.path.join(BASE_DIR, "..", "results", time_str)
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    class_names = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

    num_classes = 10

    MAX_EPOCH = 182     # 182     # 64000 / (45000 / 128) = 182 epochs
    BATCH_SIZE = 128
    LR = 0.1
    log_interval = 1
    val_interval = 1
    start_epoch = -1
    milestones = [92, 136]  # divide it by 10 at 32k and 48k iterations

    # ============================ step 1/5 数据 ============================
    norm_mean = [0.485, 0.456, 0.406]
    norm_std = [0.229, 0.224, 0.225]

    train_transform = transforms.Compose([
        transforms.Resize(32),
        transforms.RandomHorizontalFlip(p=0.5),
        transforms.RandomCrop(32, padding=4),
        transforms.ToTensor(),
        transforms.Normalize(norm_mean, norm_std),
    ])

    valid_transform = transforms.Compose([
        transforms.Resize((32, 32)),
        transforms.ToTensor(),
        transforms.Normalize(norm_mean, norm_std),
    ])

    # 构建MyDataset实例
    train_data = CifarDataset(data_dir=train_dir, transform=train_transform)
    valid_data = CifarDataset(data_dir=test_dir, transform=valid_transform)

    # 构建DataLoder
    train_loader = DataLoader(dataset=train_data, batch_size=BATCH_SIZ
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个使用ResNet50模型在CIFAR-10数据集进行训练的示例代码,你可以在此基础上进行修改和调整: ``` import tensorflow as tf from tensorflow.keras.datasets import cifar10 from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, GlobalAveragePooling2D, Add, Dense, Activation, BatchNormalization from tensorflow.keras.models import Model from tensorflow.keras.regularizers import l2 # 加载数据集 (x_train, y_train), (x_test, y_test) = cifar10.load_data() # 数据预处理 x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float32') / 255 y_train = tf.keras.utils.to_categorical(y_train, 10) y_test = tf.keras.utils.to_categorical(y_test, 10) # 数据增强 datagen_train = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True ) datagen_train.fit(x_train) # 定义ResNet50模型 def resnet_block(inputs, filters, strides=1): x = Conv2D(filters, kernel_size=3, strides=strides, padding='same', kernel_regularizer=l2(1e-4))(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters, kernel_size=3, strides=1, padding='same', kernel_regularizer=l2(1e-4))(x) x = BatchNormalization()(x) if strides != 1 or inputs.shape[3] != filters: inputs = Conv2D(filters, kernel_size=1, strides=strides, padding='same', kernel_regularizer=l2(1e-4))(inputs) inputs = BatchNormalization()(inputs) x = Add()([inputs, x]) x = Activation('relu')(x) return x inputs = Input(shape=(32, 32, 3)) x = Conv2D(64, kernel_size=3, strides=1, padding='same', kernel_regularizer=l2(1e-4))(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) x = resnet_block(x, 64) x = resnet_block(x, 64) x = resnet_block(x, 128, strides=2) x = resnet_block(x, 128) x = resnet_block(x, 256, strides=2) x = resnet_block(x, 256) x = GlobalAveragePooling2D()(x) outputs = Dense(10, activation='softmax')(x) model = Model(inputs=inputs, outputs=outputs) # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(datagen_train.flow(x_train, y_train, batch_size=128), steps_per_epoch=x_train.shape[0] // 128, epochs=50, validation_data=(x_test, y_test)) ``` 在上面的代码中,我们使用了ResNet50模型,并在模型的基础上构建了一个CIFAR-10分类器。我们还使用了数据增强来增加模型的鲁棒性,并使用了L2正则化来避免过拟合。最后,我们使用Adam优化器来训练模型,并在50个epoch后进行评估。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码农男孩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值