1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SageMakerのトレーニングジョブを使用してStable_diffusionをトレーニングしてみた。

Posted at

SageMakerのトレーニングジョブを使用してStable_diffusionをトレーニング

AWS SageMakerのnotebookを使用して、Stable Diffusion(画像生成モデル)を使用してトレーニングジョブを実施したので、技術メモになります。

■ 実施環境
AWS SageMaker
Notebook Instanse : ml.t3.xlarge
Training Instanse : ml.r5.2xlarge
コンテナイメージ : ubuntu:20.04

トレーニングコンテナ環境

下記がDockerfileになります。
コンテナイメージの中でstable_diffusion_engineなどのライブラリをインポートしている。

FROM ubuntu:20.04

# タイムゾーンの設定
ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 必要なパッケージのインストール
RUN apt-get update && \
    apt-get install -yq --no-install-recommends \
        python3.9 python3.9-dev python3.9-distutils \
        build-essential \
        libopencv-dev \
        python3-dev \
        wget \
        git \
        curl \
        tzdata && \
    apt-get upgrade -y && \
    apt-get clean

# シンボリックリンクを作成
RUN ln -sf /usr/bin/python3.9 /usr/bin/python

# 確実なpipインストールのための追加ステップ
RUN apt-get install -y python3-pip && \
    python3.9 -m pip install --upgrade pip

# 必要なPythonパッケージをインストール
RUN python3.9 -m pip install --no-cache-dir numpy==1.19.5 \
    opencv-python==4.5.5.64 \
    transformers==4.16.2 \
    diffusers==0.2.4 \
    tqdm==4.64.0 \
    openvino==2022.1.0 \
    huggingface_hub==0.9.0 \
    scipy==1.9.0 \
    streamlit==1.12.0 \
    watchdog==2.1.9 \
    sagemaker-training
RUN git clone https://github.com/bes-dev/stable_diffusion.openvino
WORKDIR /stable_diffusion.openvino

# Download resources
RUN python -c "from stable_diffusion_engine import StableDiffusionEngine; StableDiffusionEngine(None)"

Stable_diffusionのトレーニングスクリプト

下記のスクリプトは、サンプルスクリプトをベースにして今回用に作成した、Stable Diffusionを使った画像生成のトレーニングスクリプトです。

主な処理の流れ

引数の解析
モデル、プロンプト、初期画像などの設定をコマンドライン引数や環境変数から取得します。

スケジューラーの設定
初期画像がある場合とない場合で、画像生成アルゴリズム(スケジューラー)を切り替えます。
画像生成
StableDiffusionEngine を使って、プロンプトに基づいた画像を生成します。
必要に応じて初期画像やマスクを適用します。

画像の保存
指定されたファイル名で生成した画像を保存します。同時に、実行時の設定(引数)をJSON形式で保存します

下記が実際のコードになります。

training.py
# https://github.com/bes-dev/stable_diffusion.openvino/blob/master/demo.py を改変

# 必要なモジュールのインポート
import argparse
import os
from stable_diffusion_engine import StableDiffusionEngine  # Stable Diffusionモデルを実行するエンジン
from diffusers import LMSDiscreteScheduler, PNDMScheduler  # スケジューラー(画像生成のアルゴリズムを制御)
import cv2  # 画像操作ライブラリ
import json  # JSON操作ライブラリ
import numpy as np  # 数値計算ライブラリ

# メイン関数
def main(args):
    # ランダムシードの設定(再現性を確保)
    if args.seed is not None:
        np.random.seed(args.seed)

    # 初期画像が指定されているかでスケジューラーを切り替え
    if args.init_image is None:
        # 初期画像なし(テキストから画像生成用)
        scheduler = LMSDiscreteScheduler(
            beta_start=args.beta_start,
            beta_end=args.beta_end,
            beta_schedule=args.beta_schedule,
            tensor_format="np"
        )
    else:
        # 初期画像あり(画像編集やinpainting用)
        scheduler = PNDMScheduler(
            beta_start=args.beta_start,
            beta_end=args.beta_end,
            beta_schedule=args.beta_schedule,
            skip_prk_steps=True,
            tensor_format="np"
        )

    # Stable Diffusionエンジンの初期化
    engine = StableDiffusionEngine(
        model=args.model,  # モデル名
        scheduler=scheduler,  # スケジューラー
        tokenizer=args.tokenizer  # トークナイザー(プロンプトを処理)
    )

    # 指定された回数分画像を生成
    for i in range(args.n_out_imgs):
        print("\n--- image", i+1, "/", args.n_out_imgs, "---")
        # Stable Diffusionエンジンで画像生成
        image = engine(
            prompt=args.prompt,  # テキストプロンプト
            init_image=None if args.init_image is None else cv2.imread(args.init_image),  # 初期画像
            mask=None if args.mask is None else cv2.imread(args.mask, 0),  # マスク画像(白黒画像)
            strength=args.strength,  # 初期画像にどれだけ影響させるか
            num_inference_steps=args.num_inference_steps,  # 推論ステップ数
            guidance_scale=args.guidance_scale,  # プロンプトの影響度
            eta=args.eta  # ノイズの調整
        )
        # 出力ファイル名を設定(複数画像の場合は連番に)
        if args.n_out_imgs > 1:
            splits = os.path.splitext(args.output)
            outpath = f"{splits[0]}_{i:04}{splits[1]}"
        else:
            outpath = args.output

        # 出力ディレクトリを作成して画像を保存
        os.makedirs(os.path.dirname(outpath), exist_ok=True)
        cv2.imwrite(outpath, image)

    # 実行時の引数をJSONファイルに保存
    with open(os.path.join(os.path.dirname(args.output), "arguments.json"), "w", encoding="utf-8") as fp:
        json.dump(vars(args), fp, ensure_ascii=False, indent=4, separators=(',', ': '))

# スクリプトのエントリーポイント
if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    # モデルの設定
    parser.add_argument("--model", type=str, default="bes-dev/stable-diffusion-v1-4-openvino", help="model name")

    # ランダムシード
    parser.add_argument("--seed", type=int, default=None, help="random seed for generating consistent images per prompt")

    # スケジューラーのパラメータ
    parser.add_argument("--beta-start", type=float, default=0.00085, help="LMSDiscreteScheduler::beta_start")
    parser.add_argument("--beta-end", type=float, default=0.012, help="LMSDiscreteScheduler::beta_end")
    parser.add_argument("--beta-schedule", type=str, default="scaled_linear", help="LMSDiscreteScheduler::beta_schedule")

    # 推論のパラメータ
    parser.add_argument("--num-inference-steps", type=int, default=32, help="num inference steps")
    parser.add_argument("--guidance-scale", type=float, default=7.5, help="guidance scale")
    parser.add_argument("--eta", type=float, default=0.0, help="eta")

    # トークナイザー
    parser.add_argument("--tokenizer", type=str, default="openai/clip-vit-large-patch14", help="tokenizer")

    # プロンプト(生成する画像の説明文)
    parser.add_argument("--prompt", type=str, default="Street-art painting of Emilia Clarke in style of Banksy, photorealism", help="prompt")

    # 初期画像の設定
    parser.add_argument("--init-image", type=str, default=None, help="path to initial image")
    parser.add_argument("--strength", type=float, default=0.5, help="how strong the initial image should be noised [0.0, 1.0]")

    # マスク画像の設定
    parser.add_argument("--mask", type=str, default=None, help="mask of the region to inpaint on the initial image")

    # 出力ファイル名
    parser.add_argument("--output", type=str, default="output.png", help="output image name")
    parser.add_argument("--n_out_imgs", type=int, default=1, help="number of output images")

    # 引数を解析
    args = parser.parse_args()

    # SageMaker用の環境変数が設定されていれば、それを引数に反映
    if "SM_HPS" in os.environ.keys():
        hps = json.loads(os.environ["SM_HPS"])
        for key, value in hps.items():
            if args.__contains__(key):
                args.__setattr__(key, value)

    print(args)

    # メイン処理を実行
    main(args)

トレーニングスクリプトの呼び出しスクリプト

Sagemakerのnotebook上でAWS SageMaker を使って Stable Diffusion モデルのトレーニングスクリプトは下記になります。

1.インポート
SageMaker の Python SDK を使うために sagemaker をインポートし、モデルのトレーニングを管理する Estimator クラスを使用する準備をしています。
2.SageMaker セッションと IAM ロールの取得
セッション作成とIAMロールの取得を行いいます。
3.SageMaker Estimator の設定
コンテナイメージ(ECR上のURI)、IAMロール、トレーニングのインスタンス、トレーニングスクリプトの場所、ハイパーパラメータをしていいしています。ハイパーパラメータのプロンプトには、
"prompt": "A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai"
上記のようなコメントをしてみました。

python.py
import sagemaker
from sagemaker.estimator import Estimator

# Create a SageMaker session
session = sagemaker.Session()

# Get the execution role
role = sagemaker.get_execution_role()


# SageMaker Estimatorの設定
estimator = Estimator(
    # 使用するDockerイメージURI。PyTorch + TIMMを含むカスタムイメージを指定
    image_uri="255456539449.dkr.ecr.ap-northeast-1.amazonaws.com/sagemaker:stable_diffusion",

    # SageMakerで使用するIAMロール
    role=role,

    # トレーニングに使用するインスタンスタイプ。GPUを使用する場合に適したml.g4dn.2xlargeを指定
    instance_type="ml.r5.2xlarge",

    # 使用するインスタンスの数
    instance_count=1,

    # トレーニング結果を保存するS3のパス
    output_path="s3://takuma-s3-sagemaker-kensho/sagemaker/logs",

    # コードスクリプトが保存されているS3のパス
    code_location="s3://takuma-s3-sagemaker-kensho/sagemaker/logs",

    # ベースジョブ名(ジョブ名のプレフィックスとして使用される)
    base_job_name=f"stable-diffusion",

    #use_spot_instances=True,

    #max_wait=24*60*60, 

    # SageMakerセッションオブジェクト
    sagemaker_session=session,

    #checkpoint_s3_uri="s3://takuma-s3-sagemaker-kensho/sagemaker/checkpoints/catsanddogs",
    #checkpoint_local_path="/opt/ml/checkpoints", 

    # トレーニングスクリプトのエントリーポイント
    entry_point="sagemaker_scripts/11_stable_diffusion/11_stable_diffusion.sh",

    # ハイパーパラメータ(トレーニングスクリプトに渡されるパラメータ)
    hyperparameters={
        "prompt": "A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai",
        "n_out_imgs": 3,  
        "output": "/opt/ml/model/output.png"  
    }, 
   
    # トレーニングスクリプトに必要な依存関係が保存されているディレクトリ
    dependencies=["sagemaker_scripts/11_stable_diffusion"] 
)

# fit()を使ってトレーニングジョブを開始
estimator.fit()

実施ログ

Jupyter notebookを実行した実行ログは下記の通りです。

[01/26/25 09:48:48] INFO     SageMaker Python SDK will collect telemetry to help us better  telemetry_logging.py:90
                             understand our user's needs, diagnose issues, and deliver                             
                             additional features.                                                                  
                             To opt out of telemetry, please disable via TelemetryOptOut                           
                             parameter in SDK defaults config. For more information, refer                         
                             to                                                                                    
                             https://sagemaker.readthedocs.io/en/stable/overview.html#confi                        
                             guring-and-using-defaults-with-the-sagemaker-python-sdk.                              
                    INFO     Creating training-job with name:                                       session.py:1042
                             stable-diffusion-2025-01-26-09-48-48-716                                              
2025-01-26 09:48:53 Starting - Starting the training job...
2025-01-26 09:49:08 Starting - Preparing the instances for training...
2025-01-26 09:49:42 Downloading - Downloading the training image............
2025-01-26 09:51:39 Training - Training image download completed. Training in progress.....2025-01-26 18:52:22,955 sagemaker-training-toolkit INFO     No GPUs detected (normal if no gpus installed)
2025-01-26 18:52:22,956 sagemaker-training-toolkit INFO     Failed to parse hyperparameter output value /opt/ml/model/output.png to Json.
Returning the value itself
2025-01-26 18:52:22,956 sagemaker-training-toolkit INFO     Failed to parse hyperparameter prompt value A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai to Json.
Returning the value itself
2025-01-26 18:52:22,958 sagemaker-training-toolkit INFO     No Neurons detected (normal if no neurons installed)
2025-01-26 18:52:22,975 sagemaker-training-toolkit INFO     No GPUs detected (normal if no gpus installed)
2025-01-26 18:52:22,975 sagemaker-training-toolkit INFO     Failed to parse hyperparameter output value /opt/ml/model/output.png to Json.
Returning the value itself
2025-01-26 18:52:22,975 sagemaker-training-toolkit INFO     Failed to parse hyperparameter prompt value A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai to Json.
Returning the value itself
2025-01-26 18:52:22,977 sagemaker-training-toolkit INFO     No Neurons detected (normal if no neurons installed)
2025-01-26 18:52:22,992 sagemaker-training-toolkit INFO     No GPUs detected (normal if no gpus installed)
2025-01-26 18:52:22,992 sagemaker-training-toolkit INFO     Failed to parse hyperparameter output value /opt/ml/model/output.png to Json.
Returning the value itself
2025-01-26 18:52:22,992 sagemaker-training-toolkit INFO     Failed to parse hyperparameter prompt value A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai to Json.
Returning the value itself
2025-01-26 18:52:22,994 sagemaker-training-toolkit INFO     No Neurons detected (normal if no neurons installed)
2025-01-26 18:52:23,007 sagemaker-training-toolkit INFO     Invoking user script
Training Env:
{
    "additional_framework_parameters": {},
    "channel_input_dirs": {},
    "current_host": "algo-1",
    "current_instance_group": "homogeneousCluster",
    "current_instance_group_hosts": [
        "algo-1"
    ],
    "current_instance_type": "ml.r5.2xlarge",
    "distribution_hosts": [],
    "distribution_instance_groups": [],
    "framework_module": null,
    "hosts": [
        "algo-1"
    ],
    "hyperparameters": {
        "n_out_imgs": 3,
        "output": "/opt/ml/model/output.png",
        "prompt": "A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai"
    },
    "input_config_dir": "/opt/ml/input/config",
    "input_data_config": {},
    "input_dir": "/opt/ml/input",
    "instance_groups": [
        "homogeneousCluster"
    ],
    "instance_groups_dict": {
        "homogeneousCluster": {
            "instance_group_name": "homogeneousCluster",
            "instance_type": "ml.r5.2xlarge",
            "hosts": [
                "algo-1"
            ]
        }
    },
    "is_hetero": false,
    "is_master": true,
    "is_modelparallel_enabled": null,
    "is_smddpmprun_installed": false,
    "is_smddprun_installed": false,
    "job_name": "stable-diffusion-2025-01-26-09-48-48-716",
    "log_level": 20,
    "master_hostname": "algo-1",
    "model_dir": "/opt/ml/model",
    "module_dir": "s3://takuma-s3-sagemaker-kensho/sagemaker/logs/stable-diffusion-2025-01-26-09-48-48-716/source/sourcedir.tar.gz",
    "module_name": "11_stable_diffusion.sh",
    "network_interface_name": "eth0",
    "num_cpus": 8,
    "num_gpus": 0,
    "num_neurons": 0,
    "output_data_dir": "/opt/ml/output/data",
    "output_dir": "/opt/ml/output",
    "output_intermediate_dir": "/opt/ml/output/intermediate",
    "resource_config": {
        "current_host": "algo-1",
        "current_instance_type": "ml.r5.2xlarge",
        "current_group_name": "homogeneousCluster",
        "hosts": [
            "algo-1"
        ],
        "instance_groups": [
            {
                "instance_group_name": "homogeneousCluster",
                "instance_type": "ml.r5.2xlarge",
                "hosts": [
                    "algo-1"
                ]
            }
        ],
        "network_interface_name": "eth0"
    },
    "user_entry_point": "11_stable_diffusion.sh"
}
Environment variables:
SM_HOSTS=["algo-1"]
SM_NETWORK_INTERFACE_NAME=eth0
SM_HPS={"n_out_imgs":3,"output":"/opt/ml/model/output.png","prompt":"A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai"}
SM_USER_ENTRY_POINT=11_stable_diffusion.sh
SM_FRAMEWORK_PARAMS={}
SM_RESOURCE_CONFIG={"current_group_name":"homogeneousCluster","current_host":"algo-1","current_instance_type":"ml.r5.2xlarge","hosts":["algo-1"],"instance_groups":[{"hosts":["algo-1"],"instance_group_name":"homogeneousCluster","instance_type":"ml.r5.2xlarge"}],"network_interface_name":"eth0"}
SM_INPUT_DATA_CONFIG={}
SM_OUTPUT_DATA_DIR=/opt/ml/output/data
SM_CHANNELS=[]
SM_CURRENT_HOST=algo-1
SM_CURRENT_INSTANCE_TYPE=ml.r5.2xlarge
SM_CURRENT_INSTANCE_GROUP=homogeneousCluster
SM_CURRENT_INSTANCE_GROUP_HOSTS=["algo-1"]
SM_INSTANCE_GROUPS=["homogeneousCluster"]
SM_INSTANCE_GROUPS_DICT={"homogeneousCluster":{"hosts":["algo-1"],"instance_group_name":"homogeneousCluster","instance_type":"ml.r5.2xlarge"}}
SM_DISTRIBUTION_INSTANCE_GROUPS=[]
SM_IS_HETERO=false
SM_MODULE_NAME=11_stable_diffusion.sh
SM_LOG_LEVEL=20
SM_FRAMEWORK_MODULE=
SM_INPUT_DIR=/opt/ml/input
SM_INPUT_CONFIG_DIR=/opt/ml/input/config
SM_OUTPUT_DIR=/opt/ml/output
SM_NUM_CPUS=8
SM_NUM_GPUS=0
SM_NUM_NEURONS=0
SM_MODEL_DIR=/opt/ml/model
SM_MODULE_DIR=s3://takuma-s3-sagemaker-kensho/sagemaker/logs/stable-diffusion-2025-01-26-09-48-48-716/source/sourcedir.tar.gz
SM_TRAINING_ENV={"additional_framework_parameters":{},"channel_input_dirs":{},"current_host":"algo-1","current_instance_group":"homogeneousCluster","current_instance_group_hosts":["algo-1"],"current_instance_type":"ml.r5.2xlarge","distribution_hosts":[],"distribution_instance_groups":[],"framework_module":null,"hosts":["algo-1"],"hyperparameters":{"n_out_imgs":3,"output":"/opt/ml/model/output.png","prompt":"A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai"},"input_config_dir":"/opt/ml/input/config","input_data_config":{},"input_dir":"/opt/ml/input","instance_groups":["homogeneousCluster"],"instance_groups_dict":{"homogeneousCluster":{"hosts":["algo-1"],"instance_group_name":"homogeneousCluster","instance_type":"ml.r5.2xlarge"}},"is_hetero":false,"is_master":true,"is_modelparallel_enabled":null,"is_smddpmprun_installed":false,"is_smddprun_installed":false,"job_name":"stable-diffusion-2025-01-26-09-48-48-716","log_level":20,"master_hostname":"algo-1","model_dir":"/opt/ml/model","module_dir":"s3://takuma-s3-sagemaker-kensho/sagemaker/logs/stable-diffusion-2025-01-26-09-48-48-716/source/sourcedir.tar.gz","module_name":"11_stable_diffusion.sh","network_interface_name":"eth0","num_cpus":8,"num_gpus":0,"num_neurons":0,"output_data_dir":"/opt/ml/output/data","output_dir":"/opt/ml/output","output_intermediate_dir":"/opt/ml/output/intermediate","resource_config":{"current_group_name":"homogeneousCluster","current_host":"algo-1","current_instance_type":"ml.r5.2xlarge","hosts":["algo-1"],"instance_groups":[{"hosts":["algo-1"],"instance_group_name":"homogeneousCluster","instance_type":"ml.r5.2xlarge"}],"network_interface_name":"eth0"},"user_entry_point":"11_stable_diffusion.sh"}
SM_USER_ARGS=["--n_out_imgs","3","--output","/opt/ml/model/output.png","--prompt","A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai"]
SM_OUTPUT_INTERMEDIATE_DIR=/opt/ml/output/intermediate
SM_HP_N_OUT_IMGS=3
SM_HP_OUTPUT=/opt/ml/model/output.png
SM_HP_PROMPT=A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai
PYTHONPATH=/opt/ml/code:/usr/local/bin:/usr/lib/python39.zip:/usr/lib/python3.9:/usr/lib/python3.9/lib-dynload:/usr/local/lib/python3.9/dist-packages:/usr/lib/python3/dist-packages
Invoking script with the following command:
/bin/sh -c "./11_stable_diffusion.sh --n_out_imgs 3 --output /opt/ml/model/output.png --prompt 'A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai'"
2025-01-26 18:52:23,008 sagemaker-training-toolkit INFO     Exceptions not imported for SageMaker Debugger as it is not installed.
2025-01-26 18:52:23,008 sagemaker-training-toolkit INFO     Exceptions not imported for SageMaker TF as Tensorflow is not installed.
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
Namespace(model='bes-dev/stable-diffusion-v1-4-openvino', seed=None, beta_start=0.00085, beta_end=0.012, beta_schedule='scaled_linear', num_inference_steps=32, guidance_scale=7.5, eta=0.0, tokenizer='openai/clip-vit-large-patch14', prompt='A little blonde girl in a white coat, pixiv fanart, Makoto Shinkai', init_image=None, strength=0.5, mask=None, output='/opt/ml/model/output.png', n_out_imgs=3)
--- image 1 / 3 ---
#0150it [00:00, ?it/s]#0151it [00:04,  4.45s/it]#0152it [00:08,  4.36s/it]#0153it [00:13,  4.35s/it]#0154it [00:17,  4.33s/it]#0155it [00:21,  4.33s/it]#0156it [00:26,  4.33s/it]#0157it [00:30,  4.32s/it]#0158it [00:34,  4.33s/it]#0159it [00:39,  4.35s/it]#01510it [00:43,  4.34s/it]#01511it [00:47,  4.35s/it]#01512it [00:52,  4.36s/it]#01513it [00:56,  4.35s/it]#01514it [01:00,  4.33s/it]#01515it [01:05,  4.33s/it]#01516it [01:09,  4.33s/it]#01517it [01:13,  4.31s/it]#01518it [01:18,  4.32s/it]#01519it [01:22,  4.33s/it]#01520it [01:26,  4.32s/it]#01521it [01:31,  4.33s/it]#01522it [01:35,  4.32s/it]#01523it [01:39,  4.31s/it]#01524it [01:43,  4.31s/it]#01525it [01:48,  4.31s/it]#01526it [01:52,  4.30s/it]#01527it [01:56,  4.31s/it]#01528it [02:01,  4.30s/it]#01529it [02:05,  4.30s/it]#01530it [02:09,  4.31s/it]#01531it [02:14,  4.30s/it]#01532it [02:18,  4.30s/it]#01532it [02:18,  4.32s/it]
--- image 2 / 3 ---
#0150it [00:00, ?it/s]#0151it [00:04,  4.30s/it]#0152it [00:08,  4.30s/it]#0153it [00:12,  4.32s/it]#0154it [00:17,  4.31s/it]#0155it [00:21,  4.30s/it]#0156it [00:25,  4.32s/it]#0157it [00:30,  4.31s/it]#0158it [00:34,  4.30s/it]#0159it [00:38,  4.31s/it]#01510it [00:43,  4.31s/it]#01511it [00:47,  4.30s/it]#01512it [00:51,  4.31s/it]#01513it [00:56,  4.32s/it]#01514it [01:00,  4.31s/it]#01515it [01:04,  4.32s/it]#01516it [01:08,  4.31s/it]#01517it [01:13,  4.31s/it]#01518it [01:17,  4.32s/it]#01519it [01:21,  4.32s/it]#01520it [01:26,  4.32s/it]#01521it [01:30,  4.32s/it]#01522it [01:34,  4.31s/it]#01523it [01:39,  4.30s/it]#01524it [01:43,  4.31s/it]#01525it [01:47,  4.31s/it]#01526it [01:52,  4.30s/it]#01527it [01:56,  4.32s/it]#01528it [02:00,  4.32s/it]#01529it [02:05,  4.31s/it]#01530it [02:09,  4.32s/it]#01531it [02:13,  4.31s/it]#01532it [02:17,  4.31s/it]#01532it [02:17,  4.31s/it]
--- image 3 / 3 ---



2025-01-26 10:00:01 Uploading - Uploading generated training model#0150it [00:00, ?it/s]#0151it [00:04,  4.30s/it]#0152it [00:08,  4.32s/it]#0153it [00:12,  4.33s/it]#0154it [00:17,  4.32s/it]#0155it [00:21,  4.31s/it]#0156it [00:25,  4.32s/it]#0157it [00:30,  4.32s/it]#0158it [00:34,  4.32s/it]#0159it [00:38,  4.33s/it]#01510it [00:43,  4.33s/it]#01511it [00:47,  4.31s/it]#01512it [00:51,  4.32s/it]#01513it [00:56,  4.32s/it]#01514it [01:00,  4.32s/it]#01515it [01:04,  4.33s/it]#01516it [01:09,  4.32s/it]#01517it [01:13,  4.31s/it]#01518it [01:17,  4.31s/it]#01519it [01:22,  4.31s/it]#01520it [01:26,  4.30s/it]#01521it [01:30,  4.31s/it]#01522it [01:34,  4.32s/it]#01523it [01:39,  4.31s/it]#01524it [01:44,  4.51s/it]#01525it [01:48,  4.45s/it]#01526it [01:52,  4.40s/it]#01527it [01:57,  4.38s/it]#01528it [02:01,  4.35s/it]#01529it [02:05,  4.33s/it]#01530it [02:10,  4.34s/it]#01531it [02:14,  4.32s/it]#01532it [02:18,  4.31s/it]#01532it [02:18,  4.33s/it]
2025-01-26 18:59:56,032 sagemaker-training-toolkit INFO     Reporting training SUCCESS

2025-01-26 10:00:09 Completed - Training job completed
Training seconds: 637
Billable seconds: 637

出力ファイル

トレーニングジョブ呼び出しスクリプトで指定しているS3のアウトプットフォルダを見てみましょう。
S3のアウトプットフォルダは下記の通り(s3://takuma-s3-sagemaker-kensho/sagemaker/logs)

image.png

出力された画像ファイルは下記の通り。

image.png
image.png
image.png

感想・後片付け

 Stable DiffusinのSDKを使用しながら、SageMakerのNotebookインスタンスからトレーニングジョブを流して画像をテスト的に生成することができました。コンテナのビルドにもかなりの時間を要し、インスタンスもかなり性能が高いものを使用しないとリソース不足になることがわかりました。

※今回、使用したトレーニングインスタンス、ノートブックのインスタンスも容量が大きいインスタンスタイプを指定しましたので、終わった後は落としておきましょう。。
image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?