Legitking4pf commited on
Commit
66b9c5d
·
verified ·
1 Parent(s): 1daee20

Update dataset.py

Browse files
Files changed (1) hide show
  1. dataset.py +26 -14
dataset.py CHANGED
@@ -1,17 +1,18 @@
1
  import os
 
2
  from datasets import DatasetInfo, GeneratorBasedBuilder, Features, Image, ClassLabel, Split
3
  from huggingface_hub import hf_hub_download
4
 
5
  class UIR25MReference(GeneratorBasedBuilder):
6
- """UIR-2.5M Reference Dataset Loader"""
7
 
8
  VERSION = "1.0.0"
9
 
10
- # Directory where pretrained models will be downloaded
11
  PRETRAINED_DIR = os.path.expanduser("./pretrained_models")
12
  os.makedirs(PRETRAINED_DIR, exist_ok=True)
13
 
14
- # List of pretrained model files with Hugging Face URLs
15
  PRETRAINED_MODELS = [
16
  "nafnet_maskdcpt_12d.pth",
17
  "nafnet_maskdcpt_5d.pth",
@@ -20,7 +21,6 @@ class UIR25MReference(GeneratorBasedBuilder):
20
  "restormer_maskdcpt_5d.pth",
21
  "swinir_maskdcpt_5d.pth"
22
  ]
23
-
24
  PRETRAINED_REPO = "Jiakui/MaskDCPT"
25
 
26
  def download_pretrained_models(self):
@@ -63,8 +63,8 @@ class UIR25MReference(GeneratorBasedBuilder):
63
 
64
  def _split_generators(self, dl_manager):
65
  """
66
- Leverage automatic split detection:
67
- Expects directories named `train`, `test`, `validation` in external path
68
  """
69
  data_dir = dl_manager.download_and_extract("https://github.com/MILab-PKU/MaskDCPT.git")
70
  splits = []
@@ -80,18 +80,30 @@ class UIR25MReference(GeneratorBasedBuilder):
80
  return splits
81
 
82
  def _generate_examples(self, images_dir):
83
- """Yield examples for Hugging Face dataset."""
84
  low_dir = os.path.join(images_dir, "low_quality")
85
  high_dir = os.path.join(images_dir, "high_quality")
 
 
 
 
 
 
 
86
 
87
- # Expect a mapping from low to high quality images and degradation type metadata
88
- for idx, fname in enumerate(os.listdir(low_dir)):
89
- if fname.lower().endswith((".png", ".jpg", ".jpeg")):
90
- low_path = os.path.join(low_dir, fname)
91
- high_path = os.path.join(high_dir, fname)
92
- degradation_type = 0 # Replace with actual metadata mapping if available
 
 
 
93
  yield idx, {
94
  "low_quality": low_path,
95
  "high_quality": high_path,
96
  "degradation_type": degradation_type
97
- }
 
 
 
1
  import os
2
+ import json
3
  from datasets import DatasetInfo, GeneratorBasedBuilder, Features, Image, ClassLabel, Split
4
  from huggingface_hub import hf_hub_download
5
 
6
  class UIR25MReference(GeneratorBasedBuilder):
7
+ """UIR-2.5M Reference Dataset Loader with metadata mapping"""
8
 
9
  VERSION = "1.0.0"
10
 
11
+ # Directory for pretrained models
12
  PRETRAINED_DIR = os.path.expanduser("./pretrained_models")
13
  os.makedirs(PRETRAINED_DIR, exist_ok=True)
14
 
15
+ # Pretrained models info
16
  PRETRAINED_MODELS = [
17
  "nafnet_maskdcpt_12d.pth",
18
  "nafnet_maskdcpt_5d.pth",
 
21
  "restormer_maskdcpt_5d.pth",
22
  "swinir_maskdcpt_5d.pth"
23
  ]
 
24
  PRETRAINED_REPO = "Jiakui/MaskDCPT"
25
 
26
  def download_pretrained_models(self):
 
63
 
64
  def _split_generators(self, dl_manager):
65
  """
66
+ Automatic split detection:
67
+ Expects directories named train/test/validation
68
  """
69
  data_dir = dl_manager.download_and_extract("https://github.com/MILab-PKU/MaskDCPT.git")
70
  splits = []
 
80
  return splits
81
 
82
  def _generate_examples(self, images_dir):
83
+ """Yield examples using metadata JSON mapping"""
84
  low_dir = os.path.join(images_dir, "low_quality")
85
  high_dir = os.path.join(images_dir, "high_quality")
86
+ metadata_file = os.path.join(images_dir, "metadata.json")
87
+
88
+ if not os.path.exists(metadata_file):
89
+ raise FileNotFoundError(f"Metadata file not found: {metadata_file}")
90
+
91
+ with open(metadata_file, "r") as f:
92
+ metadata = json.load(f)
93
 
94
+ for idx, sample in enumerate(metadata):
95
+ low_fname = sample["low_quality"]
96
+ high_fname = sample["high_quality"]
97
+ degradation_type = sample["degradation_type"]
98
+
99
+ low_path = os.path.join(low_dir, low_fname)
100
+ high_path = os.path.join(high_dir, high_fname)
101
+
102
+ if os.path.exists(low_path) and os.path.exists(high_path):
103
  yield idx, {
104
  "low_quality": low_path,
105
  "high_quality": high_path,
106
  "degradation_type": degradation_type
107
+ }
108
+ else:
109
+ print(f"Skipping missing files: {low_fname} or {high_fname}")