Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support input_image as an array, fixing minor bug in the 'load_weights_from_pytorch_ckpt' method and adding colab demo showing it's usage #50

Merged
merged 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The easiest way to try it out is to use one of the Colab notebooks:
- [GPU Colab Img2Img](https://colab.research.google.com/drive/1gol0M611zXP6Zpggfri-fG8JDdpMEpsI?usp=sharing)
- [GPU Colab Inpainting](https://colab.research.google.com/drive/1Bf-bNmAdtQhPcYNyC-guu0uTu9MYYfLu)
- [GPU Colab - Tile / Texture generation](https://colab.research.google.com/drive/1xCxsNvQMEywzlqbjH4tGfEyXamSAeFbn?usp=sharing)
- [GPU Colab - Loading Pytorch ckpt Weights](https://colab.research.google.com/drive/1wUdqxji-jxkThYf0OVW3F-0VVpTFdjMa?usp=sharing)
- [GPU Colab + Mixed Precision](https://colab.research.google.com/drive/15mQgITh3e9HQMNys0zR8JN4R2vp06d-N)
- ~10s generation time per image (512x512) on default Colab GPU without drop in quality
([source](https://twitter.com/fchollet/status/1571954014845308928))
Expand Down
14 changes: 9 additions & 5 deletions stable_diffusion_tf/stable_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ def generate(
context = self.text_encoder.predict_on_batch([phrase, pos_ids])

input_image_tensor = None
if type(input_image) is str:
input_image = Image.open(input_image)
input_image = input_image.resize((self.img_width, self.img_height))
input_image_array = np.array(input_image, dtype=np.float32)[None,...,:3]
if input_image is not None:
if type(input_image) is str:
input_image = Image.open(input_image)
input_image = input_image.resize((self.img_width, self.img_height))

elif type(input_image) is np.ndarray:
input_image = np.resize(input_image, (self.img_height, self.img_width, input_image.shape[2]))

input_image_array = np.array(input_image, dtype=np.float32)[None,...,:3]
input_image_tensor = tf.cast((input_image_array / 255.0) * 2 - 1, self.dtype)

if type(input_mask) is str:
Expand Down Expand Up @@ -196,7 +200,7 @@ def get_x_prev_and_pred_x0(self, x, e_t, index, a_t, a_prev, temperature, seed):

def load_weights_from_pytorch_ckpt(self , pytorch_ckpt_path):
import torch
pt_weights = torch.load(pytorch_ckpt_path)
pt_weights = torch.load(pytorch_ckpt_path, map_location="cpu")
for module_name in ['text_encoder', 'diffusion_model', 'decoder', 'encoder' ]:
module_weights = []
for i , (key , perm ) in enumerate(PYTORCH_CKPT_MAPPING[module_name]):
Expand Down