{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4mDGz9V6JC0a", "outputId": "75309249-37c8-4ba6-e950-facc85a4c249" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for diffusers (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n" ] } ], "source": [ "!pip install git+https://github.com/cosmo3769/diffusers@standardize-model-card-t2i -q" ] }, { "cell_type": "code", "source": [ "!pip install --upgrade wandb -q" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hWoT-7tLrC3X", "outputId": "25b205fa-5723-41c4-bf0f-e6c0604c2c19" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m10.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m196.4/196.4 kB\u001b[0m \u001b[31m27.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m257.8/257.8 kB\u001b[0m \u001b[31m33.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.7/62.7 kB\u001b[0m \u001b[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ] }, { "cell_type": "code", "source": [ "import os\n", "import wandb\n", "from argparse import Namespace\n", "from diffusers.utils import is_wandb_available, make_image_grid\n", "from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card" ], "metadata": { "id": "r0rK5JfUrAfc" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "def save_model_card(\n", " args,\n", " repo_id: str,\n", " images=None,\n", " repo_folder=None,\n", "):\n", " img_str = \"\"\n", " if len(images) > 0:\n", " image_grid = make_image_grid(images, 1, len(args.validation_prompts))\n", " image_grid.save(os.path.join(repo_folder, \"val_imgs_grid.png\"))\n", " img_str += \"![val_imgs_grid](./val_imgs_grid.png)\\n\"\n", "\n", " model_description = f\"\"\"\n", "# Text-to-image finetuning - {repo_id}\n", "\n", "This pipeline was finetuned from **{args.pretrained_model_name_or_path}** on the **{args.dataset_name}** dataset. Below are some example images generated with the finetuned pipeline using the following prompts: {args.validation_prompts}: \\n\n", "{img_str}\n", "\n", "## Pipeline usage\n", "\n", "You can use the pipeline like so:\n", "\n", "```python\n", "from diffusers import DiffusionPipeline\n", "import torch\n", "\n", "pipeline = DiffusionPipeline.from_pretrained(\"{repo_id}\", torch_dtype=torch.float16)\n", "prompt = \"{args.validation_prompts[0]}\"\n", "image = pipeline(prompt).images[0]\n", "image.save(\"my_image.png\")\n", "```\n", "\n", "## Training info\n", "\n", "These are the key hyperparameters used during training:\n", "\n", "* Epochs: {args.num_train_epochs}\n", "* Learning rate: {args.learning_rate}\n", "* Batch size: {args.train_batch_size}\n", "* Gradient accumulation steps: {args.gradient_accumulation_steps}\n", "* Image resolution: {args.resolution}\n", "* Mixed-precision: {args.mixed_precision}\n", "\n", "\"\"\"\n", " wandb_info = \"\"\n", " if is_wandb_available():\n", " wandb_run_url = None\n", " if wandb.run is not None:\n", " wandb_run_url = wandb.run.url\n", "\n", " if wandb_run_url is not None:\n", " wandb_info = f\"\"\"\n", "More information on all the CLI arguments and the environment are available on your [`wandb` run page]({wandb_run_url}).\n", "\"\"\"\n", "\n", " model_description += wandb_info\n", "\n", " model_card = load_or_create_model_card(\n", " repo_id_or_path=repo_id,\n", " from_training=True,\n", " license=\"creativeml-openrail-m\",\n", " base_model=args.pretrained_model_name_or_path,\n", " model_description=model_description,\n", " inference=True,\n", " )\n", "\n", " tags = [\"stable-diffusion\", \"stable-diffusion-diffusers\", \"text-to-image\", \"diffusers\"]\n", " model_card = populate_model_card(model_card, tags=tags)\n", "\n", " model_card.save(os.path.join(repo_folder, \"README.md\"))" ], "metadata": { "id": "6mgnDhfzrTp4" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "args = Namespace(\n", " pretrained_model_name_or_path=\"runwayml/stable-diffusion-v1-5\",\n", " dataset_name=\"your_dataset_name\",\n", " validation_prompts=[\"prompt1\", \"prompt2\", \"prompt3\"],\n", " num_train_epochs=\"num_train_epochs\",\n", " learning_rate=\"lr\",\n", " train_batch_size=\"batch_size\",\n", " gradient_accumulation_steps=\"ga_steps\",\n", " resolution=\"img_resolution\",\n", " mixed_precision=\"boolean\"\n", ")" ], "metadata": { "id": "UfgqlRHw0hQE" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "from diffusers.utils import load_image\n", "\n", "images = [\n", " load_image(\"https://huggingface.co/datasets/diffusers/docs-images/resolve/main/amused/A%20mushroom%20in%20%5BV%5D%20style.png\")\n", " for _ in range(3)\n", "]\n", "\n", "save_model_card(\n", " args,\n", " repo_id=\"cosmo3769/test\",\n", " images=images,\n", " repo_folder=\".\",\n", ")" ], "metadata": { "id": "JTEDsOd_rm7-" }, "execution_count": 6, "outputs": [] }, { "cell_type": "code", "source": [ "!cat README.md" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NwCOmASdsUCT", "outputId": "37a56224-e66a-4b51-8233-b9b40958539a" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "---\n", "license: creativeml-openrail-m\n", "library_name: diffusers\n", "tags:\n", "- stable-diffusion\n", "- stable-diffusion-diffusers\n", "- text-to-image\n", "- diffusers\n", "inference: true\n", "base_model: runwayml/stable-diffusion-v1-5\n", "---\n", "\n", "\n", "\n", "\n", "# Text-to-image finetuning - cosmo3769/test\n", "\n", "This pipeline was finetuned from **runwayml/stable-diffusion-v1-5** on the **your_dataset_name** dataset. Below are some example images generated with the finetuned pipeline using the following prompts: ['prompt1', 'prompt2', 'prompt3']: \n", "\n", "![val_imgs_grid](./val_imgs_grid.png)\n", "\n", "\n", "## Pipeline usage\n", "\n", "You can use the pipeline like so:\n", "\n", "```python\n", "from diffusers import DiffusionPipeline\n", "import torch\n", "\n", "pipeline = DiffusionPipeline.from_pretrained(\"cosmo3769/test\", torch_dtype=torch.float16)\n", "prompt = \"prompt1\"\n", "image = pipeline(prompt).images[0]\n", "image.save(\"my_image.png\")\n", "```\n", "\n", "## Training info\n", "\n", "These are the key hyperparameters used during training:\n", "\n", "* Epochs: num_train_epochs\n", "* Learning rate: lr\n", "* Batch size: batch_size\n", "* Gradient accumulation steps: ga_steps\n", "* Image resolution: img_resolution\n", "* Mixed-precision: boolean\n", "\n", "\n", "\n", "## Intended uses & limitations\n", "\n", "#### How to use\n", "\n", "```python\n", "# TODO: add an example code snippet for running this diffusion pipeline\n", "```\n", "\n", "#### Limitations and bias\n", "\n", "[TODO: provide examples of latent issues and potential remediations]\n", "\n", "## Training details\n", "\n", "[TODO: describe the data used to train the model]" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "w7IqDNR72RGf" }, "execution_count": null, "outputs": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }