daniilidis-group / neural_renderer

A PyTorch port of the Neural 3D Mesh Renderer
Other
1.14k stars 254 forks source link

How to use for rendering 2d vertex coordinates into mask? #58

Open shivamsaboo17 opened 5 years ago

shivamsaboo17 commented 5 years ago

How can I use this library to render 2d image from coordinates (polygon). I have predictions in form of vertex coordinates (batch, num_points, x, y) and I want to render it into 2D binary mask. Is is possible? Any insight would be really helpful!

shivamsaboo17 commented 5 years ago

I tried to just run a forward pass like this to render a simple square. But I am getting segmentation fault. I am trying with 2d images hence I used z coordinate as 0 in vertices.

"""
Example 2. Optimizing vertices.
"""
from __future__ import division
import os
import argparse
import glob

import torch
import torch.nn as nn
import numpy as np
from skimage.io import imread, imsave
import tqdm
import imageio

import neural_renderer as nr

current_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(current_dir, 'data')

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        # load .obj
        #vertices, faces = nr.load_obj(filename_obj)
        vertices = torch.from_numpy(np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0]]).reshape(4, 3).astype(np.float32)).float()
        faces = torch.from_numpy(np.array([[0, 1, 2], [0, 2, 3]]).reshape(2, 3).astype(np.int64)).int()
        self.vertices = nn.Parameter(vertices[None, :, :])
        self.register_buffer('faces', faces[None, :, :])
        texture_size = 2
        textures = torch.ones(1, self.faces.shape[1], texture_size, texture_size, texture_size, 3, dtype=torch.float32)
        self.register_buffer('textures', textures)
        print(self.vertices.shape)
        print(self.faces.shape)

        # create textures
        #texture_size = 2
        #textures = torch.ones(1, self.faces.shape[1], texture_size, texture_size, texture_size, 3, dtype=torch.float32)
        #self.register_buffer('textures', textures)

        # load reference image
        #image_ref = torch.from_numpy(imread(filename_ref).astype(np.float32).mean(-1) / 255.)[None, ::]
        #self.register_buffer('image_ref', image_ref)

        # setup renderer
        renderer = nr.Renderer(camera_mode='look_at')
        self.renderer = renderer

    def forward(self):
        self.renderer.eye = nr.get_points_from_angles(0, 0, 0)
        image = self.renderer(self.vertices, self.faces, textures=self.textures, mode=None)
        #loss = torch.sum((image - self.image_ref[None, :, :])**2)
        return image

model = Model().cuda()
img = model()
print(img.shape)
emlcpfx commented 1 year ago

@shivamsaboo17 were you trying to render a UV pass like this? Did you figure it out?

image