File size: 3,860 Bytes
2252f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-

# Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is
# holder of all proprietary rights on this computer program.
# You can only use this computer program if you have closed
# a license agreement with MPG or you get the right to use the computer
# program from someone who is authorized to grant you that right.
# Any use of the computer program without a valid license is prohibited and
# liable to prosecution.
#
# Copyright©2019 Max-Planck-Gesellschaft zur Förderung
# der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute
# for Intelligent Systems. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de

from lib.net.FBNet import define_G
from lib.net.net_util import init_net, VGGLoss
from lib.net.HGFilters import *
from lib.net.BasePIFuNet import BasePIFuNet
import torch
import torch.nn as nn


class Hallucinator(BasePIFuNet):
    '''
    HG PIFu network uses Hourglass stacks as the image filter.
    It does the following:
        1. Compute image feature stacks and store it in self.im_feat_list
            self.im_feat_list[-1] is the last stack (output stack)
        2. Calculate calibration
        3. If training, it index on every intermediate stacks,
            If testing, it index on the last stack.
        4. Classification.
        5. During training, error is calculated on all stacks.
    '''

    def __init__(self, cfg, error_term=nn.SmoothL1Loss()):

        super(Hallucinator, self).__init__(error_term=error_term)

        self.l1_loss = nn.SmoothL1Loss()

        self.opt = cfg.net

        if self.training:
            self.vgg_loss = [VGGLoss()]

        self.in_nmlB = [
            item[0] for item in self.opt.in_nml
            if '_B' in item[0] or item[0] == 'image'
        ]
        self.in_nmlL = [
            item[0] for item in self.opt.in_nml
            if '_L' in item[0] or item[0] == 'image'
        ]
        self.in_nmlB_dim = sum([
            item[1] for item in self.opt.in_nml
            if '_B' in item[0] or item[0] == 'image'
        ])
        self.in_nmlL_dim = sum([
            item[1] for item in self.opt.in_nml
            if '_L' in item[0] or item[0] == 'image'
        ])

        self.netB = define_G(self.in_nmlB_dim, 3, 64, "global", 4, 9, 1, 3,
                             "instance")
        self.netL = define_G(self.in_nmlL_dim, 3, 64, "global", 4, 9, 1, 3,
                             "instance")

        init_net(self)

    def forward(self, in_tensor):

        inB_list = []
        inL_list = []

        for name in self.in_nmlB:
            inB_list.append(in_tensor[name])
        for name in self.in_nmlL:
            inL_list.append(in_tensor[name])

        nmlB = self.netB(torch.cat(inB_list, dim=1))
        nmlL = self.netL(torch.cat(inL_list, dim=1))

        # ||normal|| == 1
        nmlB = nmlB / torch.norm(nmlB, dim=1, keepdim=True)
        nmlL = nmlL / torch.norm(nmlL, dim=1, keepdim=True)

        # output: float_arr [-1,1] with [B, C, H, W]

        mask = (in_tensor['image'].abs().sum(dim=1, keepdim=True) !=
                0.0).detach().float()

        nmlB = nmlB * mask
        #nmlL = nmlL * mask

        return nmlB, nmlL

    def get_norm_error(self, prd_B, prd_L, tgt):
        """calculate normal loss

        Args:
            pred (torch.tensor): [B, 6, 512, 512]
            tagt (torch.tensor): [B, 6, 512, 512]
        """

        tgt_B, tgt_L = tgt['render_B'], tgt['render_L']

        l1_B_loss = self.l1_loss(prd_B, tgt_B)
        l1_L_loss = self.l1_loss(prd_L, tgt_L)

        with torch.no_grad():
            vgg_B_loss = self.vgg_loss[0](prd_B, tgt_B)
            vgg_L_loss = self.vgg_loss[0](prd_L, tgt_L)

        total_loss = [
            5.0 * l1_B_loss + vgg_B_loss, 5.0 * l1_L_loss + vgg_L_loss
        ]

        return total_loss