def test_clamp(): """Fix an issue when `lower` or `upper` is 0, it will be recognized as `False` and `F.clamp` will fall into wrong conditions unexpectedly. """ x = np.linspace(-6, 6, dtype="float32") assertTensorClose( F.clamp(tensor(x) + 3, 0, 6).numpy(), np.clip(x + 3, 0, 6)) assertTensorClose( F.clamp(tensor(x) - 3, -6, 0).numpy(), np.clip(x - 3, -6, 0))
def get_clipped_box(boxes, hw): """ Clip the boxes into the image region.""" # x1 >=0 box_x1 = F.clamp(boxes[:, 0::4], lower=0, upper=hw[1]) # y1 >=0 box_y1 = F.clamp(boxes[:, 1::4], lower=0, upper=hw[0]) # x2 < im_info[1] box_x2 = F.clamp(boxes[:, 2::4], lower=0, upper=hw[1]) # y2 < im_info[0] box_y2 = F.clamp(boxes[:, 3::4], lower=0, upper=hw[0]) clip_box = F.concat([box_x1, box_y1, box_x2, box_y2], axis=1) return clip_box
def _apply_lipshitz_constraint(self): """Weight clipping described in [Wasserstein GAN](https://arxiv.org/abs/1701.07875)""" for p in self.parameters(): F.add_update(p, F.clamp(p, lower=-3e-2, upper=3e-2), alpha=0)