Commit 85e1c5f3 authored by Marius Laska's avatar Marius Laska
Browse files

added new uji data provider that supports grid encoding of full dataset including all buildings

parent b55871c4
......@@ -396,8 +396,8 @@ if __name__ == "__main__":
f1 = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/output/CNN_BBOX_test_1"
f2 = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/output/CNNLoc_1"
f2 = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/output/CNNLoc_floor_0_1"
f1 = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/output/CNN_BBOX_floor_0_1"
f2 = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/full_cmp/output/CNNLoc_1"
f1 = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/full_cmp/output/CNNLoc-DLB_1"
main(p_box_f=f1,
p_reg_f=f2,
vis_idx=(0,77))
\ No newline at end of file
vis_idx=(0,500))
\ No newline at end of file
import math
import pandas as pd
import numpy as np
from base.ujiindoorloc_provider_grid import UJIndoorLocProviderGrid
class UJIndoorLocProviderFullGrid(UJIndoorLocProviderGrid):
def __init__(self):
super().__init__()
self.dps = {}
def setup_dps(self):
idx = 0
for index, comb in self.get_bld_floor_combinations().iterrows():
idx += 1
#if idx > 3:
# break
data_params = {"building": comb[0], "floor": comb[1]}
pre_params = {"standardize": False}
dp = UJIndoorLocProviderFullGrid.setup_data_provider(data_params=data_params, pre_params=pre_params)
dp.transform_to_2dim_overlapping_grid_encoding(
grid_size=40.0)
self.dps[(comb[0], comb[1])] = dp
self.merge_dps()
def merge_dps(self):
data = []
labels = []
grid_labels = []
train_idx = []
test_idx = []
last_max_idx = 0
last_grid_idx = 0
sum_max_idx = 0
sum_grid_idx = 0
mapping = {}
for key, dp in self.dps.items():
data.append(dp.data_tensor)
labels.append(dp.labels)
# num grid cells
num_row_l1 = np.ceil(dp.floorplan_height / dp.grid_size) + 1
num_col_l1 = np.ceil(dp.floorplan_width / dp.grid_size) + 1
max_grid_idx = int(num_row_l1 * num_col_l1)
max_idx = len(np.concatenate(dp.splits_indices[0], axis=0))
sum_max_idx += max_idx
sum_grid_idx += max_grid_idx
dp.grid_labels[:, 2] += last_grid_idx
grid_labels.append(dp.grid_labels)
mapping[key] = (last_max_idx, sum_max_idx, last_grid_idx, sum_grid_idx)
train_idx.append(dp.splits_indices[0][0] + last_max_idx)
test_idx.append(dp.splits_indices[0][1] + last_max_idx)
last_max_idx = sum_max_idx
last_grid_idx = sum_grid_idx
self.data_tensor = np.concatenate(data, axis=0)
self.labels = np.concatenate(labels, axis=0)
self.grid_labels = np.concatenate(grid_labels, axis=0)
self.dp_mapping = mapping
self.splits_indices = [(train_idx, test_idx)]
self.get_num_grid_cells()
self.standardize_data()
def decode_grid_labels(self, grid_labels):
sub = grid_labels[:, 4::5]
chosen = np.argmax(sub, axis=1)
# find corresponding dp
for key, mapping in self.dp_mapping.items():
c_range = np.logical_and(chosen >= mapping[2], chosen < mapping[3])
dp_idx = np.where(c_range)[0]
dp = self.dps[key]
orig_labels = self.labels[dp_idx, :]
decoded_labels = convert_from_2dim_overlapping_grid(
grid_labels[dp_idx, :], offset=mapping[2],
grid_size=dp.grid_size, padding_ratio=0.1,
height=dp.floorplan_height, width=dp.floorplan_width)
print("Enc->DEC: {}".format(np.array_equal(orig_labels, decoded_labels[:, :2])))
def get_bld_floor_combinations(self):
data = pd.read_csv("UJIndoorLoc/trainingData.csv")
bld_id = data[["BUILDINGID", "FLOOR"]].drop_duplicates().sort_values(by=["BUILDINGID", "FLOOR"])
return bld_id
def get_num_grid_cells(self):
max_key = sorted(self.dp_mapping.keys())[-1]
return self.dp_mapping[max_key][3]
def convert_from_2dim_overlapping_grid(pred_vals, width, height, offset=0, grid_size=40, padding_ratio=0.1):
# Determine #row,col for 1st layer
num_row_l1 = np.ceil(height / grid_size) + 1
num_col_l1 = np.ceil(width / grid_size) + 1
num_l1 = int(num_row_l1 * num_col_l1)
num = num_l1
origins = []
# Determine centers & origins (lower-left)
for idx in range(num):
# 1st layer
r_idx = int(idx / num_col_l1)
c_idx = idx % num_col_l1
x = c_idx * grid_size
y = r_idx * grid_size
origins.append(np.array([x, y]))
# Determine closest origin for labels (the corresponding cell is
# responsible for encoding
origins = np.array(origins)
sub = pred_vals[:, 4::5]
chosen = np.argmax(sub, axis=1)
gs_choice = chosen - offset
pred_fold = np.zeros((len(pred_vals), 4))
for idx in range(len(pred_vals)):
local_box = pred_vals[idx,
chosen[idx] * 5:(chosen[idx] + 1) * 5 - 1]
local_box[:2] = origins[gs_choice[idx], :] + local_box[:2] * (grid_size / 2.0 + grid_size * padding_ratio)
# 3rd and 4th dimension contain width and height
# convert from [-1,1] to [0,1]
local_box[2:] = (local_box[2:] + 1) / 2.0
local_box[2:] *= grid_size
pred_fold[idx, :] = local_box
return pred_fold
def test_encoding_decoding():
dp = UJIndoorLocProviderFullGrid()
dp.setup_dps()
# test prediction that chooses correct grid cell and perfect box center
pred_vals = np.zeros((len(dp.grid_labels), 267 * 5))
for gl_idx, gl in enumerate(dp.grid_labels):
pred_vals[gl_idx, int(gl[2] * 5 + 4)] = 1.0
pred_vals[gl_idx, int(gl[2] * 5)] = gl[0]
pred_vals[gl_idx, int(gl[2] * 5 + 1)] = gl[1]
dp.decode_grid_labels(pred_vals)
if __name__ == "__main__":
test_encoding_decoding()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment