{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "import os\n", "import torch\n", "import matplotlib.pyplot as plt\n", "\n", "rootdir = os.path.abspath(sys.path[0] + '/../')\n", "sys.path.append(rootdir)\n", "torch.cuda.set_device(2)\n", "print(\"Set CUDA:%d as current device.\" % torch.cuda.current_device())\n", "\n", "from data.spherical_view_syn import *\n", "from configs.spherical_view_syn import SphericalViewSynConfig\n", "from utils import netio\n", "from utils import misc\n", "from utils import img\n", "from utils import device\n", "from utils import view\n", "from components.gen_final import GenFinal\n", "\n", "\n", "def load_net(path):\n", " config = SphericalViewSynConfig()\n", " config.from_id(path[:-4])\n", " config.sa['perturb_sample'] = False\n", " config.print()\n", " net = config.create_net().to(device.default())\n", " netio.load(path, net)\n", " return net\n", "\n", "\n", "def find_file(prefix):\n", " for path in os.listdir():\n", " if path.startswith(prefix):\n", " return path\n", " return None\n", "\n", "\n", "def load_views(data_desc_file) -> view.Trans:\n", " with open(data_desc_file, 'r', encoding='utf-8') as file:\n", " data_desc = json.loads(file.read())\n", " samples = data_desc['samples'] if 'samples' in data_desc else [-1]\n", " view_centers = torch.tensor(\n", " data_desc['view_centers'], device=device.default()).view(samples + [3])\n", " view_rots = torch.tensor(\n", " data_desc['view_rots'], device=device.default()).view(samples + [3, 3])\n", " return view.Trans(view_centers, view_rots)\n", "\n", "\n", "def plot_cross(center, res):\n", " plt.plot(\n", " [\n", " (res[1] - 1) / 2 + center[0] - 5,\n", " (res[1] - 1) / 2 + center[0] + 5\n", " ],\n", " [\n", " (res[0] - 1) / 2 + center[1],\n", " (res[0] - 1) / 2 + center[1]\n", " ],\n", " color=[0, 1, 0])\n", " plt.plot(\n", " [\n", " (res[1] - 1) / 2 + center[0],\n", " (res[1] - 1) / 2 + center[0]\n", " ],\n", " [\n", " (res[0] - 1) / 2 + center[1] - 5,\n", " (res[0] - 1) / 2 + center[1] + 5\n", " ],\n", " color=[0, 1, 0])\n", "\n", "\n", "def plot_figures(left_images, right_images, left_center, right_center):\n", " # Plot Fovea raw\n", " plt.figure(figsize=(8, 4))\n", " plt.subplot(121)\n", " img.plot(left_images['fovea_raw'])\n", " plt.subplot(122)\n", " img.plot(right_images['fovea_raw'])\n", "\n", " # Plot Fovea\n", " plt.figure(figsize=(8, 4))\n", " plt.subplot(121)\n", " img.plot(left_images['fovea'])\n", " fovea_res = left_images['fovea'].size()[-2:]\n", " plot_cross((0, 0), fovea_res)\n", " plt.subplot(122)\n", " img.plot(right_images['fovea'])\n", " plot_cross((0, 0), fovea_res)\n", "\n", " #plt.subplot(1, 4, 2)\n", " # img.plot(fovea_refined)\n", "\n", " # Plot Mid\n", " plt.figure(figsize=(8, 4))\n", " plt.subplot(121)\n", " img.plot(left_images['mid'])\n", " plt.subplot(122)\n", " img.plot(right_images['mid'])\n", "\n", " # Plot Periph\n", " plt.figure(figsize=(8, 4))\n", " plt.subplot(121)\n", " img.plot(left_images['periph'])\n", " plt.subplot(122)\n", " img.plot(right_images['periph'])\n", "\n", " # Plot Blended\n", " plt.figure(figsize=(12, 6))\n", " plt.subplot(121)\n", " img.plot(left_images['blended'])\n", " full_res = left_images['blended'].size()[-2:]\n", " plot_cross(left_center, full_res)\n", " plt.subplot(122)\n", " img.plot(right_images['blended'])\n", " plot_cross(right_center, full_res)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#os.chdir(os.path.join(rootdir, 'data/__0_user_study/us_gas_all_in_one'))\n", "os.chdir(os.path.join(rootdir, 'data/__0_user_study/us_mc_all_in_one'))\n", "#os.chdir(os.path.join(rootdir, 'data/__0_user_study/bedroom_all_in_one'))\n", "print('Change working directory to ', os.getcwd())\n", "torch.autograd.set_grad_enabled(False)\n", "\n", "fovea_net = load_net(find_file('fovea'))\n", "periph_net = load_net(find_file('periph'))\n", "\n", "# Load Dataset\n", "views = load_views('views.json')\n", "#ref_dataset = SphericalViewSynDataset('ref.json', load_images=False, calculate_rays=False)\n", "print('Dataset loaded.')\n", "\n", "print('views:', views.size())\n", "#print('ref views:', ref_dataset.samples)\n", "\n", "fov_list = [20, 45, 110]\n", "res_list = [(128, 128), (256, 256), (256, 230)] # (192,256)]\n", "res_full = (1600, 1440)\n", "\n", "gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net, device.default())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "centers = [\n", " # ==gas==\n", " [(-137, 64), (-142, 64)],\n", " [(133, -44), (130, -44)],\n", " [(-20, -5), (-25, -5)],\n", " # ==mc==\n", " [(-107, 80), (-112, 80)],\n", " [(-17, -90), (-22, -90)],\n", " [(95, 30), (91, 30)]\n", "]\n", "set_id = 5\n", "\n", "view_coord = [0, 0, 0, 0, 0]\n", "for i, val in enumerate(views.size()):\n", " view_coord[i] += val // 2\n", "print('view_coord:', view_coord)\n", "test_view = views.get(*view_coord)\n", "\n", "left_images = gen(centers[set_id][0], view.Trans(\n", " test_view.trans_point(\n", " torch.tensor([-0.03, 0, 0], device=device.default())\n", " ), test_view.r), mono_trans=test_view, ret_raw=True)\n", "right_images = gen(centers[set_id][1], view.Trans(\n", " test_view.trans_point(\n", " torch.tensor([0.03, 0, 0], device=device.default())\n", " ), test_view.r), mono_trans=test_view, ret_raw=True)\n", "\n", "#plot_figures(left_images, right_images, centers[set_id][0], centers[set_id][1])\n", "\n", "misc.create_dir('output')\n", "for key in left_images:\n", " img.save(\n", " left_images[key], 'output/set%d_%s_l.png' % (set_id, key))\n", "for key in right_images:\n", " img.save(\n", " right_images[key], 'output/set%d_%s_r.png' % (set_id, key))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.9 64-bit ('pytorch': conda)", "name": "python379jvsc74a57bd0660ca2a75467d3af74a68fcc6f40bc78ab96b99ff17d2f100b5ca821fbb183f2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" }, "orig_nbformat": 2 }, "nbformat": 4, "nbformat_minor": 2 }