Files
reconstruction/models/depth_estim.py
T
2023-01-10 14:02:11 +00:00

65 lines
1.9 KiB
Python

import os
import sys
import torch
import torch.nn as nn
from torch.nn import functional as F
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
from models.convnext import ConvNeXt, CNBlockConfig
class ViewEncoder(nn.Module):
def __init__(self, features=128, device='cuda'):
super().__init__()
self.features: int = features
self.lin1 = nn.LazyLinear(features).to(device)
self.act1 = nn.GELU().to(device)
self.lin2 = nn.Linear(features, features * 15 * 20).to(device)
def forward(self, input: torch.Tensor) -> torch.Tensor:
x: torch.Tensor = self.lin1(input)
x = self.act1(x)
x = self.lin2(x)
x = x.view(x.shape[0],self.features,15,20)
return x
class DepthEstimation(nn.Module):
def __init__(self, view_features=128, device='cuda'):
super().__init__()
block_setting = [
CNBlockConfig(96, 192, 3),
CNBlockConfig(192, 384, 3),
CNBlockConfig(384, 768, 9),
CNBlockConfig(768, None, 3),
]
block_setting_reverse = [
CNBlockConfig(768 + view_features, 384, 3),
CNBlockConfig(384, 192, 9),
CNBlockConfig(192, 96, 3),
CNBlockConfig(96, 27, 3),
CNBlockConfig(27, 9, 3),
CNBlockConfig(9, None, 3),
]
self.convnext = ConvNeXt(block_setting, device=device)
self.flatten = nn.Flatten()
self.decoder = ConvNeXt(block_setting_reverse, reversed=True, device=device)
self.view_encoder = ViewEncoder(view_features)
def forward(self, input) -> torch.Tensor:
image, view = input
x, features = self.convnext(image)
view_features = self.view_encoder(view)
# x = self.flatten(x)
x = self.decoder([torch.concat((x, view_features), axis=1),features])
return x