2023-07-03 12:27:17 -06:00
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
2024-03-03 14:13:58 -07:00
"# Cog Captioning\n",
2024-03-24 08:00:00 -06:00
"<a href=\"https://colab.research.google.com/github/nawnie/EveryDream2trainer/blob/main/CaptionCog.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
2024-03-03 14:13:58 -07:00
"This notebook is an implementation of [CogVLM](https://github.com/THUDM/CogVLM) for image captioning. \n",
2023-07-03 13:24:58 -06:00
"\n",
2024-03-24 08:00:00 -06:00
"Read [Docs](doc/CAPTION_COG.md) for basic usage guide. \n",
2023-07-03 12:27:17 -06:00
"\n",
2024-03-24 08:00:00 -06:00
"Open in [Google Colab](https://colab.research.google.com/github/victorchall/EveryDream2trainer/blob/main/CaptionCog.ipynb) **OR** use Runpod/Vast/Whatever using the EveryDream2trainer docker container/template and open this notebook.\n",
"\n",
"### Requirements\n",
"Ampere or newer GPU with 16GB+ VRAM (ex. A100, 3090, 4060 Ti 16GB, etc). \n",
"The 4bit quantization loading requires bfloat16 datatype support, which is not supported on older Turing GPUs like the T4 16GB, which rules out using free tier Google Colab.\n"
2023-07-03 12:27:17 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# install dependencies\n",
2024-03-24 07:30:23 -06:00
"!pip install huggingface-hub -q\n",
"!pip install transformers -q\n",
"!pip install pynvml -q\n",
"!pip install colorama -q\n",
"!pip install peft -q\n",
"!pip install bitsandbytes -q\n",
"!pip install einops -q\n",
"!pip install xformers -q"
2023-07-03 12:27:17 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Colab only setup (do NOT run for docker/runpod/vast)\n",
"!git clone https://github.com/victorchall/EveryDream2trainer\n",
2023-07-03 13:24:58 -06:00
"%cd EveryDream2trainer\n",
2024-03-24 07:30:23 -06:00
"%mkdir -p /content/EveryDream2trainer/input\n",
"%cd /content/EveryDream2trainer"
2023-07-03 12:27:17 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2023-07-03 13:24:58 -06:00
"%cd /content/EveryDream2trainer\n",
2023-07-03 12:27:17 -06:00
"#@markdown Optional: Extract all TAR and ZIP files in the input folder (so you can just upload a large TAR/ZIP)\n",
"import os\n",
"import zipfile\n",
"import tarfile\n",
"\n",
"# Directory containing the input files\n",
"input_folder = \"input\"\n",
"\n",
"# Extract ZIP files\n",
"for file in os.listdir(input_folder):\n",
" if file.endswith(\".zip\"):\n",
" file_path = os.path.join(input_folder, file)\n",
" with zipfile.ZipFile(file_path, 'r') as zip_ref:\n",
" zip_ref.extractall(input_folder)\n",
"\n",
"# Extract TAR files\n",
"for file in os.listdir(input_folder):\n",
" if file.endswith(\".tar\"):\n",
" file_path = os.path.join(input_folder, file)\n",
" with tarfile.open(file_path, 'r') as tar_ref:\n",
" tar_ref.extractall(input_folder)"
]
},
2024-03-24 07:30:23 -06:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Connect Gdrive (Optional, will popup a warning)\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
2023-07-03 12:27:17 -06:00
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run captions.\n",
"\n",
2024-03-24 07:30:23 -06:00
"Place your images in \"input\" folder, or you can change the image_dir to point to a Gdrive folder.\n",
2024-03-24 08:00:00 -06:00
"Note: Colab may complain that you are running out of disk space, but it should still work.\n"
2023-07-03 12:27:17 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2024-03-03 14:13:58 -07:00
"# 16GB GPU, must not use more than 1 beam\n",
"# 24GB GPU, can use 3 beams\n",
2023-07-03 13:24:58 -06:00
"%cd /content/EveryDream2trainer\n",
2024-03-24 07:30:23 -06:00
"%run caption_cog.py --image_dir \"input\" --num_beams 1 --prompt \"Write a description.\" --no_overwrite"
2023-07-03 12:27:17 -06:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2024-03-03 14:13:58 -07:00
"# This is a fancier version of above with more options set\n",
2023-07-03 13:24:58 -06:00
"%cd /content/EveryDream2trainer\n",
2024-03-24 07:30:23 -06:00
"%run caption_cog.py --image_dir \"input\" --num_beams 1 \\\n",
" --prompt \"Write a description.\" \\\n",
" --starts_with \"An image of\" --remove_starts_with \\\n",
" --temp 0.9 --top_p 0.9 --top_k 40 \\\n",
" --bad_words \"depicts,showcases,appears,suggests\" \\\n",
" --no_overwrite "
2023-07-03 12:27:17 -06:00
]
2024-03-24 07:30:23 -06:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2023-07-03 12:27:17 -06:00
}
],
"metadata": {
2024-03-03 14:13:58 -07:00
"accelerator": "GPU",
2023-07-03 13:37:29 -06:00
"colab": {
2024-03-03 14:13:58 -07:00
"gpuType": "T4",
2023-07-03 13:37:29 -06:00
"machine_shape": "hm",
2024-03-03 14:13:58 -07:00
"provenance": []
},
2023-07-03 13:37:29 -06:00
"kernelspec": {
2024-03-03 14:13:58 -07:00
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
2023-07-03 13:37:29 -06:00
},
2024-03-03 14:13:58 -07:00
"orig_nbformat": 4
2023-07-03 12:27:17 -06:00
},
"nbformat": 4,
2023-07-03 13:37:29 -06:00
"nbformat_minor": 0
2024-03-03 14:13:58 -07:00
}