{"id":227,"date":"2022-03-19T04:10:08","date_gmt":"2022-03-19T04:10:08","guid":{"rendered":"https:\/\/blog.liguanxin.cn\/?p=227"},"modified":"2022-03-19T07:25:47","modified_gmt":"2022-03-19T07:25:47","slug":"%e8%ae%ba%e6%96%87%e7%ac%94%e8%ae%b0-encoding-in-style-a-stylegan-encoder-for-image-to-image-translation","status":"publish","type":"post","link":"https:\/\/blog.liguanxin.cn\/index.php\/2022\/03\/19\/%e8%ae%ba%e6%96%87%e7%ac%94%e8%ae%b0-encoding-in-style-a-stylegan-encoder-for-image-to-image-translation\/","title":{"rendered":"\u8bba\u6587\u7b14\u8bb0\u2014\u2014Encoding in Style: a StyleGAN Encoder for Image-to-Image Translation"},"content":{"rendered":"<p><strong>\u521b\u65b0\u70b9\uff1a<br \/>\n\u2460\u65b0\u7684\u7f16\u7801\u5668\u7ed3\u6784\uff0c\u628a\u56fe\u50cf\u6295\u5f71\u5230W+\u7a7a\u95f4\uff08\u4e0e\u4ee5\u5f80\u7684\u5148\u8fd8\u539f\u56fe\u50cf\uff0c\u518d\u7f16\u8f91\u4e0d\u540c\uff0c\u672c\u65b9\u6cd5\u5728W+\u7a7a\u95f4\u4e2d\u7f16\u8f91\uff09\u3002<br \/>\n\u2461\u8bc1\u660e\u4e86\u56fe\u50cf\u7684W\u7a7a\u95f4\uff0c\u53ef\u4ee5\u63d0\u4f9b\u63a7\u5236\u548c\u7f16\u8f91\u7684\u80fd\u529b<br \/>\n\u2462\u91c7\u7528\u4e86\u4e00\u4e2a\u9884\u5148\u8bad\u7ec3\u597d\u7684StyleGAN\u6765\u6062\u590d\u56fe\u50cf<\/strong><\/p>\n<hr \/>\n<h4>W+\u7a7a\u95f4<\/h4>\n<p>18\u4e2a\u4e0d\u540c\u7684512\u7ef4\u5411\u91cf\uff0c\u6bcf\u4e2aStyleGAN\u7684\u8f93\u5165\u5c42\u4e00\u4e2a\u3002<\/p>\n<h3>\u7f51\u7edc\u7ed3\u6784<\/h3>\n<p><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220318172742.png\" alt=\"\" \/><br \/>\n\u56e0\u4e3a\u7f51\u7edc\u662f\u628a\u8f93\u5165\u6295\u5c04\u523018\u4e2aW+\u7a7a\u95f4\uff0c\u56e0\u6b64\u5929\u7136\u5730\u652f\u6301\u591a\u6a21\u6001\u7684\u5b66\u4e60\uff0c\u5982\u4e0a\u56fe\u3002\u4eba\u8138\u8f6e\u5ed3\u901a\u8fc7\u4f4e\u7ef4\u5ea6\uff081-7\u5c42\uff09\u7684W+\u7a7a\u95f4\u5c42\u8f93\u5165\uff0c\u800c\u9ad8\u7ef4\u5ea6\uff088-18\u5c42\uff09\u5219\u63a5\u53d7\u6837\u672c\u6570\u636e\u7684\u968f\u673a\u8f93\u5165\u3002<\/p>\n<p>\u53c2\u8003StyleGAN\u7684Style mixing\uff0c\u628a\u83b7\u5f97\u768418\u4e2a\u7279\u5f81\u5411\u91cf\u4f20\u5165StyleGAN\u7684\u4e0d\u540c\u5c42\uff0c\u4ee3\u8868\u4e86\u4ece\u7c97\u7cd9\u5230\u7cbe\u7ec6\u7684\u4e0d\u540c\u7279\u5f81\u3002<\/p>\n<hr \/>\n<h3>\u6548\u679c<\/h3>\n<p>\u9664\u4e86\u8d85\u5206\uff0c\u8fd9\u4e2a\u7f51\u7edc\u8fd8\u53ef\u4ee5\u62d3\u5c55\u5230\u8f6e\u5ed3\u751f\u6210\u4eba\u8138\uff0c\u906e\u6321\u7684\u4eba\u8138\u8fd8\u539f\uff0c\u751a\u81f3\u662f\u732b\u8138\u72d7\u8138\u3002<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/image-20.png\" alt=\"\" \/><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/image-21.png\" alt=\"\" \/><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/image-19.png\" alt=\"\" \/><\/p>\n<hr \/>\n<h3>CODE<\/h3>\n<p>\u4e3b\u7ed3\u6784<\/p>\n<pre><code class=\"language-python\">    def forward(self, x, resize=True, latent_mask=None, input_code=False, randomize_noise=True,\n                inject_latent=None, return_latents=False, alpha=None):\n        if input_code:  # \u662f\u5426\u8f93\u5165\u7684\u662fW+\u7a7a\u95f4\u5411\u91cf\n            codes = x\n        else:\n            codes = self.encoder(x)  # \u5bf9\u8f93\u5165\u56fe\u50cf\u8fdb\u884c\u7f16\u7801\n            # normalize with respect to the center of an average face\n            # \u52a0\u4e0a\u5747\u503c\u6765\u5f52\u4e00\u5316\n            if self.opts.start_from_latent_avg:\n                if self.opts.learn_in_w:\n                    codes = codes + self.latent_avg.repeat(codes.shape[0], 1)\n                else:\n                    codes = codes + self.latent_avg.repeat(codes.shape[0], 1, 1)\n\n        # \u662f\u5426\u52a0\u5165\u906e\u6321\n        if latent_mask is not None:\n            for i in latent_mask:\n                if inject_latent is not None:\n                    if alpha is not None:\n                        codes[:, i] = alpha * inject_latent[:, i] + (1 - alpha) * codes[:, i]\n                    else:\n                        codes[:, i] = inject_latent[:, i]\n                else:\n                    codes[:, i] = 0\n\n        input_is_latent = not input_code\n        # \u89e3\u7801\u5668\uff08\u4e5f\u662fGAN\u7684\u751f\u6210\u5668\uff09\n        images, result_latent = self.decoder([codes],\n                                             input_is_latent=input_is_latent,\n                                             randomize_noise=randomize_noise,\n                                             return_latents=return_latents)\n\n        if resize:\n            images = self.face_pool(images)\n\n        if return_latents:\n            return images, result_latent\n        else:\n            return images<\/code><\/pre>\n<p>\u7f16\u7801\u5668\u7ed3\u6784<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220319110612.png\" alt=\"\" \/><\/p>\n<pre><code class=\"language-python\">    def forward(self, x):\n        x = self.input_layer(x)\n\n        latents = []\n        modulelist = list(self.body._modules.values())\n        # \u5206\u522b\u5bf9\u5e94\u7c97\u7cd9\u3001\u4e2d\u7b49\u3001\u7cbe\u7ec6\u7279\u5f81\u7684\u63d0\u53d6\n        # \u5de6\u8fb9\u4e09\u4e2a\u84dd\u8272\u5757\u7684\u5904\u7406\uff0c\u4e00\u76f4\u5377\u79ef\u5230\u6df1\u5c42\uff0c\u4fdd\u7559\u4e2d\u95f4\u7684\u4e09\u4e2a\u503c\u505a\u6b8b\u5dee\n        for i, l in enumerate(modulelist):\n            x = l(x)\n            if i == 6:\n                c1 = x\n            elif i == 20:\n                c2 = x\n            elif i == 23:\n                c3 = x\n        # c3\u4e2d\u95f4\u503c\u5206\u522b\u8fdb\u5165\u5404\u81ea\u7684\u6b8b\u5dee\u5377\u79ef\u5757\uff08\u63d0\u53d6\u4e09\u79cd\u4e0d\u540c\u7684\u5c0f\u7279\u5f81\uff09\n        for j in range(self.coarse_ind):\n            latents.append(self.styles[j](c3))\n\n        # \u5982\u56fe\u7684skip connnect\uff0cc3\u52a0\u5377\u79ef\u540e\u7684c2\n        p2 = self._upsample_add(c3, self.latlayer1(c2))\n        # p2\u4e2d\u95f4\u503c\u5206\u522b\u8fdb\u5165\u5404\u81ea\u7684\u6b8b\u5dee\u5377\u79ef\u5757\uff08\u63d0\u53d6\u56db\u79cd\u4e0d\u540c\u7684\u4e2d\u7b49\u7279\u5f81\uff09\n        for j in range(self.coarse_ind, self.middle_ind):\n            latents.append(self.styles[j](p2))\n        # \u5982\u56fe\u7684skip connnect\uff0cp2\u52a0\u5377\u79ef\u540e\u7684c1\n        p1 = self._upsample_add(p2, self.latlayer2(c1))\n        # p1\u4e2d\u95f4\u503c\u5206\u522b\u8fdb\u5165\u5404\u81ea\u7684\u6b8b\u5dee\u5377\u79ef\u5757\uff08\u63d0\u53d612\u79cd\u7684\u7684\u5927\u7279\u5f81\uff09\n        for j in range(self.middle_ind, self.style_count):\n            latents.append(self.styles[j](p1))\n        # \u628a\u7279\u5f81\u5408\u5e76\n        out = torch.stack(latents, dim=1)\n        return out<\/code><\/pre>\n<p>\u751f\u6210\u5668\u7ed3\u6784(\u6765\u81ea<a href=\"https:\/\/blog.liguanxin.cn\/index.php\/2022\/03\/19\/stylegan%e5%92%8cstylegan2\/\">StyleGAN<\/a>)<\/p>\n<pre><code class=\"language-python\">    def forward(...):\n        # \u5982\u679c\u8f93\u5165\u4e0d\u662f\u7279\u5f81\u7a7a\u95f4\uff0c\u5219\u518d\u52a0\u5165styles\u4e2afc\u5c42\u8ba9\u8f93\u5165\u8fdb\u5165\u7279\u5f81\u7a7a\u95f4\uff08\u5bf9\u5e94StyleGAN\u4ecez\u5230w\u7a7a\u95f4\uff09\n        if not input_is_latent:\n            styles = [self.style(s) for s in styles]\n        # \u662f\u5426\u52a0\u5165\u566a\u58f0\uff08\u63a7\u5236\u8f93\u51fa\u7684\u4eba\u8138\u7ec6\u8282\uff0c\u5982\u5934\u53d1\u5377\u66f2\u5ea6\uff09\n        if noise is None:\n            if randomize_noise:\n                noise = [None] * self.num_layers\n            else:\n                noise = [\n                    getattr(self.noises, f&#039;noise_{i}&#039;) for i in range(self.num_layers)\n                ]\n        # \u662f\u5426\u805a\u62e2\u6570\u636e\uff0ctruncation\u662f\u805a\u62e2\u6bd4\u4f8b\uff0ctruncation_latent\u662f\u5747\u503c\n        if truncation &lt; 1:\n            style_t = []\n\n            for style in styles:\n                style_t.append(\n                    truncation_latent + truncation * (style - truncation_latent)\n                )\n\n            styles = style_t\n\n        if len(styles) &lt; 2:\n            inject_index = self.n_latent\n\n            if styles[0].ndim &lt; 3:\n                latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)\n            else:\n                latent = styles[0]\n\n        else:\n            if inject_index is None:\n                inject_index = random.randint(1, self.n_latent - 1)\n\n            latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)\n            latent2 = styles[1].unsqueeze(1).repeat(1, self.n_latent - inject_index, 1)\n\n            latent = torch.cat([latent, latent2], 1)\n\n        # \u53c2\u6570\u5316\n        out = self.input(latent)\n        # \u7b2c\u4e00\u4e2aSytleGAN\u5c42\n        out = self.conv1(out, latent[:, 0], noise=noise[0])\n        # \u8fd8\u539f\u4e3aRGB\n        skip = self.to_rgb1(out, latent[:, 1])\n\n        i = 1\n        # \u540e\u7eed\u7684SytleGAN\u5c42\n        for conv1, conv2, noise1, noise2, to_rgb in zip(\n                self.convs[::2], self.convs[1::2], noise[1::2], noise[2::2], self.to_rgbs\n        ):\n            out = conv1(out, latent[:, i], noise=noise1)\n            out = conv2(out, latent[:, i + 1], noise=noise2)\n            skip = to_rgb(out, latent[:, i + 2], skip)\n\n            i += 2\n\n        image = skip\n\n        if return_latents:\n            return image, latent\n        elif return_features:\n            return image, out\n        else:\n            return image, None<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u65b0\u70b9\uff1a \u2460\u65b0\u7684\u7f16\u7801\u5668\u7ed3\u6784\uff0c\u628a\u56fe\u50cf\u6295\u5f71\u5230W+\u7a7a\u95f4\uff08\u4e0e\u4ee5\u5f80\u7684\u5148\u8fd8\u539f\u56fe\u50cf\uff0c\u518d\u7f16\u8f91\u4e0d\u540c\uff0c\u672c\u65b9\u6cd5\u5728W+\u7a7a\u95f4\u4e2d\u7f16\u8f91\uff09\u3002  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[14,22,20,11,23],"_links":{"self":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/227"}],"collection":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":12,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}