CARN模型是在DIV2K上预训练的,用于2x、3x和4x图像超分辨率(SR)的模型(800张图像进行训练,经过增强达到4000张图像,100张图像用于验证)。它由Ahn等人在2018年的论文中提出,并于 this repository 中首次发布。
图像超分辨的目标是从单个低分辨率(LR)图像恢复出高分辨率(HR)图像。下图显示了真实图像(HR)、双三次插值上采样和模型上采样的效果。
CARN模型提出了一种在残差网络上实现级联机制的架构,用于精确而轻量级的图像超分辨率。
您可以使用预训练的模型将图像上采样2x、3x和4x。您还可以使用训练器在自己的数据集上训练模型。
该模型可以使用 super_image 库:
pip install super-image
以下是如何使用预训练模型放大图像的示例:
from super_image import CarnModel, ImageLoader from PIL import Image import requests url = 'https://paperswithcode.com/media/datasets/Set5-0000002728-07a9793f_zA3bDjj.jpg' image = Image.open(requests.get(url, stream=True).raw) model = CarnModel.from_pretrained('eugenesiow/carn', scale=2) # scale 2, 3 and 4 models available inputs = ImageLoader.load_image(image) preds = model(inputs) ImageLoader.save_image(preds, './scaled_2x.png') # save the output 2x scaled image to `./scaled_2x.png` ImageLoader.save_compare(inputs, preds, './scaled_2x_compare.png') # save an output comparing the super-image with a bicubic scaling
2x、3x和4x图像超分辨率的模型是在 DIV2K 上进行预训练的,该数据集包含800个高质量(2K分辨率)的训练图像,经过增强处理得到4000个图像,并使用100个验证图像(图像编号为801至900)作为开发集。
我们遵循 Wang et al. 的预处理和训练方法。通过使用双三次插值作为调整图像尺寸的方法,将高分辨率(HR)图像的尺寸缩小2倍、3倍和4倍。在训练过程中,使用大小为64×64的RGB图像块从LR输入图像中提取,并与其对应的HR图像块一起使用。在预处理阶段,对训练集应用数据增强,从原始图像的四个角和中心创建了五个图像。
我们需要使用huggingface datasets 库下载数据:
pip install datasets
以下代码获取数据并对数据进行预处理和增强:
from datasets import load_dataset from super_image.data import EvalDataset, TrainDataset, augment_five_crop augmented_dataset = load_dataset('eugenesiow/Div2k', 'bicubic_x4', split='train')\ .map(augment_five_crop, batched=True, desc="Augmenting Dataset") # download and augment the data with the five_crop method train_dataset = TrainDataset(augmented_dataset) # prepare the train dataset for loading PyTorch DataLoader eval_dataset = EvalDataset(load_dataset('eugenesiow/Div2k', 'bicubic_x4', split='validation')) # prepare the eval dataset for the PyTorch DataLoader
该模型是在GPU上训练的。以下是训练代码:
from super_image import Trainer, TrainingArguments, CarnModel, CarnConfig training_args = TrainingArguments( output_dir='./results', # output directory num_train_epochs=1000, # total number of training epochs ) config = CarnConfig( scale=4, # train a model to upscale 4x bam=True, # apply balanced attention to the network ) model = CarnModel(config) trainer = Trainer( model=model, # the instantiated model to be trained args=training_args, # training arguments, defined above train_dataset=train_dataset, # training dataset eval_dataset=eval_dataset # evaluation dataset ) trainer.train()
评估数据集包括:
下面的结果栏以PSNR/SSIM表示,与双三次插值作为基准进行比较。
|数据集|缩放倍数|双三次插值|CARN||---|---|---|---||Set5|2x|33.64/0.9292|37.89/0.9602||Set5|3x|30.39/0.8678|34.88/0.9391||Set5|4x|28.42/0.8101|32.05/0.8931||Set14|2x|30.22/0.8683|33.53/0.9173||Set14|3x|27.53/0.7737|30.93/0.8566||Set14|4x|25.99/0.7023|28.67/0.7828||BSD100|2x|29.55/0.8425|33.66/0.9242||BSD100|3x|27.20/0.7382|29.56/0.8173||BSD100|4x|25.96/0.6672|28.44/0.7625||Urban100|2x|26.66/0.8408|31.62/0.9229||Urban100|3x||28.95/0.867||Urban100|4x|23.14/0.6573|25.85/0.7768|
您可以在以下链接找到一个笔记本,用于方便地对预训练模型进行评估:
@article{ahn2018fast, title={Fast, Accurate, and Lightweight Super-Resolution with Cascading Residual Network}, author={Ahn, Namhyuk and Kang, Byungkon and Sohn, Kyung-Ah}, journal={arXiv preprint arXiv:1803.08664}, year={2018} }