util
Utilities for FastEstimator.
Suppressor
¶
Bases: object
A class which can be used to silence output of function calls.
This class is intentionally not @traceable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_pyprint |
bool
|
Whether to allow python printing to occur still within this scope (and therefore only silence printing from non-python sources like c). |
False
|
show_if_exception |
bool
|
Whether to retroactively show print messages if an exception is raised from within the suppressor scope. |
False
|
Source code in fastestimator/fastestimator/util/util.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
flush
¶
teardown
staticmethod
¶
Clean up the stash files when the program exists
Source code in fastestimator/fastestimator/util/util.py
write
¶
A function which is invoked during print calls.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dummy |
str
|
The string which wanted to be printed. |
required |
Source code in fastestimator/fastestimator/util/util.py
Timer
¶
Bases: ContextDecorator
A class that can be used to time things.
This class is intentionally not @traceable.
x = lambda: list(map(lambda i: i + i/2, list(range(int(1e6)))))
with fe.util.Timer():
x() # Task took 0.1639 seconds
@fe.util.Timer("T2")
def func():
return x()
func() # T2 took 0.14819 seconds
Source code in fastestimator/fastestimator/util/util.py
cpu_count
cached
¶
Determine the number of available CPUs (correcting for docker container limits).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limit |
Optional[int]
|
If provided, the TF and Torch backends will be told to use |
None
|
Returns:
Type | Description |
---|---|
int
|
The nuber of available CPUs (correcting for docker container limits), or the user provided |
Raises:
Type | Description |
---|---|
ValueError
|
If a |
Source code in fastestimator/fastestimator/util/util.py
detach_tensors
¶
Detach tensor (collections) from current graph recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
T
|
The data to be detached. |
required |
Returns:
Type | Description |
---|---|
T
|
Output data. |
Source code in fastestimator/fastestimator/util/util.py
draw
¶
get_batch_size
¶
Infer batch size from a batch dictionary. It will ignore all dictionary value with data type that doesn't have "shape" attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
The batch dictionary. |
required |
Returns:
Type | Description |
---|---|
int
|
batch size. |
Source code in fastestimator/fastestimator/util/util.py
get_device
cached
¶
Get the torch device for the current hardware.
Returns:
Type | Description |
---|---|
device
|
The torch device most appropriate for the current hardware. |
Source code in fastestimator/fastestimator/util/util.py
get_gpu_info
cached
¶
Get summaries of all of the GPUs accessible on this machine.
Returns:
Type | Description |
---|---|
List[str]
|
A formatted summary of the GPUs available on the machine (one list entry per GPU). |
Source code in fastestimator/fastestimator/util/util.py
get_num_devices
cached
¶
Determine the number of available GPUs.
Returns:
Type | Description |
---|---|
int
|
The number of available GPUs, or 1 if none are found. |
get_num_gpus
cached
¶
Get the number of GPUs available.
Returns:
Type | Description |
---|---|
int
|
The number of GPUs available. |
Source code in fastestimator/fastestimator/util/util.py
is_valid_file
¶
Validate whether file is valid or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
location of the input file. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the file is valid. |
Source code in fastestimator/fastestimator/util/util.py
move_tensors_to_device
¶
Move torch tensor (collections) between gpu and cpu recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
T
|
The input data to be moved. |
required |
device |
Union[str, device]
|
The target device. |
required |
Returns:
Type | Description |
---|---|
T
|
Output data. |
Source code in fastestimator/fastestimator/util/util.py
pad_batch
¶
A function to pad a batch of data in-place by appending to the ends of the tensors. Tensor type needs to be numpy array otherwise would get ignored. (tf.Tensor and torch.Tensor will cause error)
data = [{"x": np.ones((2, 2)), "y": 8}, {"x": np.ones((3, 1)), "y": 4}]
fe.util.pad_batch(data, pad_value=0)
print(data) # [{'x': [[1., 1.], [1., 1.], [0., 0.]], 'y': 8}, {'x': [[1., 0.], [1., 0.], [1., 0.]]), 'y': 4}]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
List[MutableMapping[str, ndarray]]
|
A list of data to be padded. |
required |
pad_value |
Union[float, int]
|
The value to pad with. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
If the data within the batch do not have matching rank, or have different keys |
Source code in fastestimator/fastestimator/util/util.py
pad_data
¶
Pad data
by appending pad_value
s along it's dimensions until the target_shape
is reached. All entries of
target_shape should be larger than the data.shape, and have the same rank.
x = np.ones((1,2))
x = fe.util.pad_data(x, target_shape=(3, 3), pad_value = -2) # [[1, 1, -2], [-2, -2, -2], [-2, -2, -2]]
x = fe.util.pad_data(x, target_shape=(3, 3, 3), pad_value = -2) # error
x = fe.util.pad_data(x, target_shape=(4, 1), pad_value = -2) # error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
ndarray
|
The data to be padded. |
required |
target_shape |
Tuple[int, ...]
|
The desired shape for |
required |
pad_value |
Union[float, int]
|
The value to insert into |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The |
Source code in fastestimator/fastestimator/util/util.py
to_number
¶
Convert an input value into a Numpy ndarray.
This method can be used with Python and Numpy data:
b = fe.backend.to_number(5) # 5 (type==np.ndarray)
b = fe.backend.to_number(4.0) # 4.0 (type==np.ndarray)
n = np.array([1, 2, 3])
b = fe.backend.to_number(n) # [1, 2, 3] (type==np.ndarray)
This method can be used with TensorFlow tensors:
This method can be used with PyTorch tensors:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[Tensor, Tensor, ndarray, int, float, str]
|
The value to be converted into a np.ndarray. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An ndarray corresponding to the given |