EDSR遇到的问题以及解答

本文详细解析了PyTorch中加载预训练模型时出现的RuntimeError,特别是关于模型参数与预训练权重维度不匹配的问题。文章提供了具体的解决方案,包括如何调整模型的resblocks数量、feats数量及res_scale等参数,确保模型结构与预训练权重一致,避免加载失败。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python main.py --data_test Demo --scale 2 --pre_train …/experiment/model/EDSR_x2.pt --test_only --save_results
RuntimeError: While copying the parameter named head.0.weight, whose dimensions in the model are torch.Size([64, 3, 3, 3]) and whose dimensions in the checkpoint are torch.Size([256, 3, 3, 3]).
这是因为PyTorch在设置检查点时不保存模型结构。
这意味着预先训练的模型只包含参数,而不包含结构。
因此,我们必须首先构建模型来加载这些参数。
默认设置是EDSR_baseline_x4,每个层有16个resblock和64个通道。
如果您没有指定这些参数,就会发生错误,因为您试图从具有32个resblock和每个层256个通道的模型加载参数。
You have to put additional arguments(–n_resblocks, --n_feats, and --res_scale) to test that model.
Please refer to this line!
Also, I recommend you to use --chop argument if there is any trouble with GPU memory.
Thank you.

所以要

python main.py --data_test DIV2K --ext img --n_val 100 --scale 4 --n_resblocks 32 --n_feats 256 --res_scale 0.1 --pre_train ../experiment/model/EDSR_x4.pt --test_only --self_ensemble
### EDSR 模型实现与复现 #### PyTorch 实现的 EDSR 项目 EDSR 的官方 PyTorch 版本可以通过以下 GitHub 镜像仓库获取[^1]。此项目的代码提供了完整的训练和测试流程,适合用于研究和生产环境下的超分辨率重建任务。 该项目的主要特点如下: - 提供了单尺度(Single-Scale)和多尺度(Multi-Scale)版本的 EDSR 模型。 - 移除了 SRResNet 中的批量标准化 (Batch Normalization) 层,从而提升了模型性能[^2][^4]。 - 使用残差模块作为基础构建单元,并未在残差模块外部添加 ReLU 激活函数。 以下是基于上述描述的一个简化版 EDSR 模型定义: ```python import torch.nn as nn class ResidualBlock(nn.Module): def __init__(self, channels, res_scale=0.1): super(ResidualBlock, self).__init__() self.res_scale = res_scale self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False) def forward(self, x): residual = self.conv1(x) residual = self.relu(residual) residual = self.conv2(residual).mul(self.res_scale) return x + residual class EDSR(nn.Module): def __init__(self, scale_factor, num_features=64, num_blocks=32, res_scale=0.1): super(EDSR, self).__init__() self.head_conv = nn.Conv2d(3, num_features, kernel_size=3, stride=1, padding=1, bias=False) body = [] for _ in range(num_blocks): body.append(ResidualBlock(num_features, res_scale)) self.body = nn.Sequential(*body) self.tail_conv = nn.Conv2d(num_features, 3 * (scale_factor ** 2), kernel_size=3, stride=1, padding=1, bias=False) self.pixel_shuffle = nn.PixelShuffle(scale_factor) def forward(self, x): head = self.head_conv(x) out = self.body(head) out += head # Skip connection across the entire network tail = self.tail_conv(out) output = self.pixel_shuffle(tail) return output ``` #### 复现实验注意事项 尽管官方实现了高质量的 EDSR 模型,但在实际复现过程中可能会遇到一些挑战。例如,在某些实验中可能无法达到论文中的效果[^3]。此时可以考虑以下几个方面来优化结果: - 数据预处理:确保输入数据经过充分清洗并符合标准格式。 - 超参数调整:尝试不同的学习率、批次大小以及正则化策略。 - 初始化方式:采用 Xavier 或 Kaiming 初始化方法以加速收敛过程。 如果仍然存在较大差异,则建议参考其他开源项目中的改进版本,比如 RCAN 中提供的 EDSR 相关代码。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值