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raft: Text-to-Image Generation on T4 Colab\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"This script uses a custom Stable Diffusion model from Hugging Face for text-to-image generation, optimized for T4 GPU with low RAM usage.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"**Requirements**:\n",
|
| 12 |
+
"- T4 GPU runtime in Colab\n",
|
| 13 |
+
"- Hugging Face account and token (for gated models)\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"**Features**:\n",
|
| 16 |
+
"- Uses `diffusers` library with FP16 precision\n",
|
| 17 |
+
"- Enables model CPU offloading for low RAM\n",
|
| 18 |
+
"- Supports custom prompts and negative prompts\n"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "code",
|
| 23 |
+
"execution_count": null,
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"outputs": [],
|
| 26 |
+
"source": [
|
| 27 |
+
"# Install required libraries\n",
|
| 28 |
+
"!pip install -q diffusers==0.21.4 transformers==4.33.0 accelerate==0.22.0\n",
|
| 29 |
+
"!pip install -q torch==2.0.1 torchvision==0.15.2 --index-url https://download.pytorch.org/whl/cu118\n",
|
| 30 |
+
"!pip install -q xformers==0.0.22\n"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": null,
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [],
|
| 38 |
+
"source": [
|
| 39 |
+
"# Import libraries\n",
|
| 40 |
+
"import torch\n",
|
| 41 |
+
"from diffusers import StableDiffusionPipeline\n",
|
| 42 |
+
"from huggingface_hub import login\n",
|
| 43 |
+
"import os\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"# Set Hugging Face token (replace with your token)\n",
|
| 46 |
+
"os.environ['HUGGINGFACE_TOKEN'] = 'your_hf_token_here'\n",
|
| 47 |
+
"login(os.environ['HUGGINGFACE_TOKEN'])\n"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": null,
|
| 53 |
+
"metadata": {},
|
| 54 |
+
"outputs": [],
|
| 55 |
+
"source": [
|
| 56 |
+
"# Initialize the pipeline with optimizations\n",
|
| 57 |
+
"model_id = 'runwayml/stable-diffusion-v1-5' # Replace with your custom HF model ID\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"pipe = StableDiffusionPipeline.from_pretrained(\n",
|
| 60 |
+
" model_id,\n",
|
| 61 |
+
" torch_dtype=torch.float16,\n",
|
| 62 |
+
" use_auth_token=True\n",
|
| 63 |
+
")\n",
|
| 64 |
+
"\n",
|
| 65 |
+
"# Enable optimizations for T4\n",
|
| 66 |
+
"pipe = pipe.to('cuda')\n",
|
| 67 |
+
"pipe.enable_attention_slicing() # Reduces memory usage\n",
|
| 68 |
+
"pipe.enable_model_cpu_offload() # Offloads model to CPU when not in use\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"# Optional: Enable xformers for faster inference\n",
|
| 71 |
+
"try:\n",
|
| 72 |
+
" pipe.enable_xformers_memory_efficient_attention()\n",
|
| 73 |
+
"except:\n",
|
| 74 |
+
" print('xformers not supported, proceeding without it.')\n"
|
| 75 |
+
]
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"cell_type": "code",
|
| 79 |
+
"execution_count": null,
|
| 80 |
+
"metadata": {},
|
| 81 |
+
"outputs": [],
|
| 82 |
+
"source": [
|
| 83 |
+
"# Define generation parameters\n",
|
| 84 |
+
"prompt = 'A serene mountain landscape at sunset, vibrant colors, highly detailed'\n",
|
| 85 |
+
"negative_prompt = 'blurry, low quality, artifacts, text, watermark'\n",
|
| 86 |
+
"num_inference_steps = 30 # Lower steps for faster generation\n",
|
| 87 |
+
"guidance_scale = 7.5\n",
|
| 88 |
+
"\n",
|
| 89 |
+
"# Generate image\n",
|
| 90 |
+
"image = pipe(\n",
|
| 91 |
+
" prompt,\n",
|
| 92 |
+
" negative_prompt=negative_prompt,\n",
|
| 93 |
+
" num_inference_steps=num_inference_steps,\n",
|
| 94 |
+
" guidance_scale=guidance_scale,\n",
|
| 95 |
+
" height=512,\n",
|
| 96 |
+
" width=512\n",
|
| 97 |
+
").images[0]\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"# Save and display image\n",
|
| 100 |
+
"image.save('generated_image.png')\n",
|
| 101 |
+
"image\n"
|
| 102 |
+
]
|
| 103 |
+
},
|
| 104 |
+
{
|
| 105 |
+
"cell_type": "markdown",
|
| 106 |
+
"metadata": {},
|
| 107 |
+
"source": [
|
| 108 |
+
"## Notes\n",
|
| 109 |
+
"- Replace `'your_hf_token_here'` with your Hugging Face token.\n",
|
| 110 |
+
"- Replace `'runwayml/stable-diffusion-v1-5'` with your custom model ID from Hugging Face.\n",
|
| 111 |
+
"- Adjust `prompt`, `negative_prompt`, `num_inference_steps`, and `guidance_scale` as needed.\n",
|
| 112 |
+
"- The script uses FP16 and attention slicing to minimize RAM usage.\n",
|
| 113 |
+
"- Model CPU offloading reduces VRAM requirements, ideal for T4 GPUs.\n"
|
| 114 |
+
]
|
| 115 |
+
}
|
| 116 |
+
],
|
| 117 |
+
"metadata": {
|
| 118 |
+
"kernelspec": {
|
| 119 |
+
"display_name": "Python 3",
|
| 120 |
+
"language": "python",
|
| 121 |
+
"name": "python3"
|
| 122 |
+
},
|
| 123 |
+
"language_info": {
|
| 124 |
+
"codemirror_mode": {
|
| 125 |
+
"name": "ipython",
|
| 126 |
+
"version": 3
|
| 127 |
+
},
|
| 128 |
+
"file_extension": ".py",
|
| 129 |
+
"mimetype": "text/x-python",
|
| 130 |
+
"name": "python",
|
| 131 |
+
"nbconvert_exporter": "python",
|
| 132 |
+
"pygments_lexer": "ipython3",
|
| 133 |
+
"version": "3.8.10"
|
| 134 |
+
}
|
| 135 |
+
},
|
| 136 |
+
"nbformat": 4,
|
| 137 |
+
"nbformat_minor": 4
|
| 138 |
+
}
|