Buckets:
| # 무조건적 이미지 생성 | |
| 무조건적 이미지 생성 모델은 학습 중에 텍스트나 이미지에 조건을 받지 않습니다. 학습 데이터 분포와 유사한 이미지만 생성합니다. | |
| 이 가이드에서는 [train_unconditional.py](https://github.com/huggingface/diffusers/blob/main/examples/unconditional_image_generation/train_unconditional.py) 학습 스크립트를 살펴보고, 이를 이해한 후 사용자의 용도에 맞게 수정하는 방법을 안내합니다. | |
| … | |
| ```bash | |
| git clone https://github.com/huggingface/diffusers | |
| cd diffusers | |
| pip install . | |
| ``` | |
| 이후 학습 스크립트가 포함된 예제 폴더로 이동하고 필요한 의존성을 설치합니다: | |
| ```bash | |
| cd examples/unconditional_image_generation | |
| pip install -r requirements.txt | |
| ``` | |
| > [!TIP] | |
| > 🤗 Accelerate는 여러 GPU/TPU 또는 혼합 정밀도로 학습하는 것을 돕는 라이브러리입니다. 하드웨어와 환경에 따라 학습 설정을 자동으로 구성합니다. 자세한 내용은 🤗 Accelerate [Quick tour](https://huggingface.co/docs/accelerate/quicktour)를 참고하세요. | |
| 🤗 Accelerate 환경을 초기화합니다: | |
| ```bash | |
| accelerate config | |
| ``` | |
| 어떤 구성도 선택하지 않고 기본 🤗 Accelerate 환경을 설정하려면: | |
| ```bash | |
| accelerate config default | |
| ``` | |
| 또는 노트북과 같은 인터랙티브 셸을 지원하지 않는 환경에서는 다음을 사용할 수 있습니다: | |
| ```py | |
| from accelerate.utils import write_basic_config | |
| write_basic_config() | |
| ``` | |
| 마지막으로, 자체 데이터셋으로 모델을 학습하려면 [학습용 데이터셋 만들기](create_dataset) 가이드를 참고하여 학습 스크립트와 호환되는 데이터셋을 만드는 방법을 배워보세요. | |
| ## 스크립트 매개변수 | |
| > [!TIP] | |
| > 다음 섹션에서는 스크립트를 수정하는 방법을 이해하는 데 중요한 학습 스크립트의 일부 부분을 강조합니다. 그러나 스크립트의 모든 측면을 자세히 다루지는 않습니다. 더 자세히 알고 싶으시다면 [스크립트](https://github.com/huggingface/diffusers/blob/main/examples/unconditional_image_generation/train_unconditional.py)를 읽어보시고, 질문이나 우려 사항이 있으시면 알려주시기 바랍니다. | |
| 학습 스크립트는 학습 실행을 사용자 정의하는 데 도움이 되는 많은 매개변수를 제공합니다. 모든 매개변수와 그 설명은 [`parse_args()`](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L55) 함수에서 찾을 수 있습니다. 학습 배치 크기나 학습률과 같이 각 매개변수에 대한 기본값을 제공하지만, 원하시는 경우 학습 명령에서 직접 값을 설정할 수도 있습니다. | |
| 예를 들어, bf16 형식을 사용하여 혼합 정밀도로 학습 속도를 높이려면 학습 명령에 `--mixed_precision` 매개변수를 추가합니다: | |
| ```bash | |
| accelerate launch train_unconditional.py \ | |
| --mixed_precision="bf16" | |
| ``` | |
| 지정해야 할 몇 가지 기본적이고 중요한 매개변수는 다음과 같습니다: | |
| - `--dataset_name`: Hub에 있는 데이터셋의 이름 또는 학습에 사용할 데이터셋의 로컬 경로 | |
| - `--output_dir`: 학습된 모델을 저장할 위치 | |
| - `--push_to_hub`: 학습된 모델을 Hub에 푸시할지 여부 | |
| - `--checkpointing_steps`: 모델 학습 중 체크포인트를 저장하는 주기입니다. 학습이 중단된 경우, 학습 명령에 `--resume_from_checkpoint`를 추가하여 해당 체크포인트에서 학습을 계속할 수 있습니다. | |
| 데이터셋만 가져오세요. 나머지 모든 것은 학습 스크립트가 처리합니다! | |
| ## 학습 스크립트 | |
| 데이터셋 전처리 및 학습 루프에 대한 코드는 [`main()`](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L275) 함수에 있습니다. 학습 스크립트를 수정해야 하는 경우, 이 부분을 변경해야 합니다. | |
| `train_unconditional` 스크립트는 모델 구성을 제공하지 않은 경우 [`UNet2DModel`을 초기화](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L356)합니다. 원하시는 경우 여기서 UNet을 구성할 수 있습니다: | |
| ```py | |
| model = UNet2DModel( | |
| sample_size=args.resolution, | |
| in_channels=3, | |
| out_channels=3, | |
| layers_per_block=2, | |
| block_out_channels=(128, 128, 256, 256, 512, 512), | |
| down_block_types=( | |
| "DownBlock2D", | |
| "DownBlock2D", | |
| "DownBlock2D", | |
| "DownBlock2D", | |
| "AttnDownBlock2D", | |
| "DownBlock2D", | |
| ), | |
| up_block_types=( | |
| "UpBlock2D", | |
| "AttnUpBlock2D", | |
| "UpBlock2D", | |
| "UpBlock2D", | |
| "UpBlock2D", | |
| "UpBlock2D", | |
| ), | |
| ) | |
| ``` | |
| 다음으로, 스크립트는 [scheduler](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L418)와 [optimizer](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L429)를 초기화합니다: | |
| ```py | |
| # Initialize the scheduler | |
| accepts_prediction_type = "prediction_type" in set(inspect.signature(DDPMScheduler.__init__).parameters.keys()) | |
| if accepts_prediction_type: | |
| noise_scheduler = DDPMScheduler( | |
| num_train_timesteps=args.ddpm_num_steps, | |
| beta_schedule=args.ddpm_beta_schedule, | |
| prediction_type=args.prediction_type, | |
| ) | |
| else: | |
| noise_scheduler = DDPMScheduler(num_train_timesteps=args.ddpm_num_steps, beta_schedule=args.ddpm_beta_schedule) | |
| # Initialize the optimizer | |
| optimizer = torch.optim.AdamW( | |
| model.parameters(), | |
| lr=args.learning_rate, | |
| betas=(args.adam_beta1, args.adam_beta2), | |
| weight_decay=args.adam_weight_decay, | |
| eps=args.adam_epsilon, | |
| ) | |
| ``` | |
| 그 후 [데이터셋을 로드](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L451)하며, [전처리](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L455) 방법을 지정할 수 있습니다: | |
| ```py | |
| dataset = load_dataset("imagefolder", data_dir=args.train_data_dir, cache_dir=args.cache_dir, split="train") | |
| augmentations = transforms.Compose( | |
| [ | |
| transforms.Resize(args.resolution, interpolation=transforms.InterpolationMode.BILINEAR), | |
| transforms.CenterCrop(args.resolution) if args.center_crop else transforms.RandomCrop(args.resolution), | |
| transforms.RandomHorizontalFlip() if args.random_flip else transforms.Lambda(lambda x: x), | |
| transforms.ToTensor(), | |
| transforms.Normalize([0.5], [0.5]), | |
| ] | |
| ) | |
| ``` | |
| 마지막으로, [training loop](https://github.com/huggingface/diffusers/blob/096f84b05f9514fae9f185cbec0a4d38fbad9919/examples/unconditional_image_generation/train_unconditional.py#L540)은 이미지에 노이즈를 추가하고, 노이즈 잔차를 예측하며, 손실을 계산하고, 지정된 단계에서 체크포인트를 저장하고, 모델을 Hub에 저장하고 푸시하는 등 나머지 모든 작업을 처리합니다. training loop의 작동 방식에 대해 더 자세히 알고 싶다면, 디노이징 과정의 기본 패턴을 분석하는 [Understanding pipelines, models and schedulers](../using-diffusers/write_own_pipeline) 튜토리얼을 확인해 보세요. | |
| ## 스크립트 실행 | |
| 모든 변경 사항을 완료했거나 기본 구성으로 진행하는 데 문제가 없다면, 학습 스크립트를 실행할 준비가 된 것입니다! 🚀 | |
| > [!WARNING] | |
| > 전체 학습 실행은 4xV100 GPU에서 2시간이 소요됩니다. | |
| ```bash | |
| accelerate launch train_unconditional.py \ | |
| --dataset_name="huggan/flowers-102-categories" \ | |
| --output_dir="ddpm-ema-flowers-64" \ | |
| --mixed_precision="fp16" \ | |
| --push_to_hub | |
| ``` | |
| 여러 개의 GPU로 학습하는 경우, 학습 명령에 `--multi_gpu` 매개변수를 추가하세요: | |
| ```bash | |
| accelerate launch --multi_gpu train_unconditional.py \ | |
| --dataset_name="huggan/flowers-102-categories" \ | |
| --output_dir="ddpm-ema-flowers-64" \ | |
| --mixed_precision="fp16" \ | |
| --push_to_hub | |
| ``` | |
| 트레이닝 스크립트는 저장소에 체크포인트 파일을 생성하고 저장합니다. 이제 학습된 모델을 로드하여 추론에 사용할 수 있습니다: | |
| ```py | |
| from diffusers import DiffusionPipeline | |
| import torch | |
| pipeline = DiffusionPipeline.from_pretrained("anton-l/ddpm-butterflies-128").to("cuda") # or "mps", "xpu", "cpu" | |
| image = pipeline().images[0] | |
| ``` | |
Xet Storage Details
- Size:
- 8.92 kB
- Xet hash:
- a159a0982c01be772f873bba4879686a552bb5d37b5388c6184d267f04c3bc31
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.