Skip to content

Commit 686cd31

Browse files
author
Edward Grigoryan
committed
png compression uses 4 times smaller chunks now
1 parent b7b2db6 commit 686cd31

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

benchmarks/bench_png_compression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
img_path = "./benchmarks/sample.png"
11-
count = 12
11+
count = 20
1212

1313

1414
def bench_pil_compression(img_path=img_path, count=count):

hub/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CHUNK_DEFAULT_SIZE = 2 ** 24
22
OBJECT_CHUNK = 128
3+
DEFAULT_COMPRESSOR = "default"

hub/store/dynamic_tensor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from hub.store.nested_store import NestedStore
1212
from hub.store.shape_detector import ShapeDetector
13+
from hub.defaults import DEFAULT_COMPRESSOR
1314

1415
from hub.exceptions import (
1516
DynamicTensorNotFoundException,
@@ -42,7 +43,7 @@ def __init__(
4243
max_shape=None,
4344
dtype="float64",
4445
chunks=None,
45-
compressor="default",
46+
compressor=DEFAULT_COMPRESSOR,
4647
):
4748
"""Constructor
4849
Parameters
@@ -64,7 +65,9 @@ def __init__(
6465
"""
6566
if not (shape is None):
6667
# otherwise shape detector fails
67-
shapeDt = ShapeDetector(shape, max_shape, chunks, dtype)
68+
shapeDt = ShapeDetector(
69+
shape, max_shape, chunks, dtype, compressor=compressor
70+
)
6871
shape = shapeDt.shape
6972
max_shape = shapeDt.max_shape
7073
chunks = shapeDt.chunks

hub/store/shape_detector.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from hub.numcodecs import PngCodec
12
import math
23

34
import numpy as np
45

5-
from hub.defaults import CHUNK_DEFAULT_SIZE, OBJECT_CHUNK
6+
from hub.defaults import CHUNK_DEFAULT_SIZE, OBJECT_CHUNK, DEFAULT_COMPRESSOR
67
from hub.exceptions import HubException
78

89

@@ -17,19 +18,27 @@ def __init__(
1718
dtype="float64",
1819
chunksize=CHUNK_DEFAULT_SIZE,
1920
object_chunking=OBJECT_CHUNK,
21+
compressor=DEFAULT_COMPRESSOR,
2022
):
2123
self._int32max = np.iinfo(np.dtype("int32")).max
2224

2325
self._dtype = dtype = np.dtype(dtype)
24-
self._chunksize = chunksize
2526
self._object_chunking = object_chunking
27+
self._compressor = compressor
2628

29+
self._chunksize = chunksize = self._get_chunksize(chunksize, compressor)
2730
self._shape = shape = self._get_shape(shape)
2831
self._max_shape = max_shape = self._get_max_shape(shape, max_shape)
2932
self._chunks = chunks = self._get_chunks(
3033
shape, max_shape, chunks, dtype, chunksize
3134
)
3235

36+
def _get_chunksize(self, chunksize, compressor):
37+
if isinstance(compressor, PngCodec):
38+
return int(math.ceil(0.25 * chunksize))
39+
else:
40+
return chunksize
41+
3342
def _get_shape(self, shape):
3443
assert shape is not None
3544
shape = (shape,) if isinstance(shape, int) else tuple(shape)

hub/store/tests/test_shape_detector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ def test_shape_detector():
88
assert s.chunks[1:] == (10, 10)
99

1010

11+
def test_shape_detector_2():
12+
s = ShapeDetector((10, 10, 10), 10, compressor="png")
13+
assert str(s.dtype) == "float64"
14+
assert s.chunks[1:] == (10, 10)
15+
16+
1117
def test_shape_detector_wrong_shape():
1218
try:
1319
ShapeDetector((10, 10, 10), (10, 10, 20))

0 commit comments

Comments
 (0)