### Install Kandinsky 2.0 Library Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_text2img.ipynb Installs the Kandinsky 2.0 library from its GitHub repository. This is a prerequisite for using the model. ```python !pip install "git+https://github.com/ai-forever/Kandinsky-2.0.git" ``` -------------------------------- ### Install Diffusers Library Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Installs the diffusers library from the cloned local path. This command should be run after cloning the repository. ```bash !pip install /content/diffusers ``` -------------------------------- ### Install Kandinsky 2.1 and CLIP Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_text2img.ipynb Installs the necessary libraries for Kandinsky 2.1 and CLIP. Run these commands in a notebook environment. ```python !pip install 'git+https://github.com/ai-forever/Kandinsky-2.git' !pip install git+https://github.com/openai/CLIP.git ``` -------------------------------- ### Install Diffusers and Dependencies Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Installs the diffusers library from GitHub and other required packages like transformers and accelerate. ```python !pip install git+https://github.com/ai-forever/diffusers.git !pip install transformers !pip install accelerate ``` -------------------------------- ### Install Additional Dependencies Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Installs essential Python packages required for the project, including transformers, accelerate, and fastparquet. ```bash !pip install transformers !pip install accelerate !pip install fastparquet ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Clones the Kandinsky-2 repository and installs necessary packages, including CLIP from a specific Git URL. ```bash !git clone https://github.com/ai-forever/Kandinsky-2 !pip install './Kandinsky-2' !pip install git+https://github.com/openai/CLIP.git ``` -------------------------------- ### Install Dependencies for Kandinsky 2.2 and ControlNet Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Installs required Python packages including diffusers, ControlNet, einops, timm, transformers, basicsr, and accelerate. Ensure you have a compatible Python environment. ```python !pip install git+https://github.com/huggingface/diffusers.git !git clone https://github.com/lllyasviel/ControlNet !pip install einops !pip install timm !pip install transformers !pip install basicsr !pip install accelerate ``` -------------------------------- ### Check PyTorch Version Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Verifies the installed PyTorch version. Ensure you have a compatible version for Kandinsky and ControlNet. ```python import torch torch.__version__ ``` -------------------------------- ### Load Dataset with Pandas Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Loads the downloaded parquet file into a pandas DataFrame for further processing. Ensure pandas, PIL, io, and os are installed. ```python import pandas as pd from PIL import Image import io import os df_dataset = pd.read_parquet('/content/train-00000-of-00001-566cc9b19d7203f8.parquet', engine='fastparquet') ``` -------------------------------- ### Image-to-Image Translation with Kandinsky 2.0 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Translate an input image to a new image based on a text prompt and a strength parameter. This example uses 'cuda' and allows customization of generation parameters. ```python from kandinsky2 import get_kandinsky2 from PIL import Image model = get_kandinsky2('cuda', task_type='img2img') init_image = Image.open('image.jpg') images = model.generate_img2img('кошка', init_image, strength=0.8, num_steps=50, denoised_type='dynamic_threshold', dynamic_threshold_v=99.5, sampler='ddim_sampler', ddim_eta=0.05, guidance_scale=10) ``` -------------------------------- ### Execute Decoder LoRA Training Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Executes the constructed command to start the training process for the Kandinsky 2.2 decoder LoRA adapter. This will download model components and begin the training loop. ```bash !{command} ``` -------------------------------- ### Inpainting with Kandinsky 2.0 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Perform inpainting on an image using a text prompt, an initial image, and a mask. This example uses 'cuda' and specifies generation parameters. The mask is defined using NumPy. ```python from kandinsky2 import get_kandinsky2 from PIL import Image import numpy as np model = get_kandinsky2('cuda', task_type='inpainting') init_image = Image.open('image.jpg') mask = np.ones((512, 512), dtype=np.float32) mask[100:] = 0 images = model.generate_inpainting('Девушка в красном платье', init_image, mask, num_steps=50, denoised_type='dynamic_threshold', dynamic_threshold_v=99.5, sampler='ddim_sampler', ddim_eta=0.05, guidance_scale=10) ``` -------------------------------- ### Clone Diffusers Repository Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Clones the diffusers repository from GitHub. This is a prerequisite for installing the library locally. ```bash !git clone https://github.com/ai-forever/diffusers ``` -------------------------------- ### Text-to-Image Generation with Kandinsky 2.0 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Generate images from text prompts using the Kandinsky 2.0 model. This example uses 'cuda' for processing and specifies parameters like batch size, image dimensions, and sampling steps. ```python from kandinsky2 import get_kandinsky2 model = get_kandinsky2('cuda', task_type='text2img') images = model.generate_text2img('A teddy bear на красной площади', batch_size=4, h=512, w=512, num_steps=75, denoised_type='dynamic_threshold', dynamic_threshold_v=99.5, sampler='ddim_sampler', ddim_eta=0.05, guidance_scale=10) ``` -------------------------------- ### Generate Image Embeddings and ControlNet Hint Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb This snippet generates image embeddings for the prior and the final decoder, and creates a depth hint from the input image. It also defines negative prompts to guide the generation away from undesirable features. ```python negative_prior_prompt ='lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature' torch.manual_seed(42) clip_img_emb = prior.interpolate(images_and_prompts=[img], weights=[1], num_images_per_prompt=1, ).image_embeds img_emb = prior(prompt='A capybara, 4k photo', image=clip_img_emb, strength=0.85, num_inference_steps=25, num_images_per_prompt=1,) negative_emb = prior(prompt=negative_prior_prompt, image=clip_img_emb, strength=1, num_inference_steps=25, num_images_per_prompt=1) hint = make_hint(img, detector).unsqueeze(0).half().to('cuda').repeat(1, 1, 1, 1) ``` -------------------------------- ### Generate Image Embeddings with Prior Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Generates image embeddings using the LoRA-enabled prior model. A negative prompt is defined to guide the generation away from undesirable artifacts. ```python negative_prior_prompt ='lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature' torch.manual_seed(42) img_emb = prior(prompt='A robot pokemon, 4k photo', num_inference_steps=25, num_images_per_prompt=1,) ``` -------------------------------- ### Prepare Sample Dataset and Directory Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Creates a sample CSV file for training data and an 'images' directory with a placeholder image. ```python #create data, load your data pd.DataFrame([['images/test.jpg', 'test']], columns=['image_name', 'caption']).to_csv('test.csv', index=False) os.mkdir('images') Image.new('RGB', (768, 768)).save('images/test.jpg') ``` -------------------------------- ### Execute Prior Model Training Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Runs the training script for the prior model using the specified configuration file. ```bash !python Kandinsky-2/train_prior.py --config train_config.yaml ``` -------------------------------- ### Load Training Configuration Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_2_1_unclip.ipynb Loads the UnCLIP 2.1 training configuration from a YAML file. ```python config = OmegaConf.load("Kandinsky-2/train_configs/config_unclip_2_1.yaml") ``` -------------------------------- ### Load Training Configuration Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Loads the prior training configuration from a YAML file using OmegaConf. ```python config = OmegaConf.load("Kandinsky-2/train_configs/config_prior.yaml") ``` -------------------------------- ### Import Libraries and Set Device Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Imports essential libraries for image generation and sets the computation device to CUDA. ```python import sys from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline import torch import PIL import torch from diffusers.utils import load_image from torchvision import transforms from transformers import CLIPVisionModelWithProjection from diffusers.models import UNet2DConditionModel import numpy as np DEVICE = torch.device('cuda:7') ``` -------------------------------- ### Execute Kandinsky 2.1 UnCLIP Training Script Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_2_1_unclip.ipynb Runs the main training script for Kandinsky 2.1 UnCLIP using the prepared configuration file. ```bash !python Kandinsky-2/train_2_1_unclip.py --config train_config.yaml ``` -------------------------------- ### Image Hint Generation Function Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Prepares an input image for ControlNet by resizing and detecting features using a specified detector (e.g., Midas for depth). The output is a normalized tensor suitable for model input. ```python def make_hint(img, detector): input_image = np.array(img) img = resize_image(HWC3(input_image), input_image.shape[1]) H, W, C = img.shape detected_map, _ = detector(img) detected_map = HWC3(detected_map) detected_map = torch.from_numpy(detected_map.copy()).float() / 255.0 hint = detected_map.permute(2, 0, 1) return hint ``` -------------------------------- ### Update Training Configuration Parameters Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Modifies the loaded configuration to point to the downloaded model weights, the prepared dataset, and sets the number of epochs and save path. It also ensures the save directory exists. ```python config['params_path'] = 'kandinsky2/2_1/prior_fp16.ckpt' config['clip_mean_std_path'] = 'kandinsky2/2_1/ViT-L-14_stats.th' config['data']['train']['df_path'] = 'test.csv' config['num_epochs'] = 1001 config['save_path'] = 'saves' if not os.path.exists('saves'): os.mkdir('saves') ``` -------------------------------- ### Execute Model Weight Download Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Calls the function to download the necessary model weights for Kandinsky 2.1. ```python get_kandinsky2_1() ``` -------------------------------- ### Load Kandinsky Prior Pipeline Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Initializes the KandinskyV22PriorPipeline using the pre-loaded image encoder. The pipeline is configured with float16 precision and moved to the specified device. ```python prior = KandinskyV22PriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior', image_encoder=image_encoder, torch_dtype=torch.float16 ).to(DEVICE) ``` -------------------------------- ### Download Dataset Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Downloads the Pokemon BLIP captions dataset from Hugging Face using wget. ```python %%capture !wget https://huggingface.co/datasets/lambdalabs/pokemon-blip-captions/resolve/main/data/train-00000-of-00001-566cc9b19d7203f8.parquet ``` -------------------------------- ### Download Kandinsky 2.1 Model Weights Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_2_1_unclip.ipynb Downloads the necessary model weights and configuration files for Kandinsky 2.1 text-to-image or inpainting tasks using Hugging Face Hub. ```python def get_kandinsky2_1( task_type="text2img", cache_dir="./kandinsky2", use_auth_token=None, ): cache_dir = os.path.join(cache_dir, "2_1") if task_type == "text2img": model_name = "decoder_fp16.ckpt" config_file_url = hf_hub_url(repo_id="sberbank-ai/Kandinsky_2.1", filename=model_name) elif task_type == "inpainting": model_name = "inpainting_fp16.ckpt" config_file_url = hf_hub_url(repo_id="sberbank-ai/Kandinsky_2.1", filename=model_name) cached_download( config_file_url, cache_dir=cache_dir, force_filename=model_name, use_auth_token=use_auth_token, ) cache_dir_text_en = os.path.join(cache_dir, "text_encoder") for name in [ "config.json", "pytorch_model.bin", "sentencepiece.bpe.model", "special_tokens_map.json", "tokenizer.json", "tokenizer_config.json", ]: config_file_url = hf_hub_url(repo_id="sberbank-ai/Kandinsky_2.1", filename=f"text_encoder/{name}") cached_download( config_file_url, cache_dir=cache_dir_text_en, force_filename=name, use_auth_token=use_auth_token, ) config_file_url = hf_hub_url(repo_id="sberbank-ai/Kandinsky_2.1", filename="movq_final.ckpt") cached_download( config_file_url, cache_dir=cache_dir, force_filename="movq_final.ckpt", use_auth_token=use_auth_token, ) ``` -------------------------------- ### Create Image Directory Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Creates a directory named 'images' in the /content directory to store processed images. Requires the 'os' module. ```python dir_name = '/content/images' os.mkdir(dir_name) ``` -------------------------------- ### Image Fusion with Kandinsky 2.1 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Fuse multiple images and text prompts with specified weights. Requires Pillow for image handling. Adjust parameters like num_steps, guidance_scale, and sampler for desired output. ```python from kandinsky2 import get_kandinsky2 from PIL import Image model = get_kandinsky2('cuda', task_type='text2img', model_version='2.1', use_flash_attention=False) images_texts = ['red cat', Image.open('img1.jpg'), Image.open('img2.jpg'), 'a man'] weights = [0.25, 0.25, 0.25, 0.25] images = model.mix_images( images_texts, weights, num_steps=150, batch_size=1, guidance_scale=5, h=768, w=768, sampler='p_sampler', prior_cf_scale=4, prior_steps="5" ) ``` -------------------------------- ### Load Input Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Loads an image from a URL and resizes it to the specified dimensions. This image will be used as the input for the image-to-image generation. ```python img = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ).resize((512, 512)) ``` -------------------------------- ### Generate Text-to-Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_text2img.ipynb Generates images from a text prompt using the initialized model. Customize parameters like batch size, image dimensions, number of steps, denoising settings, sampler, and guidance scale. ```python images = model.generate_text2img('кошка в космосе', batch_size=4, h=512, w=512, num_steps=75, denoised_type='dynamic_threshold', dynamic_threshold_v=99.5, sampler='ddim_sampler', ddim_eta=0.01, guidance_scale=10) ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Imports essential libraries for using the Kandinsky pipelines, PyTorch, PIL, and Hugging Face utilities. ```python import sys from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline import torch import PIL import torch from diffusers.utils import load_image from torchvision import transforms from transformers import CLIPVisionModelWithProjection from diffusers.models import UNet2DConditionModel import numpy as np ``` -------------------------------- ### Initialize Kandinsky 2.0 Model Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_text2img.ipynb Initializes the Kandinsky 2.0 text-to-image model. Specify the device (e.g., 'cuda'), model version, and task type. ```python model = get_kandinsky2('cuda', model_version="2.0", task_type='text2img') ``` -------------------------------- ### Generate Another Image from Text Prompt Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_mixing.ipynb Generate a second image from a different text prompt, using similar generation parameters as the first image. ```python image_tiger = model.generate_text2img( "tiger", num_steps=100, batch_size=1, guidance_scale=4, h=768, w=768, sampler='p_sampler', prior_cf_scale=4, prior_steps="5" )[0] ``` -------------------------------- ### Process and Save Images and Captions Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Iterates through the dataset, opens image bytes, saves them as JPG files, and creates a new DataFrame with image paths and captions. This DataFrame is then saved as 'captions.csv'. Requires pandas, PIL, io, and os. ```python df = [] for i in range(len(df_dataset)): image = Image.open(io.BytesIO(df_dataset['image.bytes'].iloc[i])) img_path = os.path.join(dir_name, f'{i}.jpg') image.save(img_path) df.append([img_path, df_dataset['text'].iloc[i]]) df = pd.DataFrame(df) df.columns = ['paths', 'caption'] df.to_csv('captions.csv', index=False) ``` -------------------------------- ### Download Kandinsky 2.1 Prior Model Weights Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Defines a function to download the prior model checkpoint and associated statistics from Hugging Face Hub, caching them locally. ```python def get_kandinsky2_1( cache_dir="./kandinsky2", use_auth_token=None, ): cache_dir = os.path.join(cache_dir, "2_1") prior_name = "prior_fp16.ckpt" config_file_url = hf_hub_url(repo_id="sberbank-ai/Kandinsky_2.1", filename=prior_name) cached_download( config_file_url, cache_dir=cache_dir, force_filename=prior_name, use_auth_token=use_auth_token, ) config_file_url = hf_hub_url(repo_id="sberbank-ai/Kandinsky_2.1", filename="ViT-L-14_stats.th") cached_download( config_file_url, cache_dir=cache_dir, force_filename="ViT-L-14_stats.th", use_auth_token=use_auth_token, ) ``` -------------------------------- ### Generate Image-to-Image with Kandinsky 2.0 Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_img2img.ipynb Generates a new image based on an initial image ('image.jpg') and a text prompt ('кошка'). Configures generation parameters such as strength, steps, dynamic thresholding, sampler, and guidance scale. ```python init_image = Image.open('image.jpg') images = model.generate_img2img('кошка', init_image, strength=0.8, num_steps=50, denoised_type='dynamic_threshold', dynamic_threshold_v=99.5, sampler='ddim_sampler', ddim_eta=0.05, guidance_scale=10) ``` -------------------------------- ### Generate Image Embeddings and Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Sets a manual seed for reproducibility, defines negative prompts, generates image embeddings using the prior pipeline, and then generates the final image using the decoder pipeline with specified dimensions and steps. ```python torch.manual_seed(42) negative_prior_prompt ='lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature' img_emb = prior( prompt='A robot, 4k photo', num_inference_steps=25, num_images_per_prompt=1 ) negative_emb = prior( prompt=negative_prior_prompt, num_inference_steps=25, num_images_per_prompt=1 ) images = decoder(image_embeds=img_emb.image_embeds, negative_image_embeds=negative_emb.image_embeds, num_inference_steps=50, height=512, width=512) ``` -------------------------------- ### Load Kandinsky Decoder Pipeline Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Initializes the KandinskyV22Pipeline (decoder) using the pre-loaded UNet model. The pipeline is configured with float16 precision and moved to the specified device. ```python decoder = KandinskyV22Pipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder', unet=unet, torch_dtype=torch.float16 ).to(DEVICE) ``` -------------------------------- ### Load Image Encoder Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Loads the CLIP Vision Model with Projection for the image encoder, essential for the prior pipeline. It's loaded in half precision and moved to the specified device. ```python image_encoder = CLIPVisionModelWithProjection.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior', subfolder='image_encoder' ).half().to(DEVICE) ``` -------------------------------- ### Text-to-Image Generation with Kandinsky 2.2 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Generates an image from a text prompt using the Kandinsky 2.2 model. Ensure 'cuda' is available for GPU acceleration. ```python from kandinsky2 import get_kandinsky2 model = get_kandinsky2('cuda', task_type='text2img', model_version='2.2') images = model.generate_text2img( "red cat, 4k photo", decoder_steps=50, batch_size=1, h=1024, w=768, ) ``` -------------------------------- ### Define Prior LoRA Training Command Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Constructs the command-line arguments for training the Kandinsky 2.2 prior LoRA adapter. Similar to the decoder training, this specifies dataset path, batch size, mixed precision, training steps, learning rate, output directory, and LoRA rank. ```python command = (f'python3 /content/diffusers/examples/kandinsky2_2_train/tune_prior_lora.py ' f'--train_images_paths_csv=/content/captions.csv ' f'--train_batch_size=1 ' f'--gradient_accumulation_steps=1 ' f'--gradient_checkpointing ' f'--mixed_precision="fp16" ' f'--max_train_steps=500 ' f'--lr=1e-05 ' f'--max_grad_norm=1 ' f'--lr_scheduler="constant" ' f'--lr_warmup_steps=0 ' f'--output_dir=/content/decoder_prior_saves ' f'--rank=4 ' f'--checkpointing_steps=500') ``` -------------------------------- ### Initialize Kandinsky 2.0 Image-to-Image Model Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_img2img.ipynb Initializes the Kandinsky 2.0 model for image-to-image generation on a CUDA-enabled device. Specifies the model version and task type. ```python model = get_kandinsky2('cuda', model_version="2.0", task_type='img2img') ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Imports essential Python libraries for file operations, model configuration, image handling, and data manipulation. ```python import os from huggingface_hub import hf_hub_url, cached_download from copy import deepcopy from omegaconf.dictconfig import DictConfig from omegaconf import OmegaConf from PIL import Image import numpy as np import pandas as pd ``` -------------------------------- ### Display DataFrame Head Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Displays the first few rows of the loaded DataFrame to inspect its structure and content. ```python df_dataset ``` -------------------------------- ### Text-to-Image Generation with Kandinsky 2.1 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Generates an image from a text prompt using the Kandinsky 2.1 model. Requires PyTorch and specific model version. Flash attention is disabled. ```python from kandinsky2 import get_kandinsky2 model = get_kandinsky2('cuda', task_type='text2img', model_version='2.1', use_flash_attention=False) images = model.generate_text2img( "red cat, 4k photo", num_steps=100, batch_size=1, guidance_scale=4, h=768, w=768, sampler='p_sampler', prior_cf_scale=4, prior_steps="5" ) ``` -------------------------------- ### Inpainting with Kandinsky 2.1 Source: https://github.com/ai-forever/kandinsky-2/blob/main/README.md Perform inpainting on an image using a text prompt and a mask. Requires Pillow and NumPy. The mask defines the area to be inpainted. Customize generation parameters as needed. ```python from kandinsky2 import get_kandinsky2 from PIL import Image import numpy as np model = get_kandinsky2('cuda', task_type='inpainting', model_version='2.1', use_flash_attention=False) init_image = Image.open('img.jpg') mask = np.ones((768, 768), dtype=np.float32) mask[:,:550] = 0 images = model.generate_inpainting( 'man 4k photo', init_image, mask, num_steps=150, batch_size=1, guidance_scale=5, h=768, w=768, sampler='p_sampler', prior_cf_scale=4, prior_steps="5" ) ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Imports all required libraries and modules for Kandinsky 2.2 and ControlNet functionality, including image processing and diffusion models. ```python import sys sys.path.append('/content/ControlNet/') from annotator.util import resize_image, HWC3 from annotator.midas import MidasDetector from diffusers import KandinskyV22PriorEmb2EmbPipeline from diffusers import KandinskyV22ControlnetImg2ImgPipeline import torch import PIL import torch from diffusers.utils import load_image from torchvision import transforms from transformers import CLIPVisionModelWithProjection from diffusers.models import UNet2DConditionModel import numpy as np ``` -------------------------------- ### Display Generated Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_mixing.ipynb Display the generated image. This is typically done in an environment that supports image rendering, like a Jupyter notebook. ```python image_yoda ``` -------------------------------- ### Load UNet Model Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Loads the UNet2DConditionModel for the decoder pipeline. The model is loaded in half precision and moved to the specified device. ```python unet = UNet2DConditionModel.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder', subfolder='unet' ).half().to(DEVICE) ``` -------------------------------- ### Generate Image Embeddings with Prior Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Generates image embeddings using the prior model. Specify the prompt, number of inference steps, and images per prompt. ```python negative_emb = prior(prompt=negative_prior_prompt, num_inference_steps=25, num_images_per_prompt=1) ``` -------------------------------- ### Display Second Generated Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_mixing.ipynb Display the second generated image. ```python image_tiger ``` -------------------------------- ### Define Decoder LoRA Training Command Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Constructs the command-line arguments for training the Kandinsky 2.2 decoder LoRA adapter. This includes dataset path, image resolution, batch size, mixed precision, training steps, learning rate, output directory, and LoRA rank. ```python command = (f'python3 /content/diffusers/examples/kandinsky2_2_train/tune_decoder_lora.py ' f'--train_images_paths_csv=/content/captions.csv ' f'--image_resolution=768 ' f'--train_batch_size=1 ' f'--gradient_accumulation_steps=1 ' f'--gradient_checkpointing ' f'--mixed_precision="fp16" ' f'--max_train_steps=500 ' f'--lr=1e-05 ' f'--max_grad_norm=1 ' f'--lr_scheduler="constant" ' f'--lr_warmup_steps=0 ' f'--output_dir=/content/decoder_lora_saves ' f'--rank=4 ' f'--checkpointing_steps=500') ``` -------------------------------- ### Initialize Midas Detector Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Initializes the Midas depth estimation detector. This is used to generate depth maps from input images. ```python detector = MidasDetector() ``` -------------------------------- ### Load Kandinsky Prior Pipeline Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Loads the Kandinsky 2.2 Prior embedding-to-embedding pipeline, using the pre-loaded image encoder and setting the device to CUDA with float16 precision. ```python prior = KandinskyV22PriorEmb2EmbPipeline.from_pretrained('kandinsky-community/kandinsky-2-2-prior', image_encoder=image_encoder, torch_dtype=torch.float16) prior = prior.to("cuda") ``` -------------------------------- ### Generate Image from Text Prompt Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_text2img.ipynb Generates an image based on a text prompt using the loaded Kandinsky 2.1 model. Adjust parameters like `num_steps`, `guidance_scale`, image dimensions, and sampler for desired results. ```python images = model.generate_text2img( "red cat, 4k photo", num_steps=100, batch_size=1, guidance_scale=4, h=768, w=768, sampler='p_sampler', prior_cf_scale=4, prior_steps="5" ) ``` -------------------------------- ### Load Kandinsky 2.2 ControlNet Pipeline Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Loads the Kandinsky 2.2 ControlNet depth pipeline from pre-trained weights. Ensure you have the necessary dependencies and models downloaded. ```python decoder = KandinskyV22ControlnetImg2ImgPipeline.from_pretrained('kandinsky-community/kandinsky-2-2-controlnet-depth', unet=unet, torch_dtype=torch.float16) decoder = decoder.to("cuda") ``` -------------------------------- ### Decode Image with ControlNet Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Generates the final image using the decoder, incorporating the input image, text prompts, negative embeddings, and the ControlNet hint. Adjust `num_inference_steps` for quality vs. speed. ```python images = decoder(image=img, strength=0.5, image_embeds=img_emb.image_embeds, negative_image_embeds=negative_emb.image_embeds, hint=hint, num_inference_steps=50, height=512, width=512) ``` -------------------------------- ### Load Kandinsky Models and Move to CUDA Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Loads the pre-trained Kandinsky V2.2 prior and decoder models, including the image encoder and UNet, and moves them to the CUDA-enabled GPU with float16 precision. ```python image_encoder = CLIPVisionModelWithProjection.from_pretrained('kandinsky-community/kandinsky-2-2-prior', subfolder='image_encoder').to(torch.float16).to('cuda') unet = UNet2DConditionModel.from_pretrained('kandinsky-community/kandinsky-2-2-decoder', subfolder='unet').to(torch.float16).to('cuda') prior = KandinskyV22PriorPipeline.from_pretrained('kandinsky-community/kandinsky-2-2-prior', image_encoder=image_encoder, torch_dtype=torch.float16) prior = prior.to("cuda") decoder = KandinskyV22Pipeline.from_pretrained('kandinsky-community/kandinsky-2-2-decoder', unet=unet, torch_dtype=torch.float16) decoder = decoder.to("cuda") ``` -------------------------------- ### Load Kandinsky 2.1 Model Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_text2img.ipynb Loads the Kandinsky 2.1 model for text-to-image generation. Specify the device, task type, cache directory, model version, and flash attention usage. ```python model = get_kandinsky2( 'cuda', task_type='text2img', cache_dir='/tmp/kandinsky2', model_version='2.1', use_flash_attention=False ) ``` -------------------------------- ### Display Generated Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2.ipynb Accesses and displays the first generated image from the 'images' object. ```python images.images[0] ``` -------------------------------- ### Save Updated Training Configuration Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/train_prior.ipynb Saves the modified training configuration to a new YAML file named 'train_config.yaml'. ```python with open("train_config.yaml", "w") as f: OmegaConf.save(config, f) ``` -------------------------------- ### Display Mixed Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_mixing.ipynb Display the resulting image after mixing the two input images. ```python image_mixed ``` -------------------------------- ### Import Kandinsky 2.0 Function Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_text2img.ipynb Imports the `get_kandinsky2` function, which is used to load the model. ```python from kandinsky2 import get_kandinsky2 ``` -------------------------------- ### Load Kandinsky 2.0 Inpainting Model Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_inpainting.ipynb Loads the Kandinsky 2.0 model for inpainting tasks, specifying the device (e.g., 'cuda') and model version. Ensure CUDA is available if 'cuda' is specified. ```python model = get_kandinsky2('cuda', model_version="2.0", task_type='inpainting') ``` -------------------------------- ### Apply LoRA to Kandinsky Prior Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Initializes LoRA attention processors for the prior model and loads the LoRA weights. This snippet configures LoRA for the prior's attention layers. ```python from diffusers.models.attention_processor import LoRAAttnProcessor, LoRAAttnAddedKVProcessor lora_attn_procs = {} for name in prior.prior.attn_processors.keys(): lora_attn_procs[name] = LoRAAttnProcessor(hidden_size=2048).to('cuda') prior.prior.set_attn_processor(lora_attn_procs) prior.prior.load_state_dict(torch.load('/content/decoder_prior_saves/checkpoint-500/pytorch_model.bin'), strict=False) None ``` -------------------------------- ### Import necessary libraries Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_inpainting.ipynb Imports the required modules from the Kandinsky library and standard Python libraries like PIL and NumPy for image manipulation. ```python from kandinsky2 import get_kandinsky2 from PIL import Image import numpy as np ``` -------------------------------- ### Mix Two Generated Images Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky_2_1_mixing.ipynb Mix two previously generated images with specified weights. This function allows blending the content of multiple images. ```python image_mixed = model.mix_images( [image_yoda, image_tiger], [0.5, 0.5], num_steps=100, batch_size=1, guidance_scale=4, h=768, w=768, sampler='p_sampler', prior_cf_scale=4, prior_steps="5", )[0] ``` -------------------------------- ### Load Kandinsky Image Encoder Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Loads the CLIP image encoder for Kandinsky 2.2, specifying the model path and moving it to the CUDA device with float16 precision. ```python image_encoder = CLIPVisionModelWithProjection.from_pretrained('kandinsky-community/kandinsky-2-2-prior', subfolder='image_encoder').to(torch.float16).to('cuda') ``` -------------------------------- ### Access Generated Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_text2img.ipynb Accesses the first generated image from the list of results. The output is a PIL Image object. ```python images[0] ``` -------------------------------- ### Apply LoRA to Kandinsky Decoder Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Initializes LoRA attention processors for the decoder's UNet and loads the LoRA weights. This snippet configures LoRA for cross-attention layers within the decoder's UNet. ```python from diffusers.models.attention_processor import LoRAAttnProcessor, LoRAAttnAddedKVProcessor lora_attn_procs = {} d = torch.load('/content/decoder_lora_saves/checkpoint-500/pytorch_model.bin') for name in decoder.unet.attn_processors.keys(): cross_attention_dim = None if name.endswith("attn1.processor") else decoder.unet.config.cross_attention_dim if name.startswith("mid_block"): hidden_size = decoder.unet.config.block_out_channels[-1] elif name.startswith("up_blocks"): block_id = int(name[len("up_blocks.")]) hidden_size = list(reversed(decoder.unet.config.block_out_channels))[block_id] elif name.startswith("down_blocks"): block_id = int(name[len("down_blocks.")]) hidden_size = decoder.unet.config.block_out_channels[block_id] lora_attn_procs[name] = LoRAAttnAddedKVProcessor( hidden_size=hidden_size, cross_attention_dim=cross_attention_dim, rank=4, ).to('cuda') decoder.unet.set_attn_processor(lora_attn_procs) decoder.unet.load_state_dict(d, strict=False) None ``` -------------------------------- ### Load Kandinsky ControlNet UNet Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/kandinsky2_2_controlnet.ipynb Loads the UNet model specifically for Kandinsky 2.2 ControlNet (depth variant), ensuring it's on the CUDA device with float16 precision. ```python unet = UNet2DConditionModel.from_pretrained('kandinsky-community/kandinsky-2-2-controlnet-depth', subfolder='unet').to(torch.float16).to('cuda') ``` -------------------------------- ### Decode Embeddings to Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/lora_decoder.ipynb Decodes image embeddings into an image using the decoder model. Parameters include image embeddings, negative image embeddings, inference steps, height, and width. ```python images = decoder(image_embeds=img_emb.image_embeds, negative_image_embeds=negative_emb.image_embeds, num_inference_steps=50, height=512, width=512) ``` -------------------------------- ### Generate Inpainted Image Source: https://github.com/ai-forever/kandinsky-2/blob/main/notebooks/Kandinsky_2_0_inpainting.ipynb Generates an inpainted image using the loaded Kandinsky 2.0 model. Requires an initial image, a mask defining the area to be inpainted, and various generation parameters. ```python init_image = Image.open('image.jpg') mask = np.ones((512, 512), dtype=np.float32) mask[-300:] = 0 images = model.generate_inpainting('красная кошка', init_image, mask, num_steps=50, denoised_type='dynamic_threshold', dynamic_threshold_v=99.5, sampler='ddim_sampler', ddim_eta=0.05, guidance_scale=10) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.