natanielruiz
2017-09-27 0be0ecf0a8fc6df1f9e354f8aea12b7008f658f1
code/hopenet.py
@@ -341,11 +341,11 @@
        return pre_yaw, pre_pitch, pre_roll, angles, sr_output
class Hopenet_LSTM(nn.Module):
class Hopenet_new(nn.Module):
    # This is just Hopenet with 3 output layers for yaw, pitch and roll.
    def __init__(self, block, layers, num_bins):
        self.inplanes = 64
        super(Hopenet_LSTM, self).__init__()
        super(Hopenet_new, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
@@ -361,14 +361,11 @@
        self.fc_roll = nn.Linear(512 * block.expansion, num_bins)
        self.softmax = nn.Softmax()
        self.fc_finetune = nn.Linear(512 * block.expansion + 3, 3)
        self.fc_finetune_new = nn.Linear(512 * block.expansion + 256 * block.expansion + 3, 3)
        self.conv1x1 = nn.Conv2d(1024, 64, kernel_size = 1, stride = 1, bias=False)
        self.maxpool_interm = nn.MaxPool2d(kernel_size=5, stride=3, padding=1)
        self.idx_tensor = Variable(torch.FloatTensor(range(66))).cuda()
        self.lstm = nn.LSTM(512 * block.expansion + 3, 256 * block.expansion, 2, batch_first=True)
        self.fc_lstm = nn.Linear(256 * block.expansion, 3)
        self.block_expansion = block.expansion
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
@@ -396,7 +393,6 @@
        return nn.Sequential(*layers)
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
@@ -405,6 +401,11 @@
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x_interm = self.conv1x1(x)
        x_interm = self.relu(x_interm)
        x_interm = self.maxpool_interm(x_interm)
        x_interm = x_interm.view(x_interm.size(0), -1)
        x = self.layer4(x)
        x = self.avgpool(x)
@@ -413,7 +414,6 @@
        pre_pitch = self.fc_pitch(x)
        pre_roll = self.fc_roll(x)
        # Yaw, pitch, roll
        yaw = self.softmax(pre_yaw)
        yaw = Variable(torch.sum(yaw.data * self.idx_tensor.data, 1), requires_grad=True) * 3 - 99
        pitch = self.softmax(pre_pitch)
@@ -425,8 +425,8 @@
        roll = roll.view(roll.size(0), 1)
        preangles = torch.cat([yaw, pitch, roll], 1)
        residuals, _ = self.lstm(torch.cat((preangles, x), 1), (h0, c0))
        residuals = self.fc_lstm(residuals[:, -1, :])
        # angles predicts the residual
        residuals = self.fc_finetune_new(torch.cat((preangles, x_interm, x), 1))
        final_angles = preangles + residuals
        return pre_yaw, pre_pitch, pre_roll, preangles, final_angles