deep-floyd / IF

Other
7.64k stars 497 forks source link

How to save image #77

Open bolongliu opened 1 year ago

bolongliu commented 1 year ago

where is the api for save image of IF?

bolongliu commented 1 year ago

Here's an example of how to save an image:

Dream demo

prompt = 'ultra close-up color photo portrait of rainbow owl with deer horns in the woods'

count = 4

result = dream(
    t5=t5, if_I=if_I, if_II=if_II, if_III=if_III,
    prompt=[prompt]*count,
    seed=42,
    if_I_kwargs={
        "guidance_scale": 7.0,
        "sample_timestep_respacing": "smart100",
    },
    if_II_kwargs={
        "guidance_scale": 4.0,
        "sample_timestep_respacing": "smart50",
    },
)
if_I.show(result['I'], size=3)
if_I.show(result['II'], size=6)
if_I.show(result['III'], size=14)

define a function named save_image()


def save_image(pil_images_list, save_path = "images", img_name = "generated_image"):  
    if not os.path.exists(save_path):  
        os.makedirs(save_path)  
    for i, img in enumerate(pil_images_list):  
        img = img.detach()  
        img.save("{}{}_{}.png".format(save_path, img_name, i))  

call the save_image() function

pil_images_list = model_result["III"]  
save_image(pil_images_list, save_path = "images", img_name = "generated_image")
klei22 commented 1 year ago

@bolongliu

Seems that the img doesn't have the attribute detach.

    save_image(pil_images_list, save_path = "images", img_name = get_timestamped_filename())
  File "create_deepfloyd_image.py", line 48, in save_image
    img = img.detach()
  File "${HOME}/miniconda3/envs/deepfloyd/lib/python3.8/site-packages/PIL/Image.py", line 529, in __getattr__
    raise AttributeError(name)
AttributeError: detach

I'm going to try to figure this out, however let me know if you have any pointers : )

klei22 commented 1 year ago

Figured it out : ) Should note that I resize the image ahead of time to 768x512 resolution. Sharing the main code (testing this now, will remove this stub once I triple check that it still works after removing seemingly unused imports and code lines):

from PIL import Image
from deepfloyd_if.modules import IFStageI, IFStageII, StableStageIII
from deepfloyd_if.modules.t5 import T5Embedder
from deepfloyd_if.pipelines import style_transfer
import argparse
import datetime
import os

def parse_args():
    p = argparse.ArgumentParser(
            description='Implements designated styles into an image',
            formatter_class=argparse.RawDescriptionHelpFormatter
    )

    p.add_argument(
        "-i",
        "--input-file",
        type=str,
        required=True,
        help="input file for blending",
    )

    p.add_argument(
        "-t",
        "--tag",
        type=int,
        required=True,
        help="can loop over these, functions to set the random seed value (note: same random seed value and input image yield same results)",
    )

    return p.parse_args()

def get_timestamped_filename():
    now = datetime.datetime.now()
    timestamp = now.strftime('%Y%m%d%H%M%S')
    filename = timestamp + '.png'
    return filename

def save_image(pil_images_list, save_path = "images", img_name = "generated_image"):
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    print('pil_images_list')
    print(pil_images_list)
    print(type(pil_images_list))
    counter = 0
    for img in pil_images_list:
        print(type(img))
        print(img)
        img.save("{}/{}_{}.png".format(save_path, img_name, counter))
        counter += 1

def main():
    args = parse_args()
    device = 'cuda:0'

    if_I = IFStageI('IF-I-XL-v1.0', device=device, hf_token=hf_token)
    if_II = IFStageII('IF-II-L-v1.0', device=device, hf_token=hf_token)

    t5 = T5Embedder(device="cpu")

    result = style_transfer(
        t5=t5, if_I=if_I, if_II=if_II,
        support_pil_img=Image.open(args.input_file).convert('RGB'),
        style_prompt=[
            'in style of professional origami',
            'in style of oil art, Tate modern',
            'in style of plastic building bricks',
            'in style of classic anime from 1990',
            ],
        seed=args.tag,
        if_I_kwargs={
            "guidance_scale": 10.0,
            "sample_timestep_respacing": "10,10,10,10,10,10,10,10,0,0",
            'support_noise_less_qsample_steps': 5,
        },
        if_II_kwargs={
            "guidance_scale": 4.0,
            "sample_timestep_respacing": 'smart50',
            "support_noise_less_qsample_steps": 5,
        },
    )

    pil_images_list = result["II"]
    # if_I.show(result['II'], 1, 20)
    save_image(pil_images_list, save_path = "my_images", img_name = get_timestamped_filename())
    print("doing next image ", args.tag)

if __name__ == "__main__":
    main()

usage:

python3 style_transfer_if3.py -i <image-to-style> -t <random-seed-value>
EvanTheBoy commented 1 year ago
save_path = "images", img_name = "generated_image"

Hi, klei22! I don't get it even after reading your code, is this a dream demo?