Last active
April 29, 2025 08:13
-
-
Save MikyPo/25348239d62f399d509b2e97fb465d00 to your computer and use it in GitHub Desktop.
image-to-image_in_google_colab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Developed by MikyPo | |
| # More code for DA here: https://dzen.ru/mikypo | |
| !pip install torch diffusers transformers accelerate --quiet | |
| !pip install xformers --quiet | |
| import torch | |
| torch.cuda.empty_cache() # Очистка кеша GPU | |
| from diffusers import AutoPipelineForImage2Image | |
| from diffusers.utils import load_image | |
| from PIL import Image | |
| from google.colab import files | |
| # 1. Установка xformers | |
| try: | |
| !pip install -q xformers | |
| import xformers | |
| xformers_installed = True | |
| except: | |
| xformers_installed = False | |
| print("xFormers не установился. Работаем без него.") | |
| # 2. Инициализация пайплайна stable-diffusion-xl-refiner-1.0 | |
| pipeline = AutoPipelineForImage2Image.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-refiner-1.0", | |
| torch_dtype=torch.float16 | |
| ).to("cuda") | |
| # 3. Включение xformers с обработкой ошибок | |
| if xformers_installed: | |
| try: | |
| pipeline.enable_xformers_memory_efficient_attention() | |
| print("xFormers успешно включен!") | |
| except: | |
| print("Не удалось включить xFormers, продолжаем без него") | |
| xformers_installed = False | |
| # 4. Загрузка изображения | |
| img_url = "some_url_image" | |
| init_image = load_image(img_url) | |
| # 5. Генерация изображения | |
| prompt = "May beetle on a mushroom" | |
| image = pipeline( | |
| prompt=prompt, | |
| image=init_image, | |
| strength=0.6, # Лучше 0.5-0.7 | |
| num_inference_steps=25, # Чем больше шагов, тем лучше качество (стандартно 20) | |
| guidance_scale=7.0 # Оптимальный баланс креативности/качества | |
| ).images[0] | |
| # 7. Сохранение результата | |
| image.save("result.jpg") | |
| print("Изображение сохранено как result.jpg") | |
| # 8. Скачивание на локальный компьютер | |
| files.download("result.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment