Commit 639047a5 authored by Jean Ibarz's avatar Jean Ibarz
Browse files

Added a 'one_sided' model, that localize azimuth in [0,180] only, and added...

Added a 'one_sided' model, that localize azimuth in [0,180] only, and added code to be able to train this model.
parent dd006670
......@@ -8,8 +8,11 @@ def model_factory(model_config, model_name: str = 'default'):
return default_model_creator(model_config)
elif model_name == 'left_center_right':
return left_center_or_right_model_creator(model_config)
elif model_name == 'one_sided':
return one_sided_model_creator(model_config)
else:
raise ValueError(f'model_name {model_name} unknown. Possible values: {{''default'', ''left_center_right''}}')
raise ValueError(
f'model_name {model_name} unknown. Possible values: {{''default'', ''left_center_right'', ''one_sided''}}')
def default_model_creator(model_config):
......@@ -62,3 +65,35 @@ def left_center_or_right_model_creator(model_config):
])
return model
def one_sided_model_creator(model_config):
"""
input: (batch, time, channels, 1),
output: the guessed azimuth in [0,180]
"""
model = tf.keras.Sequential([
RandomScale2DLayer(minval=model_config['random_scale']['minval'],
maxval=model_config['random_scale']['maxval']),
RandomShift2DLayer(minval=model_config['random_shift']['minval'],
maxval=model_config['random_shift']['maxval'],
axis=1),
tkl.Flatten(),
tkl.Dense(units=320, activation='relu'),
tkl.Dropout(rate=0.05),
tkl.Dense(units=160, activation='relu'),
tkl.Dropout(rate=0.05),
tkl.Dense(units=80, activation='relu'),
tkl.Dropout(rate=0.05),
tkl.Dense(units=40, activation='relu'),
tkl.Dropout(rate=0.0),
tkl.Dense(units=10, activation='relu'),
tkl.Dense(units=10, activation='relu'),
tkl.Dense(units=10, activation='relu'),
tkl.Dense(units=10, activation='relu'),
tkl.Dense(units=10, activation='relu'),
tkl.Dense(units=1, activation='sigmoid'),
tkl.experimental.preprocessing.Rescaling(scale=180, offset=0.0)
])
return model
......@@ -27,7 +27,8 @@ exp_config = ExperimentConfiguration({'n_augment': 1,
'n_iters': 20,
'batch_size': 512,
'learning_rate': 0.0005,
'model_name': 'default', # possible values: {'default', 'left_center_right'}
'model_name': 'one_sided',
# possible values: {'default', 'left_center_right', 'one_sided'}
'model_config': {
'random_scale': {
'minval': -10,
......@@ -93,6 +94,15 @@ if __name__ == '__main__':
# reset index
df.reset_index(drop=True, inplace=True)
if exp_config['model_name'] == 'one_sided':
# convert azimuth labels in ]180,360[ to azimuth in [0,180] and swap left/right input channels
# ensure first that azimuth values are all in [0,360]
assert len(df[(df['azimuth'] < 0) & (df['azimuth'] > 360)]) == 0
df[['hrir_l', 'hrir_r']] = df[['hrir_l', 'hrir_r']].where(df['azimuth'] <= 180,
df[['hrir_r', 'hrir_l']].values)
df[['azimuth']] = df[['azimuth']].where(df['azimuth'] <= 180, 360 - df[['azimuth']].values)
all_subject_ids = df['subject_id'].astype('category').cat.categories
valid_train_subject_ids = 'train_subject_ids' in exp_config.keys() and exp_config[
......@@ -137,7 +147,7 @@ if __name__ == '__main__':
opt = tf.keras.optimizers.RMSprop(learning_rate=exp_config['learning_rate'])
if exp_config['model_name'] == 'default':
if exp_config['model_name'] in ['default', 'one_sided']:
model.compile(optimizer=opt,
loss=MeanAbsoluteAzimuthError(), # tf.keras.losses.MeanSquaredError(),
metrics=[])
......@@ -179,6 +189,7 @@ if __name__ == '__main__':
n_augment=1)
if exp_config['model_name'] == 'left_center_right':
# convert labels with azimuth in [0,360] to left,center,right with one_hot encoding
training_labels = azimuth_to_left_center_right_onehot(training_labels, in_place=True)
test_labels = azimuth_to_left_center_right_onehot(test_labels, in_place=True)
......
Supports Markdown
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