From 8330f09b10accbdc40c2e7106ddccd8229559eb9 Mon Sep 17 00:00:00 2001 From: "Wu, Zhenyu" Date: Sun, 9 Aug 2026 20:58:50 +0800 Subject: [PATCH] Remove clamp_(0, 1) postprocessing should not occur in neural network interference. For quantization, it is incorrect to assume the range is [0, 1]. --- compressai/models/base.py | 2 +- compressai/models/cca.py | 2 +- compressai/models/dcae.py | 2 +- compressai/models/google.py | 8 ++++---- compressai/models/mlic.py | 2 +- compressai/models/saaf.py | 2 +- compressai/models/stf.py | 2 +- compressai/models/vbr.py | 6 +++--- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/compressai/models/base.py b/compressai/models/base.py index 2fd63654..7be61187 100644 --- a/compressai/models/base.py +++ b/compressai/models/base.py @@ -205,7 +205,7 @@ def compress(self, x): def decompress(self, *args, **kwargs): y_out = self.latent_codec.decompress(*args, **kwargs) y_hat = y_out["y_hat"] - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return { "x_hat": x_hat, } diff --git a/compressai/models/cca.py b/compressai/models/cca.py index 106c519f..e3d00975 100644 --- a/compressai/models/cca.py +++ b/compressai/models/cca.py @@ -684,7 +684,7 @@ def decompress( shape: Dict[str, Tuple[int, ...]], ) -> Dict[str, Tensor]: y_out = self.latent_codec.decompress(strings, shape) - return {"x_hat": self.g_s(y_out["y_hat"]).clamp_(0, 1)} + return {"x_hat": self.g_s(y_out["y_hat"])} def update( self, scale_table: Optional[Tensor] = None, force: bool = False, **kwargs diff --git a/compressai/models/dcae.py b/compressai/models/dcae.py index 31f13056..05aac7a4 100644 --- a/compressai/models/dcae.py +++ b/compressai/models/dcae.py @@ -690,7 +690,7 @@ def decompress( self, strings: Sequence[Sequence[bytes]], shape: Sequence[int] ) -> Dict[str, Tensor]: out = self.latent_codec.decompress(strings, shape) - return {"x_hat": self.g_s(out["y_hat"]).clamp_(0, 1)} + return {"x_hat": self.g_s(out["y_hat"])} @classmethod def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "DCAE": diff --git a/compressai/models/google.py b/compressai/models/google.py index 712ce031..a318c432 100644 --- a/compressai/models/google.py +++ b/compressai/models/google.py @@ -159,7 +159,7 @@ def compress(self, x): def decompress(self, strings, shape): assert isinstance(strings, list) and len(strings) == 1 y_hat = self.entropy_bottleneck.decompress(strings[0], shape) - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} @@ -329,7 +329,7 @@ def decompress(self, strings, shape): scales_hat = self.h_s(z_hat) indexes = self.gaussian_conditional.build_indexes(scales_hat) y_hat = self.gaussian_conditional.decompress(strings[0], indexes, z_hat.dtype) - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} @@ -426,7 +426,7 @@ def decompress(self, strings, shape): y_hat = self.gaussian_conditional.decompress( strings[0], indexes, means=means_hat ) - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} @@ -688,7 +688,7 @@ def decompress(self, strings, shape): ) y_hat = F.pad(y_hat, (-padding, -padding, -padding, -padding)) - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} def _decompress_ar( diff --git a/compressai/models/mlic.py b/compressai/models/mlic.py index 0299685f..2e1fee5e 100644 --- a/compressai/models/mlic.py +++ b/compressai/models/mlic.py @@ -340,7 +340,7 @@ def decompress( shape: Dict[str, Union[List[Tuple[int, ...]], Tuple[int, ...]]], ) -> Dict[str, Tensor]: y_out = self.latent_codec.decompress(strings, shape) - return {"x_hat": self.g_s(y_out["y_hat"]).clamp_(0, 1)} + return {"x_hat": self.g_s(y_out["y_hat"])} @classmethod def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "_BaseMLIC": diff --git a/compressai/models/saaf.py b/compressai/models/saaf.py index 88387ed5..37b43bbe 100644 --- a/compressai/models/saaf.py +++ b/compressai/models/saaf.py @@ -878,7 +878,7 @@ def decompress( self, strings: Sequence[Sequence[bytes]], shape: Sequence[int] ) -> Dict[str, Tensor]: out = self.latent_codec.decompress(strings, shape) - return {"x_hat": self._decode(out["y_hat"]).clamp_(0, 1)} + return {"x_hat": self._decode(out["y_hat"])} @classmethod def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "SAAF": diff --git a/compressai/models/stf.py b/compressai/models/stf.py index e01ac586..b7b0ce36 100644 --- a/compressai/models/stf.py +++ b/compressai/models/stf.py @@ -741,7 +741,7 @@ def decompress( y_out = self.latent_codec.decompress(strings, shape) y_hat = y_out["y_hat"] height, width = y_hat.shape[2:] - return {"x_hat": self._synthesis_transform(y_hat, height, width).clamp_(0, 1)} + return {"x_hat": self._synthesis_transform(y_hat, height, width)} @classmethod def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "SymmetricalTransFormer": diff --git a/compressai/models/vbr.py b/compressai/models/vbr.py index 35769333..15241d80 100644 --- a/compressai/models/vbr.py +++ b/compressai/models/vbr.py @@ -297,7 +297,7 @@ def decompress(self, strings, shape, stage: int = 2, s: int = 1, inputscale=0): y_hat = signs * (q_abs + q_offsets) y_ch_means = 0 y_hat = y_hat * rescale + y_ch_means - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} @@ -499,7 +499,7 @@ def decompress(self, strings, shape, stage: int = 2, s: int = 1, inputscale=0): y_hat = signs * (q_abs + q_offsets) y_hat = y_hat * rescale + means_hat - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} @@ -866,7 +866,7 @@ def decompress(self, strings, shape, stage: int = 2, s: int = 1, inputscale=0): ) y_hat = F.pad(y_hat, (-padding, -padding, -padding, -padding)) - x_hat = self.g_s(y_hat).clamp_(0, 1) + x_hat = self.g_s(y_hat) return {"x_hat": x_hat} def _decompress_ar( # noqa: C901