Datasets:

Languages:
English
Size:
n<1K
License:
srikarym commited on
Commit
ef632b3
·
verified ·
1 Parent(s): 3bef110

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. ZoomLDM-demo-dataset.py +58 -45
ZoomLDM-demo-dataset.py CHANGED
@@ -1,7 +1,8 @@
1
  import datasets
2
- import os
3
  import numpy as np
4
- from PIL import Image
 
 
5
 
6
  _DATASET_VERSION = datasets.Version("1.0.0")
7
 
@@ -9,50 +10,62 @@ _N_EMBED = {
9
  "20x": 1,
10
  "10x": 4,
11
  "5x": 16,
12
- "2_5x": 64,
13
- "1_25x": 256,
14
  }
15
 
16
  _MAG_DICT = {
17
- "20x": 0,
18
- "10x": 1,
19
- "5x": 2,
20
- "2_5x": 3,
21
- "1_25x": 4,
22
  }
23
 
24
  _FIXED_SSL_FEATURE_DIM_1 = 1024
25
 
26
  def get_ssl_feat_shape(mag_level):
27
  first_dim = _N_EMBED[mag_level]
28
- return (first_dim, _FIXED_SSL_FEATURE_DIM_1)
 
29
 
30
  def preprocess_features(feat_array):
31
- feat_array = feat_array.astype(np.float32)
 
 
 
32
  mean = feat_array.mean(axis=0, keepdims=True)
33
  std = feat_array.std(axis=0, keepdims=True)
34
- processed_feat = (feat_array - mean) / (std + 1e-8)
35
- return processed_feat
 
 
 
 
 
 
 
 
36
 
37
  class MagnificationConfig(datasets.BuilderConfig):
38
- def __init__(self, mag_level=None, ssl_feat_shape=None, **kwargs):
39
  super(MagnificationConfig, self).__init__(**kwargs)
40
  self.mag_level = mag_level
41
  self.ssl_feat_shape = ssl_feat_shape
 
42
 
43
  class TCGADataset(datasets.GeneratorBasedBuilder):
44
  VERSION = _DATASET_VERSION
45
- MAGNIFICATIONS = ["20x", "10x", "5x", "2_5x", "1_25x"]
46
  BUILDER_CONFIGS = []
47
- for mag_level in MAGNIFICATIONS:
48
- feature_shape = get_ssl_feat_shape(mag_level)
49
-
50
  builder_config_instance = MagnificationConfig(
51
- name=mag_level,
52
  version=_DATASET_VERSION,
53
- description=f"Dataset at {mag_level} mag. SSL feature shape: {feature_shape}",
54
- data_dir=mag_level,
55
- mag_level=mag_level,
56
  ssl_feat_shape=feature_shape
57
  )
58
  BUILDER_CONFIGS.append(builder_config_instance)
@@ -60,64 +73,64 @@ class TCGADataset(datasets.GeneratorBasedBuilder):
60
  DEFAULT_CONFIG_NAME = "20x"
61
 
62
  def _info(self):
63
-
64
- _CITATION = """\
65
- @inproceedings{yellapragada2024zoomldm,
66
- title={Learned representation-guided diffusion models for large-image generation},
67
- author={Yellapragada, Srikar and Graikos, Alexandros and Triaridis, Kostas and Prasanna, Prateek and Gupta, Rajarsi R and Saltz, Joel and Samaras, Dimitris},
68
- booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
69
- year={2025}
70
- }
71
- """
72
-
73
  return datasets.DatasetInfo(
74
  description=f"Dataset with images and SSL features. Configuration: {self.config.name}",
75
  features=datasets.Features(
76
  {
77
  "image": datasets.Image(),
78
- "ssl_feat": datasets.Array2D(shape=self.config.ssl_feat_shape, dtype="float16"),
79
  "filename_img": datasets.Value("string"),
80
  "filename_feat": datasets.Value("string"),
81
  "mag": datasets.Value("string"),
82
  }
83
  ),
84
  homepage="https://github.com/cvlab-stonybrook/ZoomLDM",
85
- citation=_CITATION,
86
  )
87
 
88
  def _split_generators(self, dl_manager):
 
 
 
 
 
 
 
 
 
89
  mag_folder_name = self.config.data_dir
90
- mag_data_path = os.path.join("data", mag_folder_name)
91
-
 
 
 
92
  return [
93
  datasets.SplitGenerator(
94
  name=datasets.Split.TRAIN,
95
  gen_kwargs={
96
- "mag_folder_path": mag_data_path,
97
  "mag_level": self.config.mag_level,
98
  },
99
  ),
100
  ]
101
 
102
- def _generate_examples(self, mag_folder_path, mag_level):
103
  idx = 0
104
  for i in range(16):
105
  img_filename = f"{i}.jpg"
106
  feat_filename = f"{i}_ssl_feat.npy"
107
 
108
- img_path = os.path.join(mag_folder_path, img_filename)
109
- feat_path = os.path.join(mag_folder_path, feat_filename)
110
 
111
- if not (os.path.exists(img_path) and os.path.exists(feat_path)):
112
- continue
113
 
114
  ssl_feat_data = np.load(feat_path)
115
-
116
  processed_feature = preprocess_features(ssl_feat_data)
117
 
118
  yield idx, {
119
- "image": img_path,
120
- "ssl_feat": processed_feature.astype(np.float16),
 
 
121
  "mag": _MAG_DICT[mag_level],
122
  }
123
  idx += 1
 
1
  import datasets
 
2
  import numpy as np
3
+ from pathlib import Path
4
+ import torch
5
+ import torch.nn.functional as F
6
 
7
  _DATASET_VERSION = datasets.Version("1.0.0")
8
 
 
10
  "20x": 1,
11
  "10x": 4,
12
  "5x": 16,
13
+ "2.5x": 64,
14
+ "1.25x": 256,
15
  }
16
 
17
  _MAG_DICT = {
18
+ "20x": "20x",
19
+ "10x": "10x",
20
+ "5x": "5x",
21
+ "2.5x": "2.5x",
22
+ "1.25x": "1.25x",
23
  }
24
 
25
  _FIXED_SSL_FEATURE_DIM_1 = 1024
26
 
27
  def get_ssl_feat_shape(mag_level):
28
  first_dim = _N_EMBED[mag_level]
29
+ h = int(np.sqrt(first_dim))
30
+ return (_FIXED_SSL_FEATURE_DIM_1, h, h)
31
 
32
  def preprocess_features(feat_array):
33
+
34
+ if len(feat_array.shape) == 1:
35
+ feat_array = feat_array[:, None]
36
+
37
  mean = feat_array.mean(axis=0, keepdims=True)
38
  std = feat_array.std(axis=0, keepdims=True)
39
+ feat_array = (feat_array - mean) / (std + 1e-8)
40
+
41
+ h = np.sqrt(feat_array.shape[1]).astype(int)
42
+ feat_array = torch.tensor(feat_array.reshape((-1, h, h)))
43
+
44
+ if h > 8:
45
+ shape = (8, 8)
46
+ feat_array = F.adaptive_avg_pool2d(feat_array, shape)
47
+
48
+ return feat_array
49
 
50
  class MagnificationConfig(datasets.BuilderConfig):
51
+ def __init__(self, mag_level=None, ssl_feat_shape=None, data_dir=None, **kwargs):
52
  super(MagnificationConfig, self).__init__(**kwargs)
53
  self.mag_level = mag_level
54
  self.ssl_feat_shape = ssl_feat_shape
55
+ self.data_dir = data_dir
56
 
57
  class TCGADataset(datasets.GeneratorBasedBuilder):
58
  VERSION = _DATASET_VERSION
59
+ MAGNIFICATIONS = ["20x", "10x", "5x", "2.5x", "1.25x"]
60
  BUILDER_CONFIGS = []
61
+ for mag_level_str in MAGNIFICATIONS:
62
+ feature_shape = get_ssl_feat_shape(mag_level_str)
 
63
  builder_config_instance = MagnificationConfig(
64
+ name=mag_level_str,
65
  version=_DATASET_VERSION,
66
+ description=f"Dataset at {mag_level_str} mag. SSL feature shape: {feature_shape}",
67
+ data_dir=mag_level_str,
68
+ mag_level=mag_level_str,
69
  ssl_feat_shape=feature_shape
70
  )
71
  BUILDER_CONFIGS.append(builder_config_instance)
 
73
  DEFAULT_CONFIG_NAME = "20x"
74
 
75
  def _info(self):
 
 
 
 
 
 
 
 
 
 
76
  return datasets.DatasetInfo(
77
  description=f"Dataset with images and SSL features. Configuration: {self.config.name}",
78
  features=datasets.Features(
79
  {
80
  "image": datasets.Image(),
81
+ "ssl_feat": datasets.Array3D(shape=self.config.ssl_feat_shape, dtype="float32"),
82
  "filename_img": datasets.Value("string"),
83
  "filename_feat": datasets.Value("string"),
84
  "mag": datasets.Value("string"),
85
  }
86
  ),
87
  homepage="https://github.com/cvlab-stonybrook/ZoomLDM",
 
88
  )
89
 
90
  def _split_generators(self, dl_manager):
91
+ # self.base_path is set by the datasets library to the directory
92
+ # of the original script when loading a local script file.
93
+ # e.g., if script is at './zoomldm_data/ZoomLDM-demo-dataset.py',
94
+ # self.base_path will be the absolute path to './zoomldm_data/'
95
+ if not self.base_path:
96
+ # This should not happen when loading a local script file directly
97
+ raise ValueError("Dataset Builder's base_path is not set. Cannot locate local data.")
98
+
99
+ original_script_dir = Path(self.base_path)
100
  mag_folder_name = self.config.data_dir
101
+
102
+ # Construct the absolute path to the 'data/magnification_folder_name'
103
+ # relative to the original script's directory.
104
+ mag_data_abs_path = original_script_dir / "data" / mag_folder_name
105
+
106
  return [
107
  datasets.SplitGenerator(
108
  name=datasets.Split.TRAIN,
109
  gen_kwargs={
110
+ "mag_folder_abs_path": mag_data_abs_path, # Pass the absolute Path object
111
  "mag_level": self.config.mag_level,
112
  },
113
  ),
114
  ]
115
 
116
+ def _generate_examples(self, mag_folder_abs_path: Path, mag_level: str):
117
  idx = 0
118
  for i in range(16):
119
  img_filename = f"{i}.jpg"
120
  feat_filename = f"{i}_ssl_feat.npy"
121
 
122
+ img_path = mag_folder_abs_path / img_filename
123
+ feat_path = mag_folder_abs_path / feat_filename
124
 
 
 
125
 
126
  ssl_feat_data = np.load(feat_path)
 
127
  processed_feature = preprocess_features(ssl_feat_data)
128
 
129
  yield idx, {
130
+ "image": str(img_path), # datasets.Image() handles path strings
131
+ "ssl_feat": processed_feature,
132
+ "filename_img": img_filename,
133
+ "filename_feat": feat_filename,
134
  "mag": _MAG_DICT[mag_level],
135
  }
136
  idx += 1