Skip to content

Commit 5a976a4

Browse files
committed
Remove clamp_(0, 1)
postprocessing should not occur in neural network interference. For quantization, it is incorrect to assume the range is [0, 1].
1 parent 8de6133 commit 5a976a4

8 files changed

Lines changed: 13 additions & 13 deletions

File tree

compressai/models/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def compress(self, x):
205205
def decompress(self, *args, **kwargs):
206206
y_out = self.latent_codec.decompress(*args, **kwargs)
207207
y_hat = y_out["y_hat"]
208-
x_hat = self.g_s(y_hat).clamp_(0, 1)
208+
x_hat = self.g_s(y_hat)
209209
return {
210210
"x_hat": x_hat,
211211
}

compressai/models/cca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ def decompress(
684684
shape: Dict[str, Tuple[int, ...]],
685685
) -> Dict[str, Tensor]:
686686
y_out = self.latent_codec.decompress(strings, shape)
687-
return {"x_hat": self.g_s(y_out["y_hat"]).clamp_(0, 1)}
687+
return {"x_hat": self.g_s(y_out["y_hat"])}
688688

689689
def update(
690690
self, scale_table: Optional[Tensor] = None, force: bool = False, **kwargs

compressai/models/dcae.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def decompress(
690690
self, strings: Sequence[Sequence[bytes]], shape: Sequence[int]
691691
) -> Dict[str, Tensor]:
692692
out = self.latent_codec.decompress(strings, shape)
693-
return {"x_hat": self.g_s(out["y_hat"]).clamp_(0, 1)}
693+
return {"x_hat": self.g_s(out["y_hat"])}
694694

695695
@classmethod
696696
def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "DCAE":

compressai/models/google.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def compress(self, x):
159159
def decompress(self, strings, shape):
160160
assert isinstance(strings, list) and len(strings) == 1
161161
y_hat = self.entropy_bottleneck.decompress(strings[0], shape)
162-
x_hat = self.g_s(y_hat).clamp_(0, 1)
162+
x_hat = self.g_s(y_hat)
163163
return {"x_hat": x_hat}
164164

165165

@@ -329,7 +329,7 @@ def decompress(self, strings, shape):
329329
scales_hat = self.h_s(z_hat)
330330
indexes = self.gaussian_conditional.build_indexes(scales_hat)
331331
y_hat = self.gaussian_conditional.decompress(strings[0], indexes, z_hat.dtype)
332-
x_hat = self.g_s(y_hat).clamp_(0, 1)
332+
x_hat = self.g_s(y_hat)
333333
return {"x_hat": x_hat}
334334

335335

@@ -426,7 +426,7 @@ def decompress(self, strings, shape):
426426
y_hat = self.gaussian_conditional.decompress(
427427
strings[0], indexes, means=means_hat
428428
)
429-
x_hat = self.g_s(y_hat).clamp_(0, 1)
429+
x_hat = self.g_s(y_hat)
430430
return {"x_hat": x_hat}
431431

432432

@@ -688,7 +688,7 @@ def decompress(self, strings, shape):
688688
)
689689

690690
y_hat = F.pad(y_hat, (-padding, -padding, -padding, -padding))
691-
x_hat = self.g_s(y_hat).clamp_(0, 1)
691+
x_hat = self.g_s(y_hat)
692692
return {"x_hat": x_hat}
693693

694694
def _decompress_ar(

compressai/models/mlic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def decompress(
340340
shape: Dict[str, Union[List[Tuple[int, ...]], Tuple[int, ...]]],
341341
) -> Dict[str, Tensor]:
342342
y_out = self.latent_codec.decompress(strings, shape)
343-
return {"x_hat": self.g_s(y_out["y_hat"]).clamp_(0, 1)}
343+
return {"x_hat": self.g_s(y_out["y_hat"])}
344344

345345
@classmethod
346346
def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "_BaseMLIC":

compressai/models/saaf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ def decompress(
878878
self, strings: Sequence[Sequence[bytes]], shape: Sequence[int]
879879
) -> Dict[str, Tensor]:
880880
out = self.latent_codec.decompress(strings, shape)
881-
return {"x_hat": self._decode(out["y_hat"]).clamp_(0, 1)}
881+
return {"x_hat": self._decode(out["y_hat"])}
882882

883883
@classmethod
884884
def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "SAAF":

compressai/models/stf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def decompress(
741741
y_out = self.latent_codec.decompress(strings, shape)
742742
y_hat = y_out["y_hat"]
743743
height, width = y_hat.shape[2:]
744-
return {"x_hat": self._synthesis_transform(y_hat, height, width).clamp_(0, 1)}
744+
return {"x_hat": self._synthesis_transform(y_hat, height, width)}
745745

746746
@classmethod
747747
def from_state_dict(cls, state_dict: Dict[str, Tensor]) -> "SymmetricalTransFormer":

compressai/models/vbr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def decompress(self, strings, shape, stage: int = 2, s: int = 1, inputscale=0):
297297
y_hat = signs * (q_abs + q_offsets)
298298
y_ch_means = 0
299299
y_hat = y_hat * rescale + y_ch_means
300-
x_hat = self.g_s(y_hat).clamp_(0, 1)
300+
x_hat = self.g_s(y_hat)
301301
return {"x_hat": x_hat}
302302

303303

@@ -499,7 +499,7 @@ def decompress(self, strings, shape, stage: int = 2, s: int = 1, inputscale=0):
499499

500500
y_hat = signs * (q_abs + q_offsets)
501501
y_hat = y_hat * rescale + means_hat
502-
x_hat = self.g_s(y_hat).clamp_(0, 1)
502+
x_hat = self.g_s(y_hat)
503503
return {"x_hat": x_hat}
504504

505505

@@ -866,7 +866,7 @@ def decompress(self, strings, shape, stage: int = 2, s: int = 1, inputscale=0):
866866
)
867867

868868
y_hat = F.pad(y_hat, (-padding, -padding, -padding, -padding))
869-
x_hat = self.g_s(y_hat).clamp_(0, 1)
869+
x_hat = self.g_s(y_hat)
870870
return {"x_hat": x_hat}
871871

872872
def _decompress_ar( # noqa: C901

0 commit comments

Comments
 (0)