(
batch,
error_str="Unsupported callback return type. Got: `{}`.",
non_uniform_str="Uniform input is required (batch of tensors of equal shapes), got {}.",
)
| 143 | |
| 144 | |
| 145 | def batch_to_numpy( |
| 146 | batch, |
| 147 | error_str="Unsupported callback return type. Got: `{}`.", |
| 148 | non_uniform_str="Uniform input is required (batch of tensors of equal shapes), got {}.", |
| 149 | ): |
| 150 | import_numpy() |
| 151 | assert_cpu_batch_data_type(batch, error_str) |
| 152 | if isinstance(batch, tensors.TensorListCPU): |
| 153 | if not batch.is_dense_tensor(): |
| 154 | raise ValueError(non_uniform_str.format(batch)) |
| 155 | return batch.as_array() |
| 156 | elif isinstance(batch, list): |
| 157 | result = [sample_to_numpy(sample, error_str) for sample in batch] |
| 158 | first_shape = result[0].shape |
| 159 | for sample in result: |
| 160 | if first_shape != sample.shape: |
| 161 | raise ValueError(non_uniform_str.format(batch)) |
| 162 | return np.stack(result) |
| 163 | else: |
| 164 | return sample_to_numpy(batch, error_str) |
| 165 | |
| 166 | |
| 167 | class _CycleIter: |
no test coverage detected