axinc-ai / ailia-models-unity

Unity version of ailia models repository
42 stars 2 forks source link

ADD diffusion #96

Closed kyakuno closed 11 months ago

kyakuno commented 11 months ago

diffusionフォルダに、inpainting、super resolution、stable diffusionを実装する。

kyakuno commented 11 months ago

inpaintingのアーキテクチャ

kyakuno commented 11 months ago

Pythonにおけるddimのパラメータ。steps = 5。

DdimSampling index 4 a_t [[[[0.0033228]]]] a_prev [[[[0.041947]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.99833722]]]] temperature 1
DdimSampling index 3 a_t [[[[0.041947]]]] a_prev [[[[0.22429]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.97880182]]]] temperature 1
DdimSampling index 2 a_t [[[[0.22429]]]] a_prev [[[[0.60782]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.880744]]]] temperature 1
DdimSampling index 1 a_t [[[[0.60782]]]] a_prev [[[[0.99699]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.62624276]]]] temperature 1
DdimSampling index 0 a_t [[[[0.99699]]]] a_prev [[[[0.9985]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.05486347]]]] temperature 1
kyakuno commented 11 months ago

拡散モデルの逆過程なので、indexは反転する。

kyakuno commented 11 months ago

supeer_resolutionのアーキテクチャ

folder/unfoldはタイリングのために存在するので、128x128で1タイルの場合は不要

kyakuno commented 11 months ago

ddimのパラメータはeta以外は同じ。etaは0にするとdeterministic、1にするとランダム性がある。

kyakuno commented 11 months ago

stable-diffusionでは、CLIPのTokenizerとして、「openai/clip-vit-large-patch14」を使用している。

empty token

['']
torch.Size([1, 77])
tensor([[49406, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407]])
(1, 77, 768)
[[[-0.38837662  0.02294354 -0.05219659 ... -0.4898829  -0.30660194
    0.06745388]
  [-0.37112087 -1.4496573  -0.34011394 ...  0.9488635   0.18672296
   -1.1034371 ]
  [-0.51073873 -1.4628801  -0.29255462 ...  1.0419052   0.07005814
   -1.0284139 ]
  ...
  [ 0.50059485 -0.9552323  -0.66102695 ...  1.601293   -1.0622203
   -0.21908411]
  [ 0.49881327 -0.94508964 -0.6656092  ...  1.6466695  -1.0858376
   -0.20877934]
  [ 0.49234897 -0.81244665 -0.49119222 ...  1.6107694  -1.017356
   -0.2483787 ]]]

prompt

['a photograph of an astronaut riding a horse']
torch.Size([1, 77])
tensor([[49406,   320,  8853,   539,   550, 18376,  6765,   320,  4558, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407, 49407,
         49407, 49407, 49407, 49407, 49407, 49407, 49407]])
(1, 77, 768)
[[[-0.38837662  0.02294354 -0.05219659 ... -0.4898829  -0.30660194
    0.06745388]
  [ 0.02903211 -1.3257846   0.30845708 ... -0.52567875  0.97680986
    0.6651735 ]
  [ 0.45948368  0.5617134   1.6662681  ... -1.951515   -1.2306855
    0.01038613]
  ...
  [-3.0421286  -0.06562792 -0.17929013 ...  0.39426994 -0.01895678
    0.76638776]
  [-3.0551004  -0.10357185 -0.19355965 ...  0.42363918 -0.01895028
    0.75754255]
  [-2.9854436  -0.0832037  -0.17145592 ...  0.43552238  0.00952977
    0.7485085 ]]]
kyakuno commented 11 months ago

unconditional_conditioningに空白文字のclip embeddingが与えられ、ucとcがconcatされて、ddimのapply_modelに与えられる。 batch1がuc、batch0がcとして推論される。

    x_recon = apply_model(models, x_in, t_in, c_in, True)
    e_t_uncond, e_t = np.split(x_recon, 2)
    e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)

推論結果はマージされる。

kyakuno commented 11 months ago

clipは通常はtokensに対して768次元の出力を行うが、stable dfifusionはclipのhidden stateを使用し、(77, 768)次元の値を使用する。

            z = self.onnx.predict(tokens.numpy())
            z = self.onnx.get_blob_data(self.onnx.find_blob_index_by_name("/ln_final/Add_1_output_0"))
kyakuno commented 11 months ago

stable diffusionのddim parameters

index 4 a_t [[[[0.0365]]]] a_prev [[[[0.1598]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.98158036]]]] temperature 1.0
index 3 a_t [[[[0.1598]]]] a_prev [[[[0.4229]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.91662424]]]] temperature 1.0
index 2 a_t [[[[0.4229]]]] a_prev [[[[0.7521]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.75967098]]]] temperature 1.0
index 1 a_t [[[[0.7521]]]] a_prev [[[[0.9983]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.49789557]]]] temperature 1.0
index 0 a_t [[[[0.9983]]]] a_prev [[[[0.9991]]]] sigma_t [[[[0.]]]] sqrt_one_minus_at [[[[0.04123106]]]] temperature 1.0
kyakuno commented 10 months ago

unconditional_conditioningはnegative promptのために存在している。

kyakuno commented 10 months ago

positive promptの画像とnegative promptの画像をbatch方向で合計2枚推論し、samplerで混ぜる。 ddimの場合、下記の式で計算するため、cfg_scale=1だと、negative promptは効かない。

e_t = e_t_uncond + cfg_scale * (e_t - e_t_uncond)

デフォルトだとcfg_scale=7.5なので、e_t 7.5 - e_t_uncond 6.5でnegative promptが反映される。

kyakuno commented 10 months ago

negative promptを使用しない場合は1枚の推論でいいので、2倍高速になる気もする。