MCPcopy Create free account
hub / github.com/NVIDIA/DALI / generate_data

Function generate_data

dali/test/python/test_dali_variable_batch_size.py:72–118  ·  view source on GitHub ↗

Generates an epoch of data, that will be used for variable batch size verification. :param max_batch_size: Actual sizes of every batch in the epoch will be less or equal to max_batch_size :param n_iter: Number of iterations in the epoch :param sample_shap

(max_batch_size, n_iter, sample_shape, lo=0.0, hi=1.0, dtype=np.float32)

Source from the content-addressed store, hash-verified

70
71
72def generate_data(max_batch_size, n_iter, sample_shape, lo=0.0, hi=1.0, dtype=np.float32):
73 """
74 Generates an epoch of data, that will be used for variable batch size verification.
75
76 :param max_batch_size: Actual sizes of every batch in the epoch will be less or equal
77 to max_batch_size
78 :param n_iter: Number of iterations in the epoch
79 :param sample_shape: If sample_shape is callable, shape of every sample will be determined by
80 calling sample_shape. In this case, every call to sample_shape has to
81 return a tuple of integers. If sample_shape is a tuple, this will be a
82 shape of every sample.
83 :param lo: Begin of the random range
84 :param hi: End of the random range
85 :param dtype: Numpy data type
86 :return: An epoch of data
87 """
88 batch_sizes = np.array([max_batch_size // 2, max_batch_size // 4, max_batch_size])
89
90 if isinstance(sample_shape, tuple):
91
92 def sample_shape_wrapper():
93 return sample_shape
94
95 size_fn = sample_shape_wrapper
96 elif inspect.isfunction(sample_shape):
97 size_fn = sample_shape
98 else:
99 raise RuntimeError(
100 "`sample_shape` shall be either a tuple or a callable. "
101 "Provide `(val,)` tuple for 1D shape"
102 )
103
104 if np.issubdtype(dtype, np.integer):
105 return [
106 np.random.randint(lo, hi, size=(bs,) + size_fn(), dtype=dtype) for bs in batch_sizes
107 ]
108 elif np.issubdtype(dtype, np.float32):
109 ret = (np.random.random_sample(size=(bs,) + size_fn()) for bs in batch_sizes)
110 ret = map(lambda batch: (hi - lo) * batch + lo, ret)
111 ret = map(lambda batch: batch.astype(dtype), ret)
112 return list(ret)
113 elif np.issubdtype(dtype, bool):
114 assert isinstance(lo, bool)
115 assert isinstance(hi, bool)
116 return [np.random.choice(a=[lo, hi], size=(bs,) + size_fn()) for bs in batch_sizes]
117 else:
118 raise RuntimeError(f"Invalid type argument: {dtype}")
119
120
121def single_op_pipeline(

Callers 15

image_data_helperFunction · 0.70
float_array_helperFunction · 0.70
sequence_op_helperFunction · 0.70
random_op_helperFunction · 0.70
test_external_sourceFunction · 0.70
test_batch_permuteFunction · 0.70
test_coin_flipFunction · 0.70
test_uniformFunction · 0.70
test_random_choiceFunction · 0.70
test_random_normalFunction · 0.70
test_random_betaFunction · 0.70
no_input_op_helperFunction · 0.70

Calls 2

size_fnFunction · 0.85
random_sampleMethod · 0.80

Tested by

no test coverage detected