Skip to content

Commit

Permalink
feat(chore): Change train_test to ghout dir, add ghout to .gitignore'
Browse files Browse the repository at this point in the history
  • Loading branch information
saeranv committed Apr 25, 2021
1 parent db0e1b2 commit 18fac3b
Show file tree
Hide file tree
Showing 128 changed files with 169 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ dmypy.json
# Honeybee/Ladybug data
/run
/models
/ghout
*.sql


Binary file modified data/gh/bem_simulate_2.gh
Binary file not shown.
3 changes: 0 additions & 3 deletions data/gh/cam_rh.3dm.rhl

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
103 changes: 3 additions & 100 deletions deeprad/image2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import json
from copy import copy
from pathlib import Path

# scientific python stuff
import cv2
Expand All @@ -16,9 +15,9 @@
from sklearn.mixture import GaussianMixture
from matplotlib.axes import Axes

# Path to all models in deep_rad
DEEPRAD_DATA_DIR = os.path.abspath(os.path.join(
os.getcwd(), '..', 'deeprad/data/models/'))
# deeprad modules
from .utils import to_poly_sh, to_poly_np, extract_floorplan_ids, load_floorplan_data, \
load_json, make_dir_safely, DEEPRAD_MODELS_DIR


def viz_poly_sh_arr(poly_sh_arr, a=None, scale=1, iter=False, **kwargs):
Expand Down Expand Up @@ -63,28 +62,6 @@ def viz_ortho_snap_grid(x_means: np.ndarray, y_means: np.ndarray, ax: Axes, colo
return ax


def to_poly_sh(xy_arr):
"""Shapely polygon from list of two arrays of x, y coordinates.
Args:
xy_arr: [x_arr, y_arr]
Example:
to_poly_sh(to_poly_np(poly_sh))
to_poly_np(to_poly_sh(poly_np))
"""
return geom.Polygon([(x, y) for x, y in zip(*xy_arr)])


def to_poly_np(poly_sh):
"""Two arrays of x, y coordinates from shapely polygon.
Example:
to_poly_sh(to_poly_np(poly_sh))
to_poly_np(to_poly_sh(poly_np))
"""
return np.array(poly_sh.exterior.xy)


def rooms_equal_color_mapping(_rooms: np.ndarray) -> np.ndarray:
"""Replace class indices with equally spaced intervals from 0-255 for thresholding.
Expand Down Expand Up @@ -420,79 +397,6 @@ def _get_cluster_coord_fx(polyc):
return _get_cluster_coord_fx


def extract_floorplan_ids(data_num):
"""Safely extract root model directories for polygon extraction."""

# Load all model directories
floorplan_id_arr = os.listdir(DEEPRAD_DATA_DIR)
n_ids = len(floorplan_id_arr)

if n_ids == 0:
raise Exception('No image files. Add images and try again.')

if data_num > n_ids:
print('Note: data_num {} is too high. Resetting to n_ids {}.'.format(
data_num, n_ids))
data_num = n_ids

return data_num, floorplan_id_arr


def load_json(json_fpath):
with open(json_fpath, 'r') as fp:
val_dict = json.load(fp)

return val_dict


def load_floorplan_data(targ_id_dirs, data_num):
"""Load floorplan data."""

src_img_arr = [0] * data_num
label_img_arr = [0] * data_num
hdict_arr = [0] * data_num
targ_id_dir_arr = [0] * data_num

i = -1
idx = -1
total_i = 0
null_lst = []

while (idx + 1) < data_num:
i += 1
targ_id_dir = os.path.join(
DEEPRAD_DATA_DIR, targ_id_dirs[i], 'data')
targ_src_fpath = os.path.join(targ_id_dir, 'src.jpg')
targ_label_fpath = os.path.join(targ_id_dir, 'label.jpg')
targ_json_fpath = os.path.join(targ_id_dir, 'door_vecs.json')

hdict = load_json(targ_json_fpath)

if hdict['scale'] < 0.4:
print('Skip {} b/c scale at {}. Total skipped={}.'.format(
targ_id_dirs[i], hdict['scale'], total_i))
total_i += 1
continue

idx += 1
hdict_arr[idx] = hdict
src_img_arr[idx] = cv2.imread(targ_src_fpath, cv2.COLOR_BGR2RGB)
label_img_arr[idx] = cv2.imread(targ_label_fpath, cv2.IMREAD_GRAYSCALE)
targ_id_dir_arr[idx] = targ_id_dir
null_lst.append(targ_id_dirs[i] + '\n')

# Write to null list
null_fpath = os.path.join(DEEPRAD_DATA_DIR, '_null.txt')
with open(null_fpath, 'w') as fp:
#[fp.writeline(null_) for null_ in null_lst]
fp.writelines(null_lst)
return hdict_arr, src_img_arr, label_img_arr, targ_id_dir_arr


def make_dir_safely(dest_dir):
Path(dest_dir).mkdir(parents=True, exist_ok=True)


def main(data_num):
"""Generate data_num amount of floorplan polygon vectors."""

Expand Down Expand Up @@ -617,7 +521,6 @@ def _clean_poly_sh(p): return clean_poly_sh(p, beps, stol, dpct)
# -------------------------------------------------------------
# Compute gaussian mixture models
# -------------------------------------------------------------

random_seed = 111
poly_sh_arr = [copy(p) for p in _poly_sh_arr]

Expand Down
51 changes: 51 additions & 0 deletions deeprad/preprocess_traintest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Preprocess raw outputs from GH for train/test dataset."""
import os
import numpy as np

import cv2

import deeprad.utils as utils
import deeprad.image2vec as i2v

DEEPRAD_GHOUT_DIR = utils.DEEPRAD_GHOUT_DIR
DEEPRAD_TRAINTEST_DIR = utils.DEEPRAD_TRAINTEST_DIR


def load_img_gray(img_fpath):
return cv2.imread(img_fpath, cv2.)


def preprocess_img(img_fpath):
pass


def main():
"""Preproces main."""

# load all directories in train/test
data_num, floorplan_ids = \
utils.extract_floorplan_ids(1e6, DEEPRAD_TRAINTEST_DIR, verbose=False)

# for i, floorplan_id in enumerate(floorplan_ids):
i = 0
floorplan_id = floorplan_ids[0]

in_dir = os.path.join(DEEPRAD_GHOUT_DIR, floorplan_id, 'in_data')
out_dir = os.path.join(DEEPRAD_GHOUT_DIR, floorplan_id, 'out_label')

# Preprocess labels
label_fpaths = [os.path.join(out_dir, out) for out in os.listdir(out_dir)]
label_imgs = [preprocess_img(fpath) for fpath in label_fpaths]
#[save_img(img) for img in label_imgs]

# Preprocess input images
# add as channels

# save new images
# delete all raw images

pass


if __name__ == "__main__":
main()
114 changes: 114 additions & 0 deletions deeprad/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Utility functions for deeprad."""
import sys
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np

# Path to all models in deep_rad
DEEPRAD_MODELS_DIR = os.path.abspath(os.path.join(
os.getcwd(), '..', 'deeprad/data/models/'))
DEEPRAD_GHOUT_DIR = os.path.abspath(os.path.join(
os.getcwd(), '..', 'deeprad/data/ghout/'))
DEEPRAD_TRAINTEST_DIR = os.path.abspath(os.path.join(
os.getcwd(), '..', 'deeprad/data/traintest/'))


def to_poly_sh(xy_arr):
"""Shapely polygon from list of two arrays of x, y coordinates.
Args:
xy_arr: [x_arr, y_arr]
Example:
to_poly_sh(to_poly_np(poly_sh))
to_poly_np(to_poly_sh(poly_np))
"""
return geom.Polygon([(x, y) for x, y in zip(*xy_arr)])


def to_poly_np(poly_sh):
"""Two arrays of x, y coordinates from shapely polygon.
Example:
to_poly_sh(to_poly_np(poly_sh))
to_poly_np(to_poly_sh(poly_np))
"""
return np.array(poly_sh.exterior.xy)


def extract_floorplan_ids(data_num, target_data_dir=None, verbose=True):
"""Safely extract root model directories for polygon extraction."""

if target_data_dir is None:
target_data_dir = DEEPRAD_MODELS_DIR

# Load all model directories
floorplan_id_arr = os.listdir(target_data_dir)
n_ids = len(floorplan_id_arr)

if n_ids == 0:
raise Exception('No image files. Add images and try again.')

if data_num > n_ids:
if verbose:
print('Note: data_num {} is too high. Resetting to n_ids {}.'.format(
data_num, n_ids))
data_num = n_ids

return data_num, floorplan_id_arr


def load_json(json_fpath):
with open(json_fpath, 'r') as fp:
val_dict = json.load(fp)

return val_dict


def load_floorplan_data(targ_id_dirs, data_num):
"""Load floorplan data."""

src_img_arr = [0] * data_num
label_img_arr = [0] * data_num
hdict_arr = [0] * data_num
targ_id_dir_arr = [0] * data_num

i = -1
idx = -1
total_i = 0
null_lst = []

while (idx + 1) < data_num:
i += 1
targ_id_dir = os.path.join(
DEEPRAD_MODELS_DIR, targ_id_dirs[i], 'data')
targ_src_fpath = os.path.join(targ_id_dir, 'src.jpg')
targ_label_fpath = os.path.join(targ_id_dir, 'label.jpg')
targ_json_fpath = os.path.join(targ_id_dir, 'door_vecs.json')

hdict = load_json(targ_json_fpath)

if hdict['scale'] < 0.4:
print('Skip {} b/c scale at {}. Total skipped={}.'.format(
targ_id_dirs[i], hdict['scale'], total_i))
total_i += 1
continue

idx += 1
hdict_arr[idx] = hdict
src_img_arr[idx] = cv2.imread(targ_src_fpath, cv2.COLOR_BGR2RGB)
label_img_arr[idx] = cv2.imread(targ_label_fpath, cv2.IMREAD_GRAYSCALE)
targ_id_dir_arr[idx] = targ_id_dir
null_lst.append(targ_id_dirs[i] + '\n')

# Write to null list
null_fpath = os.path.join(DEEPRAD_MODELS_DIR, '_null.txt')
with open(null_fpath, 'w') as fp:
#[fp.writeline(null_) for null_ in null_lst]
fp.writelines(null_lst)
return hdict_arr, src_img_arr, label_img_arr, targ_id_dir_arr


def make_dir_safely(dest_dir):
Path(dest_dir).mkdir(parents=True, exist_ok=True)

0 comments on commit 18fac3b

Please sign in to comment.