!8699 Add the option no need of training the epistemic model

From: @zhangxinfeng3
Reviewed-by: @zichun_ye,@wang_zi_dong
Signed-off-by: @zichun_ye
pull/8699/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit daa670c3e9

@ -47,6 +47,7 @@ class UncertaintyEvaluation:
Default: None. Default: None.
epochs (int): Total number of iterations on the data. Default: 1. epochs (int): Total number of iterations on the data. Default: 1.
epi_uncer_model_path (str): The save or read path of the epistemic uncertainty model. Default: None. epi_uncer_model_path (str): The save or read path of the epistemic uncertainty model. Default: None.
If the epi_uncer_model_path is 'Untrain', the epistemic model need not to be trained.
ale_uncer_model_path (str): The save or read path of the aleatoric uncertainty model. Default: None. ale_uncer_model_path (str): The save or read path of the aleatoric uncertainty model. Default: None.
save_model (bool): Whether to save the uncertainty model or not, if true, the epi_uncer_model_path save_model (bool): Whether to save the uncertainty model or not, if true, the epi_uncer_model_path
and ale_uncer_model_path must not be None. If false, the model to evaluate will be loaded from and ale_uncer_model_path must not be None. If false, the model to evaluate will be loaded from
@ -111,7 +112,7 @@ class UncertaintyEvaluation:
""" """
if self.epi_uncer_model is None: if self.epi_uncer_model is None:
self.epi_uncer_model = EpistemicUncertaintyModel(self.epi_model) self.epi_uncer_model = EpistemicUncertaintyModel(self.epi_model)
if self.epi_uncer_model.drop_count == 0: if self.epi_uncer_model.drop_count == 0 and self.epi_uncer_model_path != 'Untrain':
if self.task_type == 'classification': if self.task_type == 'classification':
net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = Adam(self.epi_uncer_model.trainable_params()) net_opt = Adam(self.epi_uncer_model.trainable_params())
@ -240,7 +241,7 @@ class EpistemicUncertaintyModel(Cell):
for (name, layer) in epi_model.name_cells().items(): for (name, layer) in epi_model.name_cells().items():
if isinstance(layer, Dropout): if isinstance(layer, Dropout):
self.drop_count += 1 self.drop_count += 1
return epi_model return epi_model
for (name, layer) in epi_model.name_cells().items(): for (name, layer) in epi_model.name_cells().items():
if isinstance(layer, (Conv2d, Dense)): if isinstance(layer, (Conv2d, Dense)):
uncertainty_layer = layer uncertainty_layer = layer
@ -248,7 +249,7 @@ class EpistemicUncertaintyModel(Cell):
drop = Dropout(keep_prob=dropout_rate) drop = Dropout(keep_prob=dropout_rate)
bnn_drop = SequentialCell([uncertainty_layer, drop]) bnn_drop = SequentialCell([uncertainty_layer, drop])
setattr(epi_model, uncertainty_name, bnn_drop) setattr(epi_model, uncertainty_name, bnn_drop)
return epi_model return epi_model
raise ValueError("The model has not Dense Layer or Convolution Layer, " raise ValueError("The model has not Dense Layer or Convolution Layer, "
"it can not evaluate epistemic uncertainty so far.") "it can not evaluate epistemic uncertainty so far.")

Loading…
Cancel
Save