Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/docker pipelines #166

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions Steps.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "749ffe22-1a4a-4cba-8004-8870695ebe00",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import argparse\n",
"import copy\n",
"import os\n",
"import time\n",
"\n",
"import cv2\n",
"import imageio\n",
"import numpy as np\n",
"import torch\n",
"import vispy\n",
"import yaml\n",
"from tqdm import tqdm\n",
"\n",
"import sys\n",
"sys.path.append(\"/art_pipelines/libs/3d-photo-inpainting/\")\n",
"\n",
"import MiDaS.MiDaS_utils as MiDaS_utils\n",
"from MiDaS.monodepth_net import MonoDepthNet\n",
"from MiDaS.run import run_depth\n",
"from bilateral_filtering import sparse_bilateral_filtering\n",
"from boostmonodepth_utils import run_boostmonodepth, resize_depth,replace_ext, write_depth\n",
"from mesh import output_3d_photo, read_ply, write_ply\n",
"from networks import Inpaint_Color_Net, Inpaint_Depth_Net, Inpaint_Edge_Net\n",
"from utils import get_MiDaS_samples, read_MiDaS_depth"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2d6aa81-6bc0-411f-8d47-d82b8877eb54",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"workspace_dir = \"/workspace\"\n",
"\n",
"depth_dir = f\"{workspace_dir}/depth_folder\"\n",
"\n",
"os.makedirs(f\"{workspace_dir}/mesh_folder\", exist_ok=True)\n",
"os.makedirs(f\"{workspace_dir}/video_folder\", exist_ok=True)\n",
"os.makedirs(depth_dir, exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e315181-98ab-4899-86b2-e8b827a24304",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import pydantic\n",
"from typing import List, Dict, Any, Union"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd432633-ca78-4810-ac04-2e0fee06f6ab",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"class MidasConfig(pydantic.BaseModel):\n",
" traj_types: List[str] = ['straight-line']\n",
" x_shift_range: List[float] = [-0.04]\n",
" y_shift_range: List[float] = [0.00]\n",
" z_shift_range: List[float] = [0.00]\n",
" video_postfix: List[Any] = ['left']\n",
" depth_format: str = '.npy'\n",
" num_frames: int = 4"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77cb0204-eeb6-446e-8ae3-67e9c82ca451",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"source_dir = \"/workspace/source_images\"\n",
"\n",
"config = MidasConfig().dict()\n",
"\n",
"sample_list = get_MiDaS_samples(\n",
" image_folder=source_dir,\n",
" depth_folder=depth_dir,\n",
" config=config,\n",
" specific='', \n",
" aft_certain=None\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0406700c-9c90-462e-a15b-83277e886315",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"BOOST_BASE = \"/art_pipelines/libs/3d-photo-inpainting/BoostingMonocularDepth/\"\n",
"\n",
"os.system(\n",
" f\"cd {BOOST_BASE} && python run.py --Final --data_dir {source_dir}/ --output_dir {depth_dir} --depthNet 0\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e766eb2c-b09b-422d-87cc-85eb0737486d",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a7ae8f7-f177-4454-93bc-7bf4238e1917",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"image_names = [sample[\"ref_img_fi\"] for sample in sample_list]\n",
"\n",
"for i, image_name in enumerate(image_names):\n",
" img = imageio.imread(image_name)\n",
" H, W = img.shape[:2]\n",
" scale = 640.0 / max(H, W)\n",
"\n",
" # resize and save depth\n",
" target_height, target_width = int(round(H * scale)), int(round(W * scale))\n",
" depth = imageio.imread(os.path.join(depth_dir, image_name))\n",
" depth = np.array(depth).astype(np.float32)\n",
" depth = resize_depth(depth, target_width, target_height)\n",
" output_path = os.path.join(depth_dir, replace_ext(os.path.basename(image_name), \"npy\"))\n",
" print(f\"Writing depth .npy to {output_path}\")\n",
" np.save(output_path, depth / 32768.0 - 1.0)\n",
" output_path = os.path.join(depth_dir, os.path.basename(image_name))\n",
" print(f\"Writing depth to {output_path}\")\n",
" write_depth(output_path, depth)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
43 changes: 27 additions & 16 deletions boostmonodepth_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import glob
import os

import cv2
import glob
import numpy as np
import imageio
import numpy as np
from MiDaS.MiDaS_utils import write_depth

BOOST_BASE = 'BoostingMonocularDepth'
BOOST_BASE = "BoostingMonocularDepth"

BOOST_INPUTS = "inputs"
BOOST_OUTPUTS = "outputs"

BOOST_INPUTS = 'inputs'
BOOST_OUTPUTS = 'outputs'

def run_boostmonodepth(img_names, src_folder, depth_folder):

Expand All @@ -21,37 +23,41 @@ def run_boostmonodepth(img_names, src_folder, depth_folder):

tgt_names = []
for img_name in img_names:
base_name = os.path.basename(img_name)
tgt_name = os.path.join(BOOST_BASE, BOOST_INPUTS, base_name)
os.system(f'cp {img_name} {tgt_name}')
base_name = os.path.basename(replace_ext(img_name, ".png"))
input_file = os.path.join(BOOST_BASE, BOOST_INPUTS, base_name)
os.system(f"cp {img_name} {input_file}")
tgt_names.append(base_name)

# keep only the file name here.
# they save all depth as .png file
tgt_names.append(os.path.basename(tgt_name).replace('.jpg', '.png'))

os.system(f'cd {BOOST_BASE} && python run.py --Final --data_dir {BOOST_INPUTS}/ --output_dir {BOOST_OUTPUTS} --depthNet 0')
os.system(
f"cd {BOOST_BASE} && python run.py --Final --data_dir {BOOST_INPUTS}/ --output_dir {BOOST_OUTPUTS} --depthNet 0"
)

for i, (img_name, tgt_name) in enumerate(zip(img_names, tgt_names)):
img = imageio.imread(img_name)
H, W = img.shape[:2]
scale = 640. / max(H, W)
scale = 640.0 / max(H, W)

# resize and save depth
target_height, target_width = int(round(H * scale)), int(round(W * scale))
depth = imageio.imread(os.path.join(BOOST_BASE, BOOST_OUTPUTS, tgt_name))
depth = np.array(depth).astype(np.float32)
depth = resize_depth(depth, target_width, target_height)
np.save(os.path.join(depth_folder, tgt_name.replace('.png', '.npy')), depth / 32768. - 1.)
write_depth(os.path.join(depth_folder, tgt_name.replace('.png', '')), depth)
np.save(os.path.join(depth_folder, replace_ext(tgt_name, ".npy")), depth / 32768.0 - 1.0)
write_depth(os.path.join(depth_folder, replace_ext(tgt_name, "")), depth)

def clean_folder(folder, img_exts=['.png', '.jpg', '.npy']):

def clean_folder(folder, img_exts=None):
img_exts = img_exts or [".png", ".jpg", ".npy"]
for img_ext in img_exts:
paths_to_check = os.path.join(folder, f'*{img_ext}')
paths_to_check = os.path.join(folder, f"*{img_ext}")
if len(glob.glob(paths_to_check)) == 0:
continue
print(paths_to_check)
os.system(f'rm {paths_to_check}')
os.system(f"rm {paths_to_check}")


def resize_depth(depth, width, height):
"""Resize numpy (or image read by imageio) depth map
Expand All @@ -66,3 +72,8 @@ def resize_depth(depth, width, height):
"""
depth = cv2.blur(depth, (3, 3))
return cv2.resize(depth, (width, height), interpolation=cv2.INTER_AREA)


def replace_ext(filename: str, extension: str):
extension = extension.replace(".", "")
return f"{os.path.splitext(filename)[0]}.{extension}"
27 changes: 27 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '2.3'

services:
vispy:
build:
context: .
dockerfile: ./vispy.Dockerfile
#image: pytorch/pytorch:latest
container_name: fastapi
environment:
PYTHONPATH: /art_pipelines
volumes:
- ../:/art_pipelines
- ../../server:/workspace/server:ro
- ../../temp:/workspace/temp
#entrypoint: "dir ./"
entrypoint: "bash -c 'jupyter lab --no-browser --ip=0.0.0.0 --port=8893 --allow-root'"
ports:
- "8893:8893"
# enable gpu for linux (runtime: nvidia) or windows (deploy:...)
#runtime: nvidia
deploy:
resources:
reservations:
devices:
- capabilities:
- gpu
Binary file removed image/moon.jpg
Binary file not shown.
Loading