|
|
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) |
|
|
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 |