{"id":3182,"date":"2025-07-09T23:46:34","date_gmt":"2025-07-09T15:46:34","guid":{"rendered":"https:\/\/www.gnn.club\/?p=3182"},"modified":"2025-07-14T15:52:21","modified_gmt":"2025-07-14T07:52:21","slug":"cbam-convolutional-block-attention-module","status":"publish","type":"post","link":"http:\/\/gnn.club\/?p=3182","title":{"rendered":"CBAM: Convolutional Block Attention Module"},"content":{"rendered":"<h1>\u57fa\u672c\u4fe1\u606f<\/h1>\n<ul>\n<li>\ud83d\udcf0\u6807\u9898: CBAM: Convolutional Block Attention Module<\/li>\n<li>\ud83d\udd8b\ufe0f\u4f5c\u8005: Sanghyun Woo<\/li>\n<li>\ud83c\udfdb\ufe0f\u673a\u6784: Korea Advanced Institute of Science and Technology (KAIST, \u97e9\u56fd\u79d1\u5b66\u6280\u672f\u9662)<\/li>\n<li>\ud83d\udd25\u5173\u952e\u8bcd: Attention mechanism, CNN, channel attention, spatial attention<\/li>\n<\/ul>\n<h2>\u6458\u8981\u6982\u8ff0<\/h2>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tr>\n<th style=\"border: 1px solid #ddd; padding: 8px; text-align: left; background-color: #f2f2f2;\">\u9879\u76ee<\/th>\n<th style=\"border: 1px solid #ddd; padding: 8px; text-align: left; background-color: #f2f2f2;\">\u5185\u5bb9<\/th>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\ud83d\udcd6\u7814\u7a76\u80cc\u666f<\/td>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u73b0\u6709CNN\u67b6\u6784\u7f3a\u4e4f\u663e\u5f0f\u5efa\u6a21\u7279\u5f81\u901a\u9053\u548c\u7a7a\u95f4\u4f4d\u7f6e\u91cd\u8981\u6027\u7684\u673a\u5236<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\ud83c\udfaf\u7814\u7a76\u76ee\u7684<\/td>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u63d0\u51fa\u8f7b\u91cf\u7ea7\u901a\u7528\u6ce8\u610f\u529b\u6a21\u5757\uff0c\u589e\u5f3aCNN\u7684\u7279\u5f81\u8868\u8fbe\u80fd\u529b<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u270d\ufe0f\u7814\u7a76\u65b9\u6cd5<\/td>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u8bbe\u8ba1\u4e32\u8054\u7684Channel Attention Module\u548cSpatial Attention Module<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\ud83d\udd4a\ufe0f\u7814\u7a76\u5bf9\u8c61<\/td>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u56fe\u50cf\u5206\u7c7b\u7f51\u7edc\uff08ResNet\u7b49\uff09\u548c\u76ee\u6807\u68c0\u6d4b\u7f51\u7edc\uff08Mask R-CNN\u7b49\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\ud83d\udd0d\u7814\u7a76\u7ed3\u8bba<\/td>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u5728ImageNet\u5206\u7c7b\u548cCOCO\u68c0\u6d4b\u4efb\u52a1\u4e2d\u663e\u8457\u63d0\u5347\u6027\u80fd\uff0c\u4e14\u8ba1\u7b97\u5f00\u9500\u53ef\u5ffd\u7565<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">\u2b50\u521b\u65b0\u70b9<\/td>\n<td style=\"border: 1px solid #ddd; padding: 8px;\">1. \u8054\u5408\u901a\u9053-\u7a7a\u95f4\u6ce8\u610f\u529b\u673a\u52362. \u5373\u63d2\u5373\u7528\u8bbe\u8ba13. \u4f18\u4e8eSENet\u7b49\u57fa\u7ebf<\/td>\n<\/tr>\n<\/table>\n<h1>\u80cc\u666f<\/h1>\n<ul>\n<li>\n<p><strong>\u7814\u7a76\u80cc\u666f<\/strong><br \/>\nCNN\u901a\u8fc7\u6df1\u5ea6\u3001\u5bbd\u5ea6\u548c\u7ec4\u6570\uff08cardinality\uff09\u7684\u4f18\u5316\u663e\u8457\u63d0\u5347\u4e86\u89c6\u89c9\u4efb\u52a1\u6027\u80fd\uff0c\u4f46\u73b0\u6709\u67b6\u6784\u7f3a\u4e4f\u5bf9\u7279\u5f81\u901a\u9053\u548c\u7a7a\u95f4\u4f4d\u7f6e\u91cd\u8981\u6027\u7684\u663e\u5f0f\u5efa\u6a21\u673a\u5236\u3002\u6ce8\u610f\u529b\u673a\u5236\u5728\u589e\u5f3a\u7279\u5f81\u8868\u793a\u65b9\u9762\u5177\u6709\u6f5c\u529b\uff0c\u4f46\u5982\u4f55\u6709\u6548\u6574\u5408\u5230CNN\u4e2d\u4ecd\u9700\u63a2\u7d22\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8fc7\u53bb\u65b9\u6848<\/strong><\/p>\n<\/li>\n<\/ul>\n<ol>\n<li>\n<p><strong>\u7ed3\u6784\u4f18\u5316<\/strong>\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u6df1\u5ea6<\/strong>\uff1aResNet\u901a\u8fc7\u6b8b\u5dee\u8fde\u63a5\u6784\u5efa\u6781\u6df1\u67b6\u6784<\/p>\n<\/li>\n<li>\n<p><strong>\u5bbd\u5ea6<\/strong>\uff1aWide ResNet\u8bc1\u660e\u589e\u52a0\u5bbd\u5ea6\u53ef\u8d85\u8d8a\u8d85\u6df1\u7f51\u7edc<\/p>\n<\/li>\n<li>\n<p><strong>\u7ec4\u6570<\/strong>\uff1aResNeXt\u663e\u793a\u57fa\u6570\u63d0\u5347\u6bd4\u6df1\u5ea6\/\u5bbd\u5ea6\u66f4\u9ad8\u6548<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b58\u5728\u95ee\u9898<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e0a\u8ff0\u65b9\u6cd5\u672a\u663e\u5f0f\u5efa\u6a21\u7279\u5f81\u91cd\u8981\u6027<\/li>\n<li>\u6ce8\u610f\u529b\u673a\u5236\uff08\u5982SENet\uff09\u4ec5\u8003\u8651\u901a\u9053\u7ef4\u5ea6\uff0c\u5ffd\u7565\u7a7a\u95f4\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<ul>\n<li><strong>\u7814\u7a76\u52a8\u673a<\/strong><br \/>\n\u63d0\u51fa\u8f7b\u91cf\u7ea7Convolutional Block Attention Module\uff08CBAM\uff09\uff0c\u901a\u8fc7<strong>\u901a\u9053-\u7a7a\u95f4\u53cc\u6ce8\u610f\u529b\u673a\u5236<\/strong>\u52a8\u6001\u589e\u5f3a\u5173\u952e\u7279\u5f81\u5e76\u6291\u5236\u566a\u58f0\uff0c\u5b9e\u73b0\uff1a<\/li>\n<\/ul>\n<ol>\n<li>\u5373\u63d2\u5373\u7528\u7684\u901a\u7528\u6027<\/li>\n<li>\u8054\u5408\u4f18\u5316&quot;what&quot;\uff08\u901a\u9053\uff09\u548c&quot;where&quot;\uff08\u7a7a\u95f4\uff09\u6ce8\u610f\u529b<\/li>\n<li>\u5728\u5206\u7c7b\/\u68c0\u6d4b\u4efb\u52a1\u4e2d\u540c\u6b65\u63d0\u5347\u6027\u80fd\u4e0e\u53ef\u89e3\u91ca\u6027<\/li>\n<\/ol>\n<h1>\u65b9\u6cd5<\/h1>\n<ul>\n<li><strong>\u7406\u8bba\u80cc\u666f<\/strong><br \/>\n\u672c\u7814\u7a76\u57fa\u4e8e\u7279\u5f81\u91cd\u6807\u5b9a\uff08feature recalibration\uff09\u7406\u8bba\uff0c\u8ba4\u4e3aCNN\u7684\u7279\u5f81\u4f18\u5316\u9700\u540c\u65f6\u8003\u8651\u901a\u9053\u57df\uff08channel-wise\uff09\u548c\u7a7a\u95f4\u57df\uff08spatial-wise\uff09\u7684\u6ce8\u610f\u529b\u673a\u5236\u3002\u53d7SENet\u901a\u9053\u6ce8\u610f\u529b\u542f\u53d1\uff0c\u63d0\u51fa\u901a\u9053\u4e0e\u7a7a\u95f4\u6ce8\u610f\u529b\u5b58\u5728\u4e92\u8865\u6027\uff1a\u901a\u9053\u6ce8\u610f\u529b\u89e3\u51b3&quot;what to focus&quot;\uff08\u8bed\u4e49\u91cd\u8981\u6027\uff09\uff0c\u7a7a\u95f4\u6ce8\u610f\u529b\u89e3\u51b3&quot;where to focus&quot;\uff08\u4f4d\u7f6e\u663e\u8457\u6027\uff09\uff0c\u4e8c\u8005\u4e32\u8054\u53ef\u5b9e\u73b0\u66f4\u7cbe\u7ec6\u7684\u7279\u5f81\u9009\u62e9\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/07\/20250709231341992.png\" width=\"600\" style=\"display: block; margin: 0 auto;\" \/><\/p>\n<ul>\n<li><strong>\u8be6\u7ec6\u6280\u672f\u8def\u7ebf<\/strong><\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/07\/20250709231708616.png\" width=\"600\" style=\"display: block; margin: 0 auto;\" \/><\/p>\n<p><strong>Channel Attention Module\uff08\u901a\u9053\u6ce8\u610f\u529b\u6a21\u5757\uff09<\/strong><br \/>\n\u539f\u7406\uff1a<br \/>\n\u901a\u9053\u6ce8\u610f\u529b\u6a21\u5757\u65e8\u5728\u901a\u8fc7\u52a8\u6001\u8c03\u6574\u7279\u5f81\u56fe\u5404\u901a\u9053\u7684\u6743\u91cd\uff0c\u7a81\u51fa\u91cd\u8981\u8bed\u4e49\u7279\u5f81\u5e76\u6291\u5236\u5197\u4f59\u4fe1\u606f\u3002\u5176\u6838\u5fc3\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1\uff0e\u53cc\u8def\u7a7a\u95f4\u538b\u7f29\uff1a<br \/>\n\uff0d\u5bf9\u8f93\u5165\u7279\u5f81\u56fe $F \\in \\mathbb{R}^{C \\times H \\times W}$ \u5206\u522b\u8fdb\u884c\u5168\u5c40\u5e73\u5747\u6c60\u5316\uff08GAP\uff09\u548c\u5168\u5c40\u6700\u5927\u6c60\u5316\uff08GMP\uff09\uff0c\u751f\u6210\u4e24\u79cd\u901a\u9053\u63cf\u8ff0\u7b26\uff1a<\/p>\n<ul>\n<li>$F_{\\mathrm{avg}}^c$\uff08\u5747\u503c\u6c60\u5316\uff09\uff1a\u6355\u6349\u5168\u5c40\u4e0a\u4e0b\u6587\u4fe1\u606f<\/li>\n<li>$F_{\\text {max }}^c$\uff08\u6700\u5927\u503c\u6c60\u5316\uff09\uff1a\u63d0\u53d6\u663e\u8457\u5c40\u90e8\u7279\u5f81<\/li>\n<\/ul>\n<p>2\uff0e\u5171\u4eabMLP\u5904\u7406\uff1a<br \/>\n\uff0d\u5c06\u53cc\u8def\u63cf\u8ff0\u7b26\u8f93\u5165\u5171\u4eab\u6743\u91cd\u7684\u53cc\u5c42MLP\uff08\u542bReLU\u6fc0\u6d3b\uff09\uff0c\u8f93\u51fa\u76f8\u52a0\u540e\u7ecfSigmoid\u751f\u6210\u901a\u9053\u6743\u91cd $M_c \\in \\mathbb{R}^{C \\times 1 \\times 1}$<\/p>\n<ul>\n<li>\u516c\u5f0f\u4e3a\uff1a$M_c(F)=\\sigma(\\operatorname{MLP}(\\operatorname{GAP}(F))+\\operatorname{MLP}(\\operatorname{GMP}(F)))$<\/li>\n<li>\u8bbe\u8ba1\u52a8\u673a\uff1a\u5b9e\u9a8c\u8868\u660e\u8054\u5408\u4f7f\u7528GAP\u548cGMP\u6bd4\u5355\u4e00\u6c60\u5316\uff08\u5982SENet\u4ec5\u7528GAP\uff09\u80fd\u751f\u6210\u66f4\u7cbe\u7ec6\u7684\u6ce8\u610f\u529b\u56fe\uff0cTop\uff0d1\u9519\u8bef\u7387\u964d\u4f4e $0.34 \\%$\u3002<\/li>\n<\/ul>\n<p>3\uff0e\u7279\u5f81\u91cd\u6807\u5b9a\uff1a\u5c06\u6743\u91cd $M_c$ \u4e0e\u539f\u59cb\u7279\u5f81\u9010\u901a\u9053\u76f8\u4e58\uff0c\u5b9e\u73b0\u81ea\u9002\u5e94\u7279\u5f81\u589e\u5f3a\u3002<br \/>\n\u521b\u65b0\u70b9\uff1a<\/p>\n<ul>\n<li>\u53cc\u6c60\u5316\u4e92\u8865\u6027\uff1aGAP\u4fdd\u7559\u6574\u4f53\u5206\u5e03\uff0cGMP\u805a\u7126\u5173\u952e\u533a\u57df\uff0c\u8054\u5408\u4f7f\u7528\u53ef\u907f\u514dSENet\u7684\u4fe1\u606f\u635f\u5931\u3002<\/li>\n<li>\u8f7b\u91cf\u5316\u8bbe\u8ba1\uff1aMLP\u9690\u85cf\u5c42\u7ef4\u5ea6\u4e3a $C \/ r \\quad(r=16)$ \uff0c\u53c2\u6570\u91cf\u4ec5\u589e\u52a0 0.14 M \uff08ResNet50\u4e3a\u4f8b\uff09\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/07\/20250709233756770.png\" width=\"480\" style=\"display: block; margin: 0 auto;\" \/><\/p>\n<p><strong>Spatial Attention Module\uff08\u7a7a\u95f4\u6ce8\u610f\u529b\u6a21\u5757\uff09<\/strong><br \/>\n\u539f\u7406\uff1a<br \/>\n\u7a7a\u95f4\u6ce8\u610f\u529b\u6a21\u5757\u7528\u4e8e\u5f3a\u8c03\u7279\u5f81\u56fe\u7684\u7a7a\u95f4\u663e\u8457\u533a\u57df\uff0c\u4e0e\u901a\u9053\u6ce8\u610f\u529b\u5f62\u6210\u4e92\u8865\uff1a<\/p>\n<p>1\uff0e\u53cc\u8def\u901a\u9053\u538b\u7f29\uff1a<br \/>\n\uff0d\u5bf9\u901a\u9053\u6ce8\u610f\u529b\u8f93\u51fa $F^{\\prime} \\in \\mathbb{R}^{C \\times H \\times W}$ \u6cbf\u901a\u9053\u8f74\u5206\u522b\u8fdb\u884c\u5e73\u5747\u6c60\u5316\uff08CAP\uff09\u548c\u6700\u5927\u6c60\u5316\uff08CMP\uff09\uff0c\u751f\u6210\u4e24\u4e2a2D\u7a7a\u95f4\u63cf\u8ff0\u7b26\uff1a<\/p>\n<ul>\n<li>$F_{\\mathrm{avg}}^s \\in \\mathbb{R}^{1 \\times H \\times W}$ \uff1a\u5e73\u6ed1\u7684\u7a7a\u95f4\u7edf\u8ba1\u4fe1\u606f<\/li>\n<li>$F_{\\text {max }}^s \\in \\mathbb{R}^{1 \\times H \\times W}$ \uff1a\u7a81\u51fa\u7684\u7a7a\u95f4\u54cd\u5e94<\/li>\n<\/ul>\n<p>2\uff0e\u7a7a\u95f4\u6743\u91cd\u751f\u6210\uff1a<br \/>\n\uff0d\u5c06\u53cc\u8def\u63cf\u8ff0\u7b26\u62fc\u63a5\u4e3a $2 \\times H \\times W$ \u7684\u7279\u5f81\uff0c\u7ecf $7 \\times 7$ \u5377\u79ef\u964d\u7ef4\u81f3\u5355\u901a\u9053\u540e\u901a\u8fc7Sigmoid\u751f\u6210\u7a7a\u95f4\u6743\u91cd $M_s \\in \\mathbb{R}^{1 \\times H \\times W}$ \u3002\u516c\u5f0f\u4e3a\uff1a$M_s\\left(F^{\\prime}\\right)=\\sigma\\left(f_{7 \\times 7}\\left(\\left[\\operatorname{CAP}\\left(F^{\\prime}\\right) ; \\operatorname{CMP}\\left(F^{\\prime}\\right)\\right]\\right)\\right)$<br \/>\n\uff0d\u8bbe\u8ba1\u52a8\u673a\uff1a\u5927\u5377\u79ef\u6838\uff08 $7 \\times 7$ \uff09\u80fd\u6355\u83b7\u5e7f\u57df\u7a7a\u95f4\u5173\u7cfb\uff0c\u5b9e\u9a8c\u8868\u660e\u5176\u6bd4 $3 \\times 3$ \u5377\u79ef\u964d\u4f4eTop\uff0d 1 \u9519\u8bef\u7387 $0.22 \\%$\u3002<\/p>\n<p>3\uff0e\u7279\u5f81\u91cd\u6807\u5b9a\uff1a\u5c06\u6743\u91cd $M_s$ \u4e0e\u8f93\u5165\u7279\u5f81\u9010\u4f4d\u7f6e\u76f8\u4e58\uff0c\u5f3a\u5316\u76ee\u6807\u533a\u57df\u54cd\u5e94\u3002<br \/>\n\u521b\u65b0\u70b9\uff1a<br \/>\n\uff0d\u901a\u9053\uff0d\u7a7a\u95f4\u534f\u540c\uff1a\u5148\u901a\u8fc7\u901a\u9053\u6ce8\u610f\u529b\u89e3\u51b3\uff02what\uff02\uff08\u8bed\u4e49\u91cd\u8981\u6027\uff09\uff0c\u518d\u901a\u8fc7\u7a7a\u95f4\u6ce8\u610f\u529b\u5b9a\u4f4d\uff02where\uff02\uff08\u7a7a\u95f4\u663e\u8457\u6027\uff09\uff0c\u4e24\u8005\u4e32\u8054\u6bd4\u5e76\u884c\u6548\u679c\u66f4\u4f18\uff08\u9519\u8bef\u7387\u964d\u4f4e $0.12 \\%$ \uff0c\u89c1\u88683\uff09\u3002<br \/>\n\uff0d\u9ad8\u6548\u6027\uff1a\u4ec5\u589e\u52a0 1 \u4e2a $7 \\times 7$ \u5377\u79ef\u5c42\uff0c\u8ba1\u7b97\u5f00\u9500\u53ef\u5ffd\u7565\uff08ResNet50\u4ec5\u589e\u52a00\uff0e004 GFLOPs\uff09\u3002<\/p>\n<h1>\u7ed3\u8bba<\/h1>\n<ul>\n<li>\n<p>\u672c\u7814\u7a76\u63d0\u51faCBAM\u6a21\u5757\uff0c\u901a\u8fc7\u8054\u5408\u4f18\u5316\u901a\u9053\u4e0e\u7a7a\u95f4\u6ce8\u610f\u529b\u673a\u5236\uff0c\u4e3aCNN\u7279\u5f81\u8868\u793a\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u901a\u7528\u589e\u5f3a\u65b9\u6848\uff0c\u5728\u4fdd\u6301\u8ba1\u7b97\u6548\u7387\u7684\u540c\u65f6\u663e\u8457\u63d0\u5347\u6a21\u578b\u6027\u80fd<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<ol>\n<li>\n<p>\u53cc\u6ce8\u610f\u529b\u673a\u5236\u6bd4\u5355\u4e00\u901a\u9053\u6ce8\u610f\u529b\uff08\u5982SENet\uff09\u5177\u6709\u66f4\u7cbe\u7ec6\u7684\u7279\u5f81\u9009\u62e9\u80fd\u529b<\/p>\n<\/li>\n<li>\n<p>\u5373\u63d2\u5373\u7528\u8bbe\u8ba1\u517c\u5bb9\u4e3b\u6d41CNN\u67b6\u6784<br \/>\n<strong>\u5c40\u9650<\/strong>\uff1a<br \/>\n\u672a\u8ba8\u8bba\u6a21\u5757\u5728\u8d85\u6df1\u7f51\u7edc\uff08\u5982ResNet152+\uff09\u4e2d\u7684\u53ef\u6269\u5c55\u6027<br \/>\n<strong>\u4e3b\u8981\u7ed3\u8bba<\/strong>\uff1a<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<ol>\n<li>\u8054\u5408\u4f7f\u7528max-pooling\u4e0eaverage-pooling\u7684\u901a\u9053\u6ce8\u610f\u529b\u6a21\u5757\u6bd4SENet\u4ea7\u751f\u66f4\u7cbe\u786e\u7684\u6ce8\u610f\u529b\u56fe<\/li>\n<li>\u7a7a\u95f4\u6ce8\u610f\u529b\u6a21\u5757\u901a\u8fc77\u00d77\u5377\u79ef\u6709\u6548\u6355\u6349\u4f4d\u7f6e\u91cd\u8981\u6027\uff0c\u4e0e\u901a\u9053\u6ce8\u610f\u529b\u5f62\u6210\u4e92\u8865<\/li>\n<li>\u5728ImageNet-1K\u3001MS COCO\u548cVOC 2007\u4e09\u5927\u57fa\u51c6\u6d4b\u8bd5\u4e2d\u5747\u8d85\u8d8a\u57fa\u7ebf\u6a21\u578b<\/li>\n<li>\u53ef\u89c6\u5316\u8bc1\u5b9e\u6a21\u5757\u80fd\u5f15\u5bfc\u7f51\u7edc\u51c6\u786e\u805a\u7126\u76ee\u6807\u7269\u4f53\u533a\u57df<\/li>\n<\/ol>\n<h1>Pytorch code<\/h1>\n<pre><code class=\"language-python\">import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass CBAM(nn.Module):\n    def __init__(self, in_channels, reduction_ratio=16, kernel_size=7):\n        &quot;&quot;&quot;\n        CBAM \u6a21\u5757\u5b9e\u73b0\n        Args:\n            in_channels: \u8f93\u5165\u7279\u5f81\u56fe\u7684\u901a\u9053\u6570\n            reduction_ratio: \u901a\u9053\u6ce8\u610f\u529b\u4e2d\u7684\u538b\u7f29\u6bd4\u4f8b\n            kernel_size: \u7a7a\u95f4\u6ce8\u610f\u529b\u4e2d\u7684\u5377\u79ef\u6838\u5927\u5c0f\n        &quot;&quot;&quot;\n        super(CBAM, self).__init__()\n\n        # \u901a\u9053\u6ce8\u610f\u529b\u6a21\u5757\n        self.channel_attention = ChannelAttention(in_channels, reduction_ratio)\n\n        # \u7a7a\u95f4\u6ce8\u610f\u529b\u6a21\u5757\n        self.spatial_attention = SpatialAttention(kernel_size)\n\n    def forward(self, x):\n        # \u5148\u5e94\u7528\u901a\u9053\u6ce8\u610f\u529b\n        x = self.channel_attention(x)\n\n        # \u518d\u5e94\u7528\u7a7a\u95f4\u6ce8\u610f\u529b\n        x = self.spatial_attention(x)\n\n        return x\n\nclass ChannelAttention(nn.Module):\n    def __init__(self, in_channels, reduction_ratio=16):\n        super(ChannelAttention, self).__init__()\n        self.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.max_pool = nn.AdaptiveMaxPool2d(1)\n\n        # \u5171\u4eabMLP\n        self.mlp = nn.Sequential(\n            nn.Conv2d(in_channels, in_channels \/\/ reduction_ratio, 1, bias=False),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(in_channels \/\/ reduction_ratio, in_channels, 1, bias=False)\n        )\n\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, x):\n        # \u5e73\u5747\u6c60\u5316\u5206\u652f\n        avg_out = self.mlp(self.avg_pool(x))\n\n        # \u6700\u5927\u6c60\u5316\u5206\u652f\n        max_out = self.mlp(self.max_pool(x))\n\n        # \u5408\u5e76\u5206\u652f\n        channel_weights = self.sigmoid(avg_out + max_out)\n\n        return x * channel_weights\n\nclass SpatialAttention(nn.Module):\n    def __init__(self, kernel_size=7):\n        super(SpatialAttention, self).__init__()\n\n        assert kernel_size in (3, 7), &quot;kernel size must be 3 or 7&quot;\n        padding = 3 if kernel_size == 7 else 1\n\n        self.conv = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, x):\n        # \u6cbf\u901a\u9053\u7ef4\u5ea6\u8ba1\u7b97\u5e73\u5747\u503c\u548c\u6700\u5927\u503c\n        avg_out = torch.mean(x, dim=1, keepdim=True)\n        max_out, _ = torch.max(x, dim=1, keepdim=True)\n\n        # \u62fc\u63a5\u7279\u5f81\n        combined = torch.cat([avg_out, max_out], dim=1)\n\n        # \u751f\u6210\u7a7a\u95f4\u6ce8\u610f\u529b\u56fe\n        spatial_weights = self.sigmoid(self.conv(combined))\n\n        return x * spatial_weights\n\n# ------------------- \u7528\u6cd5\u793a\u4f8b -------------------\nif __name__ == &quot;__main__&quot;:\n    # 1. \u521d\u59cb\u5316CBAM\u6a21\u5757\uff08\u8f93\u5165\u901a\u9053\u6570\u4e3a256\uff0c\u538b\u7f29\u6bd4\u4f8b16\uff09\n    cbam = CBAM(in_channels=256, reduction_ratio=16)\n\n    # 2. \u6a21\u62df\u8f93\u5165\u6570\u636e\uff08batch_size=4, \u901a\u9053=256, \u7279\u5f81\u56fe\u5c3a\u5bf8=56x56\uff09\n    dummy_input = torch.randn(4, 256, 56, 56)\n\n    # 3. \u524d\u5411\u4f20\u64ad\n    output = cbam(dummy_input)\n\n    print(f&quot;\u8f93\u5165\u5f62\u72b6: {dummy_input.shape}&quot;)\n    print(f&quot;\u8f93\u51fa\u5f62\u72b6: {output.shape}&quot;)  # \u5e94\u4e0e\u8f93\u5165\u5f62\u72b6\u4e00\u81f4<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u4fe1\u606f \ud83d\udcf0\u6807\u9898: CBAM: Convolutional Block Attention Module \ud83d\udd8b\ufe0f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3183,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,18],"tags":[],"class_list":["post-3182","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30","category-18"],"_links":{"self":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/3182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3182"}],"version-history":[{"count":15,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/3182\/revisions"}],"predecessor-version":[{"id":3231,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/3182\/revisions\/3231"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/media\/3183"}],"wp:attachment":[{"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3182"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}