Is there an easy way to convert from RGB to Lab color space and vice versa in Pytorch? I want to do it in a differentiable way within the model.
Thanks
Is there an easy way to convert from RGB to Lab color space and vice versa in Pytorch? I want to do it in a differentiable way within the model.
Thanks
I’m not aware of an easy shortcut, but the conversion is a composition of differentiable operations, so you can just implement the forward computation and backprop through it.
You can copy my implementation in go-colorful which includes this conversion here, it does the conversion by passing through XYZ space, follow the code for equations.
The only non-trivial part in the chain is a couple data-dependent switches, you could either use this trick or masked_scatter_ for that, or you might get away with just ignoring them because they apply in the very low-luminosity range only.
Thanks! I think there is a hard conditioning in RGB to LAB conversion (if value>C: …) that makes it non-differentiable. Is there a workaround for that?
That should not be a problem, the function is simply not smooth at one specific value, but we can still take the (sub-)derivative and do (sub-)gradient descent. It is essentially the same as ReLU, LeakyReLU, etc.
@lucasb-eyer as fas as I understood, I need to convert RGB to XYZ and after that I can convert my image to LAB, right? It’s still differentiable?
you can convert RGB to XYZ using kornia.rgb_to_xyz
pending to implement to Lab. Contributions are welcomed.
Look at this link for PyTorch implementation of lab2rgb and rgb2lab.