diff --git a/compressai/entropy_models/entropy_models.py b/compressai/entropy_models/entropy_models.py index 496ce5a1..499768c3 100644 --- a/compressai/entropy_models/entropy_models.py +++ b/compressai/entropy_models/entropy_models.py @@ -313,17 +313,18 @@ def decompress( cdf = self._quantized_cdf outputs = cdf.new_empty(indexes.size()) + # CDF tables are shared by all strings: convert them only once + cdf_list = cdf.tolist() + cdf_length = self._cdf_length.reshape(-1).int().tolist() + offset = self._offset.reshape(-1).int().tolist() + for i, s in enumerate(strings): values = self.entropy_coder.decode_with_indexes( - s, - indexes[i].reshape(-1).int().tolist(), - cdf.tolist(), - self._cdf_length.reshape(-1).int().tolist(), - self._offset.reshape(-1).int().tolist(), + s, indexes[i].reshape(-1).int().tolist(), cdf_list, cdf_length, offset ) - outputs[i] = torch.tensor( - values, device=outputs.device, dtype=outputs.dtype - ).reshape(outputs[i].size()) + # numpy converts large int lists much faster than torch.tensor + values = np.fromiter(values, dtype=np.int32, count=len(values)) + outputs[i] = torch.from_numpy(values).reshape(outputs[i].size()) outputs = self.dequantize(outputs, means, dtype) return outputs