Coder Social home page Coder Social logo

Comments (3)

afiaka87 avatar afiaka87 commented on May 20, 2024 1

@githubarooski I have a colab notebook for this project in the root of the github repository.

It isn't yet public/stable.

This code uses a feature of python called "generators" which are indeed a bit tough to wrap your head around. Aside from that; a lot of this code is very "reasearch-y" because that was its original intended audience. I've done my best to strike a balance between the strange realm of research where single-letter variables are encouraged and the software engineering realm I'm used to; but I'll admit it isn't perfect.

from clip-guided-diffusion.

afiaka87 avatar afiaka87 commented on May 20, 2024

@githubarooski Good question! that functionality got refactored a bit recently because I implemented multiple text prompts and image prompts. It is now called clean_and_combine_prompts and works on a list of prompts. If you just have one you can just wrap it in a list [my_prompt]

Since the code still isn't stable here and it sounds like you wanna use the original notebooks as well; perhaps you can just copy-paste this into a notebook?

ALPHANUMERIC_REGEX = r"[^\w\s]"
def alphanumeric_filter(s: str) -> str:
    return re.sub(ALPHANUMERIC_REGEX, "", s).replace(" ", "_")

def clean_and_combine_prompts(base_path, txts, batch_idx, max_length=255) -> str:
    clean_txt = "_".join([alphanumeric_filter(txt) for txt in txts])[:max_length]
    return os.path.join(base_path, clean_txt, f"{batch_idx:02}")

You may also just want the log_image method; which I use to create a directory with the prompt as a name, and a subdirectory for each batch in batch_size. Then an image is saved in both ./current.png and the output directory.

def log_image(image: th.Tensor, base_path: str, txts: list, current_step: int, batch_idx: int) -> str:
    dirname = clean_and_combine_prompts(base_path, txts, batch_idx)
    os.makedirs(dirname, exist_ok=True)
    stem = f"{current_step:04}"
    filename = os.path.join(dirname, f'{stem}.png')
    pil_image = tvf.to_pil_image(image.add(1).div(2).clamp(0, 1))
    pil_image.save(os.path.join(os.getcwd(), f'current.png'))
    pil_image.save(filename)
    return str(filename)

You can call it as such in Katherine's code (pseudocode)

prefix_path = "/content/outputs"
current_timestep = diffusion.num_timesteps - 1
for i, sample in enumerate(samples):
    current_timestep -= 1
    if i % save_frequency == 0 or current_timestep == -1:
        for j, img in enumerate(sample["pred_xstart"]):
            log_image(img, prefix_path, prompts, step, j)

from clip-guided-diffusion.

githubarooski avatar githubarooski commented on May 20, 2024

Thanks for the response. This looks like similar features I'm working to integrate. It's very slow going because my python is entry level. Are you planning to publish a colab with your helper utilities?

from clip-guided-diffusion.

Related Issues (16)

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.