GithubHelp home page GithubHelp logo

How to save image about if HOT 4 OPEN

deep-floyd avatar deep-floyd commented on August 18, 2024
How to save image

from if.

Comments (4)

bolongliu avatar bolongliu commented on August 18, 2024

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")

from if.

klei22 avatar klei22 commented on August 18, 2024

@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 : )

from if.

klei22 avatar klei22 commented on August 18, 2024

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>

from if.

EvanTheBoy avatar EvanTheBoy commented on August 18, 2024
save_path = "images", img_name = "generated_image"

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

from if.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.