| 4247 | |
| 4248 | |
| 4249 | class ColorCorrector(torch.nn.Module): |
| 4250 | def __init__( |
| 4251 | self, |
| 4252 | correction_type: str = 'wrgb', |
| 4253 | ): |
| 4254 | """ |
| 4255 | Apply the color correction to an rgbd_image |
| 4256 | |
| 4257 | Args: |
| 4258 | correction_type: |
| 4259 | 'wrgb': the correction is 3 scalars \in [0, 1] that multiply to RGB channels separately |
| 4260 | 'identify': do nothing |
| 4261 | """ |
| 4262 | super().__init__() |
| 4263 | self.correction_type = correction_type |
| 4264 | if self.correction_type == 'wrgb': |
| 4265 | self.wrgb = torch.nn.parameter.Parameter(torch.ones(3)) |
| 4266 | elif self.correction_type == 'identify': |
| 4267 | self.register_buffer('wrgb', torch.ones(3)) |
| 4268 | else: |
| 4269 | raise NotImplementedError |
| 4270 | |
| 4271 | def get_extra_state(self): |
| 4272 | return dict( |
| 4273 | correction_type=self.correction_type, |
| 4274 | ) |
| 4275 | |
| 4276 | def set_extra_state(self, state): |
| 4277 | self.correction_type = state['correction_type'] |
| 4278 | |
| 4279 | def forward( |
| 4280 | self, |
| 4281 | x: torch.Tensor, |
| 4282 | ) -> torch.Tensor: |
| 4283 | """ |
| 4284 | apply the color correction |
| 4285 | Args: |
| 4286 | x: |
| 4287 | (*, 3) |
| 4288 | Returns: |
| 4289 | (*, 3) corrected x |
| 4290 | """ |
| 4291 | if self.correction_type == 'wrgb': |
| 4292 | y = x * self.wrgb.reshape(*([1] * (x.ndim - 1)), -1) |
| 4293 | return y |
| 4294 | elif self.correction_type == 'identify': |
| 4295 | return x |
| 4296 | else: |
| 4297 | raise NotImplementedError |
nothing calls this directly
no outgoing calls
no test coverage detected