如何获取YOLOv5的第一个卷积层生成的特征图?

huangapple go评论53阅读模式
英文:

How to obtain feature map generated from first convolution layer of YOLOv5?

问题

我想获取YOLOv5(PyTorch)第一个卷积层生成的特征图。我该如何获得特征图?

英文:

I would like to obtain feature map generated from first convolution layer of YOLOv5 (PyTorch). How can I get the feature map?

答案1

得分: 1

以下是翻译好的部分:

"Here I save the output from the first conv and pooling layer as x1, and return this value as well in the forward call.":
"在这里,我将第一个卷积和池化层的输出保存为 x1,并在 forward 调用中返回这个值。"

"Now if you try to use this approach in an off-the-shelf manner (i.e. if you didn't write your own training script and loss function) this will likely cause an error during training as the loss function call probably does not expect an additional output from forward.":
"现在,如果你尝试以现成的方式使用这种方法(即如果你没有编写自己的训练脚本和损失函数),在训练过程中可能会导致错误,因为损失函数调用可能不会预期 forward 中有额外的输出。"

"Thus, the preferred solution for drop-in modifications to existing nn libraries is to use the register_hook approach linked in the comments.":
"因此,对于对现有 nn 库进行插入式修改的首选解决方案是使用评论中链接的 register_hook 方法。"

"Here's a trivial example network from pytorch.":
"这是来自 pytorch 的一个简单示例网络。"

"import torch.nn as nn":
"导入 torch.nn as nn"

"import torch.nn.functional as F":
"导入 torch.nn.functional as F"

"class Net(nn.Module):":
"class Net(nn.Module):"

"def init(self):":
"def init(self):"

"self.conv1 = nn.Conv2d(3, 6, 5)":
"self.conv1 = nn.Conv2d(3, 6, 5)"

"self.pool = nn.MaxPool2d(2, 2)":
"self.pool = nn.MaxPool2d(2, 2)"

"self.conv2 = nn.Conv2d(6, 16, 5)":
"self.conv2 = nn.Conv2d(6, 16, 5)"

"self.fc1 = nn.Linear(16 * 5 * 5, 120)":
"self.fc1 = nn.Linear(16 * 5 * 5, 120)"

"self.fc2 = nn.Linear(120, 84)":
"self.fc2 = nn.Linear(120, 84)"

"self.fc3 = nn.Linear(84, 10)":
"self.fc3 = nn.Linear(84, 10)"

"def forward(self, x):":
"def forward(self, x):"

"x1 = self.pool(F.relu(self.conv1(x)))":
"x1 = self.pool(F.relu(self.conv1(x)))"

"x = self.pool(F.relu(self.conv2(x1)))":
"x = self.pool(F.relu(self.conv2(x1)))"

"x = torch.flatten(x, 1) # flatten all dimensions except batch":
"x = torch.flatten(x, 1) # 拉平除批处理之外的所有维度"

"x = F.relu(self.fc1(x))":
"x = F.relu(self.fc1(x))"

"x = F.relu(self.fc2(x))":
"x = F.relu(self.fc2(x))"

"x = self.fc3(x)":
"x = self.fc3(x)"

"net = Net()":
"net = Net()"

英文:

It is trivial to modify the network structure to save an intermediate output. Here I save the output from the first conv and pooling layer as x1, and return this value as well in the forward call. Now if you try to use this approach in an off-the-shelf manner (i.e. if you didn't write your own training script and loss function) this will likely cause an error during training as the loss function call probably does not expect an additional output from forward. Thus, the preferred solution for drop-in modifications to existing nn libraries is to use the register_hook approach linked in the comments.

Here's a trivial example network from pytorch.

import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):
  def __init__(self):
    super().__init__()
    self.conv1 = nn.Conv2d(3, 6, 5)
    self.pool = nn.MaxPool2d(2, 2)
    self.conv2 = nn.Conv2d(6, 16, 5)
    self.fc1 = nn.Linear(16 * 5 * 5, 120)
    self.fc2 = nn.Linear(120, 84)
    self.fc3 = nn.Linear(84, 10)

  def forward(self, x):
    x1 = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x1)))
    x = torch.flatten(x, 1) # flatten all dimensions except batch
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x,x1


net = Net()

huangapple
  • 本文由 发表于 2023年5月24日 18:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定