Path: blob/master/tests/test_ffhq_degradation_dataset.py
884 views
import pytest1import yaml23from gfpgan.data.ffhq_degradation_dataset import FFHQDegradationDataset456def test_ffhq_degradation_dataset():78with open('tests/data/test_ffhq_degradation_dataset.yml', mode='r') as f:9opt = yaml.load(f, Loader=yaml.FullLoader)1011dataset = FFHQDegradationDataset(opt)12assert dataset.io_backend_opt['type'] == 'disk' # io backend13assert len(dataset) == 1 # whether to read correct meta info14assert dataset.kernel_list == ['iso', 'aniso'] # correct initialization the degradation configurations15assert dataset.color_jitter_prob == 11617# test __getitem__18result = dataset.__getitem__(0)19# check returned keys20expected_keys = ['gt', 'lq', 'gt_path']21assert set(expected_keys).issubset(set(result.keys()))22# check shape and contents23assert result['gt'].shape == (3, 512, 512)24assert result['lq'].shape == (3, 512, 512)25assert result['gt_path'] == 'tests/data/gt/00000000.png'2627# ------------------ test with probability = 0 -------------------- #28opt['color_jitter_prob'] = 029opt['color_jitter_pt_prob'] = 030opt['gray_prob'] = 031opt['io_backend'] = dict(type='disk')32dataset = FFHQDegradationDataset(opt)33assert dataset.io_backend_opt['type'] == 'disk' # io backend34assert len(dataset) == 1 # whether to read correct meta info35assert dataset.kernel_list == ['iso', 'aniso'] # correct initialization the degradation configurations36assert dataset.color_jitter_prob == 03738# test __getitem__39result = dataset.__getitem__(0)40# check returned keys41expected_keys = ['gt', 'lq', 'gt_path']42assert set(expected_keys).issubset(set(result.keys()))43# check shape and contents44assert result['gt'].shape == (3, 512, 512)45assert result['lq'].shape == (3, 512, 512)46assert result['gt_path'] == 'tests/data/gt/00000000.png'4748# ------------------ test lmdb backend -------------------- #49opt['dataroot_gt'] = 'tests/data/ffhq_gt.lmdb'50opt['io_backend'] = dict(type='lmdb')5152dataset = FFHQDegradationDataset(opt)53assert dataset.io_backend_opt['type'] == 'lmdb' # io backend54assert len(dataset) == 1 # whether to read correct meta info55assert dataset.kernel_list == ['iso', 'aniso'] # correct initialization the degradation configurations56assert dataset.color_jitter_prob == 05758# test __getitem__59result = dataset.__getitem__(0)60# check returned keys61expected_keys = ['gt', 'lq', 'gt_path']62assert set(expected_keys).issubset(set(result.keys()))63# check shape and contents64assert result['gt'].shape == (3, 512, 512)65assert result['lq'].shape == (3, 512, 512)66assert result['gt_path'] == '00000000'6768# ------------------ test with crop_components -------------------- #69opt['crop_components'] = True70opt['component_path'] = 'tests/data/test_eye_mouth_landmarks.pth'71opt['eye_enlarge_ratio'] = 1.472opt['gt_gray'] = True73opt['io_backend'] = dict(type='lmdb')7475dataset = FFHQDegradationDataset(opt)76assert dataset.crop_components is True7778# test __getitem__79result = dataset.__getitem__(0)80# check returned keys81expected_keys = ['gt', 'lq', 'gt_path', 'loc_left_eye', 'loc_right_eye', 'loc_mouth']82assert set(expected_keys).issubset(set(result.keys()))83# check shape and contents84assert result['gt'].shape == (3, 512, 512)85assert result['lq'].shape == (3, 512, 512)86assert result['gt_path'] == '00000000'87assert result['loc_left_eye'].shape == (4, )88assert result['loc_right_eye'].shape == (4, )89assert result['loc_mouth'].shape == (4, )9091# ------------------ lmdb backend should have paths ends with lmdb -------------------- #92with pytest.raises(ValueError):93opt['dataroot_gt'] = 'tests/data/gt'94opt['io_backend'] = dict(type='lmdb')95dataset = FFHQDegradationDataset(opt)969798