Eugene Siow commited on
Commit
ac5989f
1 Parent(s): 5bfa5f9

Add models.

Browse files
README.md ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - super-image
5
+ - image-super-resolution
6
+ datasets:
7
+ - eugenesiow/Div2k
8
+ - eugenesiow/Set5
9
+ - eugenesiow/Set14
10
+ - eugenesiow/BSD100
11
+ - eugenesiow/Urban100
12
+ metrics:
13
+ - pnsr
14
+ - ssim
15
+ ---
16
+ # Pixel Attention Network (PAN)
17
+ PAN model pre-trained on DIV2K (800 images training, augmented to 4000 images, 100 images validation) for 2x, 3x and 4x image super resolution. It was introduced in the paper [Efficient Image Super-Resolution Using Pixel Attention](https://arxiv.org/abs/2010.01073) by Zhao et al. (2020) and first released in [this repository](https://github.com/zhaohengyuan1/PAN).
18
+
19
+ The goal of image super resolution is to restore a high resolution (HR) image from a single low resolution (LR) image. The image below shows the ground truth (HR), the bicubic upscaling and model upscaling.
20
+
21
+ ![Comparing Bicubic upscaling against the models x4 upscaling on Set5 Image 4](images/pan_4_4_compare.png "Comparing Bicubic upscaling against the models x4 upscaling on Set5 Image 4")
22
+ ## Model description
23
+ The PAN model proposes a a lightweight convolutional neural network for image super resolution. Pixel attention (PA) is similar to channel attention and spatial attention in formulation. PA however produces 3D attention maps instead of a 1D attention vector or a 2D map. This attention scheme introduces fewer additional parameters but generates better SR results.
24
+
25
+ This model also applies the balanced attention (BAM) method invented by [Wang et al. (2021)](https://arxiv.org/abs/2104.07566) to further improve the results.
26
+
27
+ The model is very lightweight with the model being just 260k to 270k parameters (~1mb).
28
+ ## Intended uses & limitations
29
+ You can use the pre-trained models for upscaling your images 2x, 3x and 4x. You can also use the trainer to train a model on your own dataset.
30
+ ### How to use
31
+ The model can be used with the [super_image](https://github.com/eugenesiow/super-image) library:
32
+ ```bash
33
+ pip install super-image
34
+ ```
35
+ Here is how to use a pre-trained model to upscale your image:
36
+ ```python
37
+ from super_image import PanModel, ImageLoader
38
+ from PIL import Image
39
+ import requests
40
+
41
+ url = 'https://paperswithcode.com/media/datasets/Set5-0000002728-07a9793f_zA3bDjj.jpg'
42
+ image = Image.open(requests.get(url, stream=True).raw)
43
+
44
+ model = PanModel.from_pretrained('eugenesiow/pan-bam', scale=2) # scale 2, 3 and 4 models available
45
+ inputs = ImageLoader.load_image(image)
46
+ preds = model(inputs)
47
+
48
+ ImageLoader.save_image(preds, './scaled_2x.png') # save the output 2x scaled image to `./scaled_2x.png`
49
+ ImageLoader.save_compare(inputs, preds, './scaled_2x_compare.png') # save an output comparing the super-image with a bicubic scaling
50
+ ```
51
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/eugenesiow/super-image-notebooks/blob/master/notebooks/Upscale_Images_with_Pretrained_super_image_Models.ipynb "Open in Colab")
52
+ ## Training data
53
+ The models for 2x, 3x and 4x image super resolution were pretrained on [DIV2K](https://huggingface.co/datasets/eugenesiow/Div2k), a dataset of 800 high-quality (2K resolution) images for training, augmented to 4000 images and uses a dev set of 100 validation images (images numbered 801 to 900).
54
+ ## Training procedure
55
+ ### Preprocessing
56
+ We follow the pre-processing and training method of [Wang et al.](https://arxiv.org/abs/2104.07566).
57
+ Low Resolution (LR) images are created by using bicubic interpolation as the resizing method to reduce the size of the High Resolution (HR) images by x2, x3 and x4 times.
58
+ During training, RGB patches with size of 64×64 from the LR input are used together with their corresponding HR patches.
59
+ Data augmentation is applied to the training set in the pre-processing stage where five images are created from the four corners and center of the original image.
60
+
61
+ We need the huggingface [datasets](https://huggingface.co/datasets?filter=task_ids:other-other-image-super-resolution) library to download the data:
62
+ ```bash
63
+ pip install datasets
64
+ ```
65
+ The following code gets the data and preprocesses/augments the data.
66
+
67
+ ```python
68
+ from datasets import load_dataset
69
+ from super_image.data import EvalDataset, TrainDataset, augment_five_crop
70
+
71
+ augmented_dataset = load_dataset('eugenesiow/Div2k', 'bicubic_x4', split='train')\
72
+ .map(augment_five_crop, batched=True, desc="Augmenting Dataset") # download and augment the data with the five_crop method
73
+ train_dataset = TrainDataset(augmented_dataset) # prepare the train dataset for loading PyTorch DataLoader
74
+ eval_dataset = EvalDataset(load_dataset('eugenesiow/Div2k', 'bicubic_x4', split='validation')) # prepare the eval dataset for the PyTorch DataLoader
75
+ ```
76
+ ### Pretraining
77
+ The model was trained on GPU. The training code is provided below:
78
+ ```python
79
+ from super_image import Trainer, TrainingArguments, PanModel, PanConfig
80
+
81
+ training_args = TrainingArguments(
82
+ output_dir='./results', # output directory
83
+ num_train_epochs=1000, # total number of training epochs
84
+ )
85
+
86
+ config = PanConfig(
87
+ scale=4, # train a model to upscale 4x
88
+ bam=True, # apply balanced attention to the network
89
+ )
90
+ model = PanModel(config)
91
+
92
+ trainer = Trainer(
93
+ model=model, # the instantiated model to be trained
94
+ args=training_args, # training arguments, defined above
95
+ train_dataset=train_dataset, # training dataset
96
+ eval_dataset=eval_dataset # evaluation dataset
97
+ )
98
+
99
+ trainer.train()
100
+ ```
101
+
102
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/eugenesiow/super-image-notebooks/blob/master/notebooks/Train_super_image_Models.ipynb "Open in Colab")
103
+ ## Evaluation results
104
+ The evaluation metrics include [PSNR](https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio#Quality_estimation_with_PSNR) and [SSIM](https://en.wikipedia.org/wiki/Structural_similarity#Algorithm).
105
+
106
+ Evaluation datasets include:
107
+ - Set5 - [Bevilacqua et al. (2012)](https://huggingface.co/datasets/eugenesiow/Set5)
108
+ - Set14 - [Zeyde et al. (2010)](https://huggingface.co/datasets/eugenesiow/Set14)
109
+ - BSD100 - [Martin et al. (2001)](https://huggingface.co/datasets/eugenesiow/BSD100)
110
+ - Urban100 - [Huang et al. (2015)](https://huggingface.co/datasets/eugenesiow/Urban100)
111
+
112
+ The results columns below are represented below as `PSNR/SSIM`. They are compared against a Bicubic baseline.
113
+
114
+ |Dataset |Scale |Bicubic |pan-bam |
115
+ |--- |--- |--- |--- |
116
+ |Set5 |2x |33.64/0.9292 |**37.7/0.9596** |
117
+ |Set5 |3x |30.39/0.8678 |**34.62/0.9371** |
118
+ |Set5 |4x |28.42/0.8101 |**31.9/0.8911** |
119
+ |Set14 |2x |30.22/0.8683 |**33.4/0.9161** |
120
+ |Set14 |3x |27.53/0.7737 |**30.83/0.8545** |
121
+ |Set14 |4x |25.99/0.7023 |**28.54/0.7795** |
122
+ |BSD100 |2x |29.55/0.8425 |**33.6/0.9234** |
123
+ |BSD100 |3x |27.20/0.7382 |**29.47/0.8153** |
124
+ |BSD100 |4x |25.96/0.6672 |**28.32/0.7591** |
125
+ |Urban100 |2x |26.66/0.8408 |**31.35/0.92** |
126
+ |Urban100 |3x | |**28.64/0.861** |
127
+ |Urban100 |4x |23.14/0.6573 |**25.6/0.7691** |
128
+
129
+ ![Comparing Bicubic upscaling against the models x4 upscaling on Set5 Image 2](images/pan_2_4_compare.png "Comparing Bicubic upscaling against the models x4 upscaling on Set5 Image 2")
130
+
131
+ You can find a notebook to easily run evaluation on pretrained models below:
132
+
133
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/eugenesiow/super-image-notebooks/blob/master/notebooks/Evaluate_Pretrained_super_image_Models.ipynb "Open in Colab")
134
+
135
+ ## BibTeX entry and citation info
136
+ ```bibtex
137
+ @misc{wang2021bam,
138
+ title={BAM: A Lightweight and Efficient Balanced Attention Mechanism for Single Image Super Resolution},
139
+ author={Fanyi Wang and Haotian Hu and Cheng Shen},
140
+ year={2021},
141
+ eprint={2104.07566},
142
+ archivePrefix={arXiv},
143
+ primaryClass={eess.IV}
144
+ }
145
+ ```
146
+
147
+ ```bibtex
148
+ @misc{zhao2020efficient,
149
+ title={Efficient Image Super-Resolution Using Pixel Attention},
150
+ author={Hengyuan Zhao and Xiangtao Kong and Jingwen He and Yu Qiao and Chao Dong},
151
+ year={2020},
152
+ eprint={2010.01073},
153
+ archivePrefix={arXiv},
154
+ primaryClass={eess.IV}
155
+ }
156
+ ```
config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bam": true,
3
+ "data_parallel": false,
4
+ "in_nc": 3,
5
+ "model_type": "PAN",
6
+ "nb": 16,
7
+ "nf": 40,
8
+ "out_nc": 3,
9
+ "unf": 24
10
+ }
images/pan_2_4_compare.png ADDED
images/pan_4_4_compare.png ADDED
pytorch_model_2x.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:998b39377a02277fc5549f1781013a4a8d09215e733fc497e3e723ab8329c22b
3
+ size 1097637
pytorch_model_3x.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79308518a3c98f58e6006b28ce9af62fb832ef98e5c0a706ee7fb89f81a052b4
3
+ size 1097637
pytorch_model_4x.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:89d79ef888fffbb81636486c66d9285a8ea1d126c6c62da430acf0a6ecf3b61a
3
+ size 1142869