How to realize the attention Mechanism of web
This article introduces the relevant knowledge of "how to realize the attention mechanism of web". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Channel attention mechanism:
Import torchfrom torch import nnclass ChannelAttention (nn.Module): def _ init__ (self, in_planes, ratio=16): super (ChannelAttention, self). _ init__ () self.avg_pool = nn.AdaptiveAvgPool2d (1) self.max_pool = nn.AdaptiveMaxPool2d (1) self.fc1 = nn.Conv2d (in_planes, in_planes / / ratio, 1 Bias=False) self.relu1 = nn.ReLU () self.fc2 = nn.Conv2d (in_planes / / ratio, in_planes, 1, bias=False) self.sigmoid = nn.Sigmoid () def forward (self X): avg_out = self.fc2 (self.relu1 (self.fc1 (self.avg_pool (x) max_out = self.fc2 (self.relu1 (self.fc1 (self.max_pool (x) out = avg_out + max_out return self.sigmoid (out) if _ name__ = ='_ main__':CA = ChannelAttention (32) data_in = torch.randn (8Power32300300) data_out = CA (data_in) print (data_in.shape) # torch.Size ([8 32,300,300]) print (data_out.shape) # torch.Size ([8,32,1,1])
Console output result:
It took 958 milliseconds to load personal and system profiles. (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > &'D:\ Anaconda3\ envs\ ssd4pytorch2_2_0\ python.exe''c:\ Users\ chenxuqi\ .vscode\ extensions\ ms-python.python-2021.1.502429796\ pythonFiles\ lib\ python\ debugpy\ launcher' '53813' -' c:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq\ test1.py'torch.Size ([8,32,300,300]) torch.Size ([8,32,1]) 1]) (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > conda activate ssd4pytorch2_2_0 (ssd4pytorch2_2_0) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq >
Channel attention mechanism:
Import torchfrom torch import nnclass SpatialAttention (nn.Module): def _ _ init__ (self, kernel_size=7): super (SpatialAttention, self). _ _ init__ () assert kernel_size in (3,7), 'kernel size must be 3 or 7'padding = 3 if kernel_size= = 7 else 1self.conv1 = nn.Conv2d (2, 1, kernel_size, padding=padding, bias=False) # 73meme 3Lself.sigmoid = nn.Sigmoid def forward (self, x): avg_out = torch.mean (x, dim=1, keepdim=True) max_out _ = torch.max (x, dim=1, keepdim=True) x = torch.cat ([avg_out, max_out], dim=1) x = self.conv1 (x) return self.sigmoid (x) if _ name__ ='_ main__':SA = SpatialAttention (7) data_in = torch.randn (832300300) data_out = SA (data_in) print (data_in.shape) # torch.Size ([832,300,300]) print (data_out.shape) # torch.Size ([8,1,300]) 300])
Console output result:
It took 959 milliseconds to load personal and system profiles. (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > conda activate ssd4pytorch2_2_0 (ssd4pytorch2_2_0) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > &'D:\ Anaconda3\ envs\ ssd4pytorch2_2_0\ python.exe''c:\ Users\ chenxuqi\ .vscode\ extensions\ ms-python.python-2021.1.502429796\ lib\ python\ debugpy\ launcher' '53827' -' c:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq\ test2.py'torch.Size ([8 32,300,300]) torch.Size ([8,1,300,300]) (ssd4pytorch2_2_0) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > "how to implement the attention mechanism of web" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!