Giter Site home page Giter Site logo

unalkalkan / xl-textual-inversion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oss-roettger/xl-textual-inversion

0.0 0.0 0.0 22.26 MB

Textual Inversion for Stable Diffusion XL 1.0

License: GNU Affero General Public License v3.0

Jupyter Notebook 100.00%

xl-textual-inversion's Introduction

XL Textual Inversion for Stable Diffusion XL 1.0 on a 24 GB GPU

XL_Inversion.ipynb is Copyright © 2023 HANS ROETTGER and distributed under the terms of AGPLv3.

This is an implementation of the textual inversion algorithm to incorporate your own objects, faces or styles into Stable Diffusion XL 1.0. Input: a couple of template images. Output: a concept ("Embedding") that can be used in the standard Stable Diffusion XL pipeline to generate your artefacts. (Please also note my implementation variant for Deep Floyd IF.)

Please appreciate weeks of intensive work and leave me a ⭐ star in the top right!

Input Images ➡ XL Embedding

The XL_Inversion.ipynb notebook creates an Embedding for your input images within 15 minutes on a Nvidia 3090 GPU. Starting with a random Embedding, the XL Textual Inversion will optimize it quickly to match your input images:

XL Embedding ➡ Stable Diffusion XL 1.0 Pipeline

Load the T5 Embedding to a single token (e.g. "my") and use it in the standard Stable Diffusion XL prompts (see XL_Apply_Inversion.ipynb)

def set_XLembedding(base,emb,token="my"):
with torch.no_grad():            
    # Embeddings[tokenNo] to learn
    tokens=base.components["tokenizer"].encode(token)
    assert len(tokens)==3, "token is not a single token in 'tokenizer'"
    tokenNo=tokens[1]
    tokens=base.components["tokenizer_2"].encode(token)
    assert len(tokens)==3, "token is not a single token in 'tokenizer_2'"
    tokenNo2=tokens[1]
    embs=base.components["text_encoder"].text_model.embeddings.token_embedding.weight
    embs2=base.components["text_encoder_2"].text_model.embeddings.token_embedding.weight
    assert embs[tokenNo].shape==emb["emb"].shape, "different 'text_encoder'"
    assert embs2[tokenNo2].shape==emb["emb2"].shape, "different 'text_encoder_2'"
    embs[tokenNo]=emb["emb"].to(embs.dtype).to(embs.device)
    embs2[tokenNo2]=emb["emb2"].to(embs2.dtype).to(embs2.device)

def load_XLembedding(base,token="my",embedding_file="myToken.pt",path="./Embeddings/"):
    emb=torch.load(path+embedding_file)
    set_XLembedding(base,emb,token)  

learned="my"
embs_path="./Embeddings/"
emb_file="myPuppet768.pt"
load_XLembedding(base,token=learned,embedding_file=emb_file,path=embs_path)

prompt="The {} doll at the beach".format(learned)
prompt="The 3D rendering of a group of {} figurines dressed in red-striped bathing suits having fun at the beach".format(learned)
prompt="The 3D rendering of a group of {} figurines dressed in dirndl wearing sunglasses drinking beer and having fun at the Oktoberfest".format(learned)

Prerequisites

Installation

  • Copy the XL_Inversion.ipynb notebook into your Stable Diffusion XL 1.0 environment. All you need is in the small notebook.
  • Customize the parameters in the first notebook cell to use your own input images and your local copy of the Stable Diffusion Xl models.
  • Restart and run all!

xl-textual-inversion's People

Contributors

oss-roettger avatar

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.