File size: 4,259 Bytes
fd23380 ef632b3 fd23380 af2a55f fd23380 af2a55f fd23380 efddcf9 fd23380 ef632b3 efddcf9 ef632b3 fd23380 ef632b3 fd23380 ef632b3 fd23380 efddcf9 fd23380 efddcf9 ef632b3 fd23380 af2a55f efddcf9 fd23380 ef632b3 fd23380 efddcf9 ef632b3 efddcf9 fd23380 bc4ff1e efddcf9 fd23380 af2a55f ef632b3 bc4ff1e d219d08 fd23380 bc4ff1e fd23380 bc4ff1e fd23380 bc4ff1e e8b9892 fd23380 9bae667 9949a18 fd23380 efddcf9 fd23380 bc4ff1e efddcf9 fd23380 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import datasets
import numpy as np
from pathlib import Path
import torch
import torch.nn.functional as F
_DATASET_VERSION = datasets.Version("1.0.0")
_N_EMBED = {
"20x": 1,
"10x": 4,
"5x": 16,
"2_5x": 64,
"1_25x": 256,
}
_MAG_DICT = {
"20x": 0,
"10x": 1,
"5x": 2,
"2_5x": 3,
"1_25x": 4,
}
_FIXED_SSL_FEATURE_DIM_1 = 1024
def get_ssl_feat_shape(mag_level, pool=False):
first_dim = _N_EMBED[mag_level]
h = int(np.sqrt(first_dim))
if pool:
h = min(h, 8)
return (_FIXED_SSL_FEATURE_DIM_1, h, h)
def preprocess_features(feat_array):
if len(feat_array.shape) == 1:
feat_array = feat_array[:, None]
mean = feat_array.mean(axis=0, keepdims=True)
std = feat_array.std(axis=0, keepdims=True)
feat_array = (feat_array - mean) / (std + 1e-8)
return feat_array
class MagnificationConfig(datasets.BuilderConfig):
def __init__(self, mag_level=None, ssl_feat_shape_pooled=None, ssl_feat_shape_unpooled=None, data_dir=None, **kwargs):
super(MagnificationConfig, self).__init__(**kwargs)
self.mag_level = mag_level
self.ssl_feat_shape_pooled = ssl_feat_shape_pooled
self.ssl_feat_shape_unpooled = ssl_feat_shape_unpooled
self.data_dir = data_dir
class TCGADataset(datasets.GeneratorBasedBuilder):
VERSION = _DATASET_VERSION
BUILDER_CONFIGS = []
for mag_level_str in _MAG_DICT.keys():
builder_config_instance = MagnificationConfig(
name=mag_level_str,
version=_DATASET_VERSION,
description=f"Dataset at {mag_level_str} mag",
data_dir=mag_level_str,
mag_level=mag_level_str,
ssl_feat_shape_pooled=get_ssl_feat_shape(mag_level_str, pool=True),
ssl_feat_shape_unpooled=get_ssl_feat_shape(mag_level_str, pool=False)
)
BUILDER_CONFIGS.append(builder_config_instance)
DEFAULT_CONFIG_NAME = "20x"
def _info(self):
return datasets.DatasetInfo(
description=f"Dataset with images and SSL features. Configuration: {self.config.name}",
features=datasets.Features(
{
"image": datasets.Image(),
"ssl_feat": datasets.Array3D(shape=self.config.ssl_feat_shape_pooled, dtype="float32"),
"ssl_feat_unpooled": datasets.Array3D(shape=self.config.ssl_feat_shape_unpooled, dtype="float32"),
"mag": datasets.Value("int32"),
}
),
homepage="https://github.com/cvlab-stonybrook/ZoomLDM",
)
def _split_generators(self, dl_manager):
original_script_dir = Path(self.base_path)
mag_folder_name = self.config.data_dir
mag_data_abs_path = original_script_dir / "data" / mag_folder_name
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"mag_folder_abs_path": mag_data_abs_path,
"mag_level": self.config.mag_level,
},
),
]
def _generate_examples(self, mag_folder_abs_path: Path, mag_level: str):
idx = 0
for i in range(16):
img_filename = f"{i}.jpg"
feat_filename = f"{i}_ssl_feat.npy"
img_path = mag_folder_abs_path / img_filename
feat_path = mag_folder_abs_path / feat_filename
ssl_feat_data = np.load(feat_path)
ssl_feat_data = np.float32(ssl_feat_data) # Cast to float32
processed_feature = preprocess_features(ssl_feat_data)
h = np.sqrt(processed_feature.shape[1]).astype(int)
feat_array_unpooled = torch.tensor(processed_feature.reshape((-1, h, h)))
if h > 8:
shape = (8, 8)
feat_array_pooled = F.adaptive_avg_pool2d(feat_array_unpooled, shape)
else:
feat_array_pooled = feat_array_unpooled
yield idx, {
"image": str(img_path),
"ssl_feat": feat_array_pooled,
"ssl_feat_unpooled": feat_array_unpooled,
"mag": _MAG_DICT[mag_level],
}
idx += 1 |