text
stringlengths
1
93.6k
def forwardEnv(self, diffusePred, normalPred, roughPred, envmap):
envR, envC = envmap.size(2), envmap.size(3)
bn = diffusePred.size(0)
diffusePred = F.adaptive_avg_pool2d(diffusePred, (envR, envC) )
normalPred = F.adaptive_avg_pool2d(normalPred, (envR, envC) )
normalPred = normalPred / torch.sqrt( torch.clamp(
torch.sum(normalPred * normalPred, dim=1 ), 1e-6, 1).unsqueeze(1) )
roughPred = F.adaptive_avg_pool2d(roughPred, (envR, envC ) )
temp = Variable(torch.FloatTensor(1, 1, 1, 1,1) )
if self.isCuda:
temp = temp.cuda()
ldirections = self.ls.unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
camyProj = torch.einsum('b,abcd->acd',(self.up, normalPred)).unsqueeze(1).expand_as(normalPred) * normalPred
camy = F.normalize(self.up.unsqueeze(0).unsqueeze(-1).unsqueeze(-1).expand_as(camyProj) - camyProj, dim=1)
camx = -F.normalize(torch.cross(camy, normalPred,dim=1), dim=1)
l = ldirections[:, :, 0:1, :, :] * camx.unsqueeze(1) \
+ ldirections[:, :, 1:2, :, :] * camy.unsqueeze(1) \
+ ldirections[:, :, 2:3, :, :] * normalPred.unsqueeze(1)
h = (self.v.unsqueeze(1) + l) / 2;
h = h / torch.sqrt(torch.clamp(torch.sum(h*h, dim=2), min = 1e-6).unsqueeze(2) )
vdh = torch.sum( (self.v * h), dim = 2).unsqueeze(2)
temp.data[0] = 2.0
frac0 = self.F0 + (1-self.F0) * torch.pow(temp.expand_as(vdh), (-5.55472*vdh-6.98316)*vdh)
diffuseBatch = (diffusePred )/ np.pi
roughBatch = (roughPred + 1.0)/2.0
k = (roughBatch + 1) * (roughBatch + 1) / 8.0
alpha = roughBatch * roughBatch
alpha2 = alpha * alpha
ndv = torch.clamp(torch.sum(normalPred * self.v.expand_as(normalPred), dim = 1), 0, 1).unsqueeze(1).unsqueeze(2)
ndh = torch.clamp(torch.sum(normalPred.unsqueeze(1) * h, dim = 2), 0, 1).unsqueeze(2)
ndl = torch.clamp(torch.sum(normalPred.unsqueeze(1) * l, dim = 2), 0, 1).unsqueeze(2)
frac = alpha2.unsqueeze(1).expand_as(frac0) * frac0
nom0 = ndh * ndh * (alpha2.unsqueeze(1).expand_as(ndh) - 1) + 1
nom1 = ndv * (1 - k.unsqueeze(1).expand_as(ndh) ) + k.unsqueeze(1).expand_as(ndh)
nom2 = ndl * (1 - k.unsqueeze(1).expand_as(ndh) ) + k.unsqueeze(1).expand_as(ndh)
nom = torch.clamp(4*np.pi*nom0*nom0*nom1*nom2, 1e-6, 4*np.pi)
specPred = frac / nom
envmap = envmap.view([bn, 3, envR, envC, self.envWidth * self.envHeight ] )
envmap = envmap.permute([0, 4, 1, 2, 3] )
brdfDiffuse = diffuseBatch.unsqueeze(1).expand([bn, self.envWidth * self.envHeight, 3, envR, envC] ) * \
ndl.expand([bn, self.envWidth * self.envHeight, 3, envR, envC] )
colorDiffuse = torch.sum(brdfDiffuse * envmap * self.envWeight.expand_as(brdfDiffuse), dim=1)
brdfSpec = specPred.expand([bn, self.envWidth * self.envHeight, 3, envR, envC ] ) * \
ndl.expand([bn, self.envWidth * self.envHeight, 3, envR, envC] )
colorSpec = torch.sum(brdfSpec * envmap * self.envWeight.expand_as(brdfSpec), dim=1)
return colorDiffuse, colorSpec
def BatchRankingLoss(albedoPred, eqPoint, eqWeight, darkerPoint, darkerWeight):
tau = 0.5
height, width = albedoPred.size(1), albedoPred.size(2)
reflectance = torch.mean(albedoPred, dim=0)
reflectLog = torch.log(reflectance + 0.001)
reflectLog = reflectLog.view(-1)
eqPoint = Variable(torch.from_numpy(eqPoint ).long() ).cuda( )
eqWeight = Variable(torch.from_numpy(eqWeight ).float() ).cuda( )
darkerPoint = Variable(torch.from_numpy(darkerPoint ).long() ).cuda( )
darkerWeight = Variable(torch.from_numpy(darkerWeight ).float() ).cuda( )
# compute the eq loss
r1, c1, r2, c2 = torch.split(eqPoint, 1, dim=1)
p1 = (r1 * width + c1).view(-1)
p1.requires_grad = False
p2 = (r2 * width + c2).view(-1)
p2.requires_grad = False
rf1 = torch.index_select(reflectLog, 0, p1)
rf2 = torch.index_select(reflectLog, 0, p2)
eqWeight = eqWeight.view(-1)
eqLoss = torch.mean(eqWeight * torch.pow(rf1 - rf2, 2) )
# compute the darker loss
r1, c1, r2, c2 = torch.split(darkerPoint, 1, dim=1)
p1 = (r1 * width + c1).view(-1)
p1.requires_grad = False
p2 = (r2 * width + c2).view(-1)
p2.requires_grad = False
rf1 = torch.index_select(reflectLog, 0, p1)
rf2 = torch.index_select(reflectLog, 0, p2)
darkerWeight = darkerWeight.view(-1)