{"id":8416,"date":"2022-09-27T20:30:55","date_gmt":"2022-09-27T12:30:55","guid":{"rendered":"http:\/\/139.9.1.231\/?p=8416"},"modified":"2022-09-27T21:34:26","modified_gmt":"2022-09-27T13:34:26","slug":"torch-grid_sample","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/09\/27\/torch-grid_sample\/","title":{"rendered":"torch  grid_sample() \u51fd\u6570"},"content":{"rendered":"\n<p>    grid_sample\u5e95\u5c42\u662f\u5e94\u7528\u53cc\u7ebf\u6027\u63d2\u503c\uff0c\u628a\u8f93\u5165\u7684tensor\u8f6c\u6362\u4e3a\u6307\u5b9a\u5927\u5c0f\u3002\u90a3\u5b83\u548cinterpolate\u6709\u5565\u533a\u522b\u5462\uff1f<br>    interpolate\u662f\u89c4\u5219\u91c7\u6837\uff08uniform)\uff0c\u4f46\u662fgrid_sample\u7684\u8f6c\u6362\u65b9\u5f0f\uff0c\u5185\u90e8\u91c7\u70b9\u7684\u65b9\u5f0f\u5e76\u4e0d\u662f\u89c4\u5219\u7684\uff0c\u662f\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u7684\u65b9\u5f0f\u3002\u53ef\u4ee5\u8ba4\u4e3a\u91c7\u6837\u70b9\u6839\u636e grid \u77e9\u9635\u6765\u51b3\u5b9a\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">Pytorch\u4e2dgrid_sample\u51fd\u6570\u7684\u63a5\u53e3\u58f0\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>torch.nn.functional.grid_sample(input, grid, mode='bilinear', padding_mode='zeros', align_corners=None)<\/code><\/pre>\n\n\n\n<p>\u5728\u5b98\u65b9\u6587\u6863\u91cc\u9762\u5173\u4e8e\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u662f\u8fd9\u6837\u63cf\u8ff0\u7684\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Given an input and a flow-field grid, computes the output using input values and pixel locations from grid.<\/p><\/blockquote>\n\n\n\n<p>    \u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff0c\u63d0\u4f9b\u4e00\u4e2ainput\u7684Tensor\u4ee5\u53ca\u4e00\u4e2a\u5bf9\u5e94\u7684flow-field\u7f51\u683c(\u6bd4\u5982\u5149\u6d41\uff0c\u4f53\u7d20\u6d41\u7b49)\uff0c\u7136\u540e\u6839\u636e\u7f51\u683c\uff08grid\uff09\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e\u63d0\u4f9b\u7684\u5750\u6807\u4fe1\u606f(\u8fd9\u91cc\u6307input\u4e2dpixel\u7684\u5750\u6807)\uff0c\u5c06input\u4e2d\u5bf9\u5e94\u4f4d\u7f6e\u7684\u50cf\u7d20\u503c\u586b\u5145\u5230grid\u6307\u5b9a\u7684\u4f4d\u7f6e\uff0c\u5f97\u5230\u6700\u7ec8\u7684\u8f93\u51fa\u3002<\/p>\n\n\n\n<p>    \u5173\u4e8einput\u3001grid\u4ee5\u53caoutput\u7684\u5c3a\u5bf8\u5982\u4e0b\u6240\u793a\uff1a\uff08input\u4e5f\u53ef\u4ee5\u662f5D\u7684Tensor\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u8003\u86514D\u7684\u60c5\u51b5\uff09 \u6ce8\u610foutput\u7684\u5c3a\u5bf8\u53ef\u4ee5\u5927\u4e8einput\uff0c\u6240\u4ee5 grid_sample \u53ef\u4ee5\u7528\u6765\u4e0a\u91c7\u6837 <\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>input:(N,C,Hin,Win)<br>grid:(N,Hout,Wout\u200b,2)<br>output:(N,C,Hout,Wout\u200b)<\/p><\/blockquote>\n\n\n\n<p>          \u8fd9\u91cc\u7684input\u548coutput\u5c31\u662f\u8f93\u5165\u7684\u56fe\u7247\uff0c\u6216\u8005\u662f\u7f51\u7edc\u4e2d\u7684feature map\u3002\u5173\u952e\u7684\u5904\u7406\u8fc7\u7a0b\u5728\u4e8egrid\uff0cgrid\u7684\u6700\u540e\u4e00\u7ef4\u7684\u5927\u5c0f\u4e3a2\uff0c\u5373\u8868\u793ainput\u4e2dpixel\u7684\u4f4d\u7f6e\u4fe1\u606f&nbsp;(x,y)&nbsp;,\u8fd9\u91cc\u4e00\u822c\u4f1a\u5c06x\u548cy\u7684\u53d6\u503c\u8303\u56f4\u5f52\u4e00\u5316\u5230&nbsp;[\u22121,1]&nbsp;\u4e4b\u95f4\uff0c&nbsp;(\u22121,\u22121)&nbsp;\u8868\u793ainput\u5de6\u4e0a\u89d2\u7684\u50cf\u7d20\u7684\u5750\u6807\uff0c(1,1)&nbsp;\u8868\u793ainput\u53f3\u4e0b\u89d2\u7684\u50cf\u7d20\u7684\u5750\u6807\uff0c\u5bf9\u4e8e\u8d85\u51fa\u8fd9\u4e2a\u8303\u56f4\u7684\u5750\u6807(x,y)\uff0c\u51fd\u6570\u5c06\u4f1a\u6839\u636e\u53c2\u6570<em>padding_mode<\/em>\u7684\u8bbe\u5b9a\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<ul><li><em>padding_mode=&#8217;zeros&#8217;<\/em>:\u5bf9\u4e8e\u8d8a\u754c\u7684\u4f4d\u7f6e\u5728\u7f51\u683c\u4e2d\u91c7\u7528pixel value=0\u8fdb\u884c\u586b\u5145\u3002<\/li><li><em>padding_mode=&#8217;border&#8217;<\/em>:\u5bf9\u4e8e\u8d8a\u754c\u7684\u4f4d\u7f6e\u5728\u7f51\u683c\u4e2d\u91c7\u7528\u8fb9\u754c\u7684pixel value\u8fdb\u884c\u586b\u5145\u3002<\/li><li><em>padding_mode=&#8217;reflection&#8217;<\/em>:\u5bf9\u4e8e\u8d8a\u754c\u7684\u4f4d\u7f6e\u5728\u7f51\u683c\u4e2d\u91c7\u7528\u5173\u4e8e\u8fb9\u754c\u7684\u5bf9\u79f0\u503c\u8fdb\u884c\u586b\u5145\u3002<\/li><\/ul>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">        \u5bf9\u4e8emode=&#8217;bilinear&#8217;\u53c2\u6570\uff0c\u5219\u5b9a\u4e49\u4e86\u5728input\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u7684pixel value\u4e2d\u8fdb\u884c\u63d2\u503c\u7684\u65b9\u6cd5\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u63d2\u503c\u5462\uff1f\u56e0\u4e3a\u524d\u9762\u6211\u4eec\u8bf4\u4e86\uff0cgrid\u4e2d\u8868\u793a\u7684\u4f4d\u7f6e\u4fe1\u606fx\u548cy\u7684\u53d6\u503c\u8303\u56f4\u5728&nbsp;[\u22121,1]&nbsp;\u4e4b\u95f4\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u6211\u4eec\u8981\u6839\u636e\u4e00\u4e2a\u6d6e\u70b9\u578b\u7684\u5750\u6807\u503c\u5728input\u4e2d\u5bf9pixel value\u8fdb\u884c\u91c7\u6837\uff0cmode\u6709<code>'bilinear'<\/code>&nbsp;|&nbsp;<code>'nearest'<\/code>&nbsp;|&nbsp;<code>'bicubic'\uff08\u53cc\u4e09\u6b21\u63d2\u503c\uff09<\/code>\u4e09\u79cd\u6a21\u5f0f\u3002 nearest\u5c31\u662f\u76f4\u63a5\u91c7\u7528\u4e0e&nbsp;(x,y)&nbsp;\u8ddd\u79bb\u6700\u8fd1\u5904\u7684\u50cf\u7d20\u503c\u6765\u586b\u5145grid\uff0c\u800cbilinear\u5219\u662f\u91c7\u7528<strong><em>\u53cc\u7ebf\u6027\u63d2\u503c<\/em><\/strong>\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u586b\u5145\uff0cmode=&#8217;bicubic&#8217;\u4ec5\u652f\u6301\u56db\u7ef4\u8f93\u5165\uff0c\u603b\u4e4b\u5176\u4e0enearest\u7684\u533a\u522b\u5c31\u662fnearest\u53ea\u8003\u8651\u6700\u8fd1\u70b9\u7684pixel value\uff0c\u800cbilinear\u5219\u91c7\u7528(x,y)\u5468\u56f4\u7684\u56db\u4e2apixel value\u8fdb\u884c\u52a0\u6743\u5e73\u5747\u503c\u6765\u586b\u5145grid\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>\u53cc\u7ebf\u6027\u63d2\u503c\uff1a<\/strong>\u53cc\u7ebf\u6027\u63d2\u503c\u662f\u7528\u539f\u56fe\u50cf\u4e2d4(2*2)\u4e2a\u70b9\u8ba1\u7b97\u65b0\u56fe\u50cf\u4e2d1\u4e2a\u70b9<\/p>\n\n\n\n<p class=\"has-light-blue-background-color has-background\"><strong>\u53cc\u4e09\u6b21\u63d2\u503c(Bicubic interpolation)\uff1a\u53cc\u4e09\u6b21\u63d2\u503c\u662f\u7528\u539f\u56fe\u50cf\u4e2d16(4*4)\u4e2a\u70b9\u8ba1\u7b97\u65b0\u56fe\u50cf\u4e2d1\u4e2a\u70b9\uff0c\u6548\u679c\u6bd4\u8f83\u597d\uff0c\u4f46\u662f\u8ba1\u7b97\u4ee3\u4ef7\u8fc7\u5927<\/strong>\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u8bb2\u5230\u53cc\u7ebf\u6027\u63d2\u503c\u4f1a\u5bf9&nbsp;(x,y)&nbsp;\u5468\u56f4\u7684\u56db\u4e2apixel value\u8fdb\u884c\u52a0\u6743\u5e73\u5747\uff0c\u90a3\u4e48\u6bcf\u4e2a\u4f4d\u7f6e\u7684\u6743\u91cd\u662f\u591a\u5c11\u5462\uff1f\u53ef\u4ee5\u7b80\u5355\u53c2\u8003\u4e0b\u56fe\u4e2d\u53cc\u7ebf\u6027\u63d2\u503c\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/pic4.zhimg.com\/80\/v2-1efda21e5b8202be3ef1e0bb2d7fa67b_720w.jpg\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p>\u5176\u53cc\u7ebf\u6027\u63d2\u503c\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic1.zhimg.com\/v2-57c8110d7a2a68c70fc90f2ce4e66448_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u91c7\u7528\u4e0b\u56fe\u6211\u4eec\u53ef\u4ee5\u5bf9\u53cc\u7ebf\u6027\u63d2\u503c\u6709\u4e2a\u66f4\u4e3a\u76f4\u89c2\u7684\u8ba4\u8bc6\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/pic1.zhimg.com\/v2-0e62cad5efc53b236f8faf0e353240d0_r.jpg\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p>\u4ece\u4e0a\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230\u53cc\u7ebf\u6027\u63d2\u503c\u5c31\u662f\u9996\u5148\u5728\u5e73\u9762&nbsp;zoy&nbsp;\u5185\uff0c\u5bf9&nbsp;f(x0,y0)&nbsp;\u548c&nbsp;f(x0,y1)&nbsp;\u8fdb\u884c\u63d2\u503c\u5f97\u5230&nbsp;z1&nbsp;\uff0c\u5bf9&nbsp;f(x1,y0)&nbsp;\u548c&nbsp;f(x1,y1)&nbsp;\u8fdb\u884c\u63d2\u503c\u5f97\u5230&nbsp;z2&nbsp;\uff0c\u968f\u540e\u5728\u5e73\u9762&nbsp;zox&nbsp;\u5185\u8fdb\u884c\u63d2\u503c\u5f97\u5230\u6700\u7ec8\u7684&nbsp;z&nbsp;\u70b9\u7684\u503c\u5c31\u662f\u6700\u7ec8\u6240\u6c42\u7684\u7ed3\u679c\uff0c\u8fd9\u91cc\u7684\u5e73\u9762\u5185\u63d2\u503c\u5176\u5b9e\u5c31\u662f\u91c7\u7528\u6211\u4eec\u9ad8\u4e2d\u5b66\u7684\uff0c\u76f4\u7ebf\u7684\u4e24\u70b9\u5f0f\u6c42\u51fa\u76f4\u7ebf\u8868\u8fbe\u662f\uff0c\u518d\u5e26\u5165\u81ea\u53d8\u91cf\uff08x\u6216y\uff09\u7684\u5750\u6807\u5f97\u5230\u63d2\u503c\u7684\u7ed3\u679c\u3002\u8054\u7acb\u4e24\u6b21\u76f4\u7ebf\u7684\u4e24\u70b9\u5f0f\u5c31\u80fd\u5f97\u5230\u53cc\u7ebf\u6027\u63d2\u503c\u7684\u7ed3\u679c\uff0c\u8bf4\u5230\u8fd9\u91cc\u201c\u53cc\u7ebf\u6027\u201d\u4e5f\u987e\u540d\u601d\u4e49\u4e86\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7ed9\u51fa\u6b63\u5f0f\u7684\u63a8\u5bfc\uff1a<\/p>\n\n\n\n<p>\u5df2\u77e5\u56db\u70b9\u7684\u5750\u6807\u5982\u4e0b\u6240\u793a:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/pic2.zhimg.com\/80\/v2-bdc18afa87a5e3531c1bf4154b62ff21_720w.jpg\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p>Q11=(x1,y1,f(x1,y1)), Q21=(x2,y1,f(x2,y2)), Q12=(x1,y2,f(x3,y3)), Q22=(x2,y2,f(x4,y4))<\/p>\n\n\n\n<p>\u5176\u4e2d\u6709z=f(x,y):<\/p>\n\n\n\n<p>\u5148\u5728x\u65b9\u5411\u4e0a\u8fdb\u884c\u63d2\u503c\u6709\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"783\" height=\"507\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-273.png\" alt=\"\" class=\"wp-image-8440\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-273.png 783w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-273-300x194.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-273-768x497.png 768w\" sizes=\"(max-width: 783px) 100vw, 783px\" \/><\/figure>\n\n\n\n<p>\u4ee5\u4e0a\u5f0f\u5b50\u4fbf\u662f\u6700\u7ec8\u53cc\u7ebf\u6027\u63d2\u503c\u7684\u6700\u7ec8\u8868\u8fbe\u5f0f\uff0c\u7531\u4e8e4\u4e2a\u70b9\u7684\u6743\u91cd\u90e8\u5206\u4e2d\u5206\u6bcd\u662f\u76f8\u540c\u7684\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff0c\u73b0\u5728\u518d\u56de\u53bb\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e0d\u662f\u5c31\u4e00\u76ee\u4e86\u7136\u4e86\u3002<\/p>\n\n\n\n<p>\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\nfrom torch.nn import functional as F\n\ninp = torch.ones(1, 1, 4, 4)\nprint(inp)\n# \u76ee\u7684\u662f\u5f97\u5230\u4e00\u4e2a \u957f\u5bbd\u4e3a20\u7684tensor\nout_h = 20\nout_w = 20\n # grid\u7684\u751f\u6210\u65b9\u5f0f\u7b49\u4ef7\u4e8e\u7528mesh_grid\nnew_h = torch.linspace(-1, 1, out_h).view(-1, 1).repeat(1, out_w)\nnew_w = torch.linspace(-1, 1, out_w).repeat(out_h, 1)\ngrid = torch.cat((new_h.unsqueeze(2), new_w.unsqueeze(2)), dim=2)\ngrid = grid.unsqueeze(0) #\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5f20\u91cf\uff0c\u5bf9\u8f93\u5165\u7684\u65e2\u5b9a\u4f4d\u7f6e\u63d2\u5165\u7ef4\u5ea6 1\nprint(grid.shape)\noutp = F.grid_sample(inp, grid=grid, mode='bilinear')\nprint(outp.shape)  #torch.Size(&#91;1, 1, 20, 20])<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u5c0f\u4e3a4&#215;4\u7684tensor\u00a0\u8f6c\u6362\u4e3a\u4e86\u4e00\u4e2a20&#215;20\u7684\u3002grid\u7684\u5927\u5c0f\u6307\u5b9a\u4e86\u8f93\u51fa\u5927\u5c0f\uff0c\u6bcf\u4e2agrid\u7684\u4f4d\u7f6e\u662f\u4e00\u4e2a\uff08x,y\uff09\u5750\u6807\uff0c\u5176\u503c\u6765\u81ea\u4e8e\uff1a\u8f93\u5165input\u7684\uff08x\uff0cy\uff09\u4e2d \u7684\u56db\u90bb\u57df\u63d2\u503c\u5f97\u5230\u7684\u3002<\/p>\n\n\n\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/20200724133948286.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0OTE0NTUx,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" class=\"\"><br>   \u56fe\u7247\u6765\u81ea\u4e8eSFnet\uff08eccv2020\uff09\u3002flow field\u662fgrid\uff0c low_resolution\u662finput\uff0c high resolution\u662foutput\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>grid_sample\u5e95\u5c42\u662f\u5e94\u7528\u53cc\u7ebf\u6027\u63d2\u503c\uff0c\u628a\u8f93\u5165\u7684tensor\u8f6c\u6362\u4e3a\u6307\u5b9a\u5927\u5c0f\u3002\u90a3\u5b83\u548cinterpolate\u6709 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/09\/27\/torch-grid_sample\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">torch  grid_sample() \u51fd\u6570<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8416"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=8416"}],"version-history":[{"count":48,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8416\/revisions"}],"predecessor-version":[{"id":8465,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8416\/revisions\/8465"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=8416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=8416"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=8416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}