Skip to content

Commit

Permalink
Use NerfUtils.jl (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
pxl-th authored Oct 8, 2023
1 parent 69df413 commit f10319d
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 778 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Manifest.toml
LocalPreferences.toml
test/image.png
*.png
docs/build/
Manifest.toml
24 changes: 20 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,32 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JpegTurbo = "b835a17e-a41a-41e7-81f0-2f016b05efe0"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NerfUtils = "99c1d5ce-7c61-4a25-a107-a5ade2e2a8e4"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Quaternions = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[compat]
AMDGPU = "0.5"
CUDA = "4.4"
AMDGPU = "0.6"
Adapt = "3.6"
BSON = "0.3"
BenchmarkTools = "1.3.2"
CUDA = "5"
ChainRulesCore = "1.16"
FileIO = "1.16"
ImageCore = "0.10"
ImageMagick = "1.3"
ImageTransformations = "0.10"
JSON = "0.21"
JpegTurbo = "0.1"
KernelAbstractions = "0.9"
Zygote = "0.6.55"
NerfUtils = "0.1"
Preferences = "1.4"
Quaternions = "0.7"
Rotations = "1.6"
StaticArrays = "1.6"
Zygote = "0.6"
julia = "1.9"
15 changes: 12 additions & 3 deletions benchmark/pipeline.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nerf
using NerfUtils
using LinearAlgebra
using StaticArrays

Expand Down Expand Up @@ -28,18 +29,26 @@ function benchmark()

Core.println("Trainer benchmark")

GC.gc(true)
GC.gc(false)
@time trainer_benchmark(trainer, 10)
GC.gc(true)
GC.gc(false)
@time trainer_benchmark(trainer, 1000)

camera = Nerf.Camera(MMatrix{3, 4, Float32}(I), dataset.intrinsics)
Nerf.set_projection!(camera, Nerf.get_pose(dataset, 1)...)
camera = Camera(MMatrix{3, 4, Float32}(I), dataset.intrinsics)
NerfUtils.set_projection!(camera, Nerf.get_pose(dataset, 1)...)
renderer = Nerf.Renderer(Backend, camera, trainer.bbox, trainer.cone)

Core.println("Renderer benchmark")

GC.gc(true)
GC.gc(false)
@time render_benchmark(renderer, trainer, 2)
GC.gc(true)
GC.gc(false)
@time render_benchmark(renderer, trainer, 10)

nothing
return
end
benchmark()
9 changes: 5 additions & 4 deletions src/Nerf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ using JSON
using KernelAbstractions
using KernelAbstractions: @atomic, unsafe_free!
using LinearAlgebra
using NerfUtils
using Preferences
using Quaternions
using Rotations
using StaticArrays
using Statistics
using Zygote

import NerfUtils as NU

struct Literal{T} end
Base.:(*)(x, ::Type{Literal{T}}) where {T} = T(x)
const u32 = Literal{UInt32}
Expand Down Expand Up @@ -75,9 +78,7 @@ include("bbox.jl")
include("data/dataset.jl")
include("ray.jl")
include("acceleration/occupancy.jl")
include("encoding/grid.jl")
include("encoding/spherical_harmonics.jl")
include("nn/nn.jl")
include("sampler.jl")
include("loss.jl")
include("trainer.jl")
Expand Down Expand Up @@ -105,10 +106,10 @@ function main()
loss = step!(trainer)
@show i, loss

i % 1000 == 0 || continue
i % 500 == 0 || continue

pose_idx = clamp(round(Int, rand() * length(dataset)), 1, length(dataset))
set_projection!(camera, get_pose(dataset, pose_idx)...)
NU.set_projection!(camera, get_pose(dataset, pose_idx)...)
render!(renderer, trainer.occupancy, trainer.bbox) do points, directions
model(points, directions)
end
Expand Down
2 changes: 1 addition & 1 deletion src/data/images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end

# TODO parametrize Images type on the number of channels
a = if size(images.data, 1) == 4
Float32(images.data[4, pixel[1], pixel[2], image_idx]) * scale
@inbounds Float32(images.data[4, pixel[1], pixel[2], image_idx]) * scale
else
1f0
end
Expand Down
34 changes: 3 additions & 31 deletions src/data/intrinsics.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
struct CameraIntrinsics{D}
distortion::SVector{4, Float32}
focal::SVector{2, Float32}
principal::SVector{2, Float32}
resolution::SVector{2, UInt32}

function CameraIntrinsics(distortion, focal, principal, resolution)
has_distortion = !iszero(distortion)
new{has_distortion}(distortion, focal, principal, resolution)
end
end

"""
CameraIntrinsics(config)
Construct CameraIntrinsics from a config file.
"""
function CameraIntrinsics(config)
function NerfUtils.CameraIntrinsics(config)
w, h = config["w"], config["h"]
resolution = SVector{2, UInt32}(w, h)

Expand All @@ -37,26 +25,10 @@ end
Construct intrinsics for synthetic dataset.
"""
function CameraIntrinsics(width, height, fov)
function NerfUtils.CameraIntrinsics(width, height, fov)
resolution = SVector{2, UInt32}(width, height)
principal = SVector{2, Float32}(0.5f0, 0.5f0)
focal = fov2focal(resolution, rad2deg(fov))
focal = NerfUtils.fov2focal(resolution, rad2deg(fov))
distortion = zeros(SVector{4, Float32})
CameraIntrinsics(distortion, focal, principal, resolution)
end

# Copy constructor.
function CameraIntrinsics(
c::CameraIntrinsics; distortion=c.distortion, focal=c.focal,
principal=c.principal, resolution=c.resolution,
)
CameraIntrinsics(distortion, focal, principal, resolution)
end

function fov2focal(resolution, degrees::Float32)
0.5f0 * resolution / tan(0.5f0 * deg2rad(degrees))
end

function focal2fov(resolution, focal_length)
2f0 * rad2deg(atan(resolution / (focal_length * 2f0)))
end
137 changes: 0 additions & 137 deletions src/encoding/grid.jl

This file was deleted.

Loading

0 comments on commit f10319d

Please sign in to comment.