{"id":193,"date":"2022-03-16T05:24:38","date_gmt":"2022-03-16T05:24:38","guid":{"rendered":"https:\/\/blog.liguanxin.cn\/?p=193"},"modified":"2022-03-16T05:24:38","modified_gmt":"2022-03-16T05:24:38","slug":"%e8%ae%ba%e6%96%87%e7%ac%94%e8%ae%b0-swinir-image-restoration-using-swin-transformer","status":"publish","type":"post","link":"https:\/\/blog.liguanxin.cn\/index.php\/2022\/03\/16\/%e8%ae%ba%e6%96%87%e7%ac%94%e8%ae%b0-swinir-image-restoration-using-swin-transformer\/","title":{"rendered":"\u8bba\u6587\u7b14\u8bb0\u2014\u2014SwinIR: Image Restoration Using Swin Transformer"},"content":{"rendered":"<p><strong>\u521b\u65b0\u70b9\uff1a<br \/>\n\u2460\u628aTransformer\u5f15\u5165\u8d85\u5206<br \/>\n\u2461SwinIR\u7531\u6d45\u5c42\u7279\u5f81\u63d0\u53d6\u3001\u6df1\u5ea6\u7279\u5f81\u63d0\u53d6\u548c\u9ad8\u8d28\u91cf\u7684\u56fe\u50cf\u91cd\u5efa\u4e09\u90e8\u5206\u7ec4\u6210\u3002<\/strong><\/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_20220316104730.png\" alt=\"\" \/><br \/>\n\u2460\u6d45\u5c42\u7279\u5f81\u63d0\u53d6\uff1a3<em>3\u5377\u79ef\u5c42<br \/>\n\u2461\u6df1\u5c42\u7279\u5f81\u63d0\u53d6\u91c7\u7528K\u4e2aresidual Swin Transformer blocks (RSTB)\u4ee5\u53ca\u4e00\u4e2a3<\/em>3\u5377\u79ef\u5c42<br \/>\n\u2462\u6700\u540e\u4e00\u4e2a\u6b8b\u5dee\u628a\u6062\u590d\u540e\u7684HQ\u52a0\u4e0a\u539f\u59cb\u7684LQ<\/p>\n<h3>RSTB\u6a21\u5757<\/h3>\n<p>\u7531\u8fde\u7eed\u7684SwinTransformer\u548c\u6b8b\u5dee\u8fde\u63a5\u6784\u6210\u3002<br \/>\nSwin Transformer layers (STL)<br \/>\n\u7b2ci\u4e2aRSTB\u7684\u7279\u5f81F\u8ba1\u7b97\u65b9\u5f0f\u4e3a\uff08j\u8868\u793a\u7b2c\u51e0\u4e2aSwin Transformer layers\uff09\uff1a<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220316124644.png\" alt=\"\" \/><br \/>\n\u7b2ci\u4e2aRSTB\u7684\u6700\u540e\u4e00\u5c42\u91c7\u7528\u5377\u79ef\uff0c\u5e76\u4e14\u4e0e\u5f53\u524dRSTB\u7684\u8f93\u5165F(i,0)\u8fdb\u884c\u6b8b\u5dee\u8fde\u63a5<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220316124651.png\" alt=\"\" \/><\/p>\n<h3>\u89e3\u91ca<\/h3>\n<p>\u6d45\u5c42\u7279\u5f81\u4e3b\u8981\u5305\u542b\u4f4e\u9891\u7279\u5f81\uff0c\u6df1\u5c42\u7279\u5f81\u7528\u4e8e\u6062\u590d\u4e22\u5931\u7684\u9ad8\u9891\u7279\u5f81\u3002\u4f4e\u9891\u901a\u8fc7\u957f\u8ddd\u79bbskip\u4f20\u8f93\u5230\u6062\u590d\u6a21\u5757\u3002<\/p>\n<h3>\u635f\u5931\u51fd\u6570<\/h3>\n<p>\u91c7\u7528L1\u8303\u6570\u8ba1\u7b97\u50cf\u7d20\u635f\u5931<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220316111256.png\" alt=\"\" \/><\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre><code class=\"language-python\">class RSTB(nn.Module):\n    ...\n    # \u4e3b\u8981\u662f\u5728\u539f\u7248SwinTransformer\u7684\u57fa\u7840\u4e0a\u5957\u4e86\u4e00\u5c42\u5377\u79ef\n    def forward(self, x, x_size):\n        return self.patch_embed(self.conv(self.patch_unembed(self.residual_group(x, x_size), x_size))) + x<\/code><\/pre>\n<pre><code class=\"language-python\">class SwinIR(nn.Module):\n        ...\n        # \u5212\u5206\u56fe\u50cf\u5230patch\n        self.patch_embed = PatchEmbed(\n            img_size=img_size, patch_size=patch_size, in_chans=embed_dim, embed_dim=embed_dim,\n            norm_layer=norm_layer if self.patch_norm else None)\n        num_patches = self.patch_embed.num_patches\n        patches_resolution = self.patch_embed.patches_resolution\n        self.patches_resolution = patches_resolution\n\n        # patch\u8fd8\u539f\u4e3a\u56fe\u50cf\n        self.patch_unembed = PatchUnEmbed(\n            img_size=img_size, patch_size=patch_size, in_chans=embed_dim, embed_dim=embed_dim,\n            norm_layer=norm_layer if self.patch_norm else None)\n        ...\n    # transformer\u64cd\u4f5c\n    def forward_features(self, x):\n        x_size = (x.shape[2], x.shape[3])\n        x = self.patch_embed(x)\n        if self.ape:\n            x = x + self.absolute_pos_embed\n        x = self.pos_drop(x)\n\n        for layer in self.layers:\n            x = layer(x, x_size)\n\n        x = self.norm(x)  # B L C\n        x = self.patch_unembed(x, x_size)\n\n        return x\n\n    def forward(self, x):\n        H, W = x.shape[2:]\n        x = self.check_image_size(x)\n\n        self.mean = self.mean.type_as(x)\n        # \u5f52\u4e00\u5316\n        x = (x - self.mean) * self.img_range\n\n        if self.upsampler == &#039;pixelshuffle&#039;:\n            # for classical SR\n            # \u6d45\u5c42\u7279\u5f81\u63d0\u53d6\n            x = self.conv_first(x)\n            # \u6df1\u5c42\u7279\u5f81\u63d0\u53d6\uff0c\u4ee5\u53ca\u521d\u59cb\u503c\u7684\u6b8b\u5dee\n            x = self.conv_after_body(self.forward_features(x)) + x\n            x = self.conv_before_upsample(x)\n            # \u57fa\u4e8esub-pixel\u7684\u4e0a\u91c7\u6837\u6765\u6062\u590dSR\u56fe\u50cf\n            x = self.conv_last(self.upsample(x))\n        elif self.upsampler == &#039;pixelshuffledirect&#039;:\n            # for lightweight SR\n            x = self.conv_first(x)\n            x = self.conv_after_body(self.forward_features(x)) + x\n            x = self.upsample(x)\n        elif self.upsampler == &#039;nearest+conv&#039;:\n            # for real-world SR\n            x = self.conv_first(x)\n            x = self.conv_after_body(self.forward_features(x)) + x\n            x = self.conv_before_upsample(x)\n            x = self.lrelu(self.conv_up1(torch.nn.functional.interpolate(x, scale_factor=2, mode=&#039;nearest&#039;)))\n            x = self.lrelu(self.conv_up2(torch.nn.functional.interpolate(x, scale_factor=2, mode=&#039;nearest&#039;)))\n            x = self.conv_last(self.lrelu(self.conv_hr(x)))\n        else:\n            # for image denoising and JPEG compression artifact reduction\n            x_first = self.conv_first(x)\n            res = self.conv_after_body(self.forward_features(x_first)) + x_first\n            x = x + self.conv_last(res)\n\n        # \u53cd\u5f52\u4e00\u5316\n        x = x \/ self.img_range + self.mean\n\n        return x[:, :, :H*self.upscale, :W*self.upscale]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u65b0\u70b9\uff1a \u2460\u628aTransformer\u5f15\u5165\u8d85\u5206 \u2461SwinIR\u7531\u6d45\u5c42\u7279\u5f81\u63d0\u53d6\u3001\u6df1\u5ea6\u7279\u5f81\u63d0\u53d6\u548c\u9ad8\u8d28\u91cf\u7684\u56fe\u50cf\u91cd\u5efa\u4e09\u90e8 [&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,13,11,21],"_links":{"self":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/193"}],"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=193"}],"version-history":[{"count":3,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions\/214"}],"wp:attachment":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}