origin
This commit is contained in:
2
digit_depth/train/__init__.py
Normal file
2
digit_depth/train/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .color2normal_dataset import Color2NormalDataset
|
||||
from .mlp_model import MLP
|
||||
21
digit_depth/train/color2normal_dataset.py
Normal file
21
digit_depth/train/color2normal_dataset.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import pandas as pd
|
||||
import torch
|
||||
from torch.utils.data import Dataset
|
||||
|
||||
|
||||
class Color2NormalDataset(Dataset):
|
||||
def __init__(self, csv):
|
||||
self.data = pd.read_csv(csv)
|
||||
def __len__(self):
|
||||
return self.data['X'].count()
|
||||
|
||||
def __getitem__(self,idx):
|
||||
x = self.data['X'][idx]/120
|
||||
y = self.data['Y'][idx]/160
|
||||
r = self.data['R'][idx]/255
|
||||
g = self.data['G'][idx]/255
|
||||
b = self.data['B'][idx]/255
|
||||
nx = self.data['Nx'][idx]/255
|
||||
ny = self.data['Ny'][idx]/255
|
||||
nz= self.data['Nz'][idx]/255
|
||||
return torch.tensor((x, y, r, g, b), dtype=torch.float32), torch.tensor((nx, ny, nz), dtype=torch.float32)
|
||||
27
digit_depth/train/mlp_model.py
Normal file
27
digit_depth/train/mlp_model.py
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
|
||||
class MLP(nn.Module):
|
||||
dropout_p = 0.05
|
||||
|
||||
def __init__(
|
||||
self, input_size=5, output_size=3, hidden_size=32):
|
||||
super().__init__()
|
||||
|
||||
self.fc1 = nn.Linear(input_size, hidden_size)
|
||||
self.fc2 = nn.Linear(hidden_size, hidden_size)
|
||||
self.fc3 = nn.Linear(hidden_size, hidden_size)
|
||||
self.fc4 = nn.Linear(hidden_size, output_size)
|
||||
self.drop = nn.Dropout(p=self.dropout_p)
|
||||
|
||||
def forward(self, x):
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.drop(x)
|
||||
x = F.relu(self.fc2(x))
|
||||
x = self.drop(x)
|
||||
x = self.fc3(x)
|
||||
x = self.drop(x)
|
||||
x = self.fc4(x)
|
||||
return x
|
||||
49
digit_depth/train/prepost_mlp.py
Normal file
49
digit_depth/train/prepost_mlp.py
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
import cv2
|
||||
import torch
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import copy
|
||||
seed = 42
|
||||
torch.seed = seed
|
||||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
||||
|
||||
|
||||
def preproc_mlp(image) -> torch.Tensor:
|
||||
""" Preprocess image for input to model.
|
||||
|
||||
Args: image: OpenCV image in BGR format
|
||||
Return: tensor of shape (R*C,5) where R=320 and C=240 for DIGIT images
|
||||
5-columns are: X,Y,R,G,B
|
||||
|
||||
"""
|
||||
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
||||
img = img / 255.0
|
||||
xy_coords = np.flip(np.column_stack(np.where(np.all(img >= 0, axis=2))), axis=1)
|
||||
rgb = np.reshape(img, (np.prod(img.shape[:2]), 3))
|
||||
pixel_numbers = np.expand_dims(np.arange(1, xy_coords.shape[0] + 1), axis=1)
|
||||
value_base = np.hstack([pixel_numbers, xy_coords, rgb])
|
||||
df_base = pd.DataFrame(value_base, columns=['pixel_number', 'X', 'Y', 'R', 'G', 'B'])
|
||||
df_base['X'] = df_base['X'] / 240
|
||||
df_base['Y'] = df_base['Y'] / 320
|
||||
del df_base['pixel_number']
|
||||
test_tensor = torch.tensor(df_base[['X', 'Y', 'R', 'G', 'B']].values, dtype=torch.float32).to(device)
|
||||
return test_tensor
|
||||
|
||||
|
||||
def post_proc_mlp(model_output: torch.Tensor):
|
||||
""" Postprocess model output to get normal map.
|
||||
|
||||
Args: model_output: torch.Tensor of shape (1,3)
|
||||
Return: two torch.Tensor of shape (1,3)
|
||||
|
||||
"""
|
||||
test_np = model_output.reshape(320, 240, 3)
|
||||
normal = copy.deepcopy(test_np) # surface normal image
|
||||
test_np = torch.tensor(test_np,
|
||||
dtype=torch.float32) # convert to torch tensor for later processing in gradient computation
|
||||
test_np = test_np.permute(2, 0, 1) # swap axes to (3,320,240)
|
||||
test_np = test_np # convert to uint8 for visualization
|
||||
return test_np, normal
|
||||
|
||||
|
||||
Reference in New Issue
Block a user