Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
renzhc
diffusers_dcu
Commits
ff89f808
Commit
ff89f808
authored
Jun 08, 2022
by
anton-l
Browse files
Merge branch 'main' of github.com:huggingface/diffusers
parents
f9cdb4dd
46dae846
Changes
7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
765 additions
and
15 deletions
+765
-15
models/vision/ddim/example.py
models/vision/ddim/example.py
+23
-0
models/vision/ddim/modeling_ddim.py
models/vision/ddim/modeling_ddim.py
+13
-14
models/vision/latent_diffusion/modeling_vqvae.py
models/vision/latent_diffusion/modeling_vqvae.py
+0
-0
src/diffusers/__init__.py
src/diffusers/__init__.py
+1
-0
src/diffusers/models/__init__.py
src/diffusers/models/__init__.py
+1
-0
src/diffusers/models/unet_ldm.py
src/diffusers/models/unet_ldm.py
+6
-1
src/diffusers/models/vqvae.py
src/diffusers/models/vqvae.py
+721
-0
No files found.
models/vision/ddim/example.py
0 → 100755
View file @
ff89f808
#!/usr/bin/env python3
import
os
import
pathlib
from
modeling_ddim
import
DDIM
import
PIL.Image
import
numpy
as
np
model_ids
=
[
"ddim-celeba-hq"
,
"ddim-lsun-church"
,
"ddim-lsun-bedroom"
]
for
model_id
in
model_ids
:
path
=
os
.
path
.
join
(
"/home/patrick/images/hf"
,
model_id
)
pathlib
.
Path
(
path
).
mkdir
(
parents
=
True
,
exist_ok
=
True
)
ddpm
=
DDIM
.
from_pretrained
(
"fusing/"
+
model_id
)
image
=
ddpm
(
batch_size
=
4
)
image_processed
=
image
.
cpu
().
permute
(
0
,
2
,
3
,
1
)
image_processed
=
(
image_processed
+
1.0
)
*
127.5
image_processed
=
image_processed
.
numpy
().
astype
(
np
.
uint8
)
for
i
in
range
(
image_processed
.
shape
[
0
]):
image_pil
=
PIL
.
Image
.
fromarray
(
image_processed
[
i
])
image_pil
.
save
(
os
.
path
.
join
(
path
,
f
"image_
{
i
}
.png"
))
models/vision/ddim/modeling_ddim.py
View file @
ff89f808
...
...
@@ -19,12 +19,6 @@ import tqdm
import
torch
def
compute_alpha
(
beta
,
t
):
beta
=
torch
.
cat
([
torch
.
zeros
(
1
).
to
(
beta
.
device
),
beta
],
dim
=
0
)
a
=
(
1
-
beta
).
cumprod
(
dim
=
0
).
index_select
(
0
,
t
+
1
).
view
(
-
1
,
1
,
1
,
1
)
return
a
class
DDIM
(
DiffusionPipeline
):
def
__init__
(
self
,
unet
,
noise_scheduler
):
...
...
@@ -32,7 +26,7 @@ class DDIM(DiffusionPipeline):
self
.
register_modules
(
unet
=
unet
,
noise_scheduler
=
noise_scheduler
)
def
__call__
(
self
,
batch_size
=
1
,
generator
=
None
,
torch_device
=
None
,
eta
=
0.0
,
num_inference_steps
=
50
):
# eta
is η in paper
# eta
corresponds to η in paper and should be between [0, 1]
if
torch_device
is
None
:
torch_device
=
"cuda"
if
torch
.
cuda
.
is_available
()
else
"cpu"
...
...
@@ -59,15 +53,20 @@ class DDIM(DiffusionPipeline):
coeff_1
=
(
alpha_prod_t_prev
-
alpha_prod_t
).
sqrt
()
*
alpha_prod_t_prev_rsqrt
*
beta_prod_t_prev_sqrt
/
beta_prod_t_sqrt
*
eta
coeff_2
=
((
1
-
alpha_prod_t_prev
)
-
coeff_1
**
2
).
sqrt
()
# model forward
with
torch
.
no_grad
():
noise_residual
=
self
.
unet
(
image
,
train_step
)
print
(
train_step
)
pred_mean
=
(
image
-
noise_residual
*
beta_prod_t_sqrt
)
*
alpha_prod_t_rsqrt
xt_next
=
alpha_prod_t_prev
.
sqrt
()
*
pred_mean
+
coeff_1
*
torch
.
randn_like
(
image
)
+
coeff_2
*
noise_residual
# xt_next = 1 / alpha_prod_t_rsqrt * pred_mean + coeff_1 * torch.randn_like(image) + coeff_2 * noise_residual
# eta
image
=
xt_next
# predict mean of prev image
pred_mean
=
alpha_prod_t_rsqrt
*
(
image
-
beta_prod_t_sqrt
*
noise_residual
)
pred_mean
=
torch
.
clamp
(
pred_mean
,
-
1
,
1
)
pred_mean
=
(
1
/
alpha_prod_t_prev_rsqrt
)
*
pred_mean
+
coeff_2
*
noise_residual
# if eta > 0.0 add noise. Note eta = 1.0 essentially corresponds to DDPM
if
eta
>
0.0
:
noise
=
self
.
noise_scheduler
.
sample_noise
(
image
.
shape
,
device
=
image
.
device
,
generator
=
generator
)
image
=
pred_mean
+
coeff_1
*
noise
else
:
image
=
pred_mean
return
image
models/vision/latent_diffusion/modeling_vqvae.py
0 → 100644
View file @
ff89f808
src/diffusers/__init__.py
View file @
ff89f808
...
...
@@ -9,6 +9,7 @@ from .models.clip_text_transformer import CLIPTextModel
from
.models.unet
import
UNetModel
from
.models.unet_glide
import
GLIDETextToImageUNetModel
,
GLIDESuperResUNetModel
from
.models.unet_ldm
import
UNetLDMModel
from
.models.vqvae
import
VQModel
from
.pipeline_utils
import
DiffusionPipeline
from
.schedulers.classifier_free_guidance
import
ClassifierFreeGuidanceScheduler
from
.schedulers.gaussian_ddpm
import
GaussianDDPMScheduler
src/diffusers/models/__init__.py
View file @
ff89f808
...
...
@@ -20,3 +20,4 @@ from .clip_text_transformer import CLIPTextModel
from
.unet
import
UNetModel
from
.unet_glide
import
GLIDETextToImageUNetModel
,
GLIDESuperResUNetModel
from
.unet_ldm
import
UNetLDMModel
from
.vqvae
import
VQModel
\ No newline at end of file
src/diffusers/models/unet_ldm.py
View file @
ff89f808
...
...
@@ -6,7 +6,12 @@ import numpy as np
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
from
einops
import
repeat
,
rearrange
try
:
from
einops
import
repeat
,
rearrange
except
:
print
(
"Einops is not installed"
)
pass
from
..configuration_utils
import
ConfigMixin
from
..modeling_utils
import
ModelMixin
...
...
src/diffusers/models/vqvae.py
0 → 100644
View file @
ff89f808
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment