本文基于PaddlePaddle2.0构建残差神经网络模型,解析ResNet原理,包括b1到b5各模块结构与数据维度变化。以CIFAR图像识别为例,用基础API实现模型训练与评估,设置100个轮次、批量大小512等参数,最终测试数据集准确率约80.3%,呈现了训练过程中的损失与准确度变化。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

作者:陆平
残差网络(ResNet)模型是由何凯明开发,它是2015年ImageNet ILSVRC-2015分类挑战赛的冠军模型。ResNet模型引入残差模块,它能够有效地消除由于模型层数增加而导致的梯度弥散或梯度爆炸问题。下面详细解析ResNet模型原理。

模块b1。多样本视角,设批量大小为m,输入数据维度大小为Rm×3×96×96。卷积层输入通道3,输出通道64,卷积核大小7×7,移动步长2,填充0的数量3。计算得出输出行列大小为(96-7+2×3)/2+1=48.5,卷积层输出大小向下取整得48,输出数据维度为Rm×64×48×48。之后进行批量归一化、激活层处理。最后,接最大池化层,其过滤器大小3×3,移动步长2,填充0的数量1。计算得出输出行列大小为(48-3+2×1)/2+1=24.5,池化层输出大小向下取整得24,输出数据维度大小为Rm×64×24×24。
模块b2是残差模块,包含两阶段残差层。输入数据维度大小为Rm×64×24×24。接下来,经过第一个残差层。在每个残差模块中,都依次经过卷积层、批量归一化、激活层、卷积层、批量归一化层,得到输出数据维度大小为Rm×64×24×24。该输出数据与该模块原始输入数据相加后,作为第二个残差层的输入,与第一个残差层类似,得到输出数据维度大小为Rm×64×24×24。
模块b3是残差模块,包含两阶段残差层。输入数据维度大小为Rm×64×24×24。接下来,经过第一个残差层,得到输出数据维度大小为Rm×128×12×12。由于通道数改变了,导致该输入数据维度大小与该输出不一致,不能直接相加。解决方法是用一个1×1的卷积层,对输入数据做变换,使其通道数拓展到128。之后两者进行相加,作为第二阶段残差层的输入,最终得到输出数据维度大小为Rm×128×12×12。
美发造型设计网站源码是基于易优cms开发,非常适合美发造型企业通过网络拓展业务、 程序内核为Thinkphp5.0开发,后台简洁,为企业网站而生。 这是一套安装就能建站的程序,不定期更新程序BUG,更新网站功能。 我们提供的不仅是模板这么简单,我们还提供程序相关咨询、协助安装等服务。 默认不包含小程序插件,需要另外单独购买插件。 ★模板安装步骤★ 1、请
0
模块b4与b5都是残差模块,与模块b3类似。每个残差模块使得行列维大小减小一半,通道数增长一倍。模块b5的最终输出数据维度大小为Rm×512×3×3。接着,通过平均池化层、线性层,输出特征数量为10。
上述便是ResNet模型的正向传播过程,接着随机化初始参数,然后便可以计算出每层的结果,每次将得到m×10的矩阵作为预测结果,其中m是小批量样本数。接下来进行反向传播过程,预测结果与真实结果之间肯定存在差异,以缩减该差异作为目标,计算模型参数梯度。进行多轮迭代,便可以优化模型,使得预测结果与真实结果之间更加接近。
采用PaddlePaddle2.0基础API,进行模型训练与评估。
import paddleimport paddle.nn.functional as Fimport numpy as npfrom paddle.vision.transforms import Compose, Resize, Transpose, Normalize#准备数据t = Compose([Resize(size=96),Normalize(mean=[127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5], data_format='HWC'),Transpose()]) #数据转换cifar10_train = paddle.vision.datasets.cifar.Cifar10(mode='train', transform=t, backend='cv2')
cifar10_test = paddle.vision.datasets.cifar.Cifar10(mode="test", transform=t, backend='cv2')#构建模型class Residual(paddle.nn.Layer):
def __init__(self, in_channel, out_channel, use_conv1x1=False, stride=1):
super(Residual, self).__init__()
self.conv1 = paddle.nn.Conv2D(in_channel, out_channel, kernel_size=3, padding=1, stride=stride)
self.conv2 = paddle.nn.Conv2D(out_channel, out_channel, kernel_size=3, padding=1) if use_conv1x1: #使用1x1卷积核
self.conv3 = paddle.nn.Conv2D(in_channel, out_channel, kernel_size=1, stride=stride) else:
self.conv3 = None
self.batchNorm1 = paddle.nn.BatchNorm2D(out_channel)
self.batchNorm2 = paddle.nn.BatchNorm2D(out_channel) def forward(self, x):
y = F.relu(self.batchNorm1(self.conv1(x)))
y = self.batchNorm2(self.conv2(y)) if self.conv3:
x = self.conv3(x)
out = F.relu(y+x) #核心代码
return outdef ResNetBlock(in_channel, out_channel, num_layers, is_first=False):
if is_first: assert in_channel == out_channel
block_list = [] for i in range(num_layers): if i == 0 and not is_first:
block_list.append(Residual(in_channel, out_channel, use_conv1x1=True, stride=2)) else:
block_list.append(Residual(out_channel, out_channel))
resNetBlock = paddle.nn.Sequential(*block_list) #用*号可以把list列表展开为元素
return resNetBlockclass ResNetModel(paddle.nn.Layer):
def __init__(self):
super(ResNetModel, self).__init__()
self.b1 = paddle.nn.Sequential(
paddle.nn.Conv2D(3, 64, kernel_size=7, stride=2, padding=3),
paddle.nn.BatchNorm2D(64),
paddle.nn.ReLU(),
paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1))
self.b2 = ResNetBlock(64, 64, 2, is_first=True)
self.b3 = ResNetBlock(64, 128, 2)
self.b4 = ResNetBlock(128, 256, 2)
self.b5 = ResNetBlock(256, 512, 2)
self.AvgPool = paddle.nn.AvgPool2D(2)
self.flatten = paddle.nn.Flatten()
self.Linear = paddle.nn.Linear(512, 10)
def forward(self, x):
x = self.b1(x)
x = self.b2(x)
x = self.b3(x)
x = self.b4(x)
x = self.b5(x)
x = self.AvgPool(x)
x = self.flatten(x)
x = self.Linear(x) return x
epoch_num = 100batch_size = 512learning_rate = 0.001val_acc_history = []
val_loss_history = []def train(model):
#开启训练模式
model.train() #优化器
opt = paddle.optimizer.Adam(learning_rate=learning_rate, parameters=model.parameters()) #数据小批量加载器
train_loader = paddle.io.DataLoader(cifar10_train, shuffle=True, batch_size=batch_size)
valid_loader = paddle.io.DataLoader(cifar10_test, batch_size=batch_size) for epoch in range(epoch_num): for batch_id, data in enumerate(train_loader()):
x_data = paddle.cast(data[0], 'float32')
y_data = paddle.cast(data[1], 'int64')
y_data = paddle.reshape(y_data, (-1, 1))
y_predict = model(x_data)
loss = F.cross_entropy(y_predict, y_data)
loss.backward()
opt.step()
opt.clear_grad() print("训练轮次: {}; 损失: {}".format(epoch, loss.numpy())) #启动评估模式
model.eval()
accuracies = []
losses = [] for batch_id, data in enumerate(valid_loader()):
x_data = paddle.cast(data[0], 'float32')
y_data = paddle.cast(data[1], 'int64')
y_data = paddle.reshape(y_data, (-1, 1))
y_predict = model(x_data)
loss = F.cross_entropy(y_predict, y_data)
acc = paddle.metric.accuracy(y_predict, y_data)
accuracies.append(np.mean(acc.numpy()))
losses.append(np.mean(loss.numpy()))
avg_acc, avg_loss = np.mean(accuracies), np.mean(losses) print("评估准确度为:{};损失为:{}".format(avg_acc, avg_loss))
val_acc_history.append(avg_acc)
val_loss_history.append(avg_loss)
model.train()
model = ResNetModel()
train(model)训练轮次: 0; 损失: [1.3326287] 评估准确度为:0.4590533375740051;损失为:1.4742478132247925 训练轮次: 1; 损失: [1.1216755] 评估准确度为:0.6050781011581421;损失为:1.1032450199127197 训练轮次: 2; 损失: [0.7329699] 评估准确度为:0.6641716361045837;损失为:0.9522638320922852 训练轮次: 3; 损失: [0.6840582] 评估准确度为:0.7174115180969238;损失为:0.8360720872879028 训练轮次: 4; 损失: [0.54061747] 评估准确度为:0.7190716862678528;损失为:0.8681131601333618 训练轮次: 5; 损失: [0.3360296] 评估准确度为:0.7193589210510254;损失为:0.9287630319595337 训练轮次: 6; 损失: [0.23328641] 评估准确度为:0.7237706780433655;损失为:1.0101532936096191 训练轮次: 7; 损失: [0.18659413] 评估准确度为:0.7138327360153198;损失为:1.2213765382766724 训练轮次: 8; 损失: [0.09929246] 评估准确度为:0.7494600415229797;损失为:1.1099485158920288 训练轮次: 9; 损失: [0.14050105] 评估准确度为:0.7417279481887817;损失为:1.1338616609573364 训练轮次: 10; 损失: [0.06412333] 评估准确度为:0.7404871582984924;损失为:1.1939129829406738 训练轮次: 11; 损失: [0.08688222] 评估准确度为:0.75390625;损失为:1.1125857830047607 训练轮次: 12; 损失: [0.0359116] 评估准确度为:0.757192075252533;损失为:1.1698567867279053 训练轮次: 13; 损失: [0.0870579] 评估准确度为:0.7476849555969238;损失为:1.366117238998413 训练轮次: 14; 损失: [0.0549598] 评估准确度为:0.7482019662857056;损失为:1.310349941253662 训练轮次: 15; 损失: [0.05735493] 评估准确度为:0.7535960674285889;损失为:1.3269926309585571 训练轮次: 16; 损失: [0.09850415] 评估准确度为:0.750620424747467;损失为:1.3456439971923828 训练轮次: 17; 损失: [0.03356194] 评估准确度为:0.7497989535331726;损失为:1.3665852546691895 训练轮次: 18; 损失: [0.05458164] 评估准确度为:0.7720817923545837;损失为:1.2095129489898682 训练轮次: 19; 损失: [0.01949521] 评估准确度为:0.7599666714668274;损失为:1.3035249710083008 训练轮次: 20; 损失: [0.06806032] 评估准确度为:0.7588120698928833;损失为:1.3639148473739624 训练轮次: 21; 损失: [0.06323621] 评估准确度为:0.7355066537857056;损失为:1.5126835107803345 训练轮次: 22; 损失: [0.03693628] 评估准确度为:0.7480928301811218;损失为:1.3733327388763428 训练轮次: 23; 损失: [0.03756324] 评估准确度为:0.7719094753265381;损失为:1.2504770755767822 训练轮次: 24; 损失: [0.02467982] 评估准确度为:0.7637293338775635;损失为:1.3366872072219849 训练轮次: 25; 损失: [0.02468643] 评估准确度为:0.7613741159439087;损失为:1.429756760597229 训练轮次: 26; 损失: [0.05002661] 评估准确度为:0.7543255686759949;损失为:1.4312909841537476 训练轮次: 27; 损失: [0.04115548] 评估准确度为:0.7695772051811218;损失为:1.3264986276626587 训练轮次: 28; 损失: [0.04184989] 评估准确度为:0.7620978951454163;损失为:1.369117021560669 训练轮次: 29; 损失: [0.04189521] 评估准确度为:0.7739487886428833;损失为:1.3266767263412476 训练轮次: 30; 损失: [0.03440252] 评估准确度为:0.759978175163269;损失为:1.3931536674499512 训练轮次: 31; 损失: [0.02010881] 评估准确度为:0.7735006809234619;损失为:1.338533878326416 训练轮次: 32; 损失: [0.01580902] 评估准确度为:0.7678825855255127;损失为:1.3710412979125977 训练轮次: 33; 损失: [0.04431158] 评估准确度为:0.7515624761581421;损失为:1.6030147075653076 训练轮次: 34; 损失: [0.03763511] 评估准确度为:0.7640222907066345;损失为:1.364061713218689 训练轮次: 35; 损失: [0.03567011] 评估准确度为:0.7590130567550659;损失为:1.4065929651260376 训练轮次: 36; 损失: [0.01388878] 评估准确度为:0.7679170370101929;损失为:1.4347902536392212 训练轮次: 37; 损失: [0.02687895] 评估准确度为:0.7690774202346802;损失为:1.4978601932525635 训练轮次: 38; 损失: [0.02075532] 评估准确度为:0.7591624855995178;损失为:1.4528758525848389 训练轮次: 39; 损失: [0.01875045] 评估准确度为:0.7669117450714111;损失为:1.355019450187683 训练轮次: 40; 损失: [0.0064705] 评估准确度为:0.7734891772270203;损失为:1.3700499534606934 训练轮次: 41; 损失: [0.01330751] 评估准确度为:0.7698012590408325;损失为:1.425915241241455 训练轮次: 42; 损失: [0.03956186] 评估准确度为:0.7599437236785889;损失为:1.536435842514038 训练轮次: 43; 损失: [0.03068891] 评估准确度为:0.7469956278800964;损失为:1.6034832000732422 训练轮次: 44; 损失: [0.01835285] 评估准确度为:0.7724953889846802;损失为:1.340801477432251 训练轮次: 45; 损失: [0.01561133] 评估准确度为:0.7809857130050659;损失为:1.2841508388519287 训练轮次: 46; 损失: [0.01947627] 评估准确度为:0.7819451093673706;损失为:1.3678362369537354 训练轮次: 47; 损失: [0.02647749] 评估准确度为:0.7691923379898071;损失为:1.4210023880004883 训练轮次: 48; 损失: [0.01650176] 评估准确度为:0.7629939913749695;损失为:1.425512671470642 训练轮次: 49; 损失: [0.01542593] 评估准确度为:0.7715820074081421;损失为:1.3973335027694702 训练轮次: 50; 损失: [0.00650754] 评估准确度为:0.7742761969566345;损失为:1.3708832263946533 训练轮次: 51; 损失: [0.00565967] 评估准确度为:0.7794175148010254;损失为:1.3928792476654053 训练轮次: 52; 损失: [0.00942376] 评估准确度为:0.7784179449081421;损失为:1.3634105920791626 训练轮次: 53; 损失: [0.00752388] 评估准确度为:0.7575885057449341;损失为:1.567503571510315 训练轮次: 54; 损失: [0.02825819] 评估准确度为:0.7664521932601929;损失为:1.4116448163986206 训练轮次: 55; 损失: [0.00855143] 评估准确度为:0.7782341241836548;损失为:1.3090441226959229 训练轮次: 56; 损失: [0.00782354] 评估准确度为:0.7653435468673706;损失为:1.4690921306610107 训练轮次: 57; 损失: [0.01815745] 评估准确度为:0.7784754037857056;损失为:1.4090263843536377 训练轮次: 58; 损失: [0.00961577] 评估准确度为:0.7755916714668274;损失为:1.47201406955719 训练轮次: 59; 损失: [0.05393994] 评估准确度为:0.7783433198928833;损失为:1.4245222806930542 训练轮次: 60; 损失: [0.02854589] 评估准确度为:0.7649586200714111;损失为:1.3377867937088013 训练轮次: 61; 损失: [0.00748871] 评估准确度为:0.7761489152908325;损失为:1.3369197845458984 训练轮次: 62; 损失: [0.00902835] 评估准确度为:0.781410813331604;损失为:1.3381050825119019 训练轮次: 63; 损失: [0.00117432] 评估准确度为:0.786293625831604;损失为:1.3492186069488525 训练轮次: 64; 损失: [0.00052344] 评估准确度为:0.7870117425918579;损失为:1.36197030544281 训练轮次: 65; 损失: [0.00678393] 评估准确度为:0.7863568067550659;损失为:1.4020657539367676 训练轮次: 66; 损失: [0.00126863] 评估准确度为:0.7841280102729797;损失为:1.4536049365997314 训练轮次: 67; 损失: [0.03560021] 评估准确度为:0.7798598408699036;损失为:1.4941426515579224 训练轮次: 68; 损失: [0.05929096] 评估准确度为:0.7748161554336548;损失为:1.411068081855774 训练轮次: 69; 损失: [0.05546713] 评估准确度为:0.7791992425918579;损失为:1.2746384143829346 训练轮次: 70; 损失: [0.01417406] 评估准确度为:0.7844669222831726;损失为:1.2911776304244995 训练轮次: 71; 损失: [0.00256652] 评估准确度为:0.793732762336731;损失为:1.2809799909591675 训练轮次: 72; 损失: [0.00282597] 评估准确度为:0.7955020666122437;损失为:1.306762933731079 训练轮次: 73; 损失: [0.0001233] 评估准确度为:0.7959156632423401;损失为:1.3295379877090454 训练轮次: 74; 损失: [0.00039457] 评估准确度为:0.7994083166122437;损失为:1.3363674879074097 训练轮次: 75; 损失: [3.121078e-05] 评估准确度为:0.801056981086731;损失为:1.3237804174423218 训练轮次: 76; 损失: [4.7259244e-05] 评估准确度为:0.8009478449821472;损失为:1.3236677646636963 训练轮次: 77; 损失: [1.1523691e-05] 评估准确度为:0.8017405271530151;损失为:1.3291720151901245 训练轮次: 78; 损失: [2.4585825e-05] 评估准确度为:0.8019358515739441;损失为:1.3325073719024658 训练轮次: 79; 损失: [2.6329966e-05] 评估准确度为:0.801838219165802;损失为:1.335172414779663 训练轮次: 80; 损失: [1.89953e-05] 评估准确度为:0.8028894662857056;损失为:1.3406250476837158 训练轮次: 81; 损失: [4.441399e-06] 评估准确度为:0.8025103807449341;损失为:1.3418700695037842 训练轮次: 82; 损失: [6.680777e-05] 评估准确度为:0.8023897409439087;损失为:1.3457533121109009 训练轮次: 83; 损失: [1.0911388e-05] 评估准确度为:0.8007410168647766;损失为:1.3459317684173584 训练轮次: 84; 损失: [9.577764e-06] 评估准确度为:0.8033777475357056;损失为:1.3521344661712646 训练轮次: 85; 损失: [6.795585e-06] 评估准确度为:0.802033543586731;损失为:1.356247067451477 训练轮次: 86; 损失: [1.496008e-05] 评估准确度为:0.801349937915802;损失为:1.3526151180267334 训练轮次: 87; 损失: [5.9306726e-06] 评估准确度为:0.8025103807449341;损失为:1.356006383895874 训练轮次: 88; 损失: [1.2822947e-05] 评估准确度为:0.8020967245101929;损失为:1.3611797094345093 训练轮次: 89; 损失: [8.149354e-06] 评估准确度为:0.8018267750740051;损失为:1.3598219156265259 训练轮次: 90; 损失: [1.4268601e-05] 评估准确度为:0.802326500415802;损失为:1.3667875528335571 训练轮次: 91; 损失: [1.2892309e-05] 评估准确度为:0.8028033375740051;损失为:1.3686493635177612 训练轮次: 92; 损失: [6.669232e-06] 评估准确度为:0.8027056455612183;损失为:1.3723241090774536 训练轮次: 93; 损失: [7.607553e-06] 评估准确度为:0.8033663034439087;损失为:1.3740990161895752 训练轮次: 94; 损失: [1.16983365e-05] 评估准确度为:0.8035615682601929;损失为:1.3803668022155762 训练轮次: 95; 损失: [3.6170006e-06] 评估准确度为:0.8029124140739441;损失为:1.3799207210540771 训练轮次: 96; 损失: [7.055238e-06] 评估准确度为:0.8024011850357056;损失为:1.385083794593811 训练轮次: 97; 损失: [5.3253243e-06] 评估准确度为:0.8032800555229187;损失为:1.3880302906036377 训练轮次: 98; 损失: [4.7645985e-06] 评估准确度为:0.8036476969718933;损失为:1.386351227760315 训练轮次: 99; 损失: [4.40941e-06] 评估准确度为:0.8033663034439087;损失为:1.3908065557479858
经过100轮epoch训练,模型在测试数据集上的准确率大约为80.3%。
以上就是基于PaddlePaddle2.0-构建残差网络模型的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号