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
OpenDAS
ColossalAI
Commits
6e9730d7
Unverified
Commit
6e9730d7
authored
Nov 08, 2022
by
Fazzie-Maqianli
Committed by
GitHub
Nov 08, 2022
Browse files
[example] add stable diffuser (#1825)
parent
b1263d32
Changes
28
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
1610 additions
and
0 deletions
+1610
-0
examples/images/diffusion/scripts/img2img.py
examples/images/diffusion/scripts/img2img.py
+293
-0
examples/images/diffusion/scripts/inpaint.py
examples/images/diffusion/scripts/inpaint.py
+98
-0
examples/images/diffusion/scripts/knn2img.py
examples/images/diffusion/scripts/knn2img.py
+398
-0
examples/images/diffusion/scripts/sample_diffusion.py
examples/images/diffusion/scripts/sample_diffusion.py
+313
-0
examples/images/diffusion/scripts/train_searcher.py
examples/images/diffusion/scripts/train_searcher.py
+147
-0
examples/images/diffusion/scripts/txt2img.py
examples/images/diffusion/scripts/txt2img.py
+344
-0
examples/images/diffusion/setup.py
examples/images/diffusion/setup.py
+13
-0
examples/images/diffusion/train.sh
examples/images/diffusion/train.sh
+4
-0
No files found.
examples/images/diffusion/scripts/img2img.py
0 → 100644
View file @
6e9730d7
This diff is collapsed.
Click to expand it.
examples/images/diffusion/scripts/inpaint.py
0 → 100644
View file @
6e9730d7
import
argparse
,
os
,
sys
,
glob
from
omegaconf
import
OmegaConf
from
PIL
import
Image
from
tqdm
import
tqdm
import
numpy
as
np
import
torch
from
main
import
instantiate_from_config
from
ldm.models.diffusion.ddim
import
DDIMSampler
def
make_batch
(
image
,
mask
,
device
):
image
=
np
.
array
(
Image
.
open
(
image
).
convert
(
"RGB"
))
image
=
image
.
astype
(
np
.
float32
)
/
255.0
image
=
image
[
None
].
transpose
(
0
,
3
,
1
,
2
)
image
=
torch
.
from_numpy
(
image
)
mask
=
np
.
array
(
Image
.
open
(
mask
).
convert
(
"L"
))
mask
=
mask
.
astype
(
np
.
float32
)
/
255.0
mask
=
mask
[
None
,
None
]
mask
[
mask
<
0.5
]
=
0
mask
[
mask
>=
0.5
]
=
1
mask
=
torch
.
from_numpy
(
mask
)
masked_image
=
(
1
-
mask
)
*
image
batch
=
{
"image"
:
image
,
"mask"
:
mask
,
"masked_image"
:
masked_image
}
for
k
in
batch
:
batch
[
k
]
=
batch
[
k
].
to
(
device
=
device
)
batch
[
k
]
=
batch
[
k
]
*
2.0
-
1.0
return
batch
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--indir"
,
type
=
str
,
nargs
=
"?"
,
help
=
"dir containing image-mask pairs (`example.png` and `example_mask.png`)"
,
)
parser
.
add_argument
(
"--outdir"
,
type
=
str
,
nargs
=
"?"
,
help
=
"dir to write results to"
,
)
parser
.
add_argument
(
"--steps"
,
type
=
int
,
default
=
50
,
help
=
"number of ddim sampling steps"
,
)
opt
=
parser
.
parse_args
()
masks
=
sorted
(
glob
.
glob
(
os
.
path
.
join
(
opt
.
indir
,
"*_mask.png"
)))
images
=
[
x
.
replace
(
"_mask.png"
,
".png"
)
for
x
in
masks
]
print
(
f
"Found
{
len
(
masks
)
}
inputs."
)
config
=
OmegaConf
.
load
(
"models/ldm/inpainting_big/config.yaml"
)
model
=
instantiate_from_config
(
config
.
model
)
model
.
load_state_dict
(
torch
.
load
(
"models/ldm/inpainting_big/last.ckpt"
)[
"state_dict"
],
strict
=
False
)
device
=
torch
.
device
(
"cuda"
)
if
torch
.
cuda
.
is_available
()
else
torch
.
device
(
"cpu"
)
model
=
model
.
to
(
device
)
sampler
=
DDIMSampler
(
model
)
os
.
makedirs
(
opt
.
outdir
,
exist_ok
=
True
)
with
torch
.
no_grad
():
with
model
.
ema_scope
():
for
image
,
mask
in
tqdm
(
zip
(
images
,
masks
)):
outpath
=
os
.
path
.
join
(
opt
.
outdir
,
os
.
path
.
split
(
image
)[
1
])
batch
=
make_batch
(
image
,
mask
,
device
=
device
)
# encode masked image and concat downsampled mask
c
=
model
.
cond_stage_model
.
encode
(
batch
[
"masked_image"
])
cc
=
torch
.
nn
.
functional
.
interpolate
(
batch
[
"mask"
],
size
=
c
.
shape
[
-
2
:])
c
=
torch
.
cat
((
c
,
cc
),
dim
=
1
)
shape
=
(
c
.
shape
[
1
]
-
1
,)
+
c
.
shape
[
2
:]
samples_ddim
,
_
=
sampler
.
sample
(
S
=
opt
.
steps
,
conditioning
=
c
,
batch_size
=
c
.
shape
[
0
],
shape
=
shape
,
verbose
=
False
)
x_samples_ddim
=
model
.
decode_first_stage
(
samples_ddim
)
image
=
torch
.
clamp
((
batch
[
"image"
]
+
1.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
mask
=
torch
.
clamp
((
batch
[
"mask"
]
+
1.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
predicted_image
=
torch
.
clamp
((
x_samples_ddim
+
1.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
inpainted
=
(
1
-
mask
)
*
image
+
mask
*
predicted_image
inpainted
=
inpainted
.
cpu
().
numpy
().
transpose
(
0
,
2
,
3
,
1
)[
0
]
*
255
Image
.
fromarray
(
inpainted
.
astype
(
np
.
uint8
)).
save
(
outpath
)
examples/images/diffusion/scripts/knn2img.py
0 → 100644
View file @
6e9730d7
This diff is collapsed.
Click to expand it.
examples/images/diffusion/scripts/sample_diffusion.py
0 → 100644
View file @
6e9730d7
This diff is collapsed.
Click to expand it.
examples/images/diffusion/scripts/train_searcher.py
0 → 100644
View file @
6e9730d7
This diff is collapsed.
Click to expand it.
examples/images/diffusion/scripts/txt2img.py
0 → 100644
View file @
6e9730d7
This diff is collapsed.
Click to expand it.
examples/images/diffusion/setup.py
0 → 100644
View file @
6e9730d7
from
setuptools
import
setup
,
find_packages
setup
(
name
=
'latent-diffusion'
,
version
=
'0.0.1'
,
description
=
''
,
packages
=
find_packages
(),
install_requires
=
[
'torch'
,
'numpy'
,
'tqdm'
,
],
)
\ No newline at end of file
examples/images/diffusion/train.sh
0 → 100755
View file @
6e9730d7
HF_DATASETS_OFFLINE
=
1
TRANSFORMERS_OFFLINE
=
1
python main.py
--logdir
/tmp
-t
--postfix
test
-b
configs/train_colossalai.yaml
Prev
1
2
Next
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