Stacked Hourglass 论文笔记

# 小郑之家~

### stacked

• 下一个stacked的输入是什么？


def forward(self, x):
out = []
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)

x = self.layer1(x)
x = self.maxpool(x)

x = self.layer2(x)
x = self.layer3(x)

for i in range(self.num_stacks):
y = self.hg[i](x)  # 即在每一个stack之后都做了一个res的操作
#print("1", y.shape)
y = self.res[i](y)
#print("2", y.shape)
y = self.fc[i](y)
#print("3", y.shape)
score = self.score[i](y)
#print("4", score.shape)
out.append(score)

if i < self.num_stacks-1:  # 不是最后一个stack的还做了这样的处理。这样是有用的，即前面的stack的输出并不是直接暴力地相加的，这样做是要好一些的。
fc_ = self.fc_[i](y)
score_ = self.score_[i](score)
x = x + fc_ + score_   # 也就是说下一个stack的输入是上一个stack的输入加上本stack的输出score_和fc_,

return out



bn = nn.BatchNorm2d(inplanes)
conv = nn.Conv2d(inplanes, outplanes, kernel_size=1, bias=True)
return nn.Sequential(
conv,
bn,
self.relu,
)



score_就是一个卷积

 score_.append(nn.Conv2d(num_classes, ch, kernel_size=1, bias=True))



            y = self.hg[i](x)  # 即在每一个stack之后都做了一个res的操作
#print("1", y.shape)
y = self.res[i](y)
#print("2", y.shape)
y = self.fc[i](y)
#print("3", y.shape)
score = self.score[i](y)
#print("4", score.shape)
out.append(score)



class Bottleneck(nn.Module):
expansion = 2  #
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(inplanes)
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=True)
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
self.bn3 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 2, kernel_size=1, bias=True)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.bn1(x)
out = self.relu(out)
out = self.conv1(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn3(out)
out = self.relu(out)
out = self.conv3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
return out



class Hourglass(nn.Module):
def __init__(self, block, planes, depth):
super(Hourglass, self).__init__()
self.depth = depth
self.block = block
self.upsample = nn.Upsample(scale_factor=2)
self.hg = self._make_hour_glass(block, planes, depth)

def _make_residual(self, block, planes):
layer = block(planes*block.expansion, planes)
return layer                                         # 这个就相当于paper里面的一个小方框.

def _make_hour_glass(self, block, planes, depth):
hg = []
for i in range(depth):          # depth是下降时经过的block的个数，即paper里面的图的最左边的4个，res指的就是这个东西。
hg.append(self._make_residual(block , planes))
return nn.ModuleList(hg)

def _hour_glass_forward(self, n, x):    # n深度。  这个过程指的是从前一个深度到后一个深度的过程，是用递归写的。
# 注意最左边的是深度3,今次是3,2,1,0,这个只是记号而已.
up1 = self.hg[n-1](x)
low1 = F.max_pool2d(x, 2, stride=2)

if n > 1:
low2 = self._hour_glass_forward(n-1, low1)
else:
low2 = self.hg[n-1](low1)
low2 = F.max_pool2d(low2, 2, stride=2)

# 到这网络相当于shape到最底了。
# 然后是三个block。
for i in range(3):
low2 = self.hg[n-1](low2)
up2 = self.upsample(low2)
out = up1 + up2
return out

def forward(self, x):
return self._hour_glass_forward(self.depth, x)