Skip to content
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

Merged
merged 41 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
901b875
Add basic functions for colorspace transforms.
SomTambe Jun 7, 2021
093f8b7
Modify docs.
SomTambe Jun 7, 2021
3c39144
Make vector a tuple
SomTambe Jun 7, 2021
684ea01
Removed CUDA from ependency
SomTambe Jun 7, 2021
f714e2a
Add single not for batch dim
SomTambe Jun 7, 2021
9f92224
Add ChainRules dependency
SomTambe Jun 7, 2021
d11a5cb
Added V to LICENSE
SomTambe Jun 8, 2021
f074375
Add tests.
SomTambe Jun 8, 2021
15d1d31
Remove newline from EOF
SomTambe Jun 8, 2021
fbcb568
Add channelify adjoint
SomTambe Jun 9, 2021
9511d4b
sample commit using gpg
SomTambe Jun 9, 2021
26aa7a0
Signed commit
SomTambe Jun 9, 2021
d348861
Add custom adjoints for `colorview`, `channelview`, `(::Colorant{T,N})`
SomTambe Jun 13, 2021
8cd6768
Make necessary changes
SomTambe Jun 13, 2021
76e77ea
\Delta changed to (\Delta...,)
SomTambe Jun 14, 2021
0fe299e
Complete adjoints for `(CT<:Colorant{T})(::Real)`
SomTambe Jun 15, 2021
0141543
Removed debugging line
SomTambe Jun 15, 2021
1a3ec8f
Add tests for `channelify`
SomTambe Jun 16, 2021
721e6c6
Change from symbols to instances
SomTambe Jun 16, 2021
5dcafa6
Few changes
SomTambe Jun 16, 2021
4723f91
Change `<:Color` to `<:Colorant` in `colorify`
SomTambe Jun 16, 2021
52c0207
Add tests for `colorify` and reversibility
SomTambe Jun 16, 2021
be22a5d
Merge branch 'main' of github.com:SomTambe/DiffImages.jl into dev
SomTambe Jun 17, 2021
5fdec09
Added Documenter package
SomTambe Jun 17, 2021
c019c8b
Merge branch 'main' of github.com:SomTambe/DiffImages.jl into dev
SomTambe Jun 17, 2021
02e9085
Make changes
SomTambe Jun 17, 2021
75797b0
Change name of folder from `tests` to `test`
SomTambe Jun 17, 2021
598b427
Add Test dependency
SomTambe Jun 17, 2021
c11cb13
Add `channelview` and `colorview` differentiability tests
SomTambe Jun 17, 2021
62a66a0
Add note for `colorview(T, gray1, gray2...)` adjoint and tests
SomTambe Jun 17, 2021
95e10a9
Change doctests a bit
SomTambe Jun 17, 2021
a71d974
Changed the docstring arguments
SomTambe Jun 17, 2021
2b1e4aa
add requirements
SomTambe Jun 17, 2021
ace5a7f
Add Images to doctests
SomTambe Jun 17, 2021
89b15d5
Add Test dependency
SomTambe Jun 17, 2021
7f4cc3f
Fix CI issue
SomTambe Jun 17, 2021
fae45dd
Add Project.toml file for docs
SomTambe Jun 18, 2021
041507c
remove comments
SomTambe Jun 18, 2021
1bbce28
Commit necessary changes
SomTambe Jun 18, 2021
45e1787
Change `unsqueeze` to `reshape`
SomTambe Jun 21, 2021
f6ddc1a
Add tests for `channelify` and `colorify`
SomTambe Jun 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ docs/site/
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml
.vscode
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Som Tambe
Copyright (c) 2021 Som V Tambe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ authors = ["SomTambe <[email protected]>"]
version = "0.1.0"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
32 changes: 31 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
using Documenter, DiffImages

makedocs(sitename="DiffImages.jl")
makedocs(;
modules = [DiffImages],
doctest = true,
authors = "Som V Tambe <[email protected]>",
repo = "https://github.com/SomTambe/DiffImages.jl/blob/{commit}{path}#L{line}",
sitename = "DiffImages.jl",
# format = Documenter.HTML(;
# prettyurls = get(ENV, "CI", "false") == "true",
# canonical = "https://fluxml.ai/Flux3D.jl",
# assets = String["assets/favicon.ico"],
# analytics = "UA-154580699-2",
# ),
pages = [
"Home" => "index.md",
# "Tutorials" => [
# ],
"Colorspace Transforms" => "colors/index.md",
# "Datasets" =>
# ["ModelNet" => "datasets/modelnet.md", "Custom Dataset" => "datasets/utils.md"],
# "Transforms" => "api/transforms.md",
# "Metrics" => "api/metrics.md",
# "API Documentation" => [
# "Conversions" => "api/conversions.md",
# "Helper function" => "api/utils.md",
# "Visualization" => "api/visualize.md",
# "3D Models" => "api/models.md",
# ],
],
)

deploydocs(; repo = "github.com/FluxML/Flux3D.jl.git")
14 changes: 14 additions & 0 deletions docs/src/colors/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```@meta
CurrentModule = DiffImages
```
# Colorspace Transforms

!!! note "Consistency with the batch dimension"
Since we require the last dimension to be the batch dimension,
kindly unsqueeze the last dimension if you would like to pass a
single image.

```@autodocs
Modules = [DiffImages]
Pages = ["colors/conversions.jl"]
```
13 changes: 8 additions & 5 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
```@meta
CurrentModule = DiffImages
```
# DiffImages Documentation

# DiffImages: Extending Automatic Differentiation support to the JuliaImages ecosystem

```@autodocs
Modules = [DiffImages]
Pages = ["conversions.jl"]
```
DiffImages.jl is a differentiable computer vision library made in pure Julia. It is being developed with the aim of extending AD support to the JuliaImages ecosystem.

This library is under heavy development and is not intended for use yet. A stable release later will enable the users to readily use this.

You can watch our development at the Discussions tab of our repository. We welcome all and everyone who intend to help us with their discussions. Open Source code simply cannot be made if the community does not give its inputs.

We hope you remain safe and healthy.
9 changes: 6 additions & 3 deletions src/DiffImages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module DiffImages
using Flux,
Images,
Zygote,
CUDA
ChainRules,
ImageTransformations

using Flux:@functor, unsqueeze
using ChainRules:NoTangent

export colorify!
include("conversions.jl")
export colorify, channelify
include("colors/conversions.jl")
include("geometry/warp.jl")

end # module
89 changes: 89 additions & 0 deletions src/colors/conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
channelify(m::AbstractArray{<:Color})
Channelify function.

Input should be in ``WHN`` order ``\\implies (*, batch)``.
Output will be in the order ``(*, channels, batch)``.

"""
function channelify(m::AbstractArray{T}) where T <: Color
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
e = eltype(m)
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
m = channelview(m)
if e <: AbstractGray
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
m = unsqueeze(m, 1)
end
t = ntuple(i->i, ndims(m))
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
m = permutedims(m, (t[2:end-1]...,1,t[end]))
return m
end

#adjoint for channelview
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
Zygote.@adjoint channelview(x) = begin
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
e = eltype(x)
y = channelview(x)
function pullback(Δ)
return (colorview(e,Δ),)
Copy link
Collaborator

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?

end
return (y, pullback)
end

# adjoint for colorview
Zygote.@adjoint colorview(T,x) = begin
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
y = colorview(T,x)
function pullback(Δ)
return (nothing, channelview(Δ))
end
return (y, pullback)
end

# adjoint for (::Colorant{T,N})(x)
for f in (:HSV,:AHSV,:HSVA,
:Gray,:AGray,:GrayA,
:HSL,:AHSL,:HSLA,
:RGB,:ARGB,:RGBA,
:BGR,:ABGR,:BGRA,
:XYZ,:AXYZ,:XYZA,
:xyY,:AxyY,:xyYA,
:Lab,:ALab,:LabA,
:Luv,:ALuv,:LuvA,
:LCHab,:ALCHab,:LCHabA,
:LCHuv,:ALCHuv,:LCHuvA,
:DIN99,:ADIN99,:DIN99A,
:LMS,:ALMS,:LMSA,
:YIQ,:AYIQ,:YIQA)
@eval Zygote.@adjoint $f(x...) = begin
y = $f(x...)
function pullback(Δ)
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
return (Δ,)
SomTambe marked this conversation as resolved.
Show resolved Hide resolved
end
return (y, pullback)
end
end

"""
colorify(color::Type{<:Color}, m::AbstractArray)
Colorify function.

Expecting an input of the type ``(*, channels, batch)``.
Converts the array to the `color` specified.


# Examples
```jldoctest; setup = :(using Images)
julia> img = rand(25,25,3,7)

julia> colorify(HSV, img)

julia> channelify(colorify(RGB,img))==img
true
```
"""
function colorify(color::Type{<:Color}, m::AbstractArray)
t = ntuple(i->i, ndims(m))
m = permutedims(m, (t[end-1],t[1:end-2]...,t[end]))
if color <: AbstractGray
m = dropdims(m; dims=1)
end
m = colorview(color, m)
return m
end
41 changes: 0 additions & 41 deletions src/conversions.jl

This file was deleted.

Empty file added src/geometry/warp.jl
Empty file.
30 changes: 30 additions & 0 deletions tests/colors/conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@testset "Colorspace transforms tests" begin
f = Chain(x->HSV.(x),channelify,flatten,Dense(768,16),Dense(16,10),x->σ.(x))
g_3 = Chain(Conv((3,3),7=>4,relu),
Conv((3,3),4=>3,relu),
x->colorify(RGB,x),
x->HSV.(x),
channelify,
flatten,
Dense(256*3,16),
Dense(16,10),
x->σ.(x))
g_1 = Chain(Conv((3,3),7=>4,relu),
Conv((3,3),4=>3,relu),
x->colorify(RGB,x),
x->Gray.(x),
channelify,
flatten,
Dense(256*1,16),
Dense(16,10),
x->σ.(x))

@testset "Testing channelify" begin
@test size(f(rand(RGB,16,16,3))) == (10,3)
@test size(f(rand(Gray,16,16,3))) == (10,3)
end
@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test for differentiability

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure

end
11 changes: 10 additions & 1 deletion tests/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
using Test,
Zygote,
Flux,
CUDA
DiffImages,
Images,
ImageTransformations

@testset "DiffImages" begin
@info "Testing Colorspace modules"
@testset "Colorspace" begin
include("colors/conversions.jl")
end
end