0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mitsua_Likes_modelをローカルに保存して実行する

Last updated at Posted at 2024-12-18

モデルを保存

  1. 下記URLのコードを参考にmitsua-likesをgit cloneする
     https://huggingface.co/Mitsua/mitsua-likes/tree/main?clone=true

  2. mitsua-likesに移動する
     (例: cd mitsua-likes)

  3. 仮想環境を作成する  
     (例: python3.12 -m venv .venv)

  4. 仮想環境を立ち上げる
     (例: source .venv/bin/activate)

  5. 下記サイトの手順1~3を実行する
     https://note.com/junk_love/n/n0df6e1582860#7e9e0667-39a4-45ae-8bfa-593e4727527f

  6. 下記コードをPythonファイルに保存して実行する
     (※ 保存するファイルはmitsua-likes直下に置く)
     (例: python3 save_model.py)

from diffusers import DiffusionPipeline
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16

pipe = DiffusionPipeline.from_pretrained("Mitsua/mitsua-likes", trust_remote_code=True).to(device, dtype=dtype)

# 保存するディレクトリを指定
save_directory = "./mitsua_likes_model"

# モデルをローカルに保存
pipe.save_pretrained(save_directory)

モデルを実行

下記コードをpythonファイルに保存して実行する
 (※ 保存するファイルはmitsua-likes直下に置く)
 (例 python3 use_model.py)

from diffusers import DiffusionPipeline
import torch
import os
from datetime import datetime
from pipeline_likes_base_unet import MitsuaLikesPipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16

# ローカルに保存されたモデルをロード
pipe = MitsuaLikesPipeline.from_pretrained("./mitsua_likes_model").to(device, dtype=dtype)

# 日本語または英語のプロンプト
prompt = "滝の中の絵藍ミツア、先生アート"
# prompt = "elanmitsua in waterfall, sensei art, analog, impressionism painting"
negative_prompt = "elan doodle, lowres"

# モデル推論を実行
ret = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    guidance_scale=5.0,
    guidance_rescale=0.7,
    width=768,
    height=768,
    num_inference_steps=40,
)

# 類似性判定AIの結果を表示
if hasattr(ret, 'detected_public_fictional_characters') and len(ret.detected_public_fictional_characters) > 0:
    print("Similarity Restriction:", ret.detected_public_fictional_characters[0])
    print("Similarity Measure:")
    for k, v in ret.detected_public_fictional_characters_info[0].items():
        print(f"{k} : {v:.3%}")

# 生成された画像を取得
image = ret.images[0]

# 保存先のフォルダを指定
output_dir = "img"
# フォルダが存在しない場合は作成
os.makedirs(output_dir, exist_ok=True)

# 現在の日時を取得してファイル名を作成
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
image_name = f"{current_time}.png"

# フォルダ内に画像を保存
image_path = os.path.join(output_dir, image_name)
image.save(image_path)

print(f"画像が {image_path} に保存されました!")

以降もmitsua-likesフォルダ直下にいる状態かつ、仮想環境が立ち上がった状態であれば
モデルを実行することが可能です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?