2022-06-27 19:46:04 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
from PIL import Image
|
|
|
|
|
2022-07-01 23:44:24 +00:00
|
|
|
from min_dalle import MinDalle
|
|
|
|
|
2022-06-27 19:46:04 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2022-06-27 20:49:42 +00:00
|
|
|
parser.add_argument('--mega', action='store_true')
|
|
|
|
parser.add_argument('--no-mega', dest='mega', action='store_false')
|
|
|
|
parser.set_defaults(mega=False)
|
2022-07-01 20:02:05 +00:00
|
|
|
parser.add_argument('--text', type=str, default='alien life')
|
2022-07-01 23:44:24 +00:00
|
|
|
parser.add_argument('--seed', type=int, default=-1)
|
2022-07-02 12:45:49 +00:00
|
|
|
parser.add_argument('--grid-size', type=int, default=1)
|
|
|
|
parser.add_argument('--image-path', type=str, default='generated')
|
|
|
|
parser.add_argument('--models-root', type=str, default='pretrained')
|
|
|
|
parser.add_argument('--token-count', type=int, default=256) # for debugging
|
2022-06-27 19:46:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ascii_from_image(image: Image.Image, size: int) -> str:
|
|
|
|
rgb_pixels = image.resize((size, int(0.55 * size))).convert('L').getdata()
|
|
|
|
chars = list('.,;/IOX')
|
|
|
|
chars = [chars[i * len(chars) // 256] for i in rgb_pixels]
|
|
|
|
chars = [chars[i * size: (i + 1) * size] for i in range(size // 2)]
|
|
|
|
return '\n'.join(''.join(row) for row in chars)
|
|
|
|
|
|
|
|
|
2022-06-27 20:49:42 +00:00
|
|
|
def save_image(image: Image.Image, path: str):
|
2022-06-27 19:46:04 +00:00
|
|
|
if os.path.isdir(path):
|
2022-07-04 11:38:16 +00:00
|
|
|
path = os.path.join(path, 'generated.jpg')
|
|
|
|
elif not path.endswith('.jpg'):
|
|
|
|
path += '.jpg'
|
2022-06-27 19:46:04 +00:00
|
|
|
print("saving image to", path)
|
|
|
|
image.save(path)
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
def generate_image(
|
|
|
|
is_mega: bool,
|
|
|
|
text: str,
|
|
|
|
seed: int,
|
2022-07-02 12:45:49 +00:00
|
|
|
grid_size: int,
|
2022-06-29 13:42:12 +00:00
|
|
|
image_path: str,
|
2022-07-02 03:39:48 +00:00
|
|
|
models_root: str,
|
2022-06-30 10:43:10 +00:00
|
|
|
token_count: int
|
2022-06-29 13:42:12 +00:00
|
|
|
):
|
2022-07-01 23:44:24 +00:00
|
|
|
model = MinDalle(
|
2022-07-01 22:16:55 +00:00
|
|
|
is_mega=is_mega,
|
2022-07-02 03:39:48 +00:00
|
|
|
models_root=models_root,
|
2022-07-01 22:16:55 +00:00
|
|
|
is_reusable=False,
|
2022-07-02 00:17:20 +00:00
|
|
|
sample_token_count=token_count,
|
|
|
|
is_verbose=True
|
2022-07-01 22:16:55 +00:00
|
|
|
)
|
2022-06-29 13:42:12 +00:00
|
|
|
|
2022-07-01 18:06:50 +00:00
|
|
|
if token_count < 256:
|
2022-07-02 12:45:49 +00:00
|
|
|
image_tokens = model.generate_image_tokens(text, seed, grid_size ** 2)
|
|
|
|
print('image tokens', image_tokens.to('cpu').detach().numpy())
|
2022-06-29 13:42:12 +00:00
|
|
|
else:
|
2022-07-02 12:45:49 +00:00
|
|
|
image = model.generate_image(text, seed, grid_size)
|
2022-07-01 18:06:50 +00:00
|
|
|
save_image(image, image_path)
|
|
|
|
print(ascii_from_image(image, size=128))
|
2022-06-29 13:42:12 +00:00
|
|
|
|
|
|
|
|
2022-06-27 19:46:04 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parser.parse_args()
|
2022-06-27 20:49:42 +00:00
|
|
|
print(args)
|
2022-06-29 13:42:12 +00:00
|
|
|
generate_image(
|
|
|
|
is_mega=args.mega,
|
|
|
|
text=args.text,
|
|
|
|
seed=args.seed,
|
2022-07-02 12:45:49 +00:00
|
|
|
grid_size=args.grid_size,
|
2022-06-29 13:42:12 +00:00
|
|
|
image_path=args.image_path,
|
2022-07-02 03:39:48 +00:00
|
|
|
models_root=args.models_root,
|
2022-06-30 10:43:10 +00:00
|
|
|
token_count=args.token_count
|
2022-06-29 13:42:12 +00:00
|
|
|
)
|