min-dalle-test/replicate_predictor.py

62 lines
2.0 KiB
Python
Raw Permalink Normal View History

2022-07-17 19:29:23 +00:00
2022-07-05 00:02:33 +00:00
from min_dalle import MinDalle
2022-06-29 19:53:25 +00:00
import tempfile
import torch, torch.backends.cudnn
2022-07-05 00:02:33 +00:00
from typing import Iterator
2022-06-29 19:53:25 +00:00
from cog import BasePredictor, Path, Input
torch.backends.cudnn.deterministic = False
2022-07-05 09:47:35 +00:00
class ReplicatePredictor(BasePredictor):
2022-06-29 19:53:25 +00:00
def setup(self):
2022-07-12 15:20:37 +00:00
self.model = MinDalle(
is_mega=True,
is_reusable=True,
2022-07-16 01:21:16 +00:00
dtype=torch.float32,
device='cuda'
2022-07-12 15:20:37 +00:00
)
2022-06-29 19:53:25 +00:00
def predict(
self,
2022-07-12 13:17:18 +00:00
text: str = Input(default='Dali painting of WALL·E'),
save_as_png: bool = Input(default=False),
progressive_outputs: bool = Input(default=True),
2022-07-17 12:34:32 +00:00
seamless: bool = Input(default=False),
2022-07-12 13:17:18 +00:00
grid_size: int = Input(ge=1, le=9, default=5),
2022-07-17 19:29:23 +00:00
temperature: float = Input(
ge=0.01,
le=16,
default=4
),
top_k: int = Input(
choices=[2 ** i for i in range(15)],
default=64,
description='Advanced Setting, see Readme below if interested.'
),
supercondition_factor: int = Input(
choices=[2 ** i for i in range(2, 7)],
default=16,
description='Advanced Setting, see Readme below if interested.'
)
2022-07-05 00:02:33 +00:00
) -> Iterator[Path]:
2022-07-12 15:20:37 +00:00
image_stream = self.model.generate_image_stream(
text = text,
seed = -1,
grid_size = grid_size,
progressive_outputs = progressive_outputs,
2022-07-17 19:29:23 +00:00
is_seamless = seamless,
temperature = temperature,
supercondition_factor = float(supercondition_factor),
top_k = top_k,
2022-07-12 15:20:37 +00:00
is_verbose = True
)
2022-07-04 22:37:07 +00:00
2022-07-12 15:20:37 +00:00
i = 0
path = Path(tempfile.mkdtemp())
for image in image_stream:
i += 1
is_final = i == 8 if progressive_outputs else True
ext = 'png' if is_final and save_as_png else 'jpg'
2022-07-12 15:20:37 +00:00
image_path = path / 'min-dalle-iter-{}.{}'.format(i, ext)
image.save(str(image_path))
yield image_path