add docs using Documenter.jl

This commit is contained in:
Laureηt 2023-07-29 17:52:36 +02:00
parent 3c9841ffd9
commit 3c455c8b2e
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI
5 changed files with 88 additions and 6 deletions

5
docs/Project.toml Normal file
View file

@ -0,0 +1,5 @@
[deps]
Diffusers = "90edb7a8-79d7-49b2-b6b1-9322c3fdead8"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

20
docs/make.jl Normal file
View file

@ -0,0 +1,20 @@
using Diffusers
using Documenter
using Plots
DocMeta.setdocmeta!(Diffusers, :DocTestSetup, :(using Diffusers); recursive=true)
makedocs(;
modules=[Diffusers],
authors="Laurent Fainsin <laurent@fainsin.bzh>",
repo="https://github.com/Laurent2916/Diffusers.jl/blob/{commit}{path}#{line}",
sitename="Diffusers.jl",
format=Documenter.HTML(;
prettyurls=get(ENV, "CI", "false") == "true",
edit_link="main",
assets=String[]
),
pages=[
"Home" => "index.md",
]
)

51
docs/src/index.md Normal file
View file

@ -0,0 +1,51 @@
```@meta
CurrentModule = Diffusers
```
# Diffusers
Documentation for [Diffusers.jl](https://github.com/Laurent2916/Diffusers.jl).
```@eval
using Diffusers.BetaSchedules
using Plots
plotlyjs()
T = 1000
linear = linear_beta_schedule(T)
scaled_linear = scaled_linear_beta_schedule(T)
cosine = cosine_beta_schedule(T)
sigmoid = sigmoid_beta_schedule(T)
plot(
[linear, scaled_linear, cosine, sigmoid],
label=["linear" "scaled_linear" "cosine" "sigmoid"],
xlabel="t",
ylabel="β",
title="Beta schedules",
legend=:topleft,
yscale=:log10,
)
savefig("beta_schedules.html")
nothing
```
```@raw html
<object type="text/html" data="beta_schedules.html" style="width:100%;height:420px;"></object>
```
```@index
```
```@autodocs
Modules = [Diffusers]
```
```@autodocs
Modules = [Diffusers.BetaSchedules]
```
```@autodocs
Modules = [Diffusers.Schedulers]
```

View file

@ -1,4 +1,6 @@
import Diffusers
import Diffusers.Schedulers
import Diffusers.Schedulers: DDPM
import Diffusers.BetaSchedules: cosine_beta_schedule, rescale_zero_terminal_snr
using Flux
using Random
@ -41,7 +43,7 @@ scatter(data[1, :], data[2, :],
)
num_timesteps = 100
scheduler = Diffusers.DDPM(
scheduler = DDPM(
Vector{Float64},
rescale_zero_terminal_snr(
cosine_beta_schedule(num_timesteps, 0.999f0, 0.001f0)
@ -51,7 +53,7 @@ scheduler = Diffusers.DDPM(
noise = randn(size(data))
anim = @animate for i in cat(1:num_timesteps, repeat([num_timesteps], 50), dims=1)
noisy_data = Diffusers.add_noise(scheduler, data, noise, [i])
noisy_data = Diffusers.Schedulers.add_noise(scheduler, data, noise, [i])
scatter(noise[1, :], noise[2, :],
alpha=0.1,
aspectratio=:equal,
@ -117,10 +119,10 @@ for epoch = 1:num_epochs
for data in dataloader
noise = randn(size(data))
timesteps = rand(2:num_timesteps, size(data)[2]) # TODO: fix start at timestep=2, bruh
noisy_data = Diffusers.add_noise(scheduler, data, noise, timesteps)
noisy_data = Diffusers.Schedulers.add_noise(scheduler, data, noise, timesteps)
grads = Flux.gradient(model) do m
model_output = m(noisy_data, timesteps)
noise_prediction, _ = Diffusers.step(scheduler, noisy_data, model_output, timesteps)
noise_prediction, _ = Diffusers.Schedulers.step(scheduler, noisy_data, model_output, timesteps)
loss(noise, noise_prediction)
end
Flux.update!(opt, params, grads)
@ -131,7 +133,7 @@ end
# sampling animation
anim = @animate for timestep in num_timesteps:-1:2
model_output = model(data, [timestep])
sampled_data, x0_pred = Diffusers.step(scheduler, data, model_output, [timestep])
sampled_data, x0_pred = Diffusers.Schedulers.step(scheduler, data, model_output, [timestep])
p1 = scatter(sampled_data[1, :], sampled_data[2, :],
alpha=0.5,

View file

@ -1,3 +1,7 @@
"""
Abstract type for schedulers.
"""
abstract type Scheduler end
"""
@ -69,7 +73,7 @@ end
Add noise to clean data using the forward diffusion process.
## Input
* `scheduler::Scheduler`: scheduler to use
* `scheduler::DDPM`: scheduler to use
* `x₀::AbstractArray`: clean data to add noise to
* `ϵ::AbstractArray`: noise to add to clean data
* `t::AbstractArray`: timesteps used to weight the noise