Eugene Siow
commited on
Commit
·
ab84f51
1
Parent(s):
491f0b6
Add training and inference code samples.
Browse files
README.md
CHANGED
@@ -27,14 +27,19 @@ pip install super-image
|
|
27 |
```
|
28 |
Here is how to use a pre-trained model to upscale your image:
|
29 |
```python
|
30 |
-
from super_image import EdsrModel,
|
31 |
from PIL import Image
|
32 |
import requests
|
33 |
-
|
|
|
34 |
image = Image.open(requests.get(url, stream=True).raw)
|
35 |
-
|
36 |
-
|
|
|
37 |
preds = model(inputs)
|
|
|
|
|
|
|
38 |
```
|
39 |
## Training data
|
40 |
The EDSR models for 2x, 3x and 4x image super resolution were pretrained on [DIV2K](https://data.vision.ee.ethz.ch/cvl/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).
|
@@ -44,17 +49,40 @@ We follow the pre-processing and training method of [Wang et. al](https://arxiv.
|
|
44 |
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.
|
45 |
During training, RGB patches with size of 64×64 from the LR input are used together with their corresponding HR patches.
|
46 |
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
### Pretraining
|
48 |
The model was trained on GPU. The training code is provided below:
|
49 |
```python
|
50 |
from super_image import Trainer, TrainingArguments, EdsrModel, EdsrConfig
|
51 |
|
52 |
training_args = TrainingArguments(
|
53 |
-
output_dir='./results',
|
54 |
-
num_train_epochs=1000,
|
55 |
)
|
56 |
|
57 |
-
config = EdsrConfig(
|
|
|
|
|
|
|
58 |
model = EdsrModel(config)
|
59 |
|
60 |
trainer = Trainer(
|
|
|
27 |
```
|
28 |
Here is how to use a pre-trained model to upscale your image:
|
29 |
```python
|
30 |
+
from super_image import EdsrModel, ImageLoader
|
31 |
from PIL import Image
|
32 |
import requests
|
33 |
+
|
34 |
+
url = 'https://paperswithcode.com/media/datasets/Set5-0000002728-07a9793f_zA3bDjj.jpg'
|
35 |
image = Image.open(requests.get(url, stream=True).raw)
|
36 |
+
|
37 |
+
model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=2)
|
38 |
+
inputs = ImageLoader.load_image(image)
|
39 |
preds = model(inputs)
|
40 |
+
|
41 |
+
ImageLoader.save_image(preds, './scaled_2x.png')
|
42 |
+
ImageLoader.save_compare(inputs, preds, './scaled_2x_compare.png')
|
43 |
```
|
44 |
## Training data
|
45 |
The EDSR models for 2x, 3x and 4x image super resolution were pretrained on [DIV2K](https://data.vision.ee.ethz.ch/cvl/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).
|
|
|
49 |
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.
|
50 |
During training, RGB patches with size of 64×64 from the LR input are used together with their corresponding HR patches.
|
51 |
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.
|
52 |
+
|
53 |
+
The following code provides some helper functions to preprocess the data.
|
54 |
+
```python
|
55 |
+
from super_image.data import EvalDataset, TrainAugmentDataset, DatasetBuilder
|
56 |
+
|
57 |
+
DatasetBuilder.prepare(
|
58 |
+
base_path='./DIV2K/DIV2K_train_HR',
|
59 |
+
output_path='./div2k_4x_train.h5',
|
60 |
+
scale=4,
|
61 |
+
do_augmentation=True
|
62 |
+
)
|
63 |
+
DatasetBuilder.prepare(
|
64 |
+
base_path='./DIV2K/DIV2K_val_HR',
|
65 |
+
output_path='./div2k_4x_val.h5',
|
66 |
+
scale=4,
|
67 |
+
do_augmentation=False
|
68 |
+
)
|
69 |
+
train_dataset = TrainAugmentDataset('./div2k_4x_train.h5', scale=4)
|
70 |
+
val_dataset = EvalDataset('./div2k_4x_val.h5')
|
71 |
+
```
|
72 |
### Pretraining
|
73 |
The model was trained on GPU. The training code is provided below:
|
74 |
```python
|
75 |
from super_image import Trainer, TrainingArguments, EdsrModel, EdsrConfig
|
76 |
|
77 |
training_args = TrainingArguments(
|
78 |
+
output_dir='./results', # output directory
|
79 |
+
num_train_epochs=1000, # total number of training epochs
|
80 |
)
|
81 |
|
82 |
+
config = EdsrConfig(
|
83 |
+
scale=4, # train a model to upscale 4x
|
84 |
+
supported_scales=[2, 3, 4],
|
85 |
+
)
|
86 |
model = EdsrModel(config)
|
87 |
|
88 |
trainer = Trainer(
|