Skip to content

Commit

Permalink
simple inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilit Yolyan committed Jun 16, 2022
1 parent 16fbc28 commit d10e5c9
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "chap9/chap9/synsin"]
path = chap9/chap9/synsin
url = https://github.com/facebookresearch/synsin
Binary file added chap9/appartement.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions chap9/chap9/synsin
Submodule synsin added at 501ec4
67 changes: 67 additions & 0 deletions chap9/inference_unseen_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import matplotlib.pyplot as plt

import quaternion
import torch
import torch.nn as nn
import torchvision.transforms as transforms

from PIL import Image
from set_up_model_for_inference import synsin_model


def inference(path_to_model, test_image, save_path):
model_to_test = synsin_model(path_to_model)
# Load the image
transform = transforms.Compose([
transforms.Resize((256,256)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])


im = Image.open(test_image)
im = transform(im)

# Parameters for the transformation
theta = -0.15
phi = -0.1
tx = 0
ty = 0
tz = 0.1

RT = torch.eye(4).unsqueeze(0)
# Set up rotation
RT[0,0:3,0:3] = torch.Tensor(quaternion.as_rotation_matrix(quaternion.from_rotation_vector([phi, theta, 0])))
# Set up translation
RT[0,0:3,3] = torch.Tensor([tx, ty, tz])

batch = {
'images' : [im.unsqueeze(0)],
'cameras' : [{
'K' : torch.eye(4).unsqueeze(0),
'Kinv' : torch.eye(4).unsqueeze(0)
}]
}

# Generate a new view at the new transformation
with torch.no_grad():
pred_imgs = model_to_test.model.module.forward_angle(batch, [RT])
depth = nn.Sigmoid()(model_to_test.model.module.pts_regressor(batch['images'][0].cuda()))

fig, axis = plt.subplots(1,3, figsize=(10,20))
axis[0].axis('off')
axis[1].axis('off')
axis[2].axis('off')

axis[0].imshow(im.permute(1,2,0) * 0.5 + 0.5)
axis[0].set_title('Input Image')
axis[1].imshow(pred_imgs[0].squeeze().cpu().permute(1,2,0).numpy() * 0.5 + 0.5)
axis[1].set_title('Generated Image')
axis[2].imshow(depth.squeeze().cpu().clamp(max=0.04))
axis[2].set_title('Predicted Depth')

plt.savefig(save_path)

if __name__ == '__main__':
inference(path_to_model= './synsin/modelcheckpoints/realestate/zbufferpts.pth',
test_image='appartement.JPG',
save_path='output.png')
Binary file added chap9/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions chap9/set_up_model_for_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import torch
import torch.nn as nn

import sys
sys.path.insert(0, './synsin')

import os
os.environ['DEBUG'] = '0'
from synsin.models.networks.sync_batchnorm import convert_model
from synsin.models.base_model import BaseModel
from synsin.options.options import get_model

# Set up the models
def synsin_model(model_path):

torch.backends.cudnn.enabled = True

opts = torch.load(model_path)['opts']
opts.render_ids = [1]

model = get_model(opts)

torch_devices = [int(gpu_id.strip()) for gpu_id in opts.gpu_ids.split(",")]

if 'sync' in opts.norm_G:
model = convert_model(model)
model = nn.DataParallel(model, torch_devices[0:1]).cuda()
else:
model = nn.DataParallel(model, torch_devices[0:1]).cuda()

# Load the original model to be tested
model_to_test = BaseModel(model, opts)
model_to_test.load_state_dict(torch.load(model_path)['state_dict'])
model_to_test.eval()

print("Loaded model")

return model_to_test
1 change: 1 addition & 0 deletions chap9/synsin
Submodule synsin added at 501ec4

0 comments on commit d10e5c9

Please sign in to comment.