Skip to content

Latest commit

 

History

History
62 lines (48 loc) · 1.99 KB

README.md

File metadata and controls

62 lines (48 loc) · 1.99 KB

Paddle-DALL-E

GitHub forks GitHub Repo stars GitHub release (latest by date including pre-releases) GitHub
A PaddlePaddle version implementation of DALL-E of OpenAI. 【origin repo】

Now this implementation only include the dVAE part, can't generate images from text.

Install Package

  • Install by pip:
$ pip install paddledalle==1.0.0 -i https://pypi.python.org/pypi 

Quick Start

import paddle
import paddle.nn.functional as F
import paddle.vision.transforms as T
import paddle.vision.transforms.functional as TF

from PIL import Image
from dall_e import load_model, map_pixels, unmap_pixels

target_image_size = 256

def preprocess(img):
    s = min(img.size)

    if s < target_image_size:
        raise ValueError(f'min dim for image {s} < {target_image_size}')

    r = target_image_size / s
    s = (round(r * img.size[1]), round(r * img.size[0]))
    img = TF.resize(img, s, interpolation='lanczos')
    img = TF.center_crop(img, output_size=2 * [target_image_size])
    img = paddle.unsqueeze(T.ToTensor()(img), 0)
    return map_pixels(img)

enc = load_model('encoder', pretrained=True)
dec = load_model('decoder', pretrained=True)

img = Image.open('1000x-1.jpg')
x = preprocess(img)

z_logits = enc(x)
z = paddle.argmax(z_logits, axis=1)
z = F.one_hot(z, num_classes=enc.vocab_size).transpose((0, 3, 1, 2))

x_stats = dec(z)
x_rec = unmap_pixels(F.sigmoid(x_stats[:, :3]))

out = (x_rec[0].transpose((1, 2, 0))*255.).astype('uint8').numpy()
out = Image.fromarray(out)
out.show()

Contact us

Email : [email protected]
QQ Group : 1005109853