test_lf_syn.ipynb 4.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/e/dengnc')\n",
    "\n",
    "from typing import List\n",
    "import torch\n",
    "from torch import nn\n",
    "import matplotlib.pyplot as plt\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
16
17
18
    "from deep_view_syn.data.lf_syn import LightFieldSynDataset\n",
    "from deep_view_syn.my import util\n",
    "from deep_view_syn.trans_unet import LatentSpaceTransformer\n",
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    "\n",
    "device = torch.device(\"cuda:2\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test data loader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_DIR = '../data/lf_syn_2020.12.23'\n",
    "TRAIN_DATA_DESC_FILE = DATA_DIR + '/train.json'\n",
    "\n",
    "train_dataset = LightFieldSynDataset(TRAIN_DATA_DESC_FILE)\n",
    "train_data_loader = torch.utils.data.DataLoader(\n",
    "    dataset=train_dataset,\n",
    "    batch_size=3,\n",
    "    num_workers=8,\n",
    "    pin_memory=True,\n",
    "    shuffle=True,\n",
    "    drop_last=False)\n",
    "print(len(train_data_loader))\n",
    "\n",
    "print(train_dataset.cam_params)\n",
    "print(train_dataset.sparse_view_positions)\n",
    "print(train_dataset.diopter_of_layers)\n",
    "plt.figure()\n",
    "util.PlotImageTensor(train_dataset.sparse_view_images[0])\n",
    "plt.figure()\n",
    "util.PlotImageTensor(train_dataset.sparse_view_depths[0] / 255 * 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test disparity wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "transformer = LatentSpaceTransformer(train_dataset.sparse_view_images.size()[2],\n",
    "                                     train_dataset.cam_params,\n",
    "                                     train_dataset.diopter_of_layers,\n",
    "                                     train_dataset.sparse_view_positions)\n",
    "novel_views = torch.stack([\n",
    "    train_dataset.view_positions[13],\n",
    "    train_dataset.view_positions[30],\n",
    "    train_dataset.view_positions[57],\n",
    "], dim=0)\n",
    "trans_images = transformer(train_dataset.sparse_view_images.to(device),\n",
    "                           train_dataset.sparse_view_depths.to(device),\n",
    "                           novel_views)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "mask = (torch.sum(trans_images[0], 1) > 1e-5).to(dtype=torch.float)\n",
    "blended = torch.sum(trans_images[0], 0)\n",
    "weight = torch.sum(mask, 0)\n",
    "blended = blended / weight.unsqueeze(0)\n",
    "\n",
    "plt.figure(figsize=(6, 6))\n",
    "util.PlotImageTensor(train_dataset.view_images[13])\n",
    "plt.figure(figsize=(6, 6))\n",
    "util.PlotImageTensor(blended)\n",
    "plt.figure(figsize=(12, 6))\n",
    "plt.subplot(2, 4, 1)\n",
    "util.PlotImageTensor(train_dataset.sparse_view_images[0])\n",
    "plt.subplot(2, 4, 2)\n",
    "util.PlotImageTensor(train_dataset.sparse_view_images[1])\n",
    "plt.subplot(2, 4, 3)\n",
    "util.PlotImageTensor(train_dataset.sparse_view_images[2])\n",
    "plt.subplot(2, 4, 4)\n",
    "util.PlotImageTensor(train_dataset.sparse_view_images[3])\n",
    "\n",
    "plt.subplot(2, 4, 5)\n",
    "util.PlotImageTensor(trans_images[0, 0])\n",
    "plt.subplot(2, 4, 6)\n",
    "util.PlotImageTensor(trans_images[0, 1])\n",
    "plt.subplot(2, 4, 7)\n",
    "util.PlotImageTensor(trans_images[0, 2])\n",
    "plt.subplot(2, 4, 8)\n",
    "util.PlotImageTensor(trans_images[0, 3])\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('pytorch': conda)",
   "metadata": {
    "interpreter": {
     "hash": "a00413fa0fb6b0da754bf9fddd63461fcd32e367fc56a5d25240eae72261060e"
    }
   },
   "name": "python3"
  },
  "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.6"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}