Upload folder using huggingface_hub
Browse files- ZoomLDM-demo-dataset.py +10 -16
ZoomLDM-demo-dataset.py
CHANGED
|
@@ -3,7 +3,6 @@ import numpy as np
|
|
| 3 |
from pathlib import Path
|
| 4 |
import torch
|
| 5 |
import torch.nn.functional as F
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
_DATASET_VERSION = datasets.Version("1.0.0")
|
| 9 |
|
|
@@ -74,7 +73,7 @@ class TCGADataset(datasets.GeneratorBasedBuilder):
|
|
| 74 |
description=f"Dataset with images and SSL features. Configuration: {self.config.name}",
|
| 75 |
features=datasets.Features(
|
| 76 |
{
|
| 77 |
-
"image": datasets.
|
| 78 |
"ssl_feat": datasets.Array3D(shape=self.config.ssl_feat_shape_pooled, dtype="float32"),
|
| 79 |
"ssl_feat_unpooled": datasets.Array3D(shape=self.config.ssl_feat_shape_unpooled, dtype="float32"),
|
| 80 |
"mag": datasets.Value("int32"),
|
|
@@ -86,35 +85,30 @@ class TCGADataset(datasets.GeneratorBasedBuilder):
|
|
| 86 |
def _split_generators(self, dl_manager):
|
| 87 |
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
return [
|
| 92 |
datasets.SplitGenerator(
|
| 93 |
name=datasets.Split.TRAIN,
|
| 94 |
gen_kwargs={
|
| 95 |
-
"
|
| 96 |
"mag_level": self.config.mag_level,
|
| 97 |
},
|
| 98 |
),
|
| 99 |
]
|
| 100 |
|
| 101 |
-
def _generate_examples(self,
|
| 102 |
idx = 0
|
| 103 |
for i in range(16):
|
| 104 |
img_filename = f"{i}.jpg"
|
| 105 |
feat_filename = f"{i}_ssl_feat.npy"
|
| 106 |
|
| 107 |
-
img_path =
|
| 108 |
-
feat_path =
|
| 109 |
-
|
| 110 |
-
print(f"base path: {self.base_path}")
|
| 111 |
-
print(f"mag folder: {mag_folder}")
|
| 112 |
-
print(f"img path: {img_path}")
|
| 113 |
-
print(f"feat path: {feat_path}")
|
| 114 |
-
|
| 115 |
|
| 116 |
-
img = Image.open(img_path).convert("RGB")
|
| 117 |
-
img_np = np.array(img)
|
| 118 |
|
| 119 |
ssl_feat_data = np.load(feat_path)
|
| 120 |
processed_feature = preprocess_features(ssl_feat_data)
|
|
@@ -131,7 +125,7 @@ class TCGADataset(datasets.GeneratorBasedBuilder):
|
|
| 131 |
|
| 132 |
|
| 133 |
yield idx, {
|
| 134 |
-
"image":
|
| 135 |
"ssl_feat": feat_array_pooled,
|
| 136 |
"ssl_feat_unpooled": feat_array_unpooled,
|
| 137 |
"mag": _MAG_DICT[mag_level],
|
|
|
|
| 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 |
|
|
|
|
| 73 |
description=f"Dataset with images and SSL features. Configuration: {self.config.name}",
|
| 74 |
features=datasets.Features(
|
| 75 |
{
|
| 76 |
+
"image": datasets.Image(),
|
| 77 |
"ssl_feat": datasets.Array3D(shape=self.config.ssl_feat_shape_pooled, dtype="float32"),
|
| 78 |
"ssl_feat_unpooled": datasets.Array3D(shape=self.config.ssl_feat_shape_unpooled, dtype="float32"),
|
| 79 |
"mag": datasets.Value("int32"),
|
|
|
|
| 85 |
def _split_generators(self, dl_manager):
|
| 86 |
|
| 87 |
|
| 88 |
+
original_script_dir = Path(self.base_path)
|
| 89 |
+
mag_folder_name = self.config.data_dir
|
| 90 |
+
|
| 91 |
+
mag_data_abs_path = original_script_dir / "data" / mag_folder_name
|
| 92 |
|
| 93 |
return [
|
| 94 |
datasets.SplitGenerator(
|
| 95 |
name=datasets.Split.TRAIN,
|
| 96 |
gen_kwargs={
|
| 97 |
+
"mag_folder_abs_path": mag_data_abs_path,
|
| 98 |
"mag_level": self.config.mag_level,
|
| 99 |
},
|
| 100 |
),
|
| 101 |
]
|
| 102 |
|
| 103 |
+
def _generate_examples(self, mag_folder_abs_path: Path, mag_level: str):
|
| 104 |
idx = 0
|
| 105 |
for i in range(16):
|
| 106 |
img_filename = f"{i}.jpg"
|
| 107 |
feat_filename = f"{i}_ssl_feat.npy"
|
| 108 |
|
| 109 |
+
img_path = mag_folder_abs_path / img_filename
|
| 110 |
+
feat_path = mag_folder_abs_path / feat_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
|
|
|
|
|
|
| 112 |
|
| 113 |
ssl_feat_data = np.load(feat_path)
|
| 114 |
processed_feature = preprocess_features(ssl_feat_data)
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
yield idx, {
|
| 128 |
+
"image": str(img_path),
|
| 129 |
"ssl_feat": feat_array_pooled,
|
| 130 |
"ssl_feat_unpooled": feat_array_unpooled,
|
| 131 |
"mag": _MAG_DICT[mag_level],
|