[{"data":1,"prerenderedAt":1981},["ShallowReactive",2],{"content-query-myfLMB4SV7":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":1975,"_id":1976,"_source":1977,"_file":1978,"_stem":1979,"_extension":1980},"/technology-blogs/zh/1674","zh",false,"","MindSpore易点通·精讲系列–网络构建之Conv2d算子","通过几种不同卷积模式（普通卷积、深度卷积、空洞卷积）的具体实现，以及数据格式、填充方式、输出维度多个角度来深入讲解Conv2d算子的具体应用。","2022-08-01","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/08/05/f837ea8edacf4f319d453e70d9916b6a.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":1954},"root",[17,25,31,36,41,61,66,114,128,133,141,146,214,225,246,251,256,277,282,289,301,306,315,320,331,372,385,393,398,410,418,429,434,439,475,502,506,510,527,531,538,543,551,555,565,616,627,635,639,650,658,666,671,682,687,694,699,704,711,775,780,800,808,812,864,875,883,887,907,915,926,946,979,1002,1007,1033,1041,1052,1060,1065,1070,1078,1091,1096,1110,1115,1137,1142,1186,1197,1243,1255,1267,1279,1284,1292,1363,1374,1384,1396,1401,1412,1430,1449,1475,1480,1485,1511,1516,1528,1533,1538,1542,1549,1581,1589,1593,1633,1644,1652,1656,1663,1674,1679,1692,1697,1705,1709,1757,1768,1776,1780,1785,1793,1804,1816,1821,1828,1833,1842,1868,1877,1949],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore易点通精讲系列网络构建之conv2d算子",[23],{"type":24,"value":8},"text",{"type":18,"tag":19,"props":26,"children":28},{"id":27},"dive-into-mindspore-conv2d-operator-for-network-construction",[29],{"type":24,"value":30},"Dive Into MindSpore – Conv2d Operator For Network Construction",{"type":18,"tag":32,"props":33,"children":34},"p",{},[35],{"type":24,"value":8},{"type":18,"tag":32,"props":37,"children":38},{},[39],{"type":24,"value":40},"本文开发环境",{"type":18,"tag":42,"props":43,"children":44},"ul",{},[45,51,56],{"type":18,"tag":46,"props":47,"children":48},"li",{},[49],{"type":24,"value":50},"Ubuntu 20.04",{"type":18,"tag":46,"props":52,"children":53},{},[54],{"type":24,"value":55},"Python 3.8",{"type":18,"tag":46,"props":57,"children":58},{},[59],{"type":24,"value":60},"MindSpore 1.7.0",{"type":18,"tag":32,"props":62,"children":63},{},[64],{"type":24,"value":65},"本文内容摘要",{"type":18,"tag":42,"props":67,"children":68},{},[69,74,79,84,89,94,99,104,109],{"type":18,"tag":46,"props":70,"children":71},{},[72],{"type":24,"value":73},"先看文档",{"type":18,"tag":46,"props":75,"children":76},{},[77],{"type":24,"value":78},"普通卷积",{"type":18,"tag":46,"props":80,"children":81},{},[82],{"type":24,"value":83},"深度卷积",{"type":18,"tag":46,"props":85,"children":86},{},[87],{"type":24,"value":88},"空洞卷积",{"type":18,"tag":46,"props":90,"children":91},{},[92],{"type":24,"value":93},"数据格式",{"type":18,"tag":46,"props":95,"children":96},{},[97],{"type":24,"value":98},"填充方式",{"type":18,"tag":46,"props":100,"children":101},{},[102],{"type":24,"value":103},"输出维度",{"type":18,"tag":46,"props":105,"children":106},{},[107],{"type":24,"value":108},"本文总结",{"type":18,"tag":46,"props":110,"children":111},{},[112],{"type":24,"value":113},"本文参考",{"type":18,"tag":115,"props":116,"children":118},"h2",{"id":117},"_1-先看文档",[119,126],{"type":18,"tag":120,"props":121,"children":125},"a",{"href":122,"rel":123},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#1.-%E5%85%88%E7%9C%8B%E6%96%87%E6%A1%A3",[124],"nofollow",[],{"type":24,"value":127},"1. 先看文档",{"type":18,"tag":32,"props":129,"children":130},{},[131],{"type":24,"value":132},"老传统，先看官方文档。",{"type":18,"tag":32,"props":134,"children":135},{},[136],{"type":18,"tag":137,"props":138,"children":140},"img",{"alt":7,"src":139},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20228/1/1659339781533424313.png",[],{"type":18,"tag":32,"props":142,"children":143},{},[144],{"type":24,"value":145},"参数解读：",{"type":18,"tag":42,"props":147,"children":148},{},[149,154,159,164,169,174,179,184,189,194],{"type":18,"tag":46,"props":150,"children":151},{},[152],{"type":24,"value":153},"in_channels – 输入通道数",{"type":18,"tag":46,"props":155,"children":156},{},[157],{"type":24,"value":158},"out_channels – 输出通道数",{"type":18,"tag":46,"props":160,"children":161},{},[162],{"type":24,"value":163},"kernel_size – 二维卷积核的高度和宽度。值应该为整型（代表高度和宽度均为该值）或两个整型的tuple（分别代表高度和宽度值）",{"type":18,"tag":46,"props":165,"children":166},{},[167],{"type":24,"value":168},"stride – 二维卷积的移动步长",{"type":18,"tag":46,"props":170,"children":171},{},[172],{"type":24,"value":173},"pad_mode – 填充模式",{"type":18,"tag":46,"props":175,"children":176},{},[177],{"type":24,"value":178},"padding – 填充数量",{"type":18,"tag":46,"props":180,"children":181},{},[182],{"type":24,"value":183},"dilation – 二维卷积核膨胀尺寸，输入值同kernel_size。空洞卷积参数",{"type":18,"tag":46,"props":185,"children":186},{},[187],{"type":24,"value":188},"group – 将过滤器拆分为组。深度卷积参数",{"type":18,"tag":46,"props":190,"children":191},{},[192],{"type":24,"value":193},"has_bias – 是否添加偏置",{"type":18,"tag":46,"props":195,"children":196},{},[197,199,206,208],{"type":24,"value":198},"data_format – 输入数据的数据格式，",{"type":18,"tag":200,"props":201,"children":203},"code",{"className":202},[],[204],{"type":24,"value":205},"NCHW",{"type":24,"value":207},"或",{"type":18,"tag":200,"props":209,"children":211},{"className":210},[],[212],{"type":24,"value":213},"NHWC",{"type":18,"tag":115,"props":215,"children":217},{"id":216},"_2-普通卷积",[218,223],{"type":18,"tag":120,"props":219,"children":222},{"href":220,"rel":221},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#2.-%E6%99%AE%E9%80%9A%E5%8D%B7%E7%A7%AF",[124],[],{"type":24,"value":224},"2. 普通卷积",{"type":18,"tag":32,"props":226,"children":227},{},[228,230,236,238,244],{"type":24,"value":229},"普通卷积，又可以称为常规卷积。由于是在深度学习相关课程中最先接触到的",{"type":18,"tag":200,"props":231,"children":233},{"className":232},[],[234],{"type":24,"value":235},"CNN",{"type":24,"value":237},"卷积方式，本文不再对其原理展开介绍。下面通过一个实例来介绍",{"type":18,"tag":200,"props":239,"children":241},{"className":240},[],[242],{"type":24,"value":243},"MindSpore",{"type":24,"value":245},"中的用法。",{"type":18,"tag":32,"props":247,"children":248},{},[249],{"type":24,"value":250},"例如：",{"type":18,"tag":32,"props":252,"children":253},{},[254],{"type":24,"value":255},"对于二维的8×8原始图像，图像格式为RGB（即通道数为3），可以认为这是一个3维图片，数据维度为 3×8×8（NCHW）或8×8×3（NHWC）。",{"type":18,"tag":32,"props":257,"children":258},{},[259,261,267,269,275],{"type":24,"value":260},"假设我们对上述图片进行普通卷积操作，卷积核大小为3×3，步长为1，卷积后的输出通道数为4，",{"type":18,"tag":200,"props":262,"children":264},{"className":263},[],[265],{"type":24,"value":266},"padding",{"type":24,"value":268},"方式为",{"type":18,"tag":200,"props":270,"children":272},{"className":271},[],[273],{"type":24,"value":274},"same",{"type":24,"value":276},"，即输入和输出的高和宽一致。",{"type":18,"tag":32,"props":278,"children":279},{},[280],{"type":24,"value":281},"其示意图如下所示：",{"type":18,"tag":32,"props":283,"children":284},{},[285],{"type":18,"tag":137,"props":286,"children":288},{"alt":7,"src":287},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20228/1/1659339802936581433.png",[],{"type":18,"tag":32,"props":290,"children":291},{},[292,294,299],{"type":24,"value":293},"如何用",{"type":18,"tag":200,"props":295,"children":297},{"className":296},[],[298],{"type":24,"value":243},{"type":24,"value":300},"来定义这样的普通卷积呢，示例代码如下：",{"type":18,"tag":32,"props":302,"children":303},{},[304],{"type":24,"value":305},"这里的批数为2",{"type":18,"tag":307,"props":308,"children":310},"pre",{"code":309},"import numpy as np\n\nfrom mindspore import nn\nfrom mindspore.common import dtype as mstype\nfrom mindspore.common import Tensor\n\n\ndef common_conv_demo():\n    img_data = np.random.rand(2, 3, 8, 8)\n\n    ms_in = Tensor(img_data, dtype=mstype.float32)\n    conv_op = nn.Conv2d(3, 4, 3, 1)\n    ms_out = conv_op(ms_in)\n\n    print(\"in shape: {}\".format(ms_in.shape), flush=True)\n    print(\"out shape: {}\".format(ms_out.shape), flush=True)\n\n\ndef main():\n    common_conv_demo()\n\n\nif __name__ == \"__main__\":\n    main()\n",[311],{"type":18,"tag":200,"props":312,"children":313},{"__ignoreMap":7},[314],{"type":24,"value":309},{"type":18,"tag":32,"props":316,"children":317},{},[318],{"type":24,"value":319},"代码解读：",{"type":18,"tag":32,"props":321,"children":322},{},[323,325],{"type":24,"value":324},"核心代码为",{"type":18,"tag":200,"props":326,"children":328},{"className":327},[],[329],{"type":24,"value":330},"nn.Conv2d(3, 4, 3, 1)",{"type":18,"tag":42,"props":332,"children":333},{},[334,339,344,349,354],{"type":18,"tag":46,"props":335,"children":336},{},[337],{"type":24,"value":338},"参数数字3表示输入通道",{"type":18,"tag":46,"props":340,"children":341},{},[342],{"type":24,"value":343},"参数数字4表示输出通道。",{"type":18,"tag":46,"props":345,"children":346},{},[347],{"type":24,"value":348},"参数数字3表示卷积核大小，这里因为高&宽的卷积值相等，所以使用整型表示。",{"type":18,"tag":46,"props":350,"children":351},{},[352],{"type":24,"value":353},"参数数字1表示卷积移动步长。",{"type":18,"tag":46,"props":355,"children":356},{},[357,363,365,370],{"type":18,"tag":200,"props":358,"children":360},{"className":359},[],[361],{"type":24,"value":362},"nn.Conv2d",{"type":24,"value":364},"默认卷积方式为",{"type":18,"tag":200,"props":366,"children":368},{"className":367},[],[369],{"type":24,"value":274},{"type":24,"value":371},"，故没有在这里的参数中体现。",{"type":18,"tag":32,"props":373,"children":374},{},[375,377,383],{"type":24,"value":376},"将上述代码保存到",{"type":18,"tag":200,"props":378,"children":380},{"className":379},[],[381],{"type":24,"value":382},"common_conv2d.py",{"type":24,"value":384},"文件，使用如下命令运行：",{"type":18,"tag":307,"props":386,"children":388},{"code":387},"python3 common_conv2d.py\n",[389],{"type":18,"tag":200,"props":390,"children":391},{"__ignoreMap":7},[392],{"type":24,"value":387},{"type":18,"tag":32,"props":394,"children":395},{},[396],{"type":24,"value":397},"输出内容为：",{"type":18,"tag":32,"props":399,"children":400},{},[401,403,408],{"type":24,"value":402},"可以看到输出的通道为4，因为填充方式为",{"type":18,"tag":200,"props":404,"children":406},{"className":405},[],[407],{"type":24,"value":274},{"type":24,"value":409},"，输出的高度和宽度与输入数据相同。",{"type":18,"tag":307,"props":411,"children":413},{"code":412},"in shape: (2, 3, 8, 8)\nout shape: (2, 4, 8, 8)\n",[414],{"type":18,"tag":200,"props":415,"children":416},{"__ignoreMap":7},[417],{"type":24,"value":412},{"type":18,"tag":115,"props":419,"children":421},{"id":420},"_3-深度卷积",[422,427],{"type":18,"tag":120,"props":423,"children":426},{"href":424,"rel":425},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#3.-%E6%B7%B1%E5%BA%A6%E5%8D%B7%E7%A7%AF",[124],[],{"type":24,"value":428},"3. 深度卷积",{"type":18,"tag":32,"props":430,"children":431},{},[432],{"type":24,"value":433},"深度卷积（Depthwise Convolution）的一个卷积核负责一个通道，一个通道只被一个卷积核卷积，可以看出其卷积方式与普通卷积明显不同。深度卷积一般与逐点卷积（Pointwise Convolution）结合，组成深度可分离卷积（Depthwise Separable Convolution），当然也可以单独使用，比如，经典的MobileNet网络就用到了深度可分离卷积。",{"type":18,"tag":32,"props":435,"children":436},{},[437],{"type":24,"value":438},"那么在MindSpore中如何实现深度卷积呢，我们先从文档说起。",{"type":18,"tag":42,"props":440,"children":441},{},[442,466],{"type":18,"tag":46,"props":443,"children":444},{},[445,451,453,464],{"type":18,"tag":446,"props":447,"children":448},"strong",{},[449],{"type":24,"value":450},"group",{"type":24,"value":452}," (",{"type":18,"tag":120,"props":454,"children":457},{"href":455,"rel":456},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#",[124],[458],{"type":18,"tag":459,"props":460,"children":461},"em",{},[462],{"type":24,"value":463},"int",{"type":24,"value":465},") – Splits filter into groups, in_channels and out_channels must be divisible by group. If the group is equal to in_channels and out_channels, this 2D convolution layer also can be called 2D depthwise convolution layer. Default: 1.",{"type":18,"tag":46,"props":467,"children":468},{},[469,473],{"type":18,"tag":446,"props":470,"children":471},{},[472],{"type":24,"value":450},{"type":24,"value":474}," (int) – 将过滤器拆分为组， in_channels 和 out_channels 必须可被 group 整除。如果组数等于 in_channels 和 out_channels ，这个二维卷积层也被称为二维深度卷积层。默认值：1.",{"type":18,"tag":32,"props":476,"children":477},{},[478,480,486,488,494,495,500],{"type":24,"value":479},"从文档可以看出，当",{"type":18,"tag":200,"props":481,"children":483},{"className":482},[],[484],{"type":24,"value":485},"in_channels",{"type":24,"value":487},"、",{"type":18,"tag":200,"props":489,"children":491},{"className":490},[],[492],{"type":24,"value":493},"out_channels",{"type":24,"value":487},{"type":18,"tag":200,"props":496,"children":498},{"className":497},[],[499],{"type":24,"value":450},{"type":24,"value":501},"三个参数的值相等时，可以认为即为2D的深度卷积。下面通过一个案例来进一步讲解。",{"type":18,"tag":32,"props":503,"children":504},{},[505],{"type":24,"value":250},{"type":18,"tag":32,"props":507,"children":508},{},[509],{"type":24,"value":255},{"type":18,"tag":32,"props":511,"children":512},{},[513,515,520,521,526],{"type":24,"value":514},"假设我们对上述图片进行深度卷积操作，卷积核大小为3×3，步长为1，卷积后的输出通道数为3（与输入通道一致），",{"type":18,"tag":200,"props":516,"children":518},{"className":517},[],[519],{"type":24,"value":266},{"type":24,"value":268},{"type":18,"tag":200,"props":522,"children":524},{"className":523},[],[525],{"type":24,"value":274},{"type":24,"value":276},{"type":18,"tag":32,"props":528,"children":529},{},[530],{"type":24,"value":281},{"type":18,"tag":32,"props":532,"children":533},{},[534],{"type":18,"tag":137,"props":535,"children":537},{"alt":7,"src":536},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20228/1/1659339830503286857.png",[],{"type":18,"tag":32,"props":539,"children":540},{},[541],{"type":24,"value":542},"MindSpore示例代码如下：",{"type":18,"tag":307,"props":544,"children":546},{"code":545},"import numpy as np\n\nfrom mindspore import nn\nfrom mindspore.common import dtype as mstype\nfrom mindspore.common import Tensor\n\n\ndef depthwise_conv_demo():\n    img_data = np.random.rand(2, 3, 8, 8)\n\n    ms_in = Tensor(img_data, dtype=mstype.float32)\n    conv_op = nn.Conv2d(3, 3, 3, 1, group=3)\n    ms_out = conv_op(ms_in)\n\n    print(\"in shape: {}\".format(ms_in.shape), flush=True)\n    print(\"out shape: {}\".format(ms_out.shape), flush=True)\n\n\ndef main():\n    depthwise_conv_demo()\n\n\nif __name__ == \"__main__\":\n    main()\n",[547],{"type":18,"tag":200,"props":548,"children":549},{"__ignoreMap":7},[550],{"type":24,"value":545},{"type":18,"tag":32,"props":552,"children":553},{},[554],{"type":24,"value":319},{"type":18,"tag":32,"props":556,"children":557},{},[558,559],{"type":24,"value":324},{"type":18,"tag":200,"props":560,"children":562},{"className":561},[],[563],{"type":24,"value":564},"nn.Conv2d(3, 3, 3, 1, group=3)",{"type":18,"tag":42,"props":566,"children":567},{},[568,573,577,581,585,600],{"type":18,"tag":46,"props":569,"children":570},{},[571],{"type":24,"value":572},"参数数字3表示输入通道。",{"type":18,"tag":46,"props":574,"children":575},{},[576],{"type":24,"value":343},{"type":18,"tag":46,"props":578,"children":579},{},[580],{"type":24,"value":348},{"type":18,"tag":46,"props":582,"children":583},{},[584],{"type":24,"value":353},{"type":18,"tag":46,"props":586,"children":587},{},[588,593,594,599],{"type":18,"tag":200,"props":589,"children":591},{"className":590},[],[592],{"type":24,"value":362},{"type":24,"value":364},{"type":18,"tag":200,"props":595,"children":597},{"className":596},[],[598],{"type":24,"value":274},{"type":24,"value":371},{"type":18,"tag":46,"props":601,"children":602},{},[603],{"type":18,"tag":446,"props":604,"children":605},{},[606,608,614],{"type":24,"value":607},"参数",{"type":18,"tag":200,"props":609,"children":611},{"className":610},[],[612],{"type":24,"value":613},"gropu=3",{"type":24,"value":615},"与前面的输入通道3和输出通道3一致，是这里实现深度卷积的关键参数。",{"type":18,"tag":32,"props":617,"children":618},{},[619,620,626],{"type":24,"value":376},{"type":18,"tag":200,"props":621,"children":623},{"className":622},[],[624],{"type":24,"value":625},"depthwise_conv2d.py",{"type":24,"value":384},{"type":18,"tag":307,"props":628,"children":630},{"code":629},"python3 depthwise_conv2d.py\n",[631],{"type":18,"tag":200,"props":632,"children":633},{"__ignoreMap":7},[634],{"type":24,"value":629},{"type":18,"tag":32,"props":636,"children":637},{},[638],{"type":24,"value":397},{"type":18,"tag":32,"props":640,"children":641},{},[642,644,649],{"type":24,"value":643},"可以看到输出的通道为3（与输入数据通道数一致），因为填充方式为",{"type":18,"tag":200,"props":645,"children":647},{"className":646},[],[648],{"type":24,"value":274},{"type":24,"value":409},{"type":18,"tag":307,"props":651,"children":653},{"code":652},"in shape: (2, 3, 8, 8)\nout shape: (2, 3, 8, 8)\n",[654],{"type":18,"tag":200,"props":655,"children":656},{"__ignoreMap":7},[657],{"type":24,"value":652},{"type":18,"tag":32,"props":659,"children":660},{},[661],{"type":18,"tag":446,"props":662,"children":663},{},[664],{"type":24,"value":665},"一点补充",{"type":18,"tag":32,"props":667,"children":668},{},[669],{"type":24,"value":670},"细心的读者可能会问，逐点卷积如何实现呢？这里逐点卷积可以看成普通卷积的特例，即卷积核为1×1的普通卷积（其他参数视具体而定），再参考第2节的内容，就可以很容易的实现出来了。",{"type":18,"tag":115,"props":672,"children":674},{"id":673},"_4-空洞卷积",[675,680],{"type":18,"tag":120,"props":676,"children":679},{"href":677,"rel":678},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#4.-%E7%A9%BA%E6%B4%9E%E5%8D%B7%E7%A7%AF",[124],[],{"type":24,"value":681},"4. 空洞卷积",{"type":18,"tag":32,"props":683,"children":684},{},[685],{"type":24,"value":686},"空洞卷积（Dilated Convolution），又称扩张卷积、膨胀卷积，是在标准的卷积核中注入空洞，以此来增加模型的感受野（reception field）。相比原来的正常卷积操作，扩张卷积多了一个参数： dilation rate，指的是卷积核的点的间隔数量，比如常规的卷积操作dilatation rate为1。",{"type":18,"tag":32,"props":688,"children":689},{},[690],{"type":18,"tag":137,"props":691,"children":693},{"alt":7,"src":692},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20228/1/1659339852192767331.jpg",[],{"type":18,"tag":32,"props":695,"children":696},{},[697],{"type":24,"value":698},"(a)图对应3x3的1-dilated conv，和普通的卷积操作一样，(b)图对应3x3的2-dilated conv，实际的卷积kernel size还是3x3，但是空洞为1，也就是对于一个7x7的图像patch，只有9个红色的点和3x3的kernel发生卷积操作，其余的点略过。也可以理解为kernel的size为7x7，但是只有图中的9个点的权重不为0，其余都为0。 可以看到虽然kernel size只有3x3，但是这个卷积的感受野已经增大到了7x7（如果考虑到这个2-dilated conv的前一层是一个1-dilated conv的话，那么每个红点就是1-dilated的卷积输出，所以感受野为3x3，所以1-dilated和2-dilated合起来就能达到7x7的conv），©图是4-dilated conv操作，同理跟在两个1-dilated和2-dilated conv的后面，能达到15x15的感受野。对比传统的conv操作，3层3x3的卷积加起来，stride为1的话，只能达到(kernel-1)*layer+1=7的感受野，也就是和层数layer成线性关系，而dilated conv的感受野是指数级的增长。",{"type":18,"tag":32,"props":700,"children":701},{},[702],{"type":24,"value":703},"空洞卷积的好处是不做pooling损失信息的情况下，加大了感受野，让每个卷积输出都包含较大范围的信息。在图像需要全局信息或者语音文本需要较长的sequence信息依赖的问题中，都能很好的应用Dilated Convolution，比如语音合成WaveNet、机器翻译ByteNet中。",{"type":18,"tag":32,"props":705,"children":706},{},[707],{"type":18,"tag":446,"props":708,"children":709},{},[710],{"type":24,"value":665},{"type":18,"tag":42,"props":712,"children":713},{},[714,742,770],{"type":18,"tag":46,"props":715,"children":716},{},[717,719,725,727,733,735,740],{"type":24,"value":718},"在上图的(b)中，对于",{"type":18,"tag":200,"props":720,"children":722},{"className":721},[],[723],{"type":24,"value":724},"kernel_size",{"type":24,"value":726},"为3×3，",{"type":18,"tag":200,"props":728,"children":730},{"className":729},[],[731],{"type":24,"value":732},"dilation rate=2",{"type":24,"value":734},"的情况，其实际",{"type":18,"tag":200,"props":736,"children":738},{"className":737},[],[739],{"type":24,"value":724},{"type":24,"value":741},"大小为7×7。",{"type":18,"tag":46,"props":743,"children":744},{},[745,747,753,755,761,763,768],{"type":24,"value":746},"但是在",{"type":18,"tag":200,"props":748,"children":750},{"className":749},[],[751],{"type":24,"value":752},"MindSpore（Pytorch）",{"type":24,"value":754},"框架内，其计算公式为",{"type":18,"tag":200,"props":756,"children":758},{"className":757},[],[759],{"type":24,"value":760},"dilation∗(kernelsize−1)+1",{"type":24,"value":762},"，即实际",{"type":18,"tag":200,"props":764,"children":766},{"className":765},[],[767],{"type":24,"value":724},{"type":24,"value":769},"大小为5×5。",{"type":18,"tag":46,"props":771,"children":772},{},[773],{"type":24,"value":774},"可以看出，上图中对卷积核的周边做了同样的膨胀，而框架在具体实现时，只对卷积核内部做膨胀。",{"type":18,"tag":32,"props":776,"children":777},{},[778],{"type":24,"value":779},"下面通过一段代码示例，来看看MindSpore中的具体实现。代码如下：",{"type":18,"tag":32,"props":781,"children":782},{},[783,785,790,792,798],{"type":24,"value":784},"为了方便观察输出数据的高度和宽度，这里将",{"type":18,"tag":200,"props":786,"children":788},{"className":787},[],[789],{"type":24,"value":266},{"type":24,"value":791},"方式设置为",{"type":18,"tag":200,"props":793,"children":795},{"className":794},[],[796],{"type":24,"value":797},"valid",{"type":24,"value":799},"。",{"type":18,"tag":307,"props":801,"children":803},{"code":802},"import numpy as np\n\nfrom mindspore import nn\nfrom mindspore.common import dtype as mstype\nfrom mindspore.common import Tensor\n\n\ndef dilated_conv_demo():\n    img_data = np.random.rand(2, 3, 8, 8)\n\n    ms_in = Tensor(img_data, dtype=mstype.float32)\n\n    common_conv_op_0 = nn.Conv2d(3, 4, 3, 1, pad_mode=\"valid\")\n    common_conv_op_1 = nn.Conv2d(3, 4, 5, 1, pad_mode=\"valid\")\n    dilated_conv_op = nn.Conv2d(3, 4, 3, 1, pad_mode=\"valid\", dilation=2)\n\n    common_out_0 = common_conv_op_0(ms_in)\n    common_out_1 = common_conv_op_1(ms_in)\n    dilated_out = dilated_conv_op(ms_in)\n\n    print(\"common out 0 shape: {}\".format(common_out_0.shape), flush=True)\n    print(\"common out 1 shape: {}\".format(common_out_1.shape), flush=True)\n    print(\"dilated out shape: {}\".format(dilated_out.shape), flush=True)\n\n\ndef main():\n    dilated_conv_demo()\n\n\nif __name__ == \"__main__\":\n    main()\n",[804],{"type":18,"tag":200,"props":805,"children":806},{"__ignoreMap":7},[807],{"type":24,"value":802},{"type":18,"tag":32,"props":809,"children":810},{},[811],{"type":24,"value":319},{"type":18,"tag":42,"props":813,"children":814},{},[815,834,845],{"type":18,"tag":46,"props":816,"children":817},{},[818,824,826,832],{"type":18,"tag":200,"props":819,"children":821},{"className":820},[],[822],{"type":24,"value":823},"common_conv_op_0",{"type":24,"value":825},"和",{"type":18,"tag":200,"props":827,"children":829},{"className":828},[],[830],{"type":24,"value":831},"common_conv_op_1",{"type":24,"value":833},"皆为普通卷积，其卷积核大小分别为3×3和5×5。",{"type":18,"tag":46,"props":835,"children":836},{},[837,843],{"type":18,"tag":200,"props":838,"children":840},{"className":839},[],[841],{"type":24,"value":842},"dilated_conv_op",{"type":24,"value":844},"为空洞卷积，卷积核为3×3，但dilation设置为2。",{"type":18,"tag":46,"props":846,"children":847},{},[848,850,855,857,862],{"type":24,"value":849},"根据公式",{"type":18,"tag":200,"props":851,"children":853},{"className":852},[],[854],{"type":24,"value":760},{"type":24,"value":856},"可知，",{"type":18,"tag":200,"props":858,"children":860},{"className":859},[],[861],{"type":24,"value":842},{"type":24,"value":863},"就卷积核大小来看，效果类似于5×5普通卷积。验证参见输出数据的数据维度。",{"type":18,"tag":32,"props":865,"children":866},{},[867,868,874],{"type":24,"value":376},{"type":18,"tag":200,"props":869,"children":871},{"className":870},[],[872],{"type":24,"value":873},"dilated_conv2d.py",{"type":24,"value":384},{"type":18,"tag":307,"props":876,"children":878},{"code":877},"python3 dilated_conv2d.py\n",[879],{"type":18,"tag":200,"props":880,"children":881},{"__ignoreMap":7},[882],{"type":24,"value":877},{"type":18,"tag":32,"props":884,"children":885},{},[886],{"type":24,"value":397},{"type":18,"tag":32,"props":888,"children":889},{},[890,892,898,899,905],{"type":24,"value":891},"可以看出",{"type":18,"tag":200,"props":893,"children":895},{"className":894},[],[896],{"type":24,"value":897},"common out 1 shape",{"type":24,"value":825},{"type":18,"tag":200,"props":900,"children":902},{"className":901},[],[903],{"type":24,"value":904},"dilated out shape",{"type":24,"value":906},"相等，验证了代码解读的第三条。",{"type":18,"tag":307,"props":908,"children":910},{"code":909},"common out 0 shape: (2, 4, 6, 6)\ncommon out 1 shape: (2, 4, 4, 4)\ndilated out shape: (2, 4, 4, 4)\n",[911],{"type":18,"tag":200,"props":912,"children":913},{"__ignoreMap":7},[914],{"type":24,"value":909},{"type":18,"tag":115,"props":916,"children":918},{"id":917},"_5-数据格式",[919,924],{"type":18,"tag":120,"props":920,"children":923},{"href":921,"rel":922},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#5.-%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F",[124],[],{"type":24,"value":925},"5. 数据格式",{"type":18,"tag":32,"props":927,"children":928},{},[929,931,936,938,944],{"type":24,"value":930},"特别注意：",{"type":18,"tag":200,"props":932,"children":934},{"className":933},[],[935],{"type":24,"value":213},{"type":24,"value":937},"数据格式目前只支持在",{"type":18,"tag":200,"props":939,"children":941},{"className":940},[],[942],{"type":24,"value":943},"GPU",{"type":24,"value":945},"硬件下使用。",{"type":18,"tag":32,"props":947,"children":948},{},[949,951,957,959,964,965,970,972,977],{"type":24,"value":950},"在",{"type":18,"tag":200,"props":952,"children":954},{"className":953},[],[955],{"type":24,"value":956},"Conv2d",{"type":24,"value":958},"中，输入数据的数据格式可选值有",{"type":18,"tag":200,"props":960,"children":962},{"className":961},[],[963],{"type":24,"value":213},{"type":24,"value":825},{"type":18,"tag":200,"props":966,"children":968},{"className":967},[],[969],{"type":24,"value":205},{"type":24,"value":971},"，默认值为",{"type":18,"tag":200,"props":973,"children":975},{"className":974},[],[976],{"type":24,"value":205},{"type":24,"value":978},"。其中各个字母的含义如下：",{"type":18,"tag":42,"props":980,"children":981},{},[982,987,992,997],{"type":18,"tag":46,"props":983,"children":984},{},[985],{"type":24,"value":986},"N – 批数",{"type":18,"tag":46,"props":988,"children":989},{},[990],{"type":24,"value":991},"C – 通道数",{"type":18,"tag":46,"props":993,"children":994},{},[995],{"type":24,"value":996},"H – 高度",{"type":18,"tag":46,"props":998,"children":999},{},[1000],{"type":24,"value":1001},"W – 宽度",{"type":18,"tag":32,"props":1003,"children":1004},{},[1005],{"type":24,"value":1006},"那么两种数据格式又有什么区别呢，先从一段错误代码讲起：",{"type":18,"tag":32,"props":1008,"children":1009},{},[1010,1012,1017,1019,1024,1026,1031],{"type":24,"value":1011},"在下面的代码中，我们创建数据img_data，并且将通道放置到了最后一个维度，即数据格式为",{"type":18,"tag":200,"props":1013,"children":1015},{"className":1014},[],[1016],{"type":24,"value":213},{"type":24,"value":1018},"。但是",{"type":18,"tag":200,"props":1020,"children":1022},{"className":1021},[],[1023],{"type":24,"value":956},{"type":24,"value":1025},"中默认的数据格式为",{"type":18,"tag":200,"props":1027,"children":1029},{"className":1028},[],[1030],{"type":24,"value":205},{"type":24,"value":1032},"，那么运行起来如何呢？",{"type":18,"tag":307,"props":1034,"children":1036},{"code":1035},"import numpy as np\n\nfrom mindspore import nn\nfrom mindspore.common import dtype as mstype\nfrom mindspore.common import Tensor\n\ndef data_format_demo():\n    img_data = np.random.rand(2, 8, 8, 3)\n    ms_in = Tensor(img_data, dtype=mstype.float32)\n\n    common_conv_op = nn.Conv2d(3, 4, 3, 1)\n    ms_out = common_conv_op(ms_in)\n\n    print(\"common out shape: {}\".format(ms_out.shape), flush=True)\n\n\ndef main():\n    data_format_demo()\n\n\nif __name__ == \"__main__\":\n    main()\n",[1037],{"type":18,"tag":200,"props":1038,"children":1039},{"__ignoreMap":7},[1040],{"type":24,"value":1035},{"type":18,"tag":32,"props":1042,"children":1043},{},[1044,1045,1051],{"type":24,"value":376},{"type":18,"tag":200,"props":1046,"children":1048},{"className":1047},[],[1049],{"type":24,"value":1050},"format_conv2d.py",{"type":24,"value":384},{"type":18,"tag":307,"props":1053,"children":1055},{"code":1054},"python3 format_conv2d.py\n",[1056],{"type":18,"tag":200,"props":1057,"children":1058},{"__ignoreMap":7},[1059],{"type":24,"value":1054},{"type":18,"tag":32,"props":1061,"children":1062},{},[1063],{"type":24,"value":1064},"会输出报错信息，报错内容如下：",{"type":18,"tag":32,"props":1066,"children":1067},{},[1068],{"type":24,"value":1069},"错误信息中并没有显式提示是数据格式问题，所以对于新手来说这个问题可能具有迷惑性。",{"type":18,"tag":307,"props":1071,"children":1073},{"code":1072},"WARNING: Logging before InitGoogleLogging() is written to STDERR\n[CRITICAL] CORE(29160,0x102270580,Python):2022-07-31-16:35:26.406.143 [build/mindspore/merge/mindspore/core/ops_merge.cc:6753] Conv2dInferShape] For 'Conv2D', 'C_in' of input 'x' shape divide by parameter 'group' should be equal to 'C_in' of input 'weight' shape: 3, but got 'C_in' of input 'x' shape: 8, and 'group': 1\n[WARNING] UTILS(29160,0x102270580,Python):2022-07-31-16:35:26.409.046 [mindspore/ccsrc/utils/comm_manager.cc:78] GetInstance] CommManager instance for CPU not found, return default instance.\nTraceback (most recent call last):\n  File \"/Users/kaierlong/Codes/OpenI/kaierlong/Dive_Into_MindSpore/code/chapter_02/01_conv2d.py\", line 74, in \n    main()\n  File \"/Users/kaierlong/Codes/OpenI/kaierlong/Dive_Into_MindSpore/code/chapter_02/01_conv2d.py\", line 70, in main\n    data_format_demo()\n  File \"/Users/kaierlong/Codes/OpenI/kaierlong/Dive_Into_MindSpore/code/chapter_02/01_conv2d.py\", line 64, in data_format_demo\n    ms_out = common_conv_op(ms_in)\n  File \"/Users/kaierlong/Pyenvs/env_mix_dl/lib/python3.9/site-packages/mindspore/nn/cell.py\", line 586, in __call__\n    out = self.compile_and_run(*args)\n  File \"/Users/kaierlong/Pyenvs/env_mix_dl/lib/python3.9/site-packages/mindspore/nn/cell.py\", line 964, in compile_and_run\n    self.compile(*inputs)\n  File \"/Users/kaierlong/Pyenvs/env_mix_dl/lib/python3.9/site-packages/mindspore/nn/cell.py\", line 937, in compile\n    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)\n  File \"/Users/kaierlong/Pyenvs/env_mix_dl/lib/python3.9/site-packages/mindspore/common/api.py\", line 1006, in compile\n    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())\nRuntimeError: build/mindspore/merge/mindspore/core/ops_merge.cc:6753 Conv2dInferShape] For 'Conv2D', 'C_in' of input 'x' shape divide by parameter 'group' should be equal to 'C_in' of input 'weight' shape: 3, but got 'C_in' of input 'x' shape: 8, and 'group': 1\nThe function call stack (See file '/Users/kaierlong/Codes/OpenI/kaierlong/Dive_Into_MindSpore/code/chapter_02/rank_0/om/analyze_fail.dat' for more details):\n# 0 In file /Users/kaierlong/Pyenvs/env_mix_dl/lib/python3.9/site-packages/mindspore/nn/layer/conv.py(286)\n        if self.has_bias:\n# 1 In file /Users/kaierlong/Pyenvs/env_mix_dl/lib/python3.9/site-packages/mindspore/nn/layer/conv.py(285)\n        output = self.conv2d(x, self.weight)\n                 ^\n",[1074],{"type":18,"tag":200,"props":1075,"children":1076},{"__ignoreMap":7},[1077],{"type":24,"value":1072},{"type":18,"tag":32,"props":1079,"children":1080},{},[1081,1083,1089],{"type":24,"value":1082},"那么如何才能正常运行呢，有两种做法，一种是修改输入数据的数据格式；一种是对算子中的",{"type":18,"tag":200,"props":1084,"children":1086},{"className":1085},[],[1087],{"type":24,"value":1088},"data_format",{"type":24,"value":1090},"参数进行调整。展开来说，可以有三种方案。",{"type":18,"tag":32,"props":1092,"children":1093},{},[1094],{"type":24,"value":1095},"方案1：",{"type":18,"tag":42,"props":1097,"children":1098},{},[1099],{"type":18,"tag":46,"props":1100,"children":1101},{},[1102,1104,1109],{"type":24,"value":1103},"在数据预处理部分就将输入数据的数据格式规范成",{"type":18,"tag":200,"props":1105,"children":1107},{"className":1106},[],[1108],{"type":24,"value":205},{"type":24,"value":799},{"type":18,"tag":32,"props":1111,"children":1112},{},[1113],{"type":24,"value":1114},"方案2：",{"type":18,"tag":42,"props":1116,"children":1117},{},[1118],{"type":18,"tag":46,"props":1119,"children":1120},{},[1121,1123,1129,1131,1136],{"type":24,"value":1122},"对",{"type":18,"tag":200,"props":1124,"children":1126},{"className":1125},[],[1127],{"type":24,"value":1128},"ms_in",{"type":24,"value":1130}," 数据做一次转置操作（Transpose），将数据调整为",{"type":18,"tag":200,"props":1132,"children":1134},{"className":1133},[],[1135],{"type":24,"value":205},{"type":24,"value":799},{"type":18,"tag":32,"props":1138,"children":1139},{},[1140],{"type":24,"value":1141},"方案3：",{"type":18,"tag":42,"props":1143,"children":1144},{},[1145],{"type":18,"tag":46,"props":1146,"children":1147},{},[1148,1150,1155,1157,1162,1164,1169,1171,1177,1178,1184],{"type":24,"value":1149},"将",{"type":18,"tag":200,"props":1151,"children":1153},{"className":1152},[],[1154],{"type":24,"value":1088},{"type":24,"value":1156},"设置为",{"type":18,"tag":200,"props":1158,"children":1160},{"className":1159},[],[1161],{"type":24,"value":213},{"type":24,"value":1163},"。特别注意，这一设置只在",{"type":18,"tag":200,"props":1165,"children":1167},{"className":1166},[],[1168],{"type":24,"value":943},{"type":24,"value":1170},"下可用，",{"type":18,"tag":200,"props":1172,"children":1174},{"className":1173},[],[1175],{"type":24,"value":1176},"CPU",{"type":24,"value":825},{"type":18,"tag":200,"props":1179,"children":1181},{"className":1180},[],[1182],{"type":24,"value":1183},"Ascend",{"type":24,"value":1185},"下目前不可用。",{"type":18,"tag":115,"props":1187,"children":1189},{"id":1188},"_6-填充方式",[1190,1195],{"type":18,"tag":120,"props":1191,"children":1194},{"href":1192,"rel":1193},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#6.-%E5%A1%AB%E5%85%85%E6%96%B9%E5%BC%8F",[124],[],{"type":24,"value":1196},"6. 填充方式",{"type":18,"tag":32,"props":1198,"children":1199},{},[1200,1201,1206,1208,1214,1216,1221,1222,1227,1228,1234,1236,1241],{"type":24,"value":950},{"type":18,"tag":200,"props":1202,"children":1204},{"className":1203},[],[1205],{"type":24,"value":956},{"type":24,"value":1207},"中，填充模式（",{"type":18,"tag":200,"props":1209,"children":1211},{"className":1210},[],[1212],{"type":24,"value":1213},"pad_mode",{"type":24,"value":1215},"）可选值为",{"type":18,"tag":200,"props":1217,"children":1219},{"className":1218},[],[1220],{"type":24,"value":274},{"type":24,"value":487},{"type":18,"tag":200,"props":1223,"children":1225},{"className":1224},[],[1226],{"type":24,"value":797},{"type":24,"value":487},{"type":18,"tag":200,"props":1229,"children":1231},{"className":1230},[],[1232],{"type":24,"value":1233},"pad",{"type":24,"value":1235},"，默认值：",{"type":18,"tag":200,"props":1237,"children":1239},{"className":1238},[],[1240],{"type":24,"value":274},{"type":24,"value":1242},"。下面来介绍这三种填充方式。",{"type":18,"tag":1244,"props":1245,"children":1247},"h3",{"id":1246},"_61-same",[1248,1253],{"type":18,"tag":120,"props":1249,"children":1252},{"href":1250,"rel":1251},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#6.1-same",[124],[],{"type":24,"value":1254},"6.1 same",{"type":18,"tag":32,"props":1256,"children":1257},{},[1258,1260,1265],{"type":24,"value":1259},"对于",{"type":18,"tag":200,"props":1261,"children":1263},{"className":1262},[],[1264],{"type":24,"value":274},{"type":24,"value":1266},"填充方式，官方描述如下：",{"type":18,"tag":32,"props":1268,"children":1269},{},[1270,1272,1277],{"type":24,"value":1271},"输出的高度和宽度分别与输入整除 stride 后的值相同。若设置该模式，",{"type":18,"tag":200,"props":1273,"children":1275},{"className":1274},[],[1276],{"type":24,"value":266},{"type":24,"value":1278}," 的值必须为0。",{"type":18,"tag":32,"props":1280,"children":1281},{},[1282],{"type":24,"value":1283},"具体示例代码参见第2小节。",{"type":18,"tag":32,"props":1285,"children":1286},{},[1287],{"type":18,"tag":446,"props":1288,"children":1289},{},[1290],{"type":24,"value":1291},"特别注意",{"type":18,"tag":1293,"props":1294,"children":1295},"ol",{},[1296,1344],{"type":18,"tag":46,"props":1297,"children":1298},{},[1299,1300,1306,1308,1313,1315,1320,1322,1327,1329,1335,1337,1342],{"type":24,"value":825},{"type":18,"tag":200,"props":1301,"children":1303},{"className":1302},[],[1304],{"type":24,"value":1305},"Pytorch",{"type":24,"value":1307},"中",{"type":18,"tag":200,"props":1309,"children":1311},{"className":1310},[],[1312],{"type":24,"value":956},{"type":24,"value":1314},"的区别，在",{"type":18,"tag":200,"props":1316,"children":1318},{"className":1317},[],[1319],{"type":24,"value":1305},{"type":24,"value":1321},"中，填充方式为",{"type":18,"tag":200,"props":1323,"children":1325},{"className":1324},[],[1326],{"type":24,"value":274},{"type":24,"value":1328},"时，只允许",{"type":18,"tag":200,"props":1330,"children":1332},{"className":1331},[],[1333],{"type":24,"value":1334},"stride",{"type":24,"value":1336},"为1，而",{"type":18,"tag":200,"props":1338,"children":1340},{"className":1339},[],[1341],{"type":24,"value":243},{"type":24,"value":1343},"可以允许大于1的整数值。",{"type":18,"tag":46,"props":1345,"children":1346},{},[1347,1349,1354,1356,1361],{"type":24,"value":1348},"由于",{"type":18,"tag":200,"props":1350,"children":1352},{"className":1351},[],[1353],{"type":24,"value":1334},{"type":24,"value":1355},"允许1之外的整数，所以",{"type":18,"tag":200,"props":1357,"children":1359},{"className":1358},[],[1360],{"type":24,"value":274},{"type":24,"value":1362},"模式下输出数据的高度和宽度未必和输入数据一致，这一点一定要谨记，至于输出数据的高度和宽度请参考第7小节。",{"type":18,"tag":1244,"props":1364,"children":1366},{"id":1365},"_62-valid",[1367,1372],{"type":18,"tag":120,"props":1368,"children":1371},{"href":1369,"rel":1370},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#6.2-valid",[124],[],{"type":24,"value":1373},"6.2 valid",{"type":18,"tag":32,"props":1375,"children":1376},{},[1377,1378,1383],{"type":24,"value":1259},{"type":18,"tag":200,"props":1379,"children":1381},{"className":1380},[],[1382],{"type":24,"value":797},{"type":24,"value":1266},{"type":18,"tag":32,"props":1385,"children":1386},{},[1387,1389,1394],{"type":24,"value":1388},"在不填充的前提下返回有效计算所得的输出。不满足计算的多余像素会被丢弃。如果设置此模式，则 ",{"type":18,"tag":200,"props":1390,"children":1392},{"className":1391},[],[1393],{"type":24,"value":266},{"type":24,"value":1395},"的值必须为0。",{"type":18,"tag":32,"props":1397,"children":1398},{},[1399],{"type":24,"value":1400},"具体示例代码参见第4小节。",{"type":18,"tag":1244,"props":1402,"children":1404},{"id":1403},"_63-pad",[1405,1410],{"type":18,"tag":120,"props":1406,"children":1409},{"href":1407,"rel":1408},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#6.3-pad",[124],[],{"type":24,"value":1411},"6.3 pad",{"type":18,"tag":32,"props":1413,"children":1414},{},[1415,1417,1422,1424,1429],{"type":24,"value":1416},"本节重点来讲解一下",{"type":18,"tag":200,"props":1418,"children":1420},{"className":1419},[],[1421],{"type":24,"value":1233},{"type":24,"value":1423},"填充方式，对于",{"type":18,"tag":200,"props":1425,"children":1427},{"className":1426},[],[1428],{"type":24,"value":1233},{"type":24,"value":1266},{"type":18,"tag":32,"props":1431,"children":1432},{},[1433,1435,1440,1442,1447],{"type":24,"value":1434},"对输入进行填充。在输入的高度和宽度方向上填充 ",{"type":18,"tag":200,"props":1436,"children":1438},{"className":1437},[],[1439],{"type":24,"value":266},{"type":24,"value":1441}," 大小的0。如果设置此模式， ",{"type":18,"tag":200,"props":1443,"children":1445},{"className":1444},[],[1446],{"type":24,"value":266},{"type":24,"value":1448}," 必须大于或等于0。",{"type":18,"tag":32,"props":1450,"children":1451},{},[1452,1454,1459,1461,1466,1468,1473],{"type":24,"value":1453},"与",{"type":18,"tag":200,"props":1455,"children":1457},{"className":1456},[],[1458],{"type":24,"value":1233},{"type":24,"value":1460},"填充方式配合使用的，还有",{"type":18,"tag":200,"props":1462,"children":1464},{"className":1463},[],[1465],{"type":24,"value":266},{"type":24,"value":1467},"参数。下面来看一下官方对",{"type":18,"tag":200,"props":1469,"children":1471},{"className":1470},[],[1472],{"type":24,"value":266},{"type":24,"value":1474},"参数的描述：",{"type":18,"tag":32,"props":1476,"children":1477},{},[1478],{"type":24,"value":1479},"输入的高度和宽度方向上填充的数量。数据类型为int或包含4个整数的tuple。如果 padding 是一个整数，那么上、下、左、右的填充都等于 padding 。如果 padding 是一个有4个整数的tuple，那么上、下、左、右的填充分别等于 padding[0] 、 padding[1] 、 padding[2] 和 padding[3] 。值应该要大于等于0，默认值：0。",{"type":18,"tag":32,"props":1481,"children":1482},{},[1483],{"type":24,"value":1484},"padding参数解读：",{"type":18,"tag":42,"props":1486,"children":1487},{},[1488,1506],{"type":18,"tag":46,"props":1489,"children":1490},{},[1491,1493],{"type":24,"value":1492},"允许两种数据形式",{"type":18,"tag":42,"props":1494,"children":1495},{},[1496,1501],{"type":18,"tag":46,"props":1497,"children":1498},{},[1499],{"type":24,"value":1500},"一个整数 – 此时表示上下左右填充值皆为padding",{"type":18,"tag":46,"props":1502,"children":1503},{},[1504],{"type":24,"value":1505},"tuple，且tuple内含四个整数 – 此时表示上、下、左、右的填充分别等于 padding[0] 、 padding[1] 、 padding[2] 和 padding[3]",{"type":18,"tag":46,"props":1507,"children":1508},{},[1509],{"type":24,"value":1510},"这里的上、下、左、右表示的高和宽，通俗解释即为上高、下高、左宽、右宽。",{"type":18,"tag":32,"props":1512,"children":1513},{},[1514],{"type":24,"value":1515},"下面通过两个示例来讲解两种数据形式。",{"type":18,"tag":1517,"props":1518,"children":1520},"h4",{"id":1519},"_631-padding为一个整数",[1521,1526],{"type":18,"tag":120,"props":1522,"children":1525},{"href":1523,"rel":1524},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#6.3.1-padding%E4%B8%BA%E4%B8%80%E4%B8%AA%E6%95%B4%E6%95%B0",[124],[],{"type":24,"value":1527},"6.3.1 padding为一个整数",{"type":18,"tag":32,"props":1529,"children":1530},{},[1531],{"type":24,"value":1532},"例如，对于二维的8×8原始图像，图像格式为RGB（即通道数为3），可以认为这是一个3维图片，数据维度为 3×8×8（NCHW）或8×8×3（NHWC）。",{"type":18,"tag":32,"props":1534,"children":1535},{},[1536],{"type":24,"value":1537},"假设我们对上述图片进行普通卷积操作，卷积核大小为3×3，步长为1，卷积后的输出通道数为4，要求输出数据的高度和宽度与输入数据一致。",{"type":18,"tag":32,"props":1539,"children":1540},{},[1541],{"type":24,"value":281},{"type":18,"tag":32,"props":1543,"children":1544},{},[1545],{"type":18,"tag":137,"props":1546,"children":1548},{"alt":7,"src":1547},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20228/1/1659339896685817106.png",[],{"type":18,"tag":32,"props":1550,"children":1551},{},[1552,1554,1559,1561,1566,1568,1573,1574,1579],{"type":24,"value":1553},"简单分析：上面的案例要求第2节中的代码就可以实现，在第2节中采用的",{"type":18,"tag":200,"props":1555,"children":1557},{"className":1556},[],[1558],{"type":24,"value":1213},{"type":24,"value":1560},"为",{"type":18,"tag":200,"props":1562,"children":1564},{"className":1563},[],[1565],{"type":24,"value":274},{"type":24,"value":1567},"，那么如果采用",{"type":18,"tag":200,"props":1569,"children":1571},{"className":1570},[],[1572],{"type":24,"value":1213},{"type":24,"value":1560},{"type":18,"tag":200,"props":1575,"children":1577},{"className":1576},[],[1578],{"type":24,"value":1233},{"type":24,"value":1580},"呢，代码如下：",{"type":18,"tag":307,"props":1582,"children":1584},{"code":1583},"import numpy as np\n\nfrom mindspore import nn\nfrom mindspore.common import dtype as mstype\nfrom mindspore.common import Tensor\n\n\ndef pad_demo_01():\n    img_data = np.random.rand(2, 3, 8, 8)\n\n    ms_in = Tensor(img_data, dtype=mstype.float32)\n    conv_op = nn.Conv2d(3, 4, 3, 1, pad_mode=\"pad\", padding=1)\n    ms_out = conv_op(ms_in)\n\n    print(\"in shape: {}\".format(ms_in.shape), flush=True)\n    print(\"out shape: {}\".format(ms_out.shape), flush=True)\n\n\ndef main():\n    pad_demo_01()\n\n\nif __name__ == \"__main__\":\n    main()\n",[1585],{"type":18,"tag":200,"props":1586,"children":1587},{"__ignoreMap":7},[1588],{"type":24,"value":1583},{"type":18,"tag":32,"props":1590,"children":1591},{},[1592],{"type":24,"value":319},{"type":18,"tag":42,"props":1594,"children":1595},{},[1596,1621],{"type":18,"tag":46,"props":1597,"children":1598},{},[1599,1601,1606,1607,1612,1614,1619],{"type":24,"value":1600},"在卷积核大小为3×3，卷积步长为1的情况下，要想保证输出数据的高宽值与输入数据一致，在",{"type":18,"tag":200,"props":1602,"children":1604},{"className":1603},[],[1605],{"type":24,"value":1213},{"type":24,"value":1560},{"type":18,"tag":200,"props":1608,"children":1610},{"className":1609},[],[1611],{"type":24,"value":1233},{"type":24,"value":1613},"模式下，",{"type":18,"tag":200,"props":1615,"children":1617},{"className":1616},[],[1618],{"type":24,"value":266},{"type":24,"value":1620},"的值应该设置1。",{"type":18,"tag":46,"props":1622,"children":1623},{},[1624,1626,1631],{"type":24,"value":1625},"这里计算",{"type":18,"tag":200,"props":1627,"children":1629},{"className":1628},[],[1630],{"type":24,"value":266},{"type":24,"value":1632},"数值的公式参加第7小节输出维度部分。",{"type":18,"tag":32,"props":1634,"children":1635},{},[1636,1637,1643],{"type":24,"value":376},{"type":18,"tag":200,"props":1638,"children":1640},{"className":1639},[],[1641],{"type":24,"value":1642},"pad_conv2d_01.py",{"type":24,"value":384},{"type":18,"tag":307,"props":1645,"children":1647},{"code":1646},"python3 pad_conv2d_01.py\n",[1648],{"type":18,"tag":200,"props":1649,"children":1650},{"__ignoreMap":7},[1651],{"type":24,"value":1646},{"type":18,"tag":32,"props":1653,"children":1654},{},[1655],{"type":24,"value":397},{"type":18,"tag":307,"props":1657,"children":1658},{"code":412},[1659],{"type":18,"tag":200,"props":1660,"children":1661},{"__ignoreMap":7},[1662],{"type":24,"value":412},{"type":18,"tag":1517,"props":1664,"children":1666},{"id":1665},"_632-padding为四个整数tuple",[1667,1672],{"type":18,"tag":120,"props":1668,"children":1671},{"href":1669,"rel":1670},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#6.3.2-padding%E4%B8%BA%E5%9B%9B%E4%B8%AA%E6%95%B4%E6%95%B0tuple",[124],[],{"type":24,"value":1673},"6.3.2 padding为四个整数tuple",{"type":18,"tag":32,"props":1675,"children":1676},{},[1677],{"type":24,"value":1678},"\"padding为四个整数tuple\"是\"padding为一个整数\"的一般情况。下面我们通过一个示例进行讲解。",{"type":18,"tag":32,"props":1680,"children":1681},{},[1682,1684,1690],{"type":24,"value":1683},"例如：输入数据仍然保持同",{"type":18,"tag":200,"props":1685,"children":1687},{"className":1686},[],[1688],{"type":24,"value":1689},"6.3.1",{"type":24,"value":1691},"中一致，但是这次我们输出数据的高度和宽度要求有所变化，要求高度与输入数据一致，宽度为7（输入数据为8），这种情况下应该如何设定padding呢？",{"type":18,"tag":32,"props":1693,"children":1694},{},[1695],{"type":24,"value":1696},"实例代码如下：",{"type":18,"tag":307,"props":1698,"children":1700},{"code":1699},"import numpy as np\n\nfrom mindspore import nn\nfrom mindspore.common import dtype as mstype\nfrom mindspore.common import Tensor\n\n\ndef pad_demo_02():\n    img_data = np.random.rand(2, 3, 8, 8)\n\n    ms_in = Tensor(img_data, dtype=mstype.float32)\n    conv_op = nn.Conv2d(3, 4, 3, 1, pad_mode=\"pad\", padding=(1, 1, 1, 0))\n    ms_out = conv_op(ms_in)\n\n    print(\"in shape: {}\".format(ms_in.shape), flush=True)\n    print(\"out shape: {}\".format(ms_out.shape), flush=True)\n\n\ndef main():\n    pad_demo_02()\n\n\nif __name__ == \"__main__\":\n    main()\n",[1701],{"type":18,"tag":200,"props":1702,"children":1703},{"__ignoreMap":7},[1704],{"type":24,"value":1699},{"type":18,"tag":32,"props":1706,"children":1707},{},[1708],{"type":24,"value":319},{"type":18,"tag":42,"props":1710,"children":1711},{},[1712,1717,1735,1745],{"type":18,"tag":46,"props":1713,"children":1714},{},[1715],{"type":24,"value":1716},"还记得padding[0] 、 padding[1] 、 padding[2] 和 padding[3] 这四个参数的意义，不记得没关系，再来一遍：通俗解释即为上高、下高、左宽、右宽。",{"type":18,"tag":46,"props":1718,"children":1719},{},[1720,1722,1727,1728,1733],{"type":24,"value":1721},"这里的要求是输出数据的高度与输入数据一致，宽度为7（输入数据为8）。所以上高、下高的",{"type":18,"tag":200,"props":1723,"children":1725},{"className":1724},[],[1726],{"type":24,"value":266},{"type":24,"value":1453},{"type":18,"tag":200,"props":1729,"children":1731},{"className":1730},[],[1732],{"type":24,"value":1689},{"type":24,"value":1734},"中一致，即1；左宽、右宽加起来的padding为1，因为不能存在非整数，这里我们分别设置为1、0（这里没有特别要求，也可以设置为0、1）。",{"type":18,"tag":46,"props":1736,"children":1737},{},[1738,1739,1744],{"type":24,"value":1625},{"type":18,"tag":200,"props":1740,"children":1742},{"className":1741},[],[1743],{"type":24,"value":266},{"type":24,"value":1632},{"type":18,"tag":46,"props":1746,"children":1747},{},[1748,1750,1756],{"type":24,"value":1749},"最终的核心代码即为",{"type":18,"tag":200,"props":1751,"children":1753},{"className":1752},[],[1754],{"type":24,"value":1755},"nn.Conv2d(3, 4, 3, 1, pad_mode=\"pad\", padding=(1, 1, 1, 0))",{"type":24,"value":799},{"type":18,"tag":32,"props":1758,"children":1759},{},[1760,1761,1767],{"type":24,"value":376},{"type":18,"tag":200,"props":1762,"children":1764},{"className":1763},[],[1765],{"type":24,"value":1766},"pad_conv2d_02.py",{"type":24,"value":384},{"type":18,"tag":307,"props":1769,"children":1771},{"code":1770},"python3 pad_conv2d_02.py\n",[1772],{"type":18,"tag":200,"props":1773,"children":1774},{"__ignoreMap":7},[1775],{"type":24,"value":1770},{"type":18,"tag":32,"props":1777,"children":1778},{},[1779],{"type":24,"value":397},{"type":18,"tag":32,"props":1781,"children":1782},{},[1783],{"type":24,"value":1784},"可以看到输出数据的高度和宽度符合我们的上面的要求。",{"type":18,"tag":307,"props":1786,"children":1788},{"code":1787},"in shape: (2, 3, 8, 8)\nout shape: (2, 4, 8, 7)\n",[1789],{"type":18,"tag":200,"props":1790,"children":1791},{"__ignoreMap":7},[1792],{"type":24,"value":1787},{"type":18,"tag":115,"props":1794,"children":1796},{"id":1795},"_7-输出维度",[1797,1802],{"type":18,"tag":120,"props":1798,"children":1801},{"href":1799,"rel":1800},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#7.-%E8%BE%93%E5%87%BA%E7%BB%B4%E5%BA%A6",[124],[],{"type":24,"value":1803},"7. 输出维度",{"type":18,"tag":32,"props":1805,"children":1806},{},[1807,1809,1814],{"type":24,"value":1808},"本节来单独介绍一下",{"type":18,"tag":200,"props":1810,"children":1812},{"className":1811},[],[1813],{"type":24,"value":956},{"type":24,"value":1815},"中数据输出维度的计算，在前面的6小节中，我们已经对部分做了铺垫。",{"type":18,"tag":32,"props":1817,"children":1818},{},[1819],{"type":24,"value":1820},"各种情况下的输出维度见下图公式。",{"type":18,"tag":32,"props":1822,"children":1823},{},[1824],{"type":18,"tag":137,"props":1825,"children":1827},{"alt":7,"src":1826},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20228/1/1659339923554171124.png",[],{"type":18,"tag":32,"props":1829,"children":1830},{},[1831],{"type":24,"value":1832},"在面对具体情况时，将相关参数带入公式即可算到要计算的部分。这里不再对公式展开解释。",{"type":18,"tag":115,"props":1834,"children":1835},{"id":108},[1836,1841],{"type":18,"tag":120,"props":1837,"children":1840},{"href":1838,"rel":1839},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#%E6%9C%AC%E6%96%87%E6%80%BB%E7%BB%93",[124],[],{"type":24,"value":108},{"type":18,"tag":32,"props":1843,"children":1844},{},[1845,1847,1852,1854,1859,1861,1866],{"type":24,"value":1846},"本文重点介绍了",{"type":18,"tag":200,"props":1848,"children":1850},{"className":1849},[],[1851],{"type":24,"value":243},{"type":24,"value":1853},"中的",{"type":18,"tag":200,"props":1855,"children":1857},{"className":1856},[],[1858],{"type":24,"value":956},{"type":24,"value":1860},"算子。通过几种不同卷积模式（普通卷积、深度卷积、空洞卷积）的具体实现，以及数据格式、填充方式、输出维度多个角度来深入讲解",{"type":18,"tag":200,"props":1862,"children":1864},{"className":1863},[],[1865],{"type":24,"value":956},{"type":24,"value":1867},"算子的具体应用。",{"type":18,"tag":115,"props":1869,"children":1870},{"id":113},[1871,1876],{"type":18,"tag":120,"props":1872,"children":1875},{"href":1873,"rel":1874},"https://bbs.huaweicloud.com/forum/thread-195905-1-1.html#%E6%9C%AC%E6%96%87%E5%8F%82%E8%80%83",[124],[],{"type":24,"value":113},{"type":18,"tag":42,"props":1878,"children":1879},{},[1880,1890,1900,1909,1919,1929,1939],{"type":18,"tag":46,"props":1881,"children":1882},{},[1883],{"type":18,"tag":120,"props":1884,"children":1887},{"href":1885,"rel":1886},"https://www.mindspore.cn/docs/zh-CN/r1.7/api_python/nn/mindspore.nn.Conv2d.html#mindspore.nn.Conv2d",[124],[1888],{"type":24,"value":1889},"Conv2d官方文档",{"type":18,"tag":46,"props":1891,"children":1892},{},[1893],{"type":18,"tag":120,"props":1894,"children":1897},{"href":1895,"rel":1896},"https://blog.csdn.net/m0_37799466/article/details/106054111",[124],[1898],{"type":24,"value":1899},"普通卷积与深度可分离卷积的区别",{"type":18,"tag":46,"props":1901,"children":1902},{},[1903],{"type":18,"tag":120,"props":1904,"children":1906},{"href":455,"rel":1905},[124],[1907],{"type":24,"value":1908},"卷积网络基础知识—Depthwise Convolution && Pointwise Convolution && Separable Convolution",{"type":18,"tag":46,"props":1910,"children":1911},{},[1912],{"type":18,"tag":120,"props":1913,"children":1916},{"href":1914,"rel":1915},"https://zhuanlan.zhihu.com/p/80041030",[124],[1917],{"type":24,"value":1918},"Depthwise卷积与Pointwise卷积",{"type":18,"tag":46,"props":1920,"children":1921},{},[1922],{"type":18,"tag":120,"props":1923,"children":1926},{"href":1924,"rel":1925},"https://zhuanlan.zhihu.com/p/339835983",[124],[1927],{"type":24,"value":1928},"Depthwise Convolution与普通卷积的区别以及其他卷积方法",{"type":18,"tag":46,"props":1930,"children":1931},{},[1932],{"type":18,"tag":120,"props":1933,"children":1936},{"href":1934,"rel":1935},"https://www.zhihu.com/question/54149221/answer/192025860",[124],[1937],{"type":24,"value":1938},"如何理解空洞卷积（dilated convolution）？",{"type":18,"tag":46,"props":1940,"children":1941},{},[1942],{"type":18,"tag":120,"props":1943,"children":1946},{"href":1944,"rel":1945},"https://blog.csdn.net/chaipp0607/article/details/99671483",[124],[1947],{"type":24,"value":1948},"如何理解扩张卷积(dilated convolution)",{"type":18,"tag":32,"props":1950,"children":1951},{},[1952],{"type":24,"value":1953},"本文为原创文章，版权归作者所有，未经授权不得转载！",{"title":7,"searchDepth":1955,"depth":1955,"links":1956},4,[1957,1959,1960,1961,1962,1963,1972,1973,1974],{"id":117,"depth":1958,"text":127},2,{"id":216,"depth":1958,"text":224},{"id":420,"depth":1958,"text":428},{"id":673,"depth":1958,"text":681},{"id":917,"depth":1958,"text":925},{"id":1188,"depth":1958,"text":1196,"children":1964},[1965,1967,1968],{"id":1246,"depth":1966,"text":1254},3,{"id":1365,"depth":1966,"text":1373},{"id":1403,"depth":1966,"text":1411,"children":1969},[1970,1971],{"id":1519,"depth":1955,"text":1527},{"id":1665,"depth":1955,"text":1673},{"id":1795,"depth":1958,"text":1803},{"id":108,"depth":1958,"text":108},{"id":113,"depth":1958,"text":113},"markdown","content:technology-blogs:zh:1674.md","content","technology-blogs/zh/1674.md","technology-blogs/zh/1674","md",1776506114826]