File size: 6,516 Bytes
24b99af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b3b23a40-8354-4287-bac2-32f9d084fff3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sdxl log-variance: -0.105\n",
      "AiArtLab_vae log-variance: -0.105\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0d4d4ef4209b42ec82f6125e39067eed",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json: 0.00B [00:00, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c88119452e114e3e91ef56cb56c06a0c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "diffusion_pytorch_model.safetensors:   0%|          | 0.00/167M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EQ-SDXL-VAE log-variance: -3.922\n",
      "Готово\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from PIL import Image\n",
    "from diffusers import AutoencoderKL\n",
    "from torchvision.transforms.functional import to_pil_image\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "from torchvision.transforms import ToTensor, Normalize, CenterCrop\n",
    "\n",
    "# путь к вашей картинке\n",
    "IMG_PATH = \"123456789.jpg\"\n",
    "OUT_DIR  = \"vaetest\"\n",
    "device   = \"cuda\"\n",
    "dtype    = torch.float32          # ← единый float32\n",
    "os.makedirs(OUT_DIR, exist_ok=True)\n",
    "\n",
    "# список VAE\n",
    "VAES = {\n",
    "    \"sdxl\": \"madebyollin/sdxl-vae-fp16-fix\",\n",
    "    \"AiArtLab_vae\": \"AiArtLab/sdxl_vae\",\n",
    "    \"EQ-SDXL-VAE\": \"KBlueLeaf/EQ-SDXL-VAE\"\n",
    "}\n",
    "\n",
    "def load_image(path):\n",
    "    img = Image.open(path).convert('RGB')\n",
    "    # обрезаем до кратности 8\n",
    "    w, h = img.size\n",
    "    img = CenterCrop((h // 8 * 8, w // 8 * 8))(img)\n",
    "    tensor = ToTensor()(img).unsqueeze(0)          # [0,1]\n",
    "    tensor = Normalize(mean=[0.5]*3, std=[0.5]*3)(tensor)  # [-1,1]\n",
    "    return img, tensor.to(device, dtype=dtype)\n",
    "\n",
    "# обратно в PIL\n",
    "def tensor_to_img(t):\n",
    "    t = (t * 0.5 + 0.5).clamp(0, 1)\n",
    "    return to_pil_image(t[0])\n",
    "\n",
    "def logvariance(latents):\n",
    "    \"\"\"Возвращает лог-дисперсию по всем элементам.\"\"\"\n",
    "    return torch.log(latents.var() + 1e-8).item()\n",
    "\n",
    "def plot_latent_distribution(latents, title, save_path):\n",
    "    \"\"\"Гистограмма + QQ-plot.\"\"\"\n",
    "    lat = latents.detach().cpu().numpy().flatten()\n",
    "    plt.figure(figsize=(10, 4))\n",
    "\n",
    "    # гистограмма\n",
    "    plt.subplot(1, 2, 1)\n",
    "    plt.hist(lat, bins=100, density=True, alpha=0.7, color='steelblue')\n",
    "    plt.title(f\"{title} histogram\")\n",
    "    plt.xlabel(\"latent value\")\n",
    "    plt.ylabel(\"density\")\n",
    "\n",
    "    # QQ-plot\n",
    "    from scipy.stats import probplot\n",
    "    plt.subplot(1, 2, 2)\n",
    "    probplot(lat, dist=\"norm\", plot=plt)\n",
    "    plt.title(f\"{title} QQ-plot\")\n",
    "\n",
    "    plt.tight_layout()\n",
    "    plt.savefig(save_path)\n",
    "    plt.close()\n",
    "\n",
    "for name, repo in VAES.items():\n",
    "    if name==\"flux\":\n",
    "        vae = AutoencoderKL.from_pretrained(repo, torch_dtype=dtype).to(device)\n",
    "    else:\n",
    "        vae = AutoencoderKL.from_pretrained(repo, torch_dtype=dtype).to(device)#, subfolder=\"vae\", variant=\"fp16\"\n",
    "\n",
    "    cfg   = vae.config\n",
    "    scale = getattr(cfg, \"scaling_factor\", 1.)\n",
    "    shift = getattr(cfg, \"shift_factor\", 0.0)\n",
    "    mean  = getattr(cfg, \"latents_mean\", None)\n",
    "    std  = getattr(cfg, \"latents_std\",  None)\n",
    "\n",
    "    C = 4  # 4 для SDXL\n",
    "    if mean is not None:\n",
    "        mean = torch.tensor(mean, device=device, dtype=dtype).view(1, C, 1, 1)\n",
    "    if std is not None:\n",
    "        std  = torch.tensor(std,  device=device, dtype=dtype).view(1, C, 1, 1)\n",
    "    if shift is not None:\n",
    "        shift = torch.tensor(shift, device=device, dtype=dtype)\n",
    "    else:\n",
    "        shift = 0.0 \n",
    "\n",
    "    scale = torch.tensor(scale, device=device, dtype=dtype)\n",
    "\n",
    "    img, x = load_image(IMG_PATH)\n",
    "    img.save(os.path.join(OUT_DIR, f\"original.jpg\"))\n",
    "\n",
    "    with torch.no_grad():\n",
    "        # encode\n",
    "        latents = vae.encode(x).latent_dist.sample().to(dtype)\n",
    "        if mean is not None and std is not None:\n",
    "            latents = (latents - mean) / std\n",
    "        latents = latents * scale + shift\n",
    "\n",
    "        lv = logvariance(latents)\n",
    "        print(f\"{name} log-variance: {lv:.3f}\")\n",
    "\n",
    "        # график\n",
    "        plot_latent_distribution(latents, f\"{name}_latents\",\n",
    "                                 os.path.join(OUT_DIR, f\"dist_{name}.png\"))\n",
    "\n",
    "        # decode\n",
    "        latents = (latents - shift) / scale\n",
    "        if mean is not None and std is not None:\n",
    "            latents = latents * std + mean\n",
    "        rec = vae.decode(latents).sample\n",
    "\n",
    "    tensor_to_img(rec).save(os.path.join(OUT_DIR, f\"decoded_{name}.png\"))\n",
    "\n",
    "print(\"Готово\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "200b72ab-1978-4d71-9aba-b1ef97cf0b27",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}