Skip to content

Commit 9213bf1

Browse files
priyakasimbegcopybara-github
authored andcommitted
internal change
PiperOrigin-RevId: 904380323
1 parent 68ddca3 commit 9213bf1

1 file changed

Lines changed: 57 additions & 5 deletions

File tree

init2winit/dataset_lib/fastmri_dataset.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import itertools
2020
import os
2121

22+
from absl import logging
2223
import h5py
2324
from init2winit.dataset_lib import data_utils
2425
import jax
@@ -212,9 +213,11 @@ def load_split(per_host_batch_size, split, hps, shuffle_rng=None):
212213
# entirely to the end of it on the last host, because otherwise we will drop
213214
# the last `{train,valid}_size % split_size` elements.
214215
if jax.process_index() == jax.process_count() - 1:
215-
if split == 'val':
216+
if split in ['train', 'eval_train']:
217+
end = hps.num_train_h5_files
218+
elif split == 'val':
216219
end = hps.num_valid_h5_files
217-
else:
220+
else: # split == 'test'
218221
end = hps.num_test_h5_files + hps.num_valid_h5_files
219222

220223
data_dir = hps.data_dir
@@ -229,9 +232,58 @@ def load_split(per_host_batch_size, split, hps, shuffle_rng=None):
229232
else: # split == 'val'
230233
data_dir = os.path.join(data_dir, hps.val_dir)
231234

232-
h5_paths = [
233-
os.path.join(data_dir, path) for path in listdir(data_dir)
234-
][start:end]
235+
try:
236+
all_files = listdir(data_dir)
237+
except tf.errors.NotFoundError as e:
238+
raise FileNotFoundError(
239+
f'FastMRI data directory not found: {data_dir}.'
240+
) from e
241+
242+
h5_paths = [os.path.join(data_dir, path) for path in all_files][start:end]
243+
244+
if not h5_paths:
245+
raise ValueError(
246+
f'No h5 files found for split={split} in {data_dir} '
247+
f'(start={start}, end={end}, total files={len(all_files)}).'
248+
)
249+
logging.info(
250+
'FastMRI %s split: loaded %d h5 paths from %s (files %d-%d of %d).',
251+
split,
252+
len(h5_paths),
253+
data_dir,
254+
start,
255+
end,
256+
len(all_files),
257+
)
258+
259+
# Probe-read the first h5 file to catch ACL / connectivity errors early,
260+
# before they are silently swallowed by tf.data.from_generator.
261+
probe_path = h5_paths[0]
262+
try:
263+
with gfile.GFile(probe_path, 'rb') as gf:
264+
with h5py.File(gf, 'r') as hf:
265+
if 'kspace' not in hf:
266+
raise ValueError(
267+
f'FastMRI h5 file {probe_path} is missing the "kspace" dataset. '
268+
'The file may be corrupt or incomplete.'
269+
)
270+
logging.info(
271+
'FastMRI probe read of %s succeeded: %d slices.',
272+
probe_path,
273+
hf['kspace'].shape[0],
274+
)
275+
except PermissionError as e:
276+
raise PermissionError(
277+
f'Cannot read FastMRI h5 file {probe_path}: permission denied. '
278+
'Check that the Borg job gfs_user has access to the FastMRI Placer '
279+
) from e
280+
except (OSError, IOError) as e:
281+
raise OSError(
282+
f'Cannot read FastMRI h5 file {probe_path}: {e}. '
283+
'This may indicate a timeout, network issue, or that the Placer '
284+
'fileset is not replicated to a nearby cell. See b/329522685.'
285+
) from e
286+
235287
ds = tf.data.Dataset.from_tensor_slices(h5_paths)
236288
ds = ds.interleave(
237289
_create_generator,

0 commit comments

Comments
 (0)