Commit 306536c9 authored by Marius Laska's avatar Marius Laska
Browse files

integration of old 2D-CNN model with DLB head

parent 150606a0
......@@ -13,7 +13,7 @@ from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.optimizers import Adam, Adamax
from base.bbox_model_definition import bbox_model_for_generator, \
cnn_deep_loc_box, ae_model
cnn_deep_loc_box, ae_model, cnn_bbox_model_for_generator
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, \
......@@ -89,6 +89,13 @@ class BboxModel(DnnModel):
'activation': elu,
'optimizer': Adamax})
elif self.type == "GRID_OVERLAP-BBOX_2D-CNN":
if 'loss' in params:
loss_func = define_yolo_loss_tanh_no_size(params['loss'])
params.update({'last_activation': tanh,
'losses': loss_func})
elif self.type == "CNNLoc_REG":
params.update({'activation': elu,
'losses': mean_squared_error,
......@@ -201,7 +208,8 @@ 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" or self.type == "GRID_OVERLAP-BBOX_CNN":
if self.type == "GRID_OVERLAP-BBOX" or self.type == "GRID_OVERLAP-BBOX_CNN" or self.type == "GRID_OVERLAP-BBOX_2D-CNN":
y_cols = 5 * (math.ceil(
dp.floorplan_height / dp.grid_size) + 1) * (math.ceil(
dp.floorplan_width / dp.grid_size) + 1)
......@@ -229,7 +237,12 @@ class BboxModel(DnnModel):
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)
self.classifier = cnn_deep_loc_box(ae_file, y_cols, params)
elif self.type == "GRID_OVERLAP-BBOX_2D-CNN":
x_cols, _ = dp.get_data_dims("CNN")
classifier_template = cnn_bbox_model_for_generator()
self.classifier = classifier_template(x_cols, y_cols, 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=[])
......@@ -249,7 +262,8 @@ class BboxModel(DnnModel):
self.classifier = classifier_template(
X_cols=x_cols, Y_cols=y_cols, params=params)
self.type = "regression"
if "2D-CNN" not in self.type:
self.type = "regression"
def _train_model(self, save_weights_only=False, evaluate=True, autoencoder=False):
"""
......@@ -264,7 +278,7 @@ class BboxModel(DnnModel):
# calculate batch sizes (might be smaller than specified batch size if
# not enough data supplied)
num_train, num_val, num_test = data_provider.get_train_val_test_num(area_labels=self.type != "regression")
num_train, num_val, num_test = data_provider.get_train_val_test_num(area_labels=False)
train_bs = min(num_train, params['batch_size'])
val_bs = min(num_val, train_bs)
......@@ -323,10 +337,11 @@ class BboxModel(DnnModel):
steps_per_epoch=train_steps_per_epoch,
epochs=params['epochs'],
callbacks=[earlyStopping, checkpoint],
verbose=1)
verbose=0)
# evaluate model and store results in summary file
if evaluate:
#self.type = "regression"
self.evaluate_model(test_bs, chkpt_file=checkpoint_file_name)
def collect_train_progress(self, test_bs):
......
......@@ -167,3 +167,36 @@ def cnn_deep_loc_box(ae_base_model_file, y_cols, params):
)
return position_model
def cnn_bbox_model_for_generator():
def define_cnn_model_for_generator(input_shape, Y_cols,
params) -> Sequential:
model = Sequential()
model.add(Conv2D(16, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape, name="Conv2D_1", padding="same"))
model.add(Dropout(params['dropout']))
model.add(Conv2D(16, (3, 3), activation='relu', name="Conv2D_2", padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2), padding="same"))
model.add(Dropout(params['dropout']))
model.add(Conv2D(8, (3, 3), activation='relu', name="Conv2D_3", padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2), padding="same"))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
#model.add(Dropout(0.25))
model.add(Dense(Y_cols, activation=params['last_activation']))
model.compile(loss=params['losses'],
optimizer=params['optimizer'](
lr=lr_normalizer(params['lr'], params['optimizer'])))
return model
return define_cnn_model_for_generator
......@@ -495,7 +495,7 @@ class DataProviderGridBase(DataProviderBase):
# augment only training data
data, labels = self.get_augmented_train_data()
if model_type == "CNN":
if model_type == "CNN" or "2D-CNN" in model_type:
data = self.transform_batch_to_image(data, pearsoncorr=False)
elif model_type == "CNN-PSSD":
data = self.transform_batch_to_pairwise_diff_image(data)
......
data:
# The data provider which should be used
provider: UJIndoorLocProvider
# File name of floor plan img
floor_plan_img: <test>.jpg
# (train, val, test) test=0.2 => 5 fold # The number of temporal epochs into which the dataset is split
split_ratio: [0.7, 0.1, 0.2]
building: 0
floor: 0
#
# are used when not locally set for pipeline
#
global_params:
# number of experiment repetitions
repetitions: 1
preprocessing:
# Whether to standardize the RSS values
standardize: True
# Whether to assign labels with no matching area to closet area
assign_closest: False
# The floor number of the Lohan dataset, which should be used
#floor: 0
# The epoch number of the split dataset
#num_epochs: 10
#epoch: 5
# How to check for area matches of labels (to label positions with matching areas)
area_assignment: convex_hull
grid_size: 40
floor_plan:
# 'segmentation' => computes floor plan segmentation,
# 'regression' => uses DBSCAN to partitions labels into train, test split
type: floor_classification
model_params:
type: GRID_OVERLAP-BBOX_CNN # (DNN, CNN, kNN, SVM) supported (require different parameters)
#first_neuron: 512
#hidden_layers: 1
lr: 0.002
batch_size: 32
epochs: 200
#dropout: 0.7
#regularization_penalty: 0.0
augmentation: 0
#autoencoder: train
#pretrain: yes
pipelines:
# - name: CNNLoc
# model_params:
# lr: 0.002
# type: CNNLoc_REG
# autoencoder: train
# - name: CNNLoc-DLB
# model_params:
# lr: 0.002
# batch_size: 32
# augmentation: 0
# autoencoder: train
#
# loss:
# grid:
# scale: 100.0
# outside:
# scale: 1.0
# delta: [10.0, 15.0, 20.0]
- name: 2D-CNN-DLB
model_params:
type: GRID_OVERLAP-BBOX_2D-CNN # (DNN, CNN, kNN, SVM) supported (require different parameters)
#first_neuron: 512
#hidden_layers: [1,2,3]
lr: 0.7
batch_size: 32
epochs: 1
dropout: 0.5
#regularization_penalty: 0.0
augmentation: 0
loss:
grid:
scale: 100.0
outside:
scale: 1.0
delta: [10.0, 15.0, 20.0]
- name: 2D-CNN
model_params:
type: CNN # (DNN, CNN, kNN, SVM) supported (require different parameters)
pred: regression
#first_neuron: 512
#hidden_layers: [1,2,3]
lr: 0.7
batch_size: 32
#epochs: 200
#dropout: 0.5
#regularization_penalty: 0.0
augmentation: 0
# - name: DNN-DLB
# model_params:
# type: GRID_OVERLAP-BBOX # (DNN, CNN, kNN, SVM) supported (require different parameters)
# first_neuron: 512
# hidden_layers: [1,2]
# lr: 0.7
# batch_size: 32
# #epochs: 200
# dropout: 0.5
# regularization_penalty: 0.0
# augmentation: 0
#
# loss:
# grid:
# scale: 100.0
# outside:
# scale: 1.0
# delta: [10.0, 15.0, 20.0]
# base directories for file storage
output:
model_dir: evaluation/uji/gpu/b0/f0/cnn/output/
summary_dir: evaluation/uji/gpu/b0/f0/cnn/summary/
img_folder: evaluation/uji/ # folder where floorplan images is located (if not present, will be downloaded)
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