Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing HDF5Matrix .dtype and .shape properties #10749

Merged
merged 3 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions keras/utils/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def __init__(self, datapath, dataset, start=0, end=None, normalizer=None):
else:
self.end = end
self.normalizer = normalizer
if self.normalizer is not None:
first_val = self.normalizer(self.data[0:1])
else:
first_val = self.data[0:1]
self._base_shape = first_val.shape[1:]
self._base_dtype = first_val.dtype

def __len__(self):
return self.end - self.start
Expand Down Expand Up @@ -101,7 +107,7 @@ def shape(self):
# Returns
A numpy-style shape tuple.
"""
return (self.end - self.start,) + self.data.shape[1:]
return (self.end - self.start,) + self._base_shape

@property
def dtype(self):
Expand All @@ -110,7 +116,7 @@ def dtype(self):
# Returns
A numpy dtype string.
"""
return self.data.dtype
return self._base_dtype

@property
def ndim(self):
Expand Down
10 changes: 10 additions & 0 deletions tests/keras/utils/io_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def test_io_utils(in_tmpdir):
normalized_X_train = HDF5Matrix(h5_path, 'my_data', start=0, end=150, normalizer=normalizer)
assert np.isclose(normalized_X_train[0][0], X_train[0][0] + 1)

# test resizing normalizer
normalizer_rs = lambda x: x[:, ::2]
normalized_rs_X_train = HDF5Matrix(h5_path, 'my_data', start=0, end=150, normalizer=normalizer_rs)
assert (normalized_rs_X_train.shape[1] == 5)

# test dtype changing normalizer
normalizer_dtype = lambda x: x.astype(np.uint8)
normalized_dtype_X_train = HDF5Matrix(h5_path, 'my_data', start=0, end=150, normalizer=normalizer_dtype)
assert (normalized_dtype_X_train.dtype == np.uint8)

os.remove(h5_path)


Expand Down