BasicSR Getting Started Tutorial
1. Installation environment
Since other installed environments already have pytorch, it is good to directly copy the environment when creating a new environment
//复制环境
conda create --name my-basicsr --clone mmediting
clone project
git clone https://github.com/XPixelGroup/BasicSR.git
Install dependencies
cd BasicSR
pip install -r requirements.txt
Install BasicSR in the root directory of BasicSR
python setup.py develop
Verify that BasicSR is successfully installed
import basicsr
When the local clone installation is successful, use the pip list command to view the BasicSR path
pip list
2. Prepare the dataset
Commonly used image super-resolution datasets are as follows:
name | data set | data description | download |
---|---|---|---|
2K Resolution | DIV2K | proposed in NTIRE17 (800 train and 100 validation) | official website |
Classical SR Testing | Set5 | Set5 test dataset | Google Drive / Baidu Drive |
Classical SR Testing | Set14 | Set14 test dataset | Google Drive / Baidu Drive |
DIV2K download link: https://data.vision.ee.ethz.ch/cvl/DIV2K/
Set5 download link: https://drive.google.com/drive/folders/1B3DJGQKB6eNdwuQIhdskA64qUuVKLZ9u
Set14 download link: https://drive.google.com/drive/folders/1B3DJGQKB6eNdwuQIhdskA64qUuVKLZ9u
Because the DIV2K data set is 2K resolution (for example: 2048×1080), and we often do not need to be so large when training (commonly 128×128 or 192×192 training patch). Therefore, we can first put the 2K The image is cropped into a 480×480 sub-image block with overlap. Then the dataloader randomly crops a 128×128 or 192×192 training patch from the 480×480 sub-image block. Run the script extract_subimages.py.
cd BasicSR
python scripts/data_preparation/extract_subimages.py
If you need to use LMDB, you need to make LMDB, and the data is ready to run the script:
python scripts/data_preparation/create_lmdb.py --dataset div2k
The directory structure of the dataset is as follows
3. Modify the configuration file
Create a new training configuration file options/train/SRResNet_SRGAN/my_train_MSRResNet_x4.yml
with the following content
# Modified SRResNet w/o BN from:
# Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
# ----------- Commands for running
# ----------- Single GPU with auto_resume
# PYTHONPATH="./:${PYTHONPATH}" CUDA_VISIBLE_DEVICES=0 python basicsr/train.py -opt options/train/SRResNet_SRGAN/train_MSRResNet_x4.yml --auto_resume
# general settings
name: 001_MSRResNet_x4_f64b16_DIV2K_10k_B16G1_wandb_myfirst
model_type: SRModel
scale: 4
num_gpu: 1 # set num_gpu: 0 for cpu mode
manual_seed: 0
# dataset and data loader settings
datasets:
train:
name: DIV2K
type: PairedImageDataset
# dataroot_gt: datasets/DF2K/DIV2K_train_HR_sub
# dataroot_lq: datasets/DF2K/DIV2K_train_LR_bicubic_X4_sub
# meta_info_file: basicsr/data/meta_info/meta_info_DIV2K800sub_GT.txt
# dataroot_gt: datasets/DIV2K/DIV2K_train_HR_sub
# dataroot_lq: datasets/DIV2K/DIV2K_train_LR_bicubic_X4_sub
# meta_info_file: basicsr/data/meta_info/meta_info_DIV2K800sub_GT.txt
# (for lmdb)
dataroot_gt: datasets/DIV2K/DIV2K_train_HR_sub.lmdb
dataroot_lq: datasets/DIV2K/DIV2K_train_LR_bicubic_X4_sub.lmdb
filename_tmpl: '{}'
io_backend:
# type: disk
# (for lmdb)
type: lmdb
gt_size: 128
use_hflip: true
use_rot: true
# data loader
num_worker_per_gpu: 6
batch_size_per_gpu: 16
dataset_enlarge_ratio: 100
prefetch_mode: ~
val:
name: Set5
type: PairedImageDataset
dataroot_gt: datasets/Set5/GTmod12
dataroot_lq: datasets/Set5/LRbicx4
io_backend:
type: disk
val_2:
name: Set14
type: PairedImageDataset
dataroot_gt: datasets/Set14/GTmod12
dataroot_lq: datasets/Set14/LRbicx4
io_backend:
type: disk
# network structures
network_g:
type: MSRResNet
num_in_ch: 3
num_out_ch: 3
num_feat: 64
num_block: 16
upscale: 4
# path
path:
pretrain_network_g: ~
param_key_g: params
strict_load_g: true
resume_state: ~
# training settings
train:
ema_decay: 0.999
optim_g:
type: Adam
lr: !!float 2e-4
weight_decay: 0
betas: [0.9, 0.99]
scheduler:
type: CosineAnnealingRestartLR
periods: [250000, 250000, 250000, 250000]
restart_weights: [1, 1, 1, 1]
eta_min: !!float 1e-7
# total_iter: 1000000
total_iter: 10000
warmup_iter: -1 # no warm up
# losses
pixel_opt:
type: L1Loss
loss_weight: 1.0
reduction: mean
# validation settings
val:
val_freq: !!float 5e3
save_img: false
metrics:
psnr: # metric name, can be arbitrary
type: calculate_psnr
crop_border: 4
test_y_channel: false
better: higher # the higher, the better. Default: higher
niqe:
type: calculate_niqe
crop_border: 4
better: lower # the lower, the better
# logging settings
logger:
print_freq: 100
save_checkpoint_freq: !!float 5e3
use_tb_logger: true
wandb:
project: ~
resume_id: ~
# dist training settings
dist_params:
backend: nccl
port: 29500
can start training
python basicsr/train.py -opt options/train/SRResNet_SRGAN/my_train_MSRResNet_x4.yml
001_MSRResNet_x4_f64b16_DIV2K_10k_B16G1_wandb_myfirst
After the training is complete, the results will be saved in a folder under the results folder
Create a new test configuration file options/test/SRResNet_SRGAN/my_test_MSRResNet_x4.yml
with the following content
# ----------- Commands for running
# ----------- Single GPU
# PYTHONPATH="./:${PYTHONPATH}" CUDA_VISIBLE_DEVICES=0 python basicsr/test.py -opt options/test/SRResNet_SRGAN/test_MSRResNet_x4.yml
# general settings
name: 001_MSRResNet_x4_f64b16_DIV2K_10k_B16G1_wandb_myfirst
model_type: SRModel
scale: 4
num_gpu: 1 # set num_gpu: 0 for cpu mode
manual_seed: 0
# test dataset settings
datasets:
test_1: # the 1st test dataset
name: Set5
type: PairedImageDataset
dataroot_gt: datasets/Set5/GTmod12
dataroot_lq: datasets/Set5/LRbicx4
io_backend:
type: disk
test_2: # the 2nd test dataset
name: Set14
type: PairedImageDataset
dataroot_gt: datasets/Set14/GTmod12
dataroot_lq: datasets/Set14/LRbicx4
io_backend:
type: disk
test_3: # the 3rd test dataset
name: DIV2K100
type: PairedImageDataset
dataroot_gt: datasets/DIV2K/DIV2K_valid_HR
dataroot_lq: datasets/DIV2K/DIV2K_valid_LR_bicubic/X4
filename_tmpl: '{}x4'
io_backend:
type: disk
# network structures
network_g:
type: MSRResNet
num_in_ch: 3
num_out_ch: 3
num_feat: 64
num_block: 16
upscale: 4
# path
path:
pretrain_network_g: experiments/001_MSRResNet_x4_f64b16_DIV2K_10k_B16G1_wandb_myfirst/models/net_g_10000.pth
param_key_g: params
strict_load_g: true
# validation settings
val:
save_img: true
suffix: ~ # add suffix to saved images, if None, use exp name
metrics:
psnr: # metric name, can be arbitrary
type: calculate_psnr
crop_border: 4
test_y_channel: false
better: higher # the higher, the better. Default: higher
ssim:
type: calculate_ssim
crop_border: 4
test_y_channel: false
better: higher
001_MSRResNet_x4_f64b16_DIV2K_10k_B16G1_wandb_myfirst
After the test is completed, the results are saved in a folder under the results folder
4. Tensorboard visualizes the training process
Set tensorboard to open in the yml configuration file for training
# logging settings
logger:
print_freq: 100
save_checkpoint_freq: !!float 5e3
use_tb_logger: true # 设置为true
wandb:
project: ~
resume_id: ~
Enter the following command on the command line to view it in the browser of the server :
tensorboard --logdir tb_logger --port 5500 --bind_all
Tensorboard can be used easily on this machine , but it needs to be set up when using the server.
Install an Xshell in the Windows system , go to File->Properties->ssh->Tunnel->Add, type local, fill in 127.0.0.1 for the source host (meaning this machine), set a port, such as 12345, and set the target host as a server. The target port is generally 5500, if 5500 is occupied, it can be changed to other ports.
127.0.0.1:12345
Just type in your local browser
Finally, thank you for your study~