{"id":339609,"date":"2022-10-12T15:01:19","date_gmt":"2022-10-12T15:01:19","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=339609"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=339609","title":{"rendered":"<span>\u0414\u0438\u0444\u0444\u0443\u0437\u0438\u043e\u043d\u043d\u044b\u0435 \u041d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u2014 \u0441\u0430\u043c\u044b\u0439 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><a href=\"https:\/\/habr.com\/ru\/company\/ruvds\/blog\/689072\/\"><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/post_images\/1e0\/536\/fe6\/1e0536fe62336ab9f9d4b7a93fb1f03a.jpg\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/1e0\/536\/fe6\/1e0536fe62336ab9f9d4b7a93fb1f03a.jpg\" data-blurred=\"true\"\/><\/div>\n<p><\/a><br \/>  \u041c\u0435\u0442\u043e\u0434 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u0434\u0438\u0444\u0444\u0443\u0437\u0438\u0438 \u043f\u043e\u0438\u0441\u0442\u0438\u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u043d\u0430\u0448\u0443\u043c\u0435\u0432\u0448\u0438\u043c \u0432 \u044d\u0442\u043e\u043c \u0433\u043e\u0434\u0443 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044f\u043c\u0438. \u0415\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442: DALLE 2, Midjourney, imagen \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 2022 \u0433\u043e\u0434\u0430.<\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0441\u0430\u043c\u044b\u0445 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u0434\u0430\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0441\u0432\u043e\u044e.<\/p>\n<p>  \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0438, \u0432 \u043e\u0431\u0449\u0435\u043c \u2014 \u0432\u0441\u0451, \u043a\u0430\u043a \u0432\u044b \u043b\u044e\u0431\u0438\u0442\u0435.<a name=\"habracut\"><\/a><\/p>\n<p>  \u0414\u0438\u0444\u0444\u0443\u0437\u0438\u043e\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0445 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0442\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u043c, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043d\u0438 \u043e\u0431\u0443\u0447\u0430\u044e\u0442\u0441\u044f.<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/5a3\/6bf\/b49\/5a36bfb497d4a7191d1f4c666d5ba2f2.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/5a3\/6bf\/b49\/5a36bfb497d4a7191d1f4c666d5ba2f2.png\"\/><\/p>\n<p>  \u041e\u0431\u0449\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\u0411\u0435\u0440\u0451\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435<\/li>\n<li>\u0418\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0413\u0430\u0443\u0441\u043e\u0432\u0441\u043a\u0438\u0439 \u0448\u0443\u043c, \u043f\u043e\u043a\u0430 \u043e\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0448\u0430 \u0438\u0437 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439<\/li>\n<li>\u041e\u0431\u0443\u0447\u0438\u0442\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u0448\u0443\u043c\u043e\u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u0430\u0448\u0443 \u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443, \u043f\u043e\u0445\u043e\u0436\u0435\u043c\u0443 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435<\/li>\n<\/ol>\n<p>  <b><font color=\"#3AC1EF\">\u041e\u0431\u0449\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438:<\/font><\/b> \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0447\u0438\u0441\u0442\u044b\u0439 \u0413\u0430\u0443\u0441\u043e\u0432\u0441\u043a\u0438\u0439 \u0448\u0443\u043c \u0438 \u0434\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0448\u0443\u043c\u043e\u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043d\u043e\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435<\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0451, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f<\/font><\/h2>\n<p>  <\/p>\n<pre><code class=\"python\">!pip install datasets &amp;>> install.log  from datasets import load_dataset from PIL import Image import torch.nn.functional as F import os from tqdm.notebook import tqdm import torch import numpy as np   def img_to_tensor(im):   return torch.tensor(np.array(im.convert('RGB'))\/255).permute(2, 0, 1).unsqueeze(0) * 2 - 1  def tensor_to_image(t):   return Image.fromarray(np.array(((t.squeeze().permute(1, 2, 0)+1)\/2).clip(0, 1)*255).astype(np.uint8))  def gather(consts: torch.Tensor, t: torch.Tensor):     c = consts.gather(-1, t)     return c.reshape(-1, 1, 1, 1)<\/code><\/pre>\n<h2><font color=\"#3AC1EF\">\u258d \u0414\u0430\u043d\u043d\u044b\u0435<\/font><\/h2>\n<p>  \u0413\u043e\u0442\u043e\u0432\u044c\u0442\u0435 \u0441\u0432\u043e\u0438 \u0431\u043b\u043e\u043a\u043d\u043e\u0442\u044b colab \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u0434\u0430, \u0431\u0443\u0434\u0435\u043c \u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 (\u0421\u043e\u0432\u0435\u0442\u0443\u044e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c GPU). \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 CIFAR-10 c 60 000 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 32\u00d732 \u043f\u0438\u043a\u0441\u0435\u043b\u044f \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0441 \u043d\u0443\u0436\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438. <\/p>\n<p>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0434\u0438\u0444\u0444\u0443\u0437\u0438\u043e\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043d\u043e \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u0438 \u0442\u0430\u043a\u043e\u0439:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/d6f\/6a2\/a29\/d6f6a2a29f6bd416d2d7c24bf6952ab9.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/d6f\/6a2\/a29\/d6f6a2a29f6bd416d2d7c24bf6952ab9.png\"\/><\/p>\n<pre><code class=\"python\"># \u0421\u043a\u0430\u0447\u0430\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 cifar10 = load_dataset('cifar10')  image = Image.new('RGB', size=(32*5, 32*2)) for i in range(10):   im = cifar10['train'][i]['img']   image.paste(im, ( (i%5)*32, (i\/\/5)*32 )) image.resize((32*5*4, 32*2*4), Image.NEAREST)<\/code><\/pre>\n<h2><font color=\"#3AC1EF\">\u258d \u0428\u0443\u043c<\/font><\/h2>\n<p>  \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0430\u0431\u043e\u0440\u0430 \u0448\u0443\u043c\u043d\u044b\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u043b\u043e\u0436\u0438\u0442\u044c \u0413\u0430\u0443\u0441\u043e\u0432\u0441\u043a\u0438\u0439 \u0448\u0443\u043c \u043d\u0430 \u043d\u0430\u0448 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u00ab\u0433\u0440\u0430\u0444\u0438\u043a \u043e\u0442\u043a\u043b\u043e\u043d\u0435\u043d\u0438\u0439\u00bb, \u043e\u043d \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0448\u0443\u043c\u0430 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0448\u0430\u0433\u0435.<\/p>\n<p>  \u041f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/47f\/db3\/fe6\/47fdb3fe696a0257c44dbdb8bb757c37.svg\" alt=\"$q(xt|xt\u22121)=N(xt;1\u2212\u03b2t\u2212\u2212\u2212\u2212\u2212\u221axt\u22121,\u03b2tI)q(x1:T|x0)=\u220fTt=1q(xt|xt\u22121)$\" data-tex=\"display\"\/><\/p>\n<p>  \u0412\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e. \u041c\u044b \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u043c xt\u22121 \u0441 \u0413\u0430\u0443\u0441\u043e\u0432\u0441\u043a\u0438\u043c \u0448\u0443\u043c\u043e\u043c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u043c \u03b2t<br \/>  \u041a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u0438\u0434\u0435\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0448\u0430\u0433\u043e\u0432 \u0448\u0443\u043c \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n<pre><code class=\"python\">n_steps = 100 beta = torch.linspace(0.0001, 0.04, n_steps)  def q_xt_xtminus1(xtm1, t):   mean = gather(1. - beta, t) ** 0.5 * xtm1 # \u221a(1\u2212\u03b2t)*xtm1   var = gather(beta, t) # \u03b2t   eps = torch.randn_like(xtm1) # \u0428\u0443\u043c \u0432 \u0444\u043e\u0440\u043c\u0435 xtm1   return mean + (var ** 0.5) * eps  # \u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044d\u0442\u0430\u043f\u0430\u0445 ims = [] start_im = cifar10['train'][3]['img'] x = img_to_tensor(start_im).squeeze() for t in range(n_steps):    # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u044b\u0435 20 \u0448\u0430\u0433\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441   if t%20 == 0:     ims.append(tensor_to_image(x))      # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c Xt \u0441 \u0443\u0447\u0451\u0442\u043e\u043c Xt-1 (\u0442.\u0435. x \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438)   t = torch.tensor(t, dtype=torch.long) # t \u043a\u0430\u043a \u0442\u0435\u043d\u0437\u043e\u0440   x = q_xt_xtminus1(x, t) # \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 x, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u044b\u0448\u0435  # \u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 image = Image.new('RGB', size=(32*5, 32)) for i, im in enumerate(ims):   image.paste(im, ((i%5)*32, 0)) image.resize((32*4*5, 32*4), Image.NEAREST)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/c64\/5b9\/161\/c645b91610c937462c25aba6d3654de3.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/c64\/5b9\/161\/c645b91610c937462c25aba6d3654de3.png\"\/><\/p>\n<p>  \u041d\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u0446\u0435\u043b\u0435\u0441\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0451\u043c \u0440\u0435\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c xt \u043b\u044e\u0431\u043e\u0433\u043e x0 \u0441\u0440\u0430\u0437\u0443:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/e42\/80d\/d5d\/e4280dd5db2c9f3a6eb9524c404c2483.svg\" alt=\"$q(xt|x0)=N(xt;\u03b1\u00aft\u2212\u2212\u221ax0,(1\u2212\u03b1\u00aft)I) where \u03b1\u00aft=\u220fTi=1\u03b1i$\" data-tex=\"display\"\/><\/p>\n<p>  <\/p>\n<blockquote><p><a href=\"https:\/\/sassafras13.github.io\/ReparamTrick\/#:~:text=So%20in%20short%2C%20the%20reparameterization,%2C%20q*%20%5B3%5D\">\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e \u0440\u0435\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0430\u0446\u0438\u044e<\/a><\/p><\/blockquote>\n<p>\u041a\u043e\u0434 \u043d\u0435 \u0442\u0430\u043a \u0441\u0442\u0440\u0430\u0448\u0435\u043d, \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0433\u043b\u044f\u0434\u044f \u043d\u0430 \u0444\u043e\u0440\u043c\u0443\u043b\u0443:<\/p>\n<pre><code class=\"python\">n_steps = 100 beta = torch.linspace(0.0001, 0.04, n_steps) alpha = 1. - beta alpha_bar = torch.cumprod(alpha, dim=0)  def q_xt_x0(x0, t):   mean = gather(alpha_bar, t) ** 0.5 * x0 # \u0442\u0435\u043f\u0435\u0440\u044c alpha_bar   var = 1-gather(alpha_bar, t) # (1-alpha_bar)   eps = torch.randn_like(x0)   return mean + (var ** 0.5) * eps  # \u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044d\u0442\u0430\u043f\u0430\u0445 ims = [] start_im = cifar10['train'][3]['img'] x0 = img_to_tensor(start_im).squeeze() for t in [0, 20, 40, 60, 80]:   x = q_xt_x0(x0, torch.tensor(t, dtype=torch.long))   ims.append(tensor_to_image(x))  image = Image.new('RGB', size=(32*5, 32)) for i, im in enumerate(ims):   image.paste(im, ((i%5)*32, 0)) image.resize((32*4*5, 32*4), Image.NEAREST)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/bb5\/3fb\/9a2\/bb53fb9a2e29ce52f60c3d8224c9b718.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/bb5\/3fb\/9a2\/bb53fb9a2e29ce52f60c3d8224c9b718.png\"\/><br \/>  <i><font color=\"#999999\">\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c *for t in range*<\/font><\/i><\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0428\u0443\u043c\u043e\u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u0435. U-NET<\/font><\/h2>\n<p>  \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043a \u043d\u0430\u0448\u0435\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u0428\u0443\u043c\u043e\u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u2014 \u0447\u0442\u043e\u0431\u044b \u0435\u0451 \u0432\u0445\u043e\u0434 \u0438 \u0432\u044b\u0445\u043e\u0434 \u0431\u044b\u043b\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430\u043c\u0438. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u043e, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0438\u0444\u0444\u0443\u0437\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c\u0438 U-Net.<\/p>\n<p>  U-NET \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435. \u0427\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u043b\u044f \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0431\u0438\u043e\u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u043d\u043e \u0438 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u0430\u0441\u044c.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/622\/e7a\/b8e\/622e7ab8e6da6a5d581407b1557df2e5.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/622\/e7a\/b8e\/622e7ab8e6da6a5d581407b1557df2e5.png\"\/><\/p>\n<p>  \u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0441 \u0440\u0430\u043d\u043d\u0438\u0445 \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0441 \u0432\u044b\u0441\u043e\u043a\u0438\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u2014 \u0432 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0437\u0434\u043d\u0438\u0435 \u0443\u0440\u043e\u0432\u043d\u0438. \u00ab\u042f\u0440\u043b\u044b\u043a\u00bb \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0435\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0443\u044e \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0443 \u0438\u0437 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0445 \u0441\u043b\u043e\u0451\u0432.<\/p>\n<p>  \u0414\u0438\u0444\u0444\u0443\u0437\u0438\u043e\u043d\u043d\u044b\u0435 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 U-NET\u0441 \u043e\u0434\u043d\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c: TimeEmbedding<br \/>  TimeEmbedding \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0448\u0430\u0433 (t) \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043a\u043e\u043d\u0434\u0438\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0435\u0451 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u0441\u0435\u0442\u0438.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0435 \u043a\u043e\u0434 \u043f\u043e \u0445\u043e\u0434\u0443 \u0441\u0442\u0430\u0442\u044c\u0438, \u0441\u043a\u043e\u043f\u0438\u0440\u0443\u0439\u0442\u0435 <a href=\"https:\/\/github.com\/nikuson\/Unet-Habr\/blob\/main\/Unet.py\">\u043a\u043e\u0434 U-NET \u043e\u0442\u0441\u044e\u0434\u0430<\/a>. (\u041d\u043e \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 colab \u0441\u043e \u0432\u0441\u0435\u043c \u043a\u043e\u0434\u043e\u043c \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438)<\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435<\/font><\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u0435\u0451 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0448\u0443\u043c \u0441 \u0443\u0447\u0451\u0442\u043e\u043c xt \u0438 t. \u041d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u0432\u043e\u0437\u0438\u043c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c, \u0430 \u043d\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0448\u0443\u043c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e?<\/p>\n<p>  \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0437-\u0437\u0430 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430. \u0428\u0443\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043e \u0441\u0440\u0435\u0434\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c, \u0440\u0430\u0432\u043d\u044b\u043c \u043d\u0443\u043b\u044e, \u0438 \u044d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438. \u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e q_xt_x0 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0430 \u043a\u0430\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \u0448\u0443\u043c\u043e\u043c (xt), \u0442\u0430\u043a \u0438 \u0441\u0430\u043c \u0448\u0443\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0446\u0435\u043b\u044c\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<pre><code class=\"python\">x = torch.randn(10, 3, 32, 32) t = torch.tensor([50.], dtype=torch.long) unet = UNet() model_output = unet(x, t) model_output.shape  # \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043c\u043e\u0434\u0435\u043b\u044c unet = UNet(n_channels=32).cuda()   # \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 n_steps = 100 beta = torch.linspace(0.0001, 0.04, n_steps).cuda() alpha = 1. - beta alpha_bar = torch.cumprod(alpha, dim=0)   # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0448\u0443\u043c def q_xt_x0(x0, t):   mean = gather(alpha_bar, t) ** 0.5 * x0   var = 1-gather(alpha_bar, t)   eps = torch.randn_like(x0).to(x0.device)   return mean + (var ** 0.5) * eps, eps # \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0448\u0443\u043c   # \u0422\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b batch_size = 128 lr = 3e-4 # \u0421\u043a\u043e\u0440\u043e\u0447\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u044f \u043d\u0430\u0437\u044b\u0432\u0430\u044e \u044d\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043e\u0439 \u041a\u0430\u0440\u043f\u0430\u0442\u0438   losses = [] # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u0435\u0440\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u0430   dataset = cifar10['train'] optim = torch.optim.AdamW(unet.parameters(), lr=lr) # \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440   for i in tqdm(range(0, len(dataset)-batch_size, batch_size)): # \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445   ims = [dataset[idx]['img'] for idx in range(i,i+batch_size)]   tims = [img_to_tensor(im).cuda() for im in ims] # \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0442\u0435\u043d\u0437\u043e\u0440\u044b   x0 = torch.cat(tims)   t = torch.randint(0, n_steps, (batch_size,), dtype=torch.long).cuda()   xt, noise = q_xt_x0(x0, t)   pred_noise = unet(xt.float(), t)   loss = F.mse_loss(noise.float(), pred_noise)   losses.append(loss.item())   optim.zero_grad()   loss.backward()   optim.step()<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u043a \u043e\u0448\u0438\u0431\u043e\u043a, \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0435 \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0435, \u0447\u0442\u043e \u044f \u0432\u0438\u0434\u0435\u043b \u0437\u0430 \u0434\u0435\u043d\u044c:<\/p>\n<pre><code class=\"python\">from matplotlib import pyplot as plt plt.plot(losses)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/d59\/c97\/7d2\/d59c977d2130f1a90c66d39589bc3be4.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/d59\/c97\/7d2\/d59c977d2130f1a90c66d39589bc3be4.png\"\/><\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0428\u0443\u043c\u043e\u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u0435<\/font><\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0448\u0443\u043c\u043e\u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u0435 p\u03b8(xt\u22121|xt).<\/p>\n<p>  \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0448\u0443\u043c\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u00ab\u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c\u00bb \u0448\u0430\u0433\u0438 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0448\u0443\u043c\u0430 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0437\u0430 \u0440\u0430\u0437, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043e\u0442 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0431\u043e\u0439 \u0447\u0438\u0441\u0442\u044b\u0439 \u0448\u0443\u043c, \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 (\u043d\u0430\u0434\u0435\u044e\u0441\u044c) \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">def p_xt(xt, noise, t):   alpha_t = gather(alpha, t)   alpha_bar_t = gather(alpha_bar, t)   eps_coef = (1 - alpha_t) \/ (1 - alpha_bar_t) ** .5   mean = 1 \/ (alpha_t ** 0.5) * (xt - eps_coef * noise)   var = gather(beta, t)   eps = torch.randn(xt.shape, device=xt.device)   return mean + (var ** 0.5) * eps    x = torch.randn(1, 3, 32, 32).cuda() # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0439 \u0448\u0443\u043c ims = [] for i in range(n_steps):   t = torch.tensor(n_steps-i-1, dtype=torch.long).cuda()   with torch.no_grad():     pred_noise = unet(x.float(), t.unsqueeze(0))     x = p_xt(x, pred_noise, t.unsqueeze(0))     if i%24 == 0:       ims.append(tensor_to_image(x.cpu()))   image = Image.new('RGB', size=(32*5, 32)) for i, im in enumerate(ims[:5]):   image.paste(im, ((i%5)*32, 0)) image.resize((32*4*5, 32*4), Image.NEAREST)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/3fd\/f48\/913\/3fdf489132e7e7b880cf6f6cb1bfb764.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/3fd\/f48\/913\/3fdf489132e7e7b880cf6f6cb1bfb764.png\"\/><\/p>\n<p>  \u041c\u044b \u0442\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u0437\u0434\u0435\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e 100 \u0448\u0430\u0433\u0430\u043c\u0438 \u0448\u0443\u043c\u0430, \u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0438\u0434\u0438\u0442 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0435\u0441\u0442\u044c \u0445\u043e\u0442\u044f \u0431\u044b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043d\u043e \u044d\u0442\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e, \u043e\u0442\u0447\u0430\u0441\u0442\u0438 \u0438\u0437-\u0437\u0430 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043c\u043e\u0434\u0435\u043b\u0438. \u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0430\u0447\u043d\u0451\u043c \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043d\u0430\u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0437\u0430\u0448\u0443\u043c\u043b\u0451\u043d\u043d\u043e\u0433\u043e, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u043e\u043d\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0434\u043e \u0447\u0435\u0433\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0433\u043e \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b?<\/p>\n<pre><code class=\"python\">horse = cifar10['train'][4]['img'] x0 = img_to_tensor(horse) x = torch.cat([q_xt_x0(x0.cuda(), torch.tensor(50, dtype=torch.long).cuda())[0] for _ in range(10)] ) example_start = q_xt_x0(x0.cuda(), torch.tensor(50, dtype=torch.long).cuda())[0] print(x.shape) ims = [] for i in range(50, n_steps):   t = torch.tensor(n_steps-i-1, dtype=torch.long).cuda()   with torch.no_grad():     pred_noise = unet(x.float(), t.unsqueeze(0))     x = p_xt(x, pred_noise, t.unsqueeze(0))   for i in range(10):   ims.append(tensor_to_image(x[i].unsqueeze(0).cpu()))   image = Image.new('RGB', size=(32*5, 32*2)) for i, im in enumerate(ims):   image.paste(im, ((i%5)*32, 32*(i\/\/5)))   if i==0:image.paste(tensor_to_image(example_start.unsqueeze(0).cpu()), ((i%5)*32, 32*(i\/\/5))) image.resize((32*4*5, 32*4*2), Image.NEAREST)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/726\/fb4\/14b\/726fb414b3a17e90e2799e4749151ea0.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/726\/fb4\/14b\/726fb414b3a17e90e2799e4749151ea0.png\"\/><\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0434\u0438\u0444\u0444\u0443\u0437\u0438\u043e\u043d\u043d\u044b\u0435 Text-to-image?<\/font><\/h2>\n<p>  \u041e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0432 Unet \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0430 (\u0447\u0430\u0449\u0435 \u0438\u0437 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438) \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0443\u0441\u043b\u043e\u0432\u043b\u0438\u0432\u0430\u044e\u0449\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<br \/>  \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0442\u043e\u043c\u0443, \u043a\u0430\u043a \u043c\u044b \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438 t. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u044d\u043c\u0431\u0435\u043d\u0434\u0438\u043d\u0433\u0438 CLIP, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0432 DALLE 2.<\/p>\n<h2><font color=\"#3AC1EF\">\u258d \u0412\u044b\u0432\u043e\u0434\u044b<\/font><\/h2>\n<p>  \u0412\u044b \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u0442\u044c\u0441\u044f \u0432 \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0438\u0440 \u0434\u0438\u0444\u0444\u0443\u0437\u0438\u043e\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043d\u0430 \u043d\u0438\u0445 \u0438\u0437\u043d\u0443\u0442\u0440\u0438, \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0432 \u043d\u0430 \u043a\u043e\u0434 \u0432\u0441\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439. <\/p>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u0430\u043c \u044d\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u0438 \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c.<\/p>\n<p>  \u041f\u043e\u0442\u044b\u043a\u0430\u0442\u044c \u043a\u043e\u0434 \u0438 \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0443 <a href=\"https:\/\/colab.research.google.com\/drive\/1Bcueb7NFAlWsFPF1NBEYDBqE6nvBqrN0?usp=sharing\">\u043c\u043e\u0436\u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u043b\u0430\u0431\u0435.<\/a><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/post_images\/b89\/ccb\/a3a\/b89ccba3a6bda8fc5ffacdb7ca5cfc23.jpg\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/b89\/ccb\/a3a\/b89ccba3a6bda8fc5ffacdb7ca5cfc23.jpg\" data-blurred=\"true\"\/>  <\/p>\n<blockquote><p><b><font color=\"#3AC1EF\">RUVDS | Community <a href=\"https:\/\/bit.ly\/3KZeaxv\">\u0432 telegram<\/a> \u0438 <a href=\"https:\/\/bit.ly\/3qoIOXs\">\u0443\u044e\u0442\u043d\u044b\u0439 \u0447\u0430\u0442<\/a><\/font><\/b><\/p><\/blockquote>\n<p><a href=\"http:\/\/ruvds.com\/ru-rub?utm_source=habr&amp;utm_medium=article&amp;utm_campaign=Nikuson&amp;utm_content=diffuzionnye_nejroseti_%E2%80%94_samyj_aktualnyj_podxod_k_generacii_izobrazhenij\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/sz\/7j\/pf\/sz7jpfj8i1pa6ocj-eia09dev4q.png\" data-src=\"https:\/\/habrastorage.org\/webt\/sz\/7j\/pf\/sz7jpfj8i1pa6ocj-eia09dev4q.png\"\/><\/a><\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/ruvds\/blog\/689072\/\"> https:\/\/habr.com\/ru\/company\/ruvds\/blog\/689072\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><a href=\"https:\/\/habr.com\/ru\/company\/ruvds\/blog\/689072\/\"><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/post_images\/1e0\/536\/fe6\/1e0536fe62336ab9f9d4b7a93fb1f03a.jpg\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/1e0\/536\/fe6\/1e0536fe62336ab9f9d4b7a93fb1f03a.jpg\" data-blurred=\"true\"\/><\/div>\n<p><\/a><br \/>  \u041c\u0435\u0442\u043e\u0434 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u0434\u0438\u0444\u0444\u0443\u0437\u0438\u0438 \u043f\u043e\u0438\u0441\u0442\u0438\u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u043d\u0430\u0448\u0443\u043c\u0435\u0432\u0448\u0438\u043c \u0432 \u044d\u0442\u043e\u043c \u0433\u043e\u0434\u0443 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044f\u043c\u0438. \u0415\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442: DALLE 2, Midjourney, imagen \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 2022 \u0433\u043e\u0434\u0430.<\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0441\u0430\u043c\u044b\u0445 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u0434\u0430\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0441\u0432\u043e\u044e.<\/p>\n<p>  \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0438, \u0432 \u043e\u0431\u0449\u0435\u043c \u2014 \u0432\u0441\u0451, \u043a\u0430\u043a \u0432\u044b \u043b\u044e\u0431\u0438\u0442\u0435.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-339609","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/339609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339609"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/339609\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}