こすたろーんエンジニアの試行錯誤部屋

作成物の備忘録を書いていきますー

【Stable Diffusion】I tried generating a kiss-waiting image of Mizuhara Chizuru from “Rent-A-Girlfriend” using AnyLoraCleanLinearMix_ClearVAE and LoRA.

スポンサーリンク

Previously, I generated images of Mizuhara Chizuru from “Rent-A-Girlfriend.”
This time, I tried generating images of Mizuhara Chizuru waiting for a kiss.

contents

スポンサーリンク

abstract

The model, LoRA, and prompt used to generate the “Kiss-Waiting” image of Mizuhara Chizuru from “Rent-A-Girlfriend.” on Google Colab and Diffusers

1.requirement

Google Colab
Diffusers transformers==4.26.0
model : AnyLoraCleanLinearMix_ClearVAE
LoRA: Mizuhara Chizuru 彼女、お借りします Ichinose Chizuru 水原千鶴 Kanojo, Okarishimasu
LoRA:Incoming kiss
LoRA:Detail Tweaker LoRA

2.result

positive prompt

brown hair, long hair, bangs,brown eyes, medium breasts, BREAK knit sweater, long sleeves, long skirt, Exquisite visuals, high-definition, masterpiece, best quality, BREAK incoming kiss, face focus, close-up, closed eyes

negative prompt

(worst quality, low quality:1.4), monochrome, zombie, (interlocked fingers:1.2), bad-hands-5, mutated hands, liquid fingers

seed=31815177576222

seed=19134324699012

seed=9552924619856

スポンサーリンク

3.refarence

civitai.com civitai.com

【stable diffusion】diffsuers v0.17.0でVAEにおけるMissing key(s) in state_dict:とUnexpected key(s) in state_dict:エラーが発生したときの対処法

スポンサーリンク

以前にgoogle colab上にdiffusers環境を構築しました
technoxs-stacker.hatenablog.com

diffusersのバージョンを0.17.0にあげたところVAEのload部分でエラーが発生しました
Missing key(s) in state_dict:
Unexpected key(s) in state_dict:

この記事は対処法の備忘録です

目次

スポンサーリンク

この記事でわかること

VAEにおけるMissing key(s) in state_dict:とUnexpected key(s) in state_dict:エラーが発生したときの対処法

1.実行環境

Google Colab
Diffusers=0.17.0

2.エラー要因

VAEのモデル情報を格納している辞書のキー名が変更になっているため発生

例:
diffusers 0.16.0の場合
"decoder.mid_block.attentions.0.key.bias"

diffusers 0.17.0の場合
"decoder.mid_block.attentions.0.to_k.bias"

keyがto_kに変更されている

3.対処法

VAEのload時にキー名を変更する

コード例:

dict["decoder.mid_block.attentions.0.to_k.bias"  ] = dict.pop("decoder.mid_block.attentions.0.key.bias"  , default_val)

スポンサーリンク

4.結果

無事に画像生成することができました
オリジナルの子のアイナです↓

参考

github.com
github.com
note.nkmk.me

【self supervised learning】yoloの自己教師あり学習結果を使って再学習してみた

スポンサーリンク

以前に物体検出タスクにて自己教師あり学習をやってみました

technoxs-stacker.hatenablog.com

今回は自己教師あり学習の結果を使って再学習する流れをやってみました
この記事は備忘録です

目次

スポンサーリンク

この記事でわかること

物体検出タスクのDNNにて自己教師あり学習の結果を使って再学習する際のサンプルコード

1.実行環境

Google Colab
ultralytics
lightly

1.1 モジュールインストール

!pip install -q lightly
!pip install -q ultralytics

2.ソースコード

import copy
import torch
import torchvision
from torch import nn
from lightly.loss import DINOLoss
from lightly.models.modules import DINOProjectionHead
from lightly.models.utils import deactivate_requires_grad, update_momentum
from lightly.transforms.dino_transform import DINOTransform
from lightly.utils.scheduler import cosine_schedule
from tqdm import tqdm
from ultralytics.nn.modules import Conv
import os

class DINO(torch.nn.Module):
    def __init__(self, backbone, input_dim):
        super().__init__()
        self.student_backbone = backbone
        self.student_head = DINOProjectionHead(
            input_dim, 512, 64, 2048, freeze_last_layer=1
        )
        self.teacher_backbone = copy.deepcopy(backbone)
        self.teacher_head = DINOProjectionHead(input_dim, 512, 64, 2048)
        deactivate_requires_grad(self.teacher_backbone)
        deactivate_requires_grad(self.teacher_head)

    def forward(self, x):
        y = self.student_backbone(x).flatten(start_dim=1)
        z = self.student_head(y)
        return z

    def forward_teacher(self, x):
        y = self.teacher_backbone(x).flatten(start_dim=1)
        z = self.teacher_head(y)
        return z

class PoolHead(nn.Module):
  def __init__(self, f: int, i: int, c1: int): # Added type hints for scripting
    super().__init__()
    self.f = f  # receive the outputs from these layers
    self.i = i  # layer number
    self.conv = Conv(c1, 1280, 1, 1, None, 1)
    self.avgpool = nn.AdaptiveAvgPool2d(1)

  def forward(self, x: torch.Tensor) -> torch.Tensor: # Added type hints for scripting
    return self.avgpool(self.conv(x))

def target_transform(t):
    return 0

import configparser
def main(model_name="yolov5n.pt",epochs=10, batch_size=256):
  from ultralytics import YOLO # Moved import inside the function
  model_name="yolov5n.pt"
  GLOBAL_CROP_SIZE = 224
  LOCAL_CROP_SIZE = 96
  backbone_layer_num=12

  # load base model
  yolo = YOLO(model_name)
  print("base model-----------------------------------------------------------------------------")
  print(yolo)
  # Only backbone
  yolo.model.model = yolo.model.model[:backbone_layer_num]  # Keep first n layer
  print("backbone model-----------------------------------------------------------------------------")
  print(yolo)

  dummy = torch.rand(2, 3, GLOBAL_CROP_SIZE, GLOBAL_CROP_SIZE)

  out = yolo.model.model[:-1](dummy) # Run forward pass only using the first n layers-> Upsampleの手前まで実行する-> Upsampleを置き換えるため手前の出力を取得する
  yolo.model.model[-1] = PoolHead(yolo.model.model[-1].f, yolo.model.model[-1].i, out.shape[1])  # Replace nth layer with PoolHead

  out = yolo.model(dummy)
  input_dim = out.flatten(start_dim=1).shape[1]
  input_dim

  backbone = yolo.model.requires_grad_()
  backbone.train()
  model = DINO(backbone, input_dim)

  device = "cuda" if torch.cuda.is_available() else "cpu"
  model.to(device)

  normalize = dict(mean=(0.0,0.0,0.0), std=(1.0,1.0,1.0))  # YOLO uses these values
  transform = DINOTransform(global_crop_size=GLOBAL_CROP_SIZE, local_crop_size=LOCAL_CROP_SIZE, normalize=normalize)

  dataset = torchvision.datasets.VOCDetection(
      "datasets/pascal_voc",
      download=True,
      # download=False,
      transform=transform,
      target_transform=target_transform,
  )

  dataloader = torch.utils.data.DataLoader(
      dataset,
      batch_size=batch_size,
      shuffle=True,
      drop_last=True,
      #num_workers=2,
      num_workers=os.cpu_count(),
      pin_memory=True,
  )

  criterion = DINOLoss(
      output_dim=2048,
      warmup_teacher_temp_epochs=5,
  )
  # move loss to correct device because it also contains parameters
  criterion = criterion.to(device)

  optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

  print("Starting Training")
  for epoch in range(epochs):
      total_loss = 0
      momentum_val = cosine_schedule(epoch, epochs, 0.996, 1)
      # Initialize the progress bar
      pbar = tqdm(dataloader, desc=f"Epoch {epoch+1}/{epochs}", unit="batch")

      for batch in pbar:
          views = batch[0]
          update_momentum(model.student_backbone, model.teacher_backbone, m=momentum_val)
          update_momentum(model.student_head, model.teacher_head, m=momentum_val)
          views = [view.to(device) for view in views]
          global_views = views[:2]
          teacher_out = [model.forward_teacher(view) for view in global_views]
          student_out = [model.forward(view) for view in views]
          loss = criterion(teacher_out, student_out, epoch=epoch)
          total_loss += loss.detach()
          loss.backward()
          # We only cancel gradients of student head.
          model.student_head.cancel_last_layer_gradients(current_epoch=epoch)
          optimizer.step()
          optimizer.zero_grad()

          # Update the progress bar with the current batch loss
          pbar.set_postfix(loss=loss.item())

      avg_loss = total_loss / len(dataloader)
      print(f"Epoch: {epoch + 1}, Loss: {avg_loss:.5f}")

  #Load and save pretrained backbone
  from ultralytics import YOLO
  # Load the same model that was used for pretraining
  yolo = YOLO(model_name)
  # Transfer weights from pretrained model
  yolo.model.load(model.student_backbone)
  # Save the model for later use
  yolo.save("pretrained.pt")

  return 0

3.自己教師あり学習実行

main()

4. 自己教師あり学習の結果を使って再学習

# Clone the YOLOv5 repository
!git clone https://github.com/ultralytics/yolov5

# Navigate to the cloned directory
%cd yolov5
# Install required packages
!pip install -r requirements.txt
!python train.py --data VOC.yaml --cache --epochs 3 --weights "/content/pretrained.pt" --cfg "/content/yolov5/models/yolov5n.yaml"

所感

まだまだ改良の余地ありますが、とりあえず自己教師あり学習から再学習までの流れを構築できました

スポンサーリンク

参考

colab.research.google.com

WSLにzを入れてディレクトリ移動を楽にする

スポンサーリンク

目次

スポンサーリンク

この記事でわかること

WSL上にzを導入する方法

1.モジュールをダウンロード

sudo git clone https://github.com/rupa/z ~/z

2.設定をbachrcへ追記

vim .bashrc
. ~/z/z.sh

スポンサーリンク

3.参考

qiita.com
qiita.com
github.com

所感

Zに慣れると戻れなくなりますね・・・

WSLにpecoを入れて開発環境を快適にする

スポンサーリンク

pecoなるモジュールを使うとターミナル上のコマンド入力が楽になりそうだったので
今回導入してみました
色んな方が便利な設定を記載してくれているので、自分に必要そうなものをWSLの環境に設定しています

N番煎じですが参考になれば嬉しいです★

目次

スポンサーリンク

この記事でわかること

WSL上でpecoを使ってコマンド入力を楽にする方法

1.インストール

以下コマンドでインストール

sudo apt install peco

2.機能追加

検索機能の置き換え

bashrcに追記してcntrk + Rの検索機能を上書きする

vim .bashrc

以下を追記する

function peco_search_history() {
  local l=$(HISTTIMEFORMAT= history | \
        sort -r | sed -E s/^\ *[0-9]\+\ \+// | \
          peco --query "$READLINE_LINE")
    READLINE_LINE="$l"
      READLINE_POINT=${#l}
}
bind -x '"\C-r": peco_search_history'

ディレクトリ移動の機能追加

bashrcに追記してディレクトリ検索機能を追加  

vim .bashrc

以下を追記する

function peco-cd {
  local sw="1"
  while [ "$sw" != "0" ]
   do
        if [ "$sw" = "1" ];then
            local list=$(echo -e "---$PWD\n../\n$( ls -F | grep / )\n---Show hidden directory\n---Show files, $(echo $(ls -F | grep -v / ))\n---HOME DIRECTORY")
        elif [ "$sw" = "2" ];then
            local list=$(echo -e "---$PWD\n$( ls -a -F | grep / | sed 1d )\n---Hide hidden directory\n---Show files, $(echo $(ls -F | grep -v / ))\n---HOME DIRECTORY")
        else
            local list=$(echo -e "---BACK\n$( ls -F | grep -v / )")
        fi
        
        local slct=$(echo -e "$list" | peco )
        
        if [ "$slct" = "---$PWD" ];then
            local sw="0"
        elif [ "$slct" = "---Hide hidden directory" ];then
            local sw="1"
        elif [ "$slct" = "---Show hidden directory" ];then
            local sw="2"
        elif [ "$slct" = "---Show files, $(echo $(ls -F | grep -v / ))" ];then
            local sw=$(($sw+2))
        elif [ "$slct" = "---HOME DIRECTORY" ];then
            cd "$HOME"
        elif [[ "$slct" =~ / ]];then
            cd "$slct"
        elif [ "$slct" = "" ];then
            :
        else
            local sw=$(($sw-2))
        fi
   done
}
alias sd='peco-cd'

※今後追加した機能があれば追記します

スポンサーリンク

3.参考

github.com
takagi.blog

bio-eco-evo.hatenablog.com

【Stable Diffusion】AnyLoraCleanLinearMix_ClearVAE and LoRA to generate wall-don images of high school girls.

スポンサーリンク

This time I tried to use AnyLoraCleanLinearMix_ClearVAE and LoRA to generate wall-don images of high school girls!
This article will be a memorandum

目次

スポンサーリンク

abstract

LoRA and prompt with model used to generate high school girls' wall-don images on google colab and diffusers

1.requirements

Google Colab
Diffusers transformers==4.26.0
model : AnyLoraCleanLinearMix_ClearVAE
LoRA: Kabedon receiver's POV | 壁ドン | 被壁咚
LoRA:Detail Tweaker LoRA

2.result

prompt
positive

1girl, BREAK kabedon pov, expressionless, looking at viewer, closed eyes, close-up, parted lips, blush, makeup, light smile, school uniform, classroom, light rays, glow, thighs, collarbone, narrow waist, (masterpiece), wallpaper

negative

open mouth, (worst quality, low quality:1.4), monochrome, zombie, (interlocked fingers:1.2), bad-hands-5, mutated hands, liquid fingers, extra fingers

seed=92031333204081

seed=87289019309784

seed=82625767944707

スポンサーリンク

3.refarence

civitai.com

【Stable Diffusion】AnyLoraCleanLinearMix_ClearVAE and LoRA generated a swimsuit image of Thor from Miss Kobayashi's Dragon Maid

スポンサーリンク

This time I tried to generate a swimsuit image of Thor from Kobayashi-san's Maidragon with AnyLoraCleanLinearMix_ClearVAE and LoRA!
This article is a reminder.

contents

スポンサーリンク

abstract

Model, LoRA and prompts used in generating swimsuit image of Thor from Miss Kobayashi's Dragon Maid on google colab and diffusers

1.requirements

Google Colab
Diffusers transformers==4.26.0
model : AnyLoraCleanLinearMix_ClearVAE
LoRA: Tohru | トール - Kobayashi-San Chi no Maid Dragon | 小林さんちのメイドラゴン [Neural Da Vinci]
LoRA:Detail Tweaker LoRA

2.result

prompt
positive

(masterpiece, best quality, detailed), 1girl, solo, TohruDragonMaid_NDV, blonde hair, BREAK bikini, sea, medium breasts, long hair, horns, multicolored hair, orange eyes, looking at viewer, arms behind back, head tilt, open mouth, smile

negative

open mouth, (worst quality, low quality:1.4), monochrome, zombie, (interlocked fingers:1.2), bad-hands-5, mutated hands, liquid fingers, extra fingers

seed=8810051585450

seed=6051653463141

seed=2461504329460

スポンサーリンク

3.refarence

civitai.com