Commit b55871c4 authored by Marius Laska's avatar Marius Laska
Browse files

support of new benchmark model (CNNLoc-Access)

parent 614ce521
......@@ -22,9 +22,10 @@ def main(dir_p="../evaluation/lohan/evaluation/new_knn/output/", f_idx=0):
f = "/home/laskama/PycharmProjects/bboxPrediction/evaluation/UJIndoorLoc/b0/f0/final/output/BBOX_1l_20_aug_1"
f = "/media/laskama/Daten/BBOX/UJIndoorLoc/b0/f1/test/output/BBOX_1l_20_aug_1"
f = "/media/laskama/Daten/BBOX/lohan/test/output/BBOX_1l_10_aug_1"
f = "/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/uji/cnn/output/CNN_BBOX_test_1"
bbox: Pipeline = Storable.load(f)#dir_p + "BBOX_1l_3o_aug_1")
convert_from_2dim_overlapping_grid(bbox, grid_size=40, quantile=False,
convert_from_2dim_overlapping_grid(bbox, grid_size=bbox.data_provider.grid_size, quantile=False,
store=True,
average_samples=True)
#convert_from_2dim_grid(bbox, grid_size=40, quantile=False, store=True,
......@@ -32,7 +33,7 @@ def main(dir_p="../evaluation/lohan/evaluation/new_knn/output/", f_idx=0):
f= "/home/laskama/Dropbox/evaluation/UJIndoorLoc/b0/f1/final/output/REG_1"
f = "/home/laskama/Dropbox/evaluation/lohan/REG_1"
reg: Pipeline = Storable.load(f)#dir_p + "REG_2l_1") # Storable.load("../evaluation/simulation/evaluation/big_2/output/" + "REG") #Storable.load("../evaluation/simulation/evaluation/big_2/output/REG")
#reg: Pipeline = Storable.load(f)#dir_p + "REG_2l_1") # Storable.load("../evaluation/simulation/evaluation/big_2/output/" + "REG") #Storable.load("../evaluation/simulation/evaluation/big_2/output/REG")
#knn: Pipeline = Storable.load(dir_p + "KNN")
......@@ -46,7 +47,7 @@ def main(dir_p="../evaluation/lohan/evaluation/new_knn/output/", f_idx=0):
# predicted positions (of fold f_idx)
y_pred_box = bbox.summary.y_pred[f_idx]
y_pred_reg = reg.summary.y_pred[f_idx]
#y_pred_reg = reg.summary.y_pred[f_idx]
#y_pred_knn = knn.summary.y_pred[f_idx]
#y_pred_bbox_m = bbox_m.summary.y_pred[f_idx]
......@@ -61,10 +62,10 @@ def main(dir_p="../evaluation/lohan/evaluation/new_knn/output/", f_idx=0):
print("box_mse: {}".format(box_mse))
reg_error = np.linalg.norm(y_true - y_pred_reg, axis=1)
reg_mse = np.mean(reg_error)
#reg_error = np.linalg.norm(y_true - y_pred_reg, axis=1)
#reg_mse = np.mean(reg_error)
print("reg_mse: {}".format(reg_mse))
#print("reg_mse: {}".format(reg_mse))
img = resource_filename('data', 'lohan/CrowdsourcedDS1floor.png')
(train, test) = bbox.data_provider.splits_indices[f_idx]
......
......@@ -42,10 +42,41 @@ def main(p_reg_f=None, p_circle_f=None, p_quantile_f=None, p_box_f=None, vis_idx
#g_s = p_box.data_provider.
g_s = p_box.data_provider.grid_size
convert_from_2dim_overlapping_grid(p_box, grid_size=g_s, quantile=False, store=True,
average_samples=True)
pred_vals = np.concatenate(p_box.summary.y_pred, axis=0)
sub = pred_vals[:, 4::5]
chosen = np.argmax(sub, axis=1)
convert_from_2dim_overlapping_grid(p_box, grid_size=g_s, quantile=False,
store=True,
average_samples=True)
# determine correct classification rate of BBOX
y_true_labels = np.concatenate(p_box.summary.y_true_labels, axis=0)
p_box.data_provider.transform_to_2dim_overlapping_grid_encoding(
grid_size=g_s, labels=y_true_labels)
y_true_grid_labels = p_box.data_provider.grid_labels
true_mask = chosen == y_true_grid_labels[:, 2]
true_idx = np.where(true_mask)[0]
false_idx = np.where(~true_mask)[0]
y_true = np.concatenate(p_box.summary.y_true_labels, axis=0)
y_pred = np.concatenate(p_box.summary.y_pred, axis=0)
#convert_from_2dim_grid(p_box, grid_size=g_s, quantile=False, store=True,
# average_samples=True)
correct_box_error = center_diff(y_true[true_idx, :], y_pred[true_idx, :])
wrong_box_error = center_diff(y_true[false_idx, :], y_pred[false_idx, :])
size = np.prod(y_pred[:, 2:4], axis=1)
correct_size = np.mean(size[true_idx])
wrong_size = np.mean(size[false_idx])
size_median = np.median(size)
# compute avg box size per fold
avg_box_size = [np.mean(np.prod(pred[:, 2:], axis=1)) for pred in p_box.summary.y_pred]
......@@ -110,7 +141,7 @@ def main(p_reg_f=None, p_circle_f=None, p_quantile_f=None, p_box_f=None, vis_idx
y_pred = np.concatenate(p_box.summary.y_pred, axis=0)
acc_box, wrong_mask, correct_mask = calc_acc_c(y_true, y_pred)
acs = bbox_acs(p_box, lambda_const=lambda_const)
#acs = bbox_acs(p_box, lambda_const=lambda_const)
#acc_box = calc_acc_elipse(y_true, y_pred)
#b_size = np.mean(PI * np.prod(y_pred[:, 2:]/2.0, axis=1))
......@@ -122,7 +153,7 @@ def main(p_reg_f=None, p_circle_f=None, p_quantile_f=None, p_box_f=None, vis_idx
print("center-dist: {}".format(center_diff(y_true, y_pred)))
print("avg_center: {}".format(np.mean(y_pred[:, :2], axis=0)))
print("avg_box_size: {}".format(b_size))
print("ACS: {}".format(acs))
#print("ACS: {}".format(acs))
print("avg_box_size (correct): {}".format(corr_size))
print("avg_box_size (wrong): {}".format(wrong_size))
......@@ -163,8 +194,8 @@ def main(p_reg_f=None, p_circle_f=None, p_quantile_f=None, p_box_f=None, vis_idx
#radius_b = find_radius_for_acc(p_reg, accuracy=acc_quantile)
#print("SIZE_REG (QUANTILE): {}".format(PI * math.pow(radius_b, 2)))
acs = reg_acs(p_reg, lambda_const=lambda_const, radius=radius_b)
print("ACS: {}".format(acs))
#acs = reg_acs(p_reg, lambda_const=lambda_const, radius=radius_b)
#print("ACS: {}".format(acs))
plt.show()
......@@ -226,8 +257,8 @@ def visualize(p_reg: Pipeline=None, p_circle: Pipeline=None, p_quantile: Pipelin
for idx in range(idx_len):#len(y_true)):
idx += vis_idx[0]
if idx not in sub_sample_mask:
continue
#if idx not in sub_sample_mask:
# continue
#if np.prod(y_pred_box[idx, 2:]) < 600:
# continue
......@@ -359,6 +390,14 @@ if __name__ == "__main__":
f1 = "/media/laskama/Daten/BBOX/UJIndoorLoc/b0/f1/test/output/BBOX_1l_20_aug_1"
f1 = "/media/laskama/Daten/BBOX/lohan/test/output/BBOX_1l_10_aug_1"
f1 = "/Users/mariuslaska/Dropbox/tmp/BBOX_1l_15_1"
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"
main(p_box_f=f1,
#p_reg_f=f2,
vis_idx=(0,300))
\ No newline at end of file
p_reg_f=f2,
vis_idx=(0,77))
\ No newline at end of file
......@@ -18,10 +18,10 @@ def compute_loss(y_true, y_pred, walls_h, walls_v):
test = compute_tf_loss(y_true, y_pred, walls_h, walls_v)
def main_all_walls():
f_idx = 1
f_idx = 2
p: Pipeline = Storable.load(
"/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/lohan/rot/output/BBOX_1l_20_modular_1")
"/Users/mariuslaska/PycharmProjects/boxprediction/evaluation/lohan/rot/output/BBOX_1l_20_modular_v3_1")
p.data_provider.transform_to_2dim_overlapping_grid_encoding(
grid_size=40, labels=p.summary.y_true_labels[f_idx])
......
......@@ -8,11 +8,12 @@ from il_pipeline.summary.summary import KFoldClassSummary
from il_pipeline.models.tf_model_definition import classification_model_for_generator, cnn_model_for_generator
from tensorflow.keras.activations import softmax, linear, sigmoid, tanh
from tensorflow.keras.losses import mean_squared_error
from tensorflow.keras.backend import categorical_crossentropy, relu, softmax
from tensorflow.keras.backend import categorical_crossentropy, relu, softmax, elu
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.optimizers import Adam, Adamax
from base.bbox_model_definition import bbox_model_for_generator
from base.bbox_model_definition import bbox_model_for_generator, \
cnn_deep_loc_box, ae_model
from base.hier_model_definition import hier_model_for_generator
from base.custom_loss import bbox_loss, define_bbox_loss, define_circle_loss, \
define_quantile_loss, \
......@@ -31,7 +32,7 @@ class BboxModel(DnnModel):
def load_weights(self, file=None):
if file is None:
file = self.output_dir + self.filename + ".hdf5"
file = self.output_dir + self.filename + "_f{}.hdf5".format(self.data_provider.current_split_idx)
self.classifier.load_weights(file)
def setup_params(self):
......@@ -45,7 +46,7 @@ class BboxModel(DnnModel):
'optimizer': Adam,
'activation': relu})
if self.type == "classification" or "CNN" in self.type:
if self.type == "classification" or self.type == "CNN":
params.update({'last_activation': softmax,
'losses': categorical_crossentropy})
......@@ -79,6 +80,21 @@ class BboxModel(DnnModel):
params.update({'last_activation': tanh,
'losses': loss_func})
elif self.type == "GRID_OVERLAP-BBOX_CNN":
if 'loss' in params:
loss_func = define_yolo_loss_tanh_no_size(params['loss'])
params.update({'last_activation': tanh,
'losses': loss_func,
'activation': elu,
'optimizer': Adamax})
elif self.type == "CNNLoc_REG":
params.update({'activation': elu,
'losses': mean_squared_error,
'optimizer': Adamax,
'last_activation': linear})
elif self.type == "GRID_ROT-BBOX":
if 'loss' in params:
height = self.data_provider.floorplan_height
......@@ -129,6 +145,13 @@ class BboxModel(DnnModel):
params.update({'last_activation': linear,
'losses': loss_func})
def train_autoencoder(self):
x_cols, _ = self.data_provider.get_data_dims("regression")
self.classifier, bottleneck_model = ae_model(x_cols, self.params)
self._train_model(save_weights_only=False, evaluate=False, autoencoder=True)
bottleneck_model.save(self.output_dir + "AE_bottleneck_{}_f{}.hdf5".format(
self.filename, self.data_provider.current_split_idx), include_optimizer=False)
def pre_train_model(self, params):
if 'loss' in params:
if self.type == "BBOX":
......@@ -165,6 +188,8 @@ class BboxModel(DnnModel):
y_cols = 4
if self.type == "S_Q":
y_cols = 1
if self.type == "CNNLoc_REG":
y_cols = 2
if self.type == "GRID-BBOX" or self.type == "GRID-QUANTILE":
if dp.grid_size is None:
# single row or column encoding
......@@ -176,7 +201,7 @@ class BboxModel(DnnModel):
y_cols = 5 * math.ceil(
dp.floorplan_height / dp.grid_size) * math.ceil(
dp.floorplan_width / dp.grid_size)
if self.type == "GRID_OVERLAP-BBOX":
if self.type == "GRID_OVERLAP-BBOX" or self.type == "GRID_OVERLAP-BBOX_CNN":
y_cols = 5 * (math.ceil(
dp.floorplan_height / dp.grid_size) + 1) * (math.ceil(
dp.floorplan_width / dp.grid_size) + 1)
......@@ -201,13 +226,22 @@ class BboxModel(DnnModel):
dp.floorplan_width / dp.grid_size) + 1)
# obtain the model definition
if "BBOX" in self.type or "CIRCLE" in self.type or "QUANTILE" in self.type or "S_Q" in self.type:
if self.type == "GRID_OVERLAP-BBOX_CNN":
ae_file = self.output_dir + "AE_bottleneck_{}_f{}.hdf5".format(
self.filename, self.data_provider.current_split_idx)
self.classifier = cnn_deep_loc_box(ae_file, y_cols, self.params)
elif "BBOX" in self.type or "CIRCLE" in self.type or "QUANTILE" in self.type or "S_Q" in self.type:
classifier_template = bbox_model_for_generator(metrics=[])
self.classifier = classifier_template(X_cols=x_cols, Y_cols=y_cols, params=params)
elif "CNN" in self.type:
elif self.type == "CNN":
classifier_template = cnn_model_for_generator(metrics=[])
self.classifier = classifier_template(input_shape=x_cols,
Y_cols=y_cols, params=params)
elif self.type == "CNNLoc_REG":
ae_file = self.output_dir + "AE_bottleneck_{}_f{}.hdf5".format(
self.filename, self.data_provider.current_split_idx)
self.classifier = cnn_deep_loc_box(ae_file, y_cols, self.params)
else:
classifier_template = classification_model_for_generator(
metrics=[])
......@@ -217,7 +251,7 @@ class BboxModel(DnnModel):
self.type = "regression"
def _train_model(self, save_weights_only=False, evaluate=True):
def _train_model(self, save_weights_only=False, evaluate=True, autoencoder=False):
"""
Trains a DNN model with the specified parameters using Keras
fit_generator function. Model is evaluated on separate test data
......@@ -246,7 +280,8 @@ class BboxModel(DnnModel):
train_steps_per_epoch = math.ceil(num_train * (num_augs + 1) / train_bs)
# setup callbacks
checkpoint_file_name = output_dir + "{}_f{}.hdf5".format(self.filename, data_provider.current_split_idx)
ae_str = "AE_" if autoencoder else ""
checkpoint_file_name = output_dir + "{}{}_f{}.hdf5".format(ae_str, self.filename, data_provider.current_split_idx)
# save best performing model parameters
checkpoint = ModelCheckpoint(checkpoint_file_name, verbose=0,
......@@ -276,17 +311,19 @@ class BboxModel(DnnModel):
model_type=self.type,
area_labels=area_labels,
augmentation=num_augs,
autoencoder=autoencoder,
noise_percentage=noise_percentage,
noisy_labels=train_on_noisy_labels),
validation_data=generator(mode='val', model_type=self.type,
area_labels=area_labels,
autoencoder=autoencoder,
noisy_labels=train_on_noisy_labels,
batch_size=val_bs),
validation_steps=val_steps_per_epoch,
steps_per_epoch=train_steps_per_epoch,
epochs=params['epochs'],
callbacks=[earlyStopping, checkpoint],
verbose=0)
verbose=1)
# evaluate model and store results in summary file
if evaluate:
......
from tensorflow.keras import regularizers, initializers
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, BatchNormalization
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, BatchNormalization, Input, Reshape, Conv1D
from tensorflow.keras.losses import MSE
from tensorflow.keras.optimizers import Adam, Adamax
from base.custom_dropout import Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.models import Sequential, Model, load_model
from il_pipeline.models.layer_generator import hidden_layers
from il_pipeline.models.lr_normalizer import lr_normalizer
from base.custom_loss import define_yolo_loss_tanh_no_size
def reg_model_for_talos(x_train, y_train, x_val, y_val, params):
model = Sequential()
......@@ -111,5 +115,55 @@ def bbox_model_for_generator(metrics):
return model
return define_classification_model_for_generator
def ae_model(X_cols, params):
input = Input((X_cols,))
encode_layer = Dense(128, activation=params['activation'], name='en1')(input)
encode_layer = Dense(64, activation=params['activation'], name='en2')(encode_layer)
decode_layer = Dense(128, activation=params['activation'], name='de2')(encode_layer)
decode_layer = Dense(X_cols, activation=params['activation'], name='de-1')(decode_layer)
encoder_model_256_128 = Model(inputs=input, outputs=decode_layer)
bottleneck_model_256_128 = Model(inputs=input,
outputs=encode_layer)
encoder_model = encoder_model_256_128
bottleneck_model = bottleneck_model_256_128
encoder_model.compile(
loss=MSE,
optimizer=Adamax(lr=params['lr'])#lr=0.005)
)
return encoder_model, bottleneck_model
def cnn_deep_loc_box(ae_base_model_file, y_cols, params):
position_base_model = load_model(ae_base_model_file, compile=False)
for layer in position_base_model.layers:
layer.trainable = True
position_net_input = Reshape((position_base_model.output_shape[1], 1))(
position_base_model.output)
if 'dropout' in params:
position_net_input = Dropout(params['dropout'])(position_net_input)
position_net = Conv1D(99, 22, activation=params['activation'])(position_net_input)
position_net = Conv1D(66, 22, activation=params['activation'])(position_net)
position_net = Conv1D(33, 22, activation=params['activation'])(position_net)
position_net = Flatten()(position_net)
position_predict_output = Dense(y_cols, activation=params['last_activation'])(
position_net)
position_model = Model(inputs=position_base_model.input,
outputs=position_predict_output)
position_model.compile(
loss=params['losses'],
optimizer=Adamax(lr=params['lr'])#lr=0.005)
)
return define_classification_model_for_generator
\ No newline at end of file
return position_model
......@@ -77,6 +77,18 @@ class BboxPipeline(Pipeline):
model.setup_model(setup_params=True)
model.train_model()
if params['type'] == "CNNLoc_REG":
model = BboxModel(params['type'], self.summary, self.data_provider,
params,
self.config.output_dir, self.filename)
model.setup_params()
if 'autoencoder' in params and params['autoencoder'] == "train":
model.train_autoencoder()
model.setup_model(setup_params=True)
model.train_model()
if "BBOX" in params['type']:
m_type = params['type']
model = BboxModel(params['type'], self.summary, self.data_provider, params,
......@@ -85,8 +97,11 @@ class BboxPipeline(Pipeline):
num_epochs = params['epochs']
if 'pretrain' in params:
model.pre_train_model(params['pretrain'])
#if 'pretrain' in params:
# model.pre_train_model(params['pretrain'])
if 'autoencoder' in params and params['autoencoder'] == "train":
model.train_autoencoder()
model.type = m_type
model.params.update({'epochs': num_epochs})
......
......@@ -911,7 +911,7 @@ def define_yolo_loss_tanh_no_size(params):
# box loss
box_loss = tf.zeros_like(y_true[:, 0])
box_loss_scale = tf.reduce_sum(tf.multiply(grid_cell_pred, grid_cell_true))
box_loss_scale = tf.reduce_sum(tf.multiply(grid_cell_pred, grid_cell_true), axis=1)
# center loss
c_loss = tf.reduce_sum(tf.square(tf.subtract(y_true[:, :2], y_pred_s[:, :2])),
......@@ -1839,7 +1839,7 @@ def define_rot_box_loss_modular(params, tf_walls_h, tf_walls_v, n_rows, n_cols,
loss += get_wall_loss(d_hor_pos_only, d_ver_neg_only, d_ver_pos_only,
d_ver_neg_only, grid_cell_true, grid_cell_pred,
MAX_DIST, scale=0.1)
MAX_DIST, scale=1.0)
#
# Angle loss: orientation should adapt to local wall structure
......
......@@ -479,7 +479,7 @@ class DataProviderGridBase(DataProviderBase):
return x_train, y_train
def in_mem_data_generator(self, split_idx=None, mode='train', area_labels=True, augmentation=None, noise_percentage=None, noisy_labels=False, grid_labels=True, model_type="classification", batch_size=32):
def in_mem_data_generator(self, split_idx=None, mode='train', area_labels=True, augmentation=None, autoencoder=False, noise_percentage=None, noisy_labels=False, grid_labels=True, model_type="classification", batch_size=32):
if split_idx is None:
split_idx = self.current_split_idx
......@@ -509,4 +509,7 @@ class DataProviderGridBase(DataProviderBase):
b_data = data[run_id * batch_size:(run_id + 1) * batch_size, :]
b_labels = labels[run_id * batch_size:(run_id + 1) * batch_size, :]
yield (b_data, b_labels)
\ No newline at end of file
if autoencoder:
yield (b_data, b_data)
else:
yield (b_data, b_labels)
\ No newline at end of file
......@@ -24,12 +24,12 @@ class UJIndoorLocProvider(DataProviderBase):
"""
train = pd.read_csv("UJIndoorLoc/trainingData.csv")
train = train[train.BUILDINGID.eq(building_id)]
train = train[train.FLOOR.eq(floor_id)]
#train = train[train.BUILDINGID.eq(building_id)]
#train = train[train.FLOOR.eq(floor_id)]
test = pd.read_csv("UJIndoorLoc/validationData.csv")
test = test[test.BUILDINGID.eq(building_id)]
test = test[test.FLOOR.eq(floor_id)]
#test = test[test.BUILDINGID.eq(building_id)]
#test = test[test.FLOOR.eq(floor_id)]
self.num_train = train.shape[0]
self.num_test = test.shape[0]
......
......@@ -59,10 +59,10 @@ global_params:
model_params:
type: GRID_ROT-BBOX # (DNN, CNN, kNN, SVM) supported (require different parameters)
first_neuron: 512
hidden_layers: 1
hidden_layers: 3
lr: 0.7
batch_size: 32
epochs: 10
epochs: 200
dropout: 0.5
regularization_penalty: 0.0
#augmentation: None
......@@ -80,7 +80,7 @@ global_params:
pipelines:
- name: BBOX_1l_20_modular
- name: BBOX_1l_20_modular_v3
# base directories for file storage
output:
......
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