{"id":371,"date":"2022-03-27T09:52:46","date_gmt":"2022-03-27T09:52:46","guid":{"rendered":"https:\/\/blog.liguanxin.cn\/?p=371"},"modified":"2022-03-27T13:27:16","modified_gmt":"2022-03-27T13:27:16","slug":"%e4%b8%ad%e5%bf%83%e5%b7%ae%e5%8d%b7%e7%a7%af","status":"publish","type":"post","link":"https:\/\/blog.liguanxin.cn\/index.php\/2022\/03\/27\/%e4%b8%ad%e5%bf%83%e5%b7%ae%e5%8d%b7%e7%a7%af\/","title":{"rendered":"\u4e2d\u5fc3\u5dee\u5377\u79ef"},"content":{"rendered":"<p>\u9996\u5148\u63cf\u8ff0\u4e00\u4e0b\u666e\u901a\u5377\u79ef\u7684\u516c\u5f0f\uff1a<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220327173435.png\" alt=\"\" \/><br \/>\np0\u4ee3\u8868\u5f53\u524d\u8f93\u5165\u8f93\u51fa\u7684\u4f4d\u7f6e\uff0cpn\u4ee3\u8868R\u7684\u6240\u6709\u679a\u4e3e\u3002<br \/>\nw(pn)\u4ee3\u8868pn\u4f4d\u7f6e\u7684\u5377\u79ef\u6838\u6743\u91cd<br \/>\n\uff0cx(p0+pn)\u4ee3\u8868p0\u504f\u79fb\u4e86pn\u4f4d\u7f6e\u540e\u53d6\u5f97\u503c\uff0c\u6743\u91cd\u548c\u503c\u4e58\u79ef\u7d2f\u52a0\u540e\u5f97\u5230y(p0)\uff0c\u4e5f\u5c31\u662fp0\u5904\u5377\u79ef\u540e\u7684\u8f93\u51fa\u3002<\/p>\n<h1>\u4e2d\u5fc3\u5dee\u5377\u79ef<\/h1>\n<p><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220327174429.png\" alt=\"\" \/><br \/>\n\u56e0\u6b64\uff0c\u4e2d\u5fc3\u5dee\u5377\u79ef\u5373\u4e3a\u91c7\u6837\u540e\uff0c\u6240\u6709\u503c\u51cf\u53bb\u4e2d\u5fc3\u503c\uff0c\u518d\u505a\u5377\u79ef\u7684\u64cd\u4f5c\uff0c\u5982\u4e0a\u56fe\u6240\u793a\u3002<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220327174635.png\" alt=\"\" \/><br \/>\n\u5f53pn=(0,0)\u5373\u4e3a\u4e2d\u5fc3\u503c\u7684\u65f6\u5019\uff0c\u68af\u5ea6\u503c\u76f8\u5bf9\u4e8e\u4e2d\u5fc3\u4f4d\u7f6ep0\u672c\u8eab\u59cb\u7ec8\u7b49\u4e8e\u96f6\u3002<\/p>\n<h1>\u7ed3\u5408\u666e\u901a\u5377\u79ef\u548c\u4e2d\u5fc3\u5dee\u5377\u79ef<\/h1>\n<p>\u5c06\u666e\u901a\u5377\u79ef\u4e0e\u4e2d\u5fc3\u5dee\u5377\u79ef\u76f8\u7ed3\u5408\u53ef\u80fd\u662f\u63d0\u4f9b\u66f4\u9c81\u68d2\u5efa\u6a21\u80fd\u529b\u7684\u53ef\u884c\u65b9\u6cd5\u3002\u56e0\u4e3a\u5f3a\u5ea6\u7ea7\u8bed\u4e49\u4fe1\u606f\u548c\u68af\u5ea6\u7ea7\u8be6\u7ec6\u4fe1\u606f\u5bf9\u4e8e\u751f\u6210\u4eba\u8138\u56fe\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u68af\u5ea6\u53ef\u4ee5\u8ba9\u751f\u6210\u7684\u56fe\u7247\u8fc7\u6e21\u66f4\u5408\u7406\u3002<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220327175022.png\" alt=\"\" \/><br \/>\n\u7b80\u5316\u516c\u5f0f\uff1a<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2022\/03\/\u5fae\u4fe1\u622a\u56fe_20220327175134.png\" alt=\"\" \/><\/p>\n<h1>CODE<\/h1>\n<pre><code class=\"language-python\">########################   Centeral-difference (second order, with 9 parameters and a const theta for 3x3 kernel) 2D Convolution   ##############################\n## | a1 a2 a3 |   | w1 w2 w3 |\n## | a4 a5 a6 | * | w4 w5 w6 | --&gt; output = \\sum_{i=1}^{9}(ai * wi) - \\sum_{i=1}^{9}wi * a5 --&gt; Conv2d (k=3) - Conv2d (k=1)\n## | a7 a8 a9 |   | w7 w8 w9 |\n##\n##   --&gt; output = \n## | a1 a2 a3 |   |  w1  w2  w3 |     \n## | a4 a5 a6 | * |  w4  w5  w6 |  -  | a | * | w\\_sum |     (kernel_size=1x1, padding=0)\n## | a7 a8 a9 |   |  w7  w8  w9 |    \nclass Conv2d_cd(nn.Module):\n    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1,\n                 padding=1, dilation=1, groups=1, bias=False, theta=0.7):\n\n        super(Conv2d_cd, self).__init__() \n        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n        self.theta = theta\n\n    def forward(self, x):\n        out_normal = self.conv(x)\n\n        if math.fabs(self.theta - 0.0) &lt; 1e-8:\n            return out_normal \n        else:\n            #pdb.set_trace()\n            [C_out,C_in, kernel_size,kernel_size] = self.conv.weight.shape\n            kernel_diff = self.conv.weight.sum(2).sum(2)\n            kernel_diff = kernel_diff[:, :, None, None]\n            out_diff = F.conv2d(input=x, weight=kernel_diff, bias=self.conv.bias, stride=self.conv.stride, padding=0, groups=self.conv.groups)\n\n            return out_normal - self.theta * out_diff<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u63cf\u8ff0\u4e00\u4e0b\u666e\u901a\u5377\u79ef\u7684\u516c\u5f0f\uff1a p0\u4ee3\u8868\u5f53\u524d\u8f93\u5165\u8f93\u51fa\u7684\u4f4d\u7f6e\uff0cpn\u4ee3\u8868R\u7684\u6240\u6709\u679a\u4e3e\u3002 w(pn)\u4ee3\u8868pn\u4f4d\u7f6e\u7684\u5377\u79ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":373,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[14],"_links":{"self":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/371"}],"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=371"}],"version-history":[{"count":0,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/media\/373"}],"wp:attachment":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}