[docs]defevaluate_model(model:Union[MltkModel,str],tflite:bool=False,weights:str=None,max_samples_per_class:int=-1,classes:List[str]=None,dump:bool=False,show:bool=False,verbose:bool=None,callbacks:List=None,update_archive:bool=True,test:bool=False,post_process:bool=False)->EvaluationResults:"""Evaluate a trained model This internally calls: * :py:func:`mltk.core.evaluate_classifier` * :py:func:`mltk.core.evaluate_autoencoder` based on the given :py:class:`mltk.core.MltkModel` instance. .. seealso:: * `Model Evaluation Guide <https://siliconlabs.github.io/mltk/docs/guides/model_evaluation.html>`_ * `Model Evaluation API Examples <https://siliconlabs.github.io/mltk/mltk/examples/evaluate_model.html>`_ Args: model: :py:class:`mltk.core.MltkModel` instance, name of MLTK model, path to model archive ``.mltk.zip`` or model specification script ``.py`` tflite: If True, evaluate the ``.tflite`` (i.e. quantized) model file. If False, evaluate the Keras``.h5`` model (i.e. float) weights: Optional, load weights from previous training session. May be one of the following: * If option omitted then evaluate using output .h5 or .tflite from training * Absolute path to a generated weights .h5 file generated by Keras during training * The keyword ``best``; find the best weights in <model log dir>/train/weights * Filename of .h5 in <model log dir>/train/weights Note: This option may only be used if the "--tflite" option is *not* used max_samples_per_class: By default, all validation samples are used. This option places an upper limit on the number of samples per class that are used for evaluation classes: If evaluating a model with the :py:class:`mltk.core.EvaluateAutoEncoderMixin`, then this should be a comma-seperated list of classes in the dataset. The first element should be considered the "normal" class, every other class is considered abnormal and compared independently. If not provided, then the classes default to: [normal, abnormal] dump: If evaluating a model with the :py:class:`mltk.core.EvaluateAutoEncoderMixin`, then, for each sample, an image will be generated comparing the sample to the decoded sample show: Display the generated performance diagrams verbose: Enable verbose console logs callbacks: List of Keras callbacks to use for evaluation update_archive: Update the model archive with the evaluation results test: Optional, load the model in "test mode" if true. post_process: This allows for post-processing the evaluation results (e.g. uploading to a cloud) if supported by the given MltkModel Returns: Dictionary of evaluation results """ifisinstance(model,MltkModel):mltk_model=modeliftest:mltk_model.enable_test_mode()elifisinstance(model,str):ifmodel.endswith(('.tflite','.h5')):raiseValueError('Must provide name of MLTK model, ''path to model archive (.mltk.zip) or model specification script(.py)')mltk_model=load_mltk_model(model,test=test)else:raiseValueError('Must provide MltkModel instance, name of MLTK model, path to ''model archive (.mltk.zip) or model specification script(.py)')mltk_model.trigger_event(MltkModelEvent.EVALUATE_STARTUP,tflite=tflite,max_samples_per_class=max_samples_per_class,post_process=post_process)# If a custom function was provided,# then be sure to use that instead of the default function that comes with# EvaluateAutoEncoderMixin or EvaluateClassifierMixineval_custom_function=getattr(mltk_model,'eval_custom_function',None)ifeval_custom_functionisNoneandisinstance(mltk_model,EvaluateAutoEncoderMixin):results=evaluate_autoencoder(mltk_model,tflite=tflite,weights=weights,max_samples_per_class=max_samples_per_class,classes=classes,dump=dump,show=show,verbose=verbose,callbacks=callbacks,update_archive=update_archive)elifeval_custom_functionisNoneandisinstance(mltk_model,EvaluateClassifierMixin):results=evaluate_classifier(mltk_model,tflite=tflite,weights=weights,max_samples_per_class=max_samples_per_class,classes=classes,show=show,verbose=verbose,callbacks=callbacks,update_archive=update_archive)elifisinstance(mltk_model,EvaluateMixin):results=evaluate_custom(mltk_model,tflite=tflite,weights=weights,verbose=verbose,callbacks=callbacks,show=show,update_archive=update_archive,max_samples_per_class=max_samples_per_class,)else:raiseRuntimeError('MltkModel instance must inherit EvaluateMixin, EvaluateClassifierMixin or EvaluateAutoEncoderMixin')mltk_model.trigger_event(MltkModelEvent.EVALUATE_SHUTDOWN,results=results,)returnresults
defevaluate_custom(mltk_model:MltkModel,tflite:bool=False,weights:str=None,callbacks:list=None,verbose:bool=False,show:bool=False,update_archive:bool=True,max_samples_per_class:int=-1)->EvaluationResults:"""Evaluate a trained model based on the model's implementation Args: mltk_model: MltkModel instance tflite: If true then evalute the .tflite (i.e. quantized) model, otherwise evaluate the keras model weights: Optional weights to load before evaluating (only valid for a keras model) verbose: Enable verbose log messages callbacks: Optional callbacks to invoke while evaluating update_archive: Update the model archive with the eval results max_samples_per_class: Maximum number of samples per class to evaluate. This is useful for large datasets Returns: Dictionary containing evaluation results """ifnotisinstance(mltk_model,TrainMixin):raiseRuntimeError('MltkModel must inherit TrainMixin')ifnotisinstance(mltk_model,EvaluateMixin):raiseRuntimeError('MltkModel must inherit EvaluateClassifierMixin')ifnotisinstance(mltk_model,DatasetMixin):raiseRuntimeError('MltkModel must inherit a DatasetMixin')subdir='eval/tflite'iftfliteelse'eval/h5'eval_dir=mltk_model.create_log_dir(subdir,delete_existing=True)logger=mltk_model.create_logger('eval',parent=get_mltk_logger())ifupdate_archive:update_archive=mltk_model.check_archive_file_is_writable()gpu.initialize(logger=logger)try:mltk_model.load_dataset(subset='evaluation',test=mltk_model.test_mode_enabled,max_samples_per_class=max_samples_per_class,)exceptExceptionase:prepend_exception_msg(e,'Failed to load model evaluation dataset')raise# Build the MLTK model's corresponding as a Keras model or .tflitetry:built_model=load_tflite_or_keras_model(mltk_model,model_type='tflite'iftfliteelse'h5',weights=weights)exceptExceptionase:prepend_exception_msg(e,'Failed to build model')raisetry:summary=summarize_model(mltk_model,built_model=built_model)logger.info(summary)exceptExceptionase:logger.debug(f'Failed to generate model summary, err: {e}',exc_info=e)logger.warning(f'Failed to generate model summary, err: {e}')logger.info(mltk_model.summarize_dataset())eval_custom_function=getattr(mltk_model,'eval_custom_function',None)try:ifeval_custom_functionisnotNone:try:results=eval_custom_function(mltk_model,built_model=built_model,eval_dir=eval_dir,show=show,logger=logger)exceptExceptionase:prepend_exception_msg(e,'Failed to evaluate using custom callback')raiseelifisinstance(built_model,KerasModel):validation_data=mltk_model.validation_dataifvalidation_dataisNone:validation_data=mltk_model.xeval_loss,eval_accuracy=built_model.evaluate(x=validation_data,y=mltk_model.y,batch_size=mltk_model.batch_size,verbose=1ifverboseelse0,callbacks=callbacks,steps=mltk_model.eval_steps_per_epoch)results=EvaluationResults(name=mltk_model.name)results['overall_loss']=eval_lossresults['overall_accuracy']=eval_accuracyelse:raiseRuntimeError('Must specify my_model.eval_custom_function to evaluate a .tflite model')finally:mltk_model.unload_dataset()eval_results_path=f'{eval_dir}/eval-results.json'withopen(eval_results_path,'w')asf:json.dump(results,f)logger.debug(f'Generated {eval_results_path}')summary_path=f'{eval_dir}/summary.txt'withopen(summary_path,'w')asf:f.write(results.generate_summary())logger.debug(f'Generated {summary_path}')try:results.generate_plots(logger=logger,output_dir=eval_dir,show=show)exceptNotImplementedError:passexceptExceptionase:logger.warning(f'Failed to generate evaluation plots',exc_info=e)ifupdate_archive:try:logger.info(f'Updating {mltk_model.archive_path}')mltk_model.add_archive_dir(subdir)exceptExceptionase:logger.warning(f'Failed to add eval results to model archive, err: {e}',exc_info=e)returnresults
Important: We use cookies only for functional and traffic analytics.
We DO NOT use cookies for any marketing purposes. By using our site you acknowledge you have read and understood our Cookie Policy.