File size: 14,945 Bytes
14c7142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dccce86b-90a0-47c7-aaad-2ebb16d90756",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ΠšΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½Π°: torch.Size([1, 3, 1280, 1280])\n",
      "\n",
      "=======================================================\n",
      "VAE : FLUX.2\n",
      "repo: AiArtLab/sdxs-1b\n",
      "latent_channels : 32\n",
      "scaling_factor  : 1.00000\n",
      "shift_factor    : 0.00000\n",
      "latents_mean    : Π½Π΅Ρ‚\n",
      "latents_std     : Π½Π΅Ρ‚\n",
      "\n",
      "[encode] raw latents: torch.Size([1, 32, 160, 160])\n",
      "[flux2]  patchify  : torch.Size([1, 32, 160, 160]) β†’ torch.Size([1, 128, 80, 80])\n",
      "[flux2]  BN norm   : mean=-0.0096  std=1.7674\n",
      "\n",
      "[STATS] послС BN Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ (128ch):\n",
      "  log-variance : -0.0767  (ΠΈΠ΄Π΅Π°Π» β‰ˆ 0.0)\n",
      "  mean         : -0.0134\n",
      "  std          : 0.9624\n",
      "  shape        : torch.Size([1, 128, 80, 80])\n",
      "\n",
      "[flux2]  BN denorm + unpatchify: torch.Size([1, 32, 160, 160])\n",
      "Π‘ΠΎΡ…Ρ€Π°Π½Π΅Π½ΠΎ: vaetest/decoded_FLUX.2.png\n",
      "\n",
      "=======================================================\n",
      "VAE : vae32ch2\n",
      "repo: vae32ch2\n",
      "latent_channels : 32\n",
      "scaling_factor  : 1.00000\n",
      "shift_factor    : 0.00000\n",
      "latents_mean    : Π΄Π° (32ch)\n",
      "latents_std     : Π΄Π° (32ch)\n",
      "\n",
      "[encode] raw latents: torch.Size([1, 32, 160, 160])\n",
      "\n",
      "[STATS] послС per-channel Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ (32ch):\n",
      "  log-variance : 0.1192  (ΠΈΠ΄Π΅Π°Π» β‰ˆ 0.0)\n",
      "  mean         : -0.0016\n",
      "  std          : 1.0614\n",
      "  shape        : torch.Size([1, 32, 160, 160])\n",
      "\n",
      "[vae32ch2] denorm: torch.Size([1, 32, 160, 160])\n",
      "Π‘ΠΎΡ…Ρ€Π°Π½Π΅Π½ΠΎ: vaetest/decoded_vae32ch2.png\n",
      "\n",
      "=======================================================\n",
      "Π“ΠΎΡ‚ΠΎΠ²ΠΎ\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import torch\n",
    "from PIL import Image\n",
    "from diffusers import AutoencoderKL, AutoencoderKLFlux2\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 = \"1234.png\"\n",
    "OUT_DIR  = \"vaetest\"\n",
    "device   = \"cuda\"\n",
    "dtype    = torch.float32\n",
    "os.makedirs(OUT_DIR, exist_ok=True)\n",
    "\n",
    "VAES = {\n",
    "    \"FLUX.2\":   (\"flux2\",    \"AiArtLab/sdxs-1b\"),\n",
    "    \"vae32ch2\": (\"vae32ch\",  \"vae32ch2\"),\n",
    "}\n",
    "\n",
    "# ── Patchify / Unpatchify ─────────────────────────────────────────────────────\n",
    "def _patchify_latents(latents):\n",
    "    B, C, H, W = latents.shape\n",
    "    latents = latents.view(B, C, H // 2, 2, W // 2, 2)\n",
    "    latents = latents.permute(0, 1, 3, 5, 2, 4)\n",
    "    latents = latents.reshape(B, C * 4, H // 2, W // 2)\n",
    "    return latents\n",
    "\n",
    "def _unpatchify_latents(latents):\n",
    "    B, C, H, W = latents.shape\n",
    "    latents = latents.reshape(B, C // 4, 2, 2, H, W)\n",
    "    latents = latents.permute(0, 1, 4, 2, 5, 3)\n",
    "    latents = latents.reshape(B, C // 4, H * 2, W * 2)\n",
    "    return latents\n",
    "\n",
    "# ── Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ ─────────────────────────────────────────────────────────\n",
    "def load_image(path):\n",
    "    img = Image.open(path).convert(\"RGB\")\n",
    "    w, h = img.size\n",
    "    img = CenterCrop((h // 8 * 8, w // 8 * 8))(img)\n",
    "    tensor = ToTensor()(img).unsqueeze(0)\n",
    "    tensor = Normalize(mean=[0.5]*3, std=[0.5]*3)(tensor)\n",
    "    return img, tensor.to(device, dtype=dtype)\n",
    "\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",
    "# ── Бтатистика ────────────────────────────────────────────────────────────────\n",
    "def logvariance(latents):\n",
    "    return torch.log(latents.var() + 1e-8).item()\n",
    "\n",
    "def print_stats(name, latents):\n",
    "    lv = logvariance(latents)\n",
    "    print(f\"  log-variance : {lv:.4f}  (ΠΈΠ΄Π΅Π°Π» β‰ˆ 0.0)\")\n",
    "    print(f\"  mean         : {latents.mean():.4f}\")\n",
    "    print(f\"  std          : {latents.std():.4f}\")\n",
    "    print(f\"  shape        : {latents.shape}\")\n",
    "\n",
    "def plot_latent_distribution(latents, title, save_path):\n",
    "    from scipy.stats import probplot\n",
    "    lat = latents.detach().cpu().float().numpy().flatten()\n",
    "\n",
    "    plt.figure(figsize=(10, 4))\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",
    "    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",
    "    print(f\"  Π³Ρ€Π°Ρ„ΠΈΠΊ сохранён: {save_path}\")\n",
    "\n",
    "# ── Нормализация ΠΈΠ· ΠΊΠΎΠ½Ρ„ΠΈΠ³Π° (per-channel для vae32ch) ────────────────────────\n",
    "def make_norm_tensors(cfg, latent_channels, device, dtype):\n",
    "    mean  = getattr(cfg, \"latents_mean\",   None)\n",
    "    std   = getattr(cfg, \"latents_std\",    None)\n",
    "    shift = getattr(cfg, \"shift_factor\",   0.0)\n",
    "    scale = getattr(cfg, \"scaling_factor\", 1.0)\n",
    "\n",
    "    if mean is not None:\n",
    "        mean = torch.tensor(mean, device=device, dtype=dtype).view(1, latent_channels, 1, 1)\n",
    "    if std is not None:\n",
    "        std  = torch.tensor(std,  device=device, dtype=dtype).view(1, latent_channels, 1, 1)\n",
    "\n",
    "    shift = torch.tensor(shift if shift else 0., device=device, dtype=dtype)\n",
    "    scale = torch.tensor(scale, device=device, dtype=dtype)\n",
    "    return mean, std, shift, scale\n",
    "\n",
    "# ── Основной Ρ†ΠΈΠΊΠ» ─────────────────────────────────────────────────────────────\n",
    "img, x = load_image(IMG_PATH)\n",
    "img.save(os.path.join(OUT_DIR, \"original.png\"))\n",
    "print(f\"ΠšΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½Π°: {x.shape}\")\n",
    "\n",
    "for name, (kind, repo) in VAES.items():\n",
    "    print(f\"\\n{'='*55}\")\n",
    "    print(f\"VAE : {name}\")\n",
    "    print(f\"repo: {repo}\")\n",
    "\n",
    "    # --- Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ класс ---\n",
    "    if kind == \"flux2\":\n",
    "        vae = AutoencoderKLFlux2.from_pretrained(\n",
    "            repo, subfolder=\"vae\", torch_dtype=dtype\n",
    "        ).to(device)\n",
    "    else:\n",
    "        vae = AutoencoderKL.from_pretrained(\n",
    "            repo, torch_dtype=dtype\n",
    "        ).to(device)\n",
    "    vae.eval()\n",
    "\n",
    "    latent_channels = vae.config.latent_channels\n",
    "    mean_t, std_t, shift_t, scale_t = make_norm_tensors(\n",
    "        vae.config, latent_channels, device, dtype\n",
    "    )\n",
    "\n",
    "    print(f\"latent_channels : {latent_channels}\")\n",
    "    print(f\"scaling_factor  : {scale_t.item():.5f}\")\n",
    "    print(f\"shift_factor    : {shift_t.item():.5f}\")\n",
    "    print(f\"latents_mean    : {'Π΄Π° (' + str(latent_channels) + 'ch)' if mean_t is not None else 'Π½Π΅Ρ‚'}\")\n",
    "    print(f\"latents_std     : {'Π΄Π° (' + str(latent_channels) + 'ch)' if std_t  is not None else 'Π½Π΅Ρ‚'}\")\n",
    "\n",
    "    with torch.no_grad():\n",
    "\n",
    "        # ── ENCODE ────────────────────────────────────────────────────────────\n",
    "        latents = vae.encode(x).latent_dist.sample().to(dtype)\n",
    "        print(f\"\\n[encode] raw latents: {latents.shape}\")\n",
    "\n",
    "        if kind == \"flux2\":\n",
    "            # 32ch β†’ patchify β†’ 128ch\n",
    "            latents_patched = _patchify_latents(latents)\n",
    "            print(f\"[flux2]  patchify  : {latents.shape} β†’ {latents_patched.shape}\")\n",
    "\n",
    "            # BN нормализация Π² 128-канальном пространствС\n",
    "            bn_mean = vae.bn.running_mean.view(1, -1, 1, 1).to(device, dtype)\n",
    "            bn_std  = torch.sqrt(\n",
    "                vae.bn.running_var.view(1, -1, 1, 1) + vae.config.batch_norm_eps\n",
    "            ).to(device, dtype)\n",
    "            latents_normed = (latents_patched - bn_mean) / bn_std\n",
    "            print(f\"[flux2]  BN norm   : mean={bn_mean.mean():.4f}  std={bn_std.mean():.4f}\")\n",
    "\n",
    "            # считаСм статистику Π² 128ch Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½ΠΎΠΌ пространствС\n",
    "            print(\"\\n[STATS] послС BN Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ (128ch):\")\n",
    "            print_stats(name, latents_normed)\n",
    "            #plot_latent_distribution(\n",
    "            #    latents_normed,\n",
    "            #    f\"{name}_latents\",\n",
    "            #    os.path.join(OUT_DIR, f\"dist_{name}.png\")\n",
    "            #)\n",
    "\n",
    "            # unpatchify β†’ 32ch (для decode)\n",
    "            latents = _unpatchify_latents(latents_normed)\n",
    "\n",
    "        else:  # vae32ch2\n",
    "            # per-channel нормализация ΠΈΠ· ΠΊΠΎΠ½Ρ„ΠΈΠ³Π°\n",
    "            if mean_t is not None and std_t is not None:\n",
    "                latents = (latents - mean_t) / std_t\n",
    "            latents = (latents - shift_t) / scale_t\n",
    "\n",
    "            print(f\"\\n[STATS] послС per-channel Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ ({latent_channels}ch):\")\n",
    "            print_stats(name, latents)\n",
    "            #plot_latent_distribution(\n",
    "            #    latents,\n",
    "            #    f\"{name}_latents\",\n",
    "            #    os.path.join(OUT_DIR, f\"dist_{name}.png\")\n",
    "            #)\n",
    "\n",
    "        # ── DECODE ────────────────────────────────────────────────────────────\n",
    "        if kind == \"flux2\":\n",
    "            # patchify β†’ denorm β†’ unpatchify\n",
    "            latents_patched  = _patchify_latents(latents)\n",
    "            latents_denormed = latents_patched * bn_std + bn_mean\n",
    "            latents          = _unpatchify_latents(latents_denormed)\n",
    "            print(f\"\\n[flux2]  BN denorm + unpatchify: {latents.shape}\")\n",
    "\n",
    "        else:  # vae32ch2\n",
    "            latents = latents * scale_t + shift_t\n",
    "            if mean_t is not None and std_t is not None:\n",
    "                latents = latents * std_t + mean_t\n",
    "            print(f\"\\n[vae32ch2] denorm: {latents.shape}\")\n",
    "\n",
    "        rec = vae.decode(latents).sample\n",
    "\n",
    "    out_path = os.path.join(OUT_DIR, f\"decoded_{name}.png\")\n",
    "    tensor_to_img(rec).save(out_path)\n",
    "    print(f\"Π‘ΠΎΡ…Ρ€Π°Π½Π΅Π½ΠΎ: {out_path}\")\n",
    "\n",
    "print(f\"\\n{'='*55}\")\n",
    "print(\"Π“ΠΎΡ‚ΠΎΠ²ΠΎ\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c219c07b-8da2-4182-ace6-8c3cc63ae3b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scipy in /usr/local/lib/python3.12/dist-packages (1.17.1)\n",
      "Requirement already satisfied: numpy<2.7,>=1.26.4 in /usr/local/lib/python3.12/dist-packages (from scipy) (2.4.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    },
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'scipy'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mModuleNotFoundError\u001b[39m                       Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m      1\u001b[39m get_ipython().system(\u001b[33m'\u001b[39m\u001b[33mpip install --user scipy\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mscipy\u001b[39;00m\n\u001b[32m      4\u001b[39m \u001b[38;5;28mprint\u001b[39m(scipy.__version__)\n",
      "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'scipy'"
     ]
    }
   ],
   "source": [
    "!pip install --user scipy\n",
    "\n",
    "import scipy\n",
    "print(scipy.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43a4e1bc-2b02-4604-b69e-1a5aa276b6ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python3 (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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}