-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic functions for colorspace transforms. #3
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good start.
One thing I notice about this PR is that permutedims
creates a memory contiguous array while colorview
doesn't. This implies that channeify
creates a contiguous dense array, while colorify
does not. This memory non-contiguity can be a source of performance regression.
Perhaps a 1-argument version of colorify
would be useful, i.e., colorify(RGB)
that creates x->colorify(RGB, x)
.
Should this function specially handle ndims(m) == 2
? I guess no? If so it's probably worth documenting something like "the last dimension is faithfully recognized as a batch dimension so you need to unsqueeze the input manually if the input is only one sample"
Yes you are right. I do require them to have the last dimension as a batch. I should document it as you say. |
@johnnychen94 Should I merge this PR? I'll add the adjoints in another PR, will that be fine? |
Every non-trivial PR should have its associated test cases. |
I completely forgot about the tests. No worries I will write them. |
@johnnychen94 I have written the tests. Could you review them please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this unit test is unnecessarily complicated. You don't need to introduce all other layers as long as the basic properties of channelify
/colorify
get tested.
Why not just test
X = rand(RGB{N0f8}, 4, 4, 5)
Y = channelify(X)
@test Y[:, :, 1, 1] == channelview(X[:, :, 1])
@test size(Y) == (4, 4, 3, 5)
@test eltype(Y) == N0f8
# make sure it has contiguous memory layout
@test stride(Y, 1) == 1
# maybe there're other properties
and also test 2d, 3d and 4d cases. And test various colorspace, e.g., RGB
, HSV
, Gray
, RGBA
, Gray24
.
I have added the adjoints for the necessary functions. I'll be adding the tests by tonight too. |
tests/colors/conversions.jl
Outdated
@testset "Testing colorify" begin | ||
@test size(g_3(rand(20,20,7,3))) == (10,3) | ||
@test size(g_1(rand(20,20,7,3))) == (10,3) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also test for differentiability
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes sure
src/colors/conversions.jl
Outdated
:DIN99,:ADIN99,:DIN99A, | ||
:LMS,:ALMS,:LMSA, | ||
:YIQ,:AYIQ,:YIQA) | ||
@eval @adjoint function $f(x...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adjoint is for RGB(1, 1, 1)
, we would also need adjoint for c=Lab(100, 0, 0); RGB(c)
@eval @adjoint function $f(x...) | |
@eval @adjoint function $f(x::Real...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should it be for Real only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My first look is that the pullback is not correct for the Colorant
inputs.
The most important thing currently is to add unit tests. More than 70% of the efforts should be paid to unit tests to make sure things are written correctly.
With unit tests appropriately written, we can then revisit this one and see if it is correct for colorant inputs.
src/colors/conversions.jl
Outdated
end | ||
|
||
# adjoint for colorview | ||
@adjoint function colorview(T, x) where {T} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also a multi inputs version: colorview(T, gray1, gray2...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does colorview handle batches?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. Yes it does; channelview
and colorview
is just a wrapper of Base.ReinterpretedArray
:
julia> X = rand(RGB, 12, 24, 64);
julia> channelview(X) |> size
(3, 12, 24, 64)
julia> colorview(eltype(X), channelview(X)) == X
true
src/colors/conversions.jl
Outdated
e = eltype(x) | ||
y = channelview(x) | ||
function pullback(Δ) | ||
return (colorview(e,Δ),) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does collect(colorview(e, Δ))
solve the issue in FluxML/Zygote.jl#993?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good start on improving coverage. Nicely written 👍
some numerical tests are still required, e.g., 1) differentiability 2) reversibility (that channelify(colorify(RGB, data)) == data
and colorify(RGB, channelify(rgb_data)) == rgb_data
I have commented out the tests which would evaluate gradients for these. This would be added in the next PR.
I am still debugging CI integration. Will be done soon. |
@johnnychen94 I have done all the required tasks we needed. I have also created #5 to track all our progress remaining over here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last round of comments with only style suggestions.
@DhairyaLGandhi any more comments?
Committed all the changes you asked for. |
@@ -42,8 +42,15 @@ jobs: | |||
- run: | | |||
julia --project=docs -e ' | |||
using Pkg | |||
Pkg.add("Documenter") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one can also be added into docs/Project.toml
- run: | | ||
julia --project=docs -e ' | ||
using Pkg | ||
using Documenter: doctest | ||
using DiffImages | ||
doctest(DiffImages)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that I didn't make it clear. I was thinking of adding doctest(DiffImages)
into test/runtests.jl
. See OffsetArrays as an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Largely LGTM , added a couple thoughts on the tests and let's get this merged!
I am merging the PR as per conversation with @DhairyaLGandhi |
(25, 25, 7) | ||
``` | ||
""" | ||
function colorify(color::Type{CT}, m::AbstractArray) where CT <: Colorant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we may want to restrict this function to AbstractArray{T,4}
since the case of (28,28,3)
with Grayscale should be a simple reinterpret
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current function gives the freedom of any number of dims >= 2. We could restrict it to 4 but it could be we may require something for higher dimensions in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main concern is the case I highlighted earlier. If that is handled correctly, then I'm fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that classic deep learning mainly deals with 4D tensor but I'm not very positive about this type constraint; especially when it's not due to implementation restriction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am the first person to not restrict types for sure, just wanted to ensure we're doing the correct thing for different inputs we can expect to get.
This PR makes the base for any colorspace transform.
colorify
andchannelify
functions.Usage:
colorify
can be used if one ever requires a colorspace transform between two layers.I am not sure if ever this will come to use. It's better to provide an option rather than not providing anything at all.
I would love to hear your comments @DhairyaLGandhi @johnnychen94
Also,
channelview
is not differentiable. I will be writing an adjoint for that.