Skip to content

Instantly share code, notes, and snippets.

@eyaler
Last active June 23, 2022 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eyaler/4af99bea9baf918e24958d2992ffe010 to your computer and use it in GitHub Desktop.
Save eyaler/4af99bea9baf918e24958d2992ffe010 to your computer and use it in GitHub Desktop.
LDM-TXT2IM.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "LDM-TXT2IM.ipynb",
"private_outputs": true,
"provenance": [],
"machine_shape": "hm",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/eyaler/4af99bea9baf918e24958d2992ffe010/ldm-txt2im.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Latent Diffusion Models Text2Image\n",
"\n",
"### https://arxiv.org/abs/2112.10752\n",
"\n",
"### Original repo: https://github.com/CompVis/latent-diffusion\n",
"\n",
"### Enhanced repo by [@RiversHaveWings](https://twitter.com/RiversHaveWings): https://github.com/crowsonkb/latent-diffusion\n",
"\n",
"### Colab optimizations taken from [@multimodalart](https://twitter.com/multimodalart): https://github.com/multimodalart/latent-diffusion-notebook\n",
"\n",
"Shortcut to this notebook: [bit.ly/txt2im](https://bit.ly/txt2im)\n",
"\n",
"Notebook by: [Eyal Gruss](https://eyalgruss.com) \\([@eyaler](https://twitter.com/eyaler)\\)\n",
"\n",
"A curated list of online generative tools: [j.mp/generativetools](https://j.mp/generativetools)"
],
"metadata": {
"id": "Bmvx0uTbF6Iw"
}
},
{
"cell_type": "code",
"source": [
"#@title Setup\n",
"%cd /content\n",
"!nvidia-smi\n",
"!git clone https://github.com/eyaler/latent-diffusion\n",
"!git clone https://github.com/CompVis/taming-transformers\n",
"!pip -q install -e ./taming-transformers\n",
"!pip -q install omegaconf pytorch-lightning torch-fidelity einops transformers\n",
"%cd latent-diffusion\n",
"!mkdir -p models/ldm/text2img-large/\n",
"!wget -nc -O models/ldm/text2img-large/model.ckpt https://ommer-lab.com/files/latent-diffusion/nitro/txt2img-f8-large/model.ckpt\n"
],
"metadata": {
"cellView": "form",
"id": "2iLdwkKD5l8a"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "g0_Gb52UwMHQ"
},
"outputs": [],
"source": [
"#@title Generate\n",
"#@markdown Note: An error probably indicates that you either:\n",
"#@markdown 1. Skipped running the above setup stage or waited too long and the Colab disconnected - run it again.\n",
"#@markdown 2. Ran out of RAM - which may be solved by Runtime->Mangage sessions->TERMINATE and starting over in hope of getting a stronger (non K80) machine, or upgrading to Colab Pro...\n",
"prompt = 'Putin riding a zebra shirtless and waving the Ukrainian flag' #@param {type: 'string'}\n",
"plms = False #@param {type: 'boolean'}\n",
"ddim_eta = 0 #@param {type: 'number'}\n",
"n_samples = 4 #@param {type: 'integer'}\n",
"n_iter = 4 #@param {type: 'integer'}\n",
"scale = 5#@param {type: 'number'}\n",
"ddim_steps = 50#@param {type: 'integer'}\n",
"W = 256 #@param {type: 'integer'}\n",
"H = 256 #@param {type: 'integer'}\n",
"outdir = 'outputs' #@param {type: 'string'}\n",
"from google.colab.patches import cv2_imshow\n",
"import cv2\n",
"from time import time\n",
"start = time()\n",
"plms_arg = ''\n",
"if plms:\n",
" plms_arg = '--plms'\n",
"!python scripts/txt2img.py --prompt \"$prompt\" --ddim_eta $ddim_eta --n_samples $n_samples --n_iter $n_iter --scale $scale --ddim_steps $ddim_steps --H $H --W $W --outdir $outdir $plms_arg\n",
"print(f'Took {time()-start:.0f} secs.')\n",
"filename = f'{outdir}/{prompt.replace(\" \", \"-\")}.png'\n",
"print(filename)\n",
"im = cv2.imread(filename)\n",
"cv2_imshow(im)"
]
},
{
"cell_type": "code",
"source": [
"#@title Download images\n",
"!zip -jrqFS ldm.zip \"$outdir\"\n",
"from google.colab import files\n",
"files.download('ldm.zip')"
],
"metadata": {
"cellView": "form",
"id": "S3PKmI74DENO"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment